Last active
June 7, 2021 03:03
-
-
Save peterwilsoncc/99257a8f3161cd8fe0c403ca5d538ce5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A testing plugin for trac ticket #48556. | |
* | |
* As a testing plugin, this includes a nasty hack to allow for easily | |
* viewing WP_Query dumps. | |
* | |
* **THIS SHOULD NEVER BE RUN ON A PUBLIC FACING SITE** | |
*/ | |
namespace Trac\WPQ_Update; | |
use WP_Query; | |
const READER_ROLE_SLUG = 'trac48556_reader'; | |
const READER_ROLE_NAME = 'Trac48556 Reader'; | |
const AUTHOR_ROLE_SLUG = 'trac48556_author'; | |
const AUTHOR_ROLE_NAME = 'Trac48556 Author'; | |
const ADMIN_ROLE_SLUG = 'trac48556_admin'; | |
const ADMIN_ROLE_NAME = 'Trac48556 Admin'; | |
function bootstrap() { | |
add_action( 'init', __NAMESPACE__ . '\\register_cpts' ); | |
add_action( 'init', __NAMESPACE__ . '\\add_roles' ); | |
add_action( 'init', __NAMESPACE__ . '\\a_nasty_hack' ); | |
} | |
add_action( 'plugins_loaded', __NAMESPACE__ . '\\bootstrap' ); | |
function a_nasty_hack() { | |
if ( empty( $_GET['trac48556'] ) ) { | |
return; | |
} | |
$post_types = wp_unslash( $_GET['trac48556'] ); | |
if ( is_array( $post_types ) ) { | |
$post_types = array_map( 'sanitize_key', $post_types ); | |
} else { | |
$post_types = sanitize_key( $post_types ); | |
} | |
var_dump( | |
new WP_Query( array( 'post_type' => $post_types ) ) | |
); | |
exit; | |
} | |
function register_cpts() { | |
register_post_type( | |
'trac48556_public', | |
array( | |
'labels' => array( | |
'name' => 'Trac 48556 public posts', | |
'singular_name' => 'Trac 48556 public post', | |
), | |
'public' => true, | |
'map_meta_cap' => true, | |
) | |
); | |
register_post_type( | |
'trac48556_private', | |
array( | |
'labels' => array( | |
'name' => 'Trac 48556 private posts', | |
'singular_name' => 'Trac 48556 private post', | |
), | |
'public' => false, | |
'show_ui' => true, | |
'map_meta_cap' => true, | |
) | |
); | |
register_post_type( | |
'trac48556_custom_cap', | |
array( | |
'labels' => array( | |
'name' => 'Trac 48556 custom caps posts', | |
'singular_name' => 'Trac 48556 custom caps post', | |
), | |
'public' => true, | |
'show_ui' => true, | |
'capability_type' => array( 'post', 'trac48556_cc_posts' ), | |
'map_meta_cap' => true, | |
) | |
); | |
register_post_type( | |
'trac48556_c_priv_cap', | |
array( | |
'labels' => array( | |
'name' => 'Trac 48556 custom caps private post types', | |
'singular_name' => 'Trac 48556 custom caps private post post type', | |
), | |
'public' => false, | |
'show_ui' => true, | |
'capability_type' => array( 'post', 'trac48556_cc_posts' ), | |
'map_meta_cap' => true, | |
) | |
); | |
} | |
function add_roles() { | |
maybe_add_role( | |
READER_ROLE_SLUG, | |
READER_ROLE_NAME, | |
array( 'read_trac48556_cc_posts' ), | |
); | |
maybe_add_role( | |
AUTHOR_ROLE_SLUG, | |
AUTHOR_ROLE_NAME, | |
array( | |
'delete_trac48556_cc_posts', | |
'delete_published_trac48556_cc_posts', | |
'edit_trac48556_cc_posts', | |
'edit_published_trac48556_cc_posts', | |
'publish_trac48556_cc_posts', | |
'read_trac48556_cc_posts', | |
), | |
); | |
maybe_add_role( | |
ADMIN_ROLE_SLUG, | |
ADMIN_ROLE_NAME, | |
array( | |
'delete_others_trac48556_cc_posts', | |
'delete_trac48556_cc_posts', | |
'delete_private_trac48556_cc_posts', | |
'delete_published_trac48556_cc_posts', | |
'edit_trac48556_cc_posts', | |
'edit_others_trac48556_cc_posts', | |
'edit_private_trac48556_cc_posts', | |
'edit_published_trac48556_cc_posts', | |
'publish_trac48556_cc_posts', | |
'read_private_trac48556_cc_posts', | |
'read_trac48556_cc_posts', | |
), | |
); | |
} | |
/** | |
* Add the role if it does not exist. | |
* | |
* Creates the role based off the subscriber capabilities | |
* and additionally grants them the custom capabilities. | |
*/ | |
function maybe_add_role( $slug, $name, $caps ) { | |
$role_obj = get_role( $slug ); | |
if ( $role_obj ) { | |
// No need to create the role. | |
return; | |
} | |
$subscriber_role = get_role( 'subscriber' ); | |
if ( ! $subscriber_role ) { | |
// No roles yet. | |
return; | |
} | |
foreach ( $caps as $cap ) { | |
$true_caps[ $cap ] = true; | |
} | |
add_role( | |
$slug, | |
$name, | |
array_merge( | |
$subscriber_role->capabilities, | |
$true_caps, | |
) | |
); | |
/** | |
* Refresh user if they are logged in and have the role. | |
* | |
* This is really for safety only, a user shouldn't have a role before the role is | |
* are created but stranger things have happened. | |
*/ | |
if ( is_user_logged_in() && current_user_can( $slug ) ) { | |
wp_get_current_user()->get_role_caps(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
wp user create author author@example.com --role=author --user_pass=password | |
wp user create contributor contributor@example.com --role=contributor --user_pass=password | |
wp user create editor editor@example.com --role=editor --user_pass=password | |
wp user create trac48556_reader trac48556_reader@example.com --role=trac48556_reader --user_pass=password | |
wp user create trac48556_author trac48556_author@example.com --role=trac48556_author --user_pass=password | |
wp user create trac48556_admin trac48556_admin@example.com --role=trac48556_admin --user_pass=password | |
wp post create --post_type=post --post_title='A public post' --post_content='A public post' --post_status=publish | |
wp post create --post_type=post --post_title='A private post' --post_content='A private post' --post_status=private | |
wp post create --post_type=trac48556_public --post_title='A trac48556_public post' --post_content='Uses standard "post" cap, a "public" post type, "publish" status' --post_status=publish | |
wp post create --post_type=trac48556_public --post_title='A trac48556_public private post' --post_content='Uses standard "post" cap, a "public" post type, "private" status' --post_status=private | |
wp post create --post_type=trac48556_private --post_title='A trac48556_private post' --post_content='Uses standard "post" cap, a "private (public=false)" post type, "publish" status' --post_status=publish | |
wp post create --post_type=trac48556_private --post_title='A trac48556_private private post' --post_content='Uses standard "post" cap, a "private (public=false)" post type, "private" status' --post_status=private | |
wp post create --post_type=trac48556_custom_cap --post_title='A trac48556_custom_cap post' --post_content='Uses custom cap, a "public" post type, "publish" status' --post_status=publish | |
wp post create --post_type=trac48556_custom_cap --post_title='A trac48556_custom_cap private post' --post_content='Uses custom cap, a "public" post type, "private" status' --post_status=private | |
wp post create --post_type=trac48556_c_priv_cap --post_title='A trac48556_c_priv_cap post' --post_content='Uses custom cap, a "private (public=false)" post type, "publish" status' --post_status=publish | |
wp post create --post_type=trac48556_c_priv_cap --post_title='A trac48556_c_priv_cap private post' --post_content='Uses custom cap, a "private (public=false)" post type, "private" status' --post_status=private | |
wp plugin install user-switching --activate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment