Skip to content

Instantly share code, notes, and snippets.

@luistar15
Last active July 26, 2022 07:50
Show Gist options
  • Save luistar15/333a25888e8804fd17490815a74ecc21 to your computer and use it in GitHub Desktop.
Save luistar15/333a25888e8804fd17490815a74ecc21 to your computer and use it in GitHub Desktop.
Wordpress bug plugin showcase
<?php
/**
* Plugin Name: Bug Patch Test trac-22895
* Description: Patch Test for https://core.trac.wordpress.org/ticket/22895
*/
/**
* NOTE: To test this plugin, after activation, login as:
* - username: editor
* - password: editor
* This user is created at plugin activation automatically and already
* has the "custom-role" role assigned to test all the cases.
*/
/**
* CASE 1 (C1): custom post type as top level menu
* ----------------------------------------------------------------------------
* When the custom post type is a top level menu
* and doesn't have submenu items or has only one.
*/
add_action( 'init', function () {
register_post_type( 'movies', [
'label' => 'Movies (C1)',
'public' => true,
'capability_type' => 'movie',
'map_meta_cap' => true,
'show_in_menu' => true, // as top level menu
] );
} );
/**
* To hide the submenu of a top level custom post type menu
* we only need to leave one submenu item.
* In this case I'm removing the second submenu item:
* post-new.php?post_type=movies.
*/
add_action( 'admin_menu', function () {
remove_submenu_page(
'edit.php?post_type=movies',
'post-new.php?post_type=movies'
);
} );
/**
* CASE 2 (C2): custom post type as submenu
* ----------------------------------------------------------------------------
* When the custom post type is a submenu under
* another top level menu.
*/
add_action( 'init', function () {
register_post_type( 'cars', [
'label' => 'Cars (C2)',
'public' => true,
'capability_type' => 'car',
'map_meta_cap' => true,
'show_in_menu' => 'tools.php', // as submenu under tools.php
] );
} );
// CASE 3 (C3): https://core.trac.wordpress.org/ticket/48511
// -----------------------------------------------------------------------------
function custom_init() {
$args = [
"labels" => [
"name" => "Promos Toy (C3)",
"singular_name" => "Promo Toy",
"all_items" => "All Promos Toy",
"add_new_item" => "Add new Promo Toy",
"edit_item" => "Edit Promo Toy",
"new_item" => "New Promo Toy",
"view_item" => "View Promo Toy",
"search_items" => "Search Promos Toy",
"not_found" => "No Promos Toy found.",
"not_found_in_trash" => "No Promos Toy found in trash."
],
"description" => "Test Post Type",
"public" => false,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_nav_menus" => true,
"has_archive" => false,
"show_in_menu" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "",
"exclude_from_search" => true,
"capability_type" => array("promo_toy", "promo_toys"),
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => "",
"menu_position" => "",
"menu_icon" => "dashicons-admin-page",
"query_var" => true,
"supports" => ["title", "revisions", "author"],
"taxonomies" => [],
"capabilities" => [
"edit_post" => "edit_promo_toy",
"edit_others_posts" => "edit_others_promo_toys",
"edit_private_posts" => "edit_private_promo_toys",
"edit_published_posts" => "edit_published_promo_toys",
"read_private_posts" => "read_private_promo_toys",
"delete_post" => "delete_promo_toy",
"delete_others_posts" => "delete_others_promo_toys",
"delete_private_posts" => "delete_private_promo_toys",
"delete_published_posts" => "delete_published_promo_toys",
"publish_posts" => "publish_promo_toys",
"create_posts" => "create_promo_toys"
]
];
register_post_type("promo_toy", $args);
}
add_action( 'init', 'custom_init' );
// CASE 4 (C4): https://core.trac.wordpress.org/ticket/39745
// -----------------------------------------------------------------------------
add_action( 'init', function () {
register_post_type( 'my_cpt', [
'public' => true,
'label' => 'My Cpt (C4)',
'capability_type' => 'my_cpt',
'capabilities' => array('create_posts' => 'create_my_cpts'),
'supports' => array('author','title')
] );
} );
// CASE 5 (C5): https://core.trac.wordpress.org/ticket/29714
// -----------------------------------------------------------------------------
add_action( 'init', function () {
register_post_type( 'oik_site', [
'public' => true,
'label' => 'Sites (C5)',
'capability_type' => 'oik_site',
'map_meta_cap' => true,
'capabilities' => array('create_posts' => 'create_oik_sites'),
] );
} );
// CASE 6 (C6): https://core.trac.wordpress.org/ticket/16808
// -----------------------------------------------------------------------------
// https://core.trac.wordpress.org/raw-attachment/ticket/16808/16808-tests.php
/**
* Register post types
*
* 1) Argus > Visitor : cap > admin, subscriber
* 2) Argus > Comics : cap > admin, subscriber
* 3) Argus > Candies : cap > admin
* 4) Top Level > Toys : cap > admin, subscriber
* 5) Top Level > Books : cap > admin
*
*/
add_action( 'init', 'trac16808_register_post_types' );
function trac16808_register_post_types(){
/**
* CPT Visitors inside Argus admin page
* Subscriber have permission to add new posts
*
*/
$args = array(
'labels' => array (
'name' => 'Visitors (C6)',
'singular_name' => 'Visitor',
'add_new' => 'Register New',
'add_new_item' => 'Register New Visitor',
),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post
'hiearchical' => false,
'supports' => array(
'title',
'editor',
),
'capabilities' => array(
'edit_post' => 'argus_visitors',
'read_post' => 'argus_visitors',
'delete_post' => 'argus_visitors',
'edit_posts' => 'argus_visitors',
'edit_others_posts' => 'argus_visitors',
'publish_posts' => 'argus_visitors',
'read_private_posts' => 'argus_visitors',
'read' => 'argus_visitors',
'delete_posts' => 'argus_visitors',
'delete_private_posts' => 'argus_visitors',
'delete_published_posts' => 'argus_visitors',
'delete_others_posts' => 'argus_visitors',
'edit_private_posts' => 'argus_visitors',
'edit_published_posts' => 'argus_visitors',
),
);
register_post_type( 'visitor', $args );
/**
* CPT Comics inside Argus admin page
* Subscriber have permission to add new posts
*
*/
$args = array(
'labels' => array (
'name' => 'Comics (C6)',
'singular_name' => 'Comic',
'add_new' => 'Add New',
'add_new_item' => 'Add New Comic',
),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post
'hiearchical' => false,
'supports' => array(
'title',
'editor',
),
'capabilities' => array(
'edit_post' => 'argus_visitors',
'read_post' => 'argus_visitors',
'delete_post' => 'argus_visitors',
'edit_posts' => 'argus_visitors',
'edit_others_posts' => 'argus_visitors',
'publish_posts' => 'argus_visitors',
'read_private_posts' => 'argus_visitors',
'read' => 'argus_visitors',
'delete_posts' => 'argus_visitors',
'delete_private_posts' => 'argus_visitors',
'delete_published_posts' => 'argus_visitors',
'delete_others_posts' => 'argus_visitors',
'edit_private_posts' => 'argus_visitors',
'edit_published_posts' => 'argus_visitors',
),
);
register_post_type( 'comic', $args );
/**
* CPT Candies inside Argus admin page
* Subscriber don't have permission to add new posts
*
*/
$args = array(
'labels' => array (
'name' => 'Candies (C6)',
'singular_name' => 'Candy',
'add_new' => 'Add New',
'add_new_item' => 'Add New Candy',
),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => 'argus',
'hiearchical' => false,
'supports' => array(
'title',
'editor',
),
'capabilities' => array(
'edit_post' => 'manage_options',
'read_post' => 'manage_options',
'delete_post' => 'manage_options',
'edit_posts' => 'manage_options',
'edit_others_posts' => 'manage_options',
'publish_posts' => 'manage_options',
'read_private_posts' => 'manage_options',
'read' => 'manage_options',
'delete_posts' => 'manage_options',
'delete_private_posts' => 'manage_options',
'delete_published_posts' => 'manage_options',
'delete_others_posts' => 'manage_options',
'edit_private_posts' => 'manage_options',
'edit_published_posts' => 'manage_options',
),
);
register_post_type( 'candy', $args );
/**
* Regular CPT Books
* Subscriber have permission to add new posts
*
*/
$args = array(
'labels' => array (
'name' => 'Toys (C6)',
'singular_name' => 'Toy',
'add_new' => 'Add New',
'add_new_item' => 'Add New Toy',
),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'hiearchical' => false,
'supports' => array(
'title',
'editor',
),
'capabilities' => array(
'edit_post' => 'argus_visitors',
'read_post' => 'argus_visitors',
'delete_post' => 'argus_visitors',
'edit_posts' => 'argus_visitors',
'edit_others_posts' => 'argus_visitors',
'publish_posts' => 'argus_visitors',
'read_private_posts' => 'argus_visitors',
'read' => 'argus_visitors',
'delete_posts' => 'argus_visitors',
'delete_private_posts' => 'argus_visitors',
'delete_published_posts' => 'argus_visitors',
'delete_others_posts' => 'argus_visitors',
'edit_private_posts' => 'argus_visitors',
'edit_published_posts' => 'argus_visitors',
),
);
register_post_type( 'toy', $args );
/**
* Regular CPT Book
* Subscriber don't have permission to add new posts
*
*/
$args = array(
'labels' => array (
'name' => 'Books (C6)',
'singular_name' => 'Book',
'add_new' => 'Add New',
'add_new_item' => 'Add New Book',
),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'hiearchical' => false,
'supports' => array(
'title',
'editor',
),
'capabilities' => array(
'edit_post' => 'manage_options',
'read_post' => 'manage_options',
'delete_post' => 'manage_options',
'edit_posts' => 'manage_options',
'edit_others_posts' => 'manage_options',
'publish_posts' => 'manage_options',
'read_private_posts' => 'manage_options',
'read' => 'manage_options',
'delete_posts' => 'manage_options',
'delete_private_posts' => 'manage_options',
'delete_published_posts' => 'manage_options',
'delete_others_posts' => 'manage_options',
'edit_private_posts' => 'manage_options',
'edit_published_posts' => 'manage_options',
),
);
register_post_type( 'book', $args );
}
/**
* Add menus
* Set priority to 0 just to add submenu on top
*
*/
add_action( 'admin_menu', 'trac16808_add_menus', 9 );
function trac16808_add_menus(){
add_menu_page( 'Argus (C6)', 'Argus Admin (C6)', 'argus_visitors', 'argus', 'trac16808_admin_page_render', false, 40 );
add_submenu_page( 'argus', 'Argus Administration (C6)', 'Control Panel (C6)', 'argus_visitors', 'argus', 'trac16808_admin_page_render' );
}
/**
* Render admin page
*
*/
function trac16808_admin_page_render(){
echo '<div class="wrap"><h2>Argus Admin Page</h2></div>';
}
// Register Role and insert User
// -----------------------------------------------------------------------------
register_activation_hook( __FILE__, function () {
add_role( 'custom-editor', 'Custom Editor', [
'read' => true,
// CASE 1 (C1)
'edit_movies' => true,
// CASE 2 (C2)
'edit_cars' => true,
// CASE 3 (C3)
'edit_promo_toys' => true,
'edit_others_promo_toys' => true,
// CASE 4 (C4)
'edit_my_cpts' => true,
'create_my_cpts' => false,
// CASE 5 (C5)
'edit_oik_sites' => true,
// CASE 6 (C6)
'argus_visitors' => true,
] );
wp_insert_user( [
'user_login' => 'editor',
'user_pass' => 'editor',
'role' => 'custom-editor',
] );
} );
register_deactivation_hook( __FILE__, function () {
remove_role( 'custom-editor' );
wp_delete_user( get_user_by( 'login', 'editor' )->ID );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment