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() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) { wp_enqueue_style( 'members-admin' ); return; } 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; // Classic meta boxes conflict with the block editor save state; use the document panel instead. $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) { return; } // Add the meta box. add_meta_box( 'members-cp', __( 'Content Permissions (Members)', '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() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || empty( $screen->post_type ) ) { return false; } return members_is_content_permissions_enabled_for_post_type( $screen->post_type ); } /** * 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 = apply_filters( 'members_wp_roles', $wp_roles->role_names, members_get_post_for_content_permissions( $post ) ); asort( $_wp_roles ); // Get the roles saved for the post. $roles = members_get_post_roles( $post->ID ); if ( empty( $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 ); ?>
role_names; $displayed = array_keys( (array) apply_filters( 'members_wp_roles', $roles, members_get_post_for_content_permissions( $post ) ) ); foreach ( (array) $current_roles as $stored ) { if ( is_string( $stored ) && '' !== $stored && isset( $roles[ $stored ] ) && ! in_array( $stored, $displayed, true ) ) { $new_roles[] = members_sanitize_role( $stored ); } } // Set the merged role list (empty clears registered roles while leaving orphans intact). members_set_post_roles( $post_id, array_values( array_unique( $new_roles ) ) ); /* === Error Message === */ // Get the old access message. $old_message = members_get_post_access_message( $post_id ); // Get the new message. $new_message = isset( $_POST['members_access_error'] ) ? wp_kses_post( wp_unslash( $_POST['members_access_error'] ) ) : ''; // If we have don't have a new message but do have an old one, delete it. if ( '' == $new_message && $old_message ) members_delete_post_access_message( $post_id ); // If the new message doesn't match the old message, set it. else if ( $new_message !== $old_message ) members_set_post_access_message( $post_id, $new_message ); } /** * Returns the instance. * * @since 2.0.0 * @access public * @return object */ public static function get_instance() { if ( ! self::$instance ) self::$instance = new self; return self::$instance; } } Meta_Box_Content_Permissions::get_instance();