Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Plugin Name: Customize Changesets Admin UI
* Description: Enable revisions for customize changeset posts and enable admin screens.
* Author: Weston Ruter, XWP
*
* @package Customize_Changesets_Admin_UI
*/
namespace Customize_Changesets_Admin_UI;
const POST_TYPE = 'customize_changeset';
/**
* Extend post type object.
*/
function extend_post_type_object() {
$post_type_obj = get_post_type_object( 'customize_changeset' );
add_post_type_support( POST_TYPE, 'revisions' );
$post_type_obj->show_ui = true;
$post_type_obj->show_in_menu = true;
$post_type_obj->menu_position = 100;
$post_type_obj->_edit_link = 'post.php?post=%d'; /* internal use only. don't use this when registering your own post type. */
}
add_action( 'init', __NAMESPACE__ . '\extend_post_type_object', 100 );
/**
* Add meta box.
*/
function add_meta_box() {
$id = 'customize_changeset_data';
$title = __( 'Changeset Data' );
$callback = __NAMESPACE__ . '\render_meta_box';
$screen = POST_TYPE;
$context = 'normal';
$priority = 'high';
\add_meta_box( $id, $title, $callback, $screen, $context, $priority );
}
add_action( 'add_meta_boxes', __NAMESPACE__ . '\add_meta_box', 100 );
/**
* Render meta box.
*
* @param int $post_id Post id.
*/
function render_meta_box( $post_id ) {
echo '<pre>';
echo esc_html( wp_json_encode( json_decode( get_post( $post_id )->post_content, true ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
echo '</pre>';
}
/**
* Re-map customize meta cap to edit_theme_options primitive cap.
*
* @param array $caps All caps.
* @param string $cap Requested cap.
*
* @return array All caps.
*/
function remap_customize_meta_cap( $caps, $cap ) {
$post_type_obj = get_post_type_object( POST_TYPE );
if ( isset( $post_type_obj->cap->$cap ) && 'customize' === $post_type_obj->cap->$cap ) {
foreach ( $caps as &$required_cap ) {
if ( 'customize' === $required_cap ) {
$required_cap = 'edit_theme_options';
}
}
}
return $caps;
}
add_filter( 'map_meta_cap', __NAMESPACE__ . '\remap_customize_meta_cap', 5, 4 );
/**
* Add admin menu item.
*/
function add_admin_menu_item() {
$post_type_object = get_post_type_object( POST_TYPE );
$capability = $post_type_object->cap->edit_posts;
$page_title = $post_type_object->labels->name;
$menu_title = $post_type_object->labels->name;
$menu_slug = 'edit.php?post_type=customize_changeset';
add_theme_page( $page_title, $menu_title, $capability, $menu_slug );
}
add_action( 'admin_menu', __NAMESPACE__ . '\add_admin_menu_item' );
@jonaslundman

This comment has been minimized.

Copy link

commented Jun 23, 2018

This is not working anymore. map_meta_cap doesnt find the req variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.