* @copyright Copyright (c) 2009 - 2018, Justin Tadlock * @link https://themehybrid.com/plugins/members * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ namespace Members\Admin; /** * Class to handle the content permissios meta box and saving the meta. * * @since 2.0.0 * @access public */ final class Meta_Box_Content_Permissions { /** * Holds the instances of this class. * * @since 2.0.0 * @access private * @var object */ private static $instance; /** * Whether this is a new post. Once the post is saved and we're * no longer on the `post-new.php` screen, this is going to be * `false`. * * @since 2.0.0 * @access public * @var bool */ public $is_new_post = false; /** * Sets up the appropriate actions. * * @since 2.0.0 * @access protected * @return void */ protected function __construct() { // If content permissions is disabled, bail. if ( ! members_content_permissions_enabled() ) return; add_action( 'load-post.php', array( $this, 'load' ) ); add_action( 'load-post-new.php', array( $this, 'load' ) ); } /** * Fires on the page load hook to add actions specifically for the post and * new post screens. * * @since 2.0.0 * @access public * @return void */ public function load() { // Make sure meta box is allowed for this post type. if ( ! $this->maybe_enable() ) return; // Is this a new post? $this->is_new_post = 'load-post-new.php' === current_action(); // Enqueue scripts/styles. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); // Add custom meta boxes. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); // Save metadata on post save. add_action( 'save_post', array( $this, 'update' ), 10, 2 ); } /** * Enqueues scripts styles. * * @since 2.0.0 * @access public * @return void */ public function enqueue() { wp_enqueue_script( 'members-edit-post' ); wp_enqueue_style( 'members-admin' ); } /** * Adds the meta box. * * @since 2.0.0 * @access public * @param string $post_type * @return void */ public function add_meta_boxes( $post_type ) { // If the current user can't restrict content, bail. if ( ! current_user_can( 'restrict_content' ) ) return; // Add the meta box. add_meta_box( 'members-cp', esc_html__( 'Content Permissions', 'members' ), array( $this, 'meta_box' ), $post_type, 'advanced', 'high' ); } /** * Checks if Content Permissions should appear for the given post type. * * @since 2.0.0 * @access public * @return bool */ public function maybe_enable() { // Get the post type object. $type = get_post_type_object( get_current_screen()->post_type ); // Only enable for public post types and non-attachments by default. $enable = 'attachment' !== $type->name && $type->public; return apply_filters( "members_enable_{$type->name}_content_permissions", $enable ); } /** * Outputs the meta box HTML. * * @since 2.0.0 * @access public * @param object $post * @global object $wp_roles * @return void */ public function meta_box( $post ) { global $wp_roles; // Get roles and sort. $_wp_roles = $wp_roles->role_names; asort( $_wp_roles ); // Get the roles saved for the post. $roles = get_post_meta( $post->ID, '_members_access_role', false ); if ( ! $roles && $this->is_new_post ) $roles = apply_filters( 'members_default_post_roles', array(), $post->ID ); // Convert old post meta to the new system if no roles were found. if ( empty( $roles ) ) $roles = members_convert_old_post_meta( $post->ID ); // Nonce field to validate on save. wp_nonce_field( 'members_cp_meta_nonce', 'members_cp_meta' ); // Hook for firing at the top of the meta box. do_action( 'members_cp_meta_box_before', $post ); ?>