diff --git a/class-unlist-posts-admin.php b/class-unlist-posts-admin.php index dc5cacf..62ba151 100644 --- a/class-unlist-posts-admin.php +++ b/class-unlist-posts-admin.php @@ -40,6 +40,7 @@ public static function instance() { private function __construct() { add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) ); add_action( 'save_post', array( $this, 'save_meta' ) ); + add_action( 'save_post', array( $this, 'save_meta_enable_robots' ) ); add_filter( 'display_post_states', array( $this, 'add_unlisted_post_status' ), 10, 2 ); add_filter( 'parse_query', array( $this, 'filter_unlisted_posts' ) ); add_action( 'init', array( $this, 'add_post_filter' ) ); @@ -74,33 +75,12 @@ function register_metabox() { * @param POST $post Currennt post object which is being displayed. */ function metabox_render( $post ) { - - $hidden_posts = get_option( 'unlist_posts', array() ); - - if ( '' === $hidden_posts ) { - $hidden_posts = array(); - } - - $checked = ''; - - if ( in_array( (int) $post->ID, $hidden_posts, true ) ) { - $checked = 'checked'; - } - - // We'll use this nonce field later on when saving. - wp_nonce_field( 'unlist_post_nounce', 'unlist_post_nounce' ); - ?> -
- -
-- render_unlist_posts( $post ); + $this->render_enable_robots( $post ); } /** - * Save meta field. + * Save meta field for unlist posts. * * @param POST $post_id Currennt post object which is being displayed. * @@ -150,6 +130,57 @@ public function save_meta( $post_id ) { update_option( 'unlist_posts', $hidden_posts ); } + /** + * Save meta field for enable robots. + * + * @param POST $post_id Currennt post object which is being displayed. + * + * @return Void + */ + public function save_meta_enable_robots( $post_id ) { + // Bail if we're doing an auto save. + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return; + } + + // if our nonce isn't there, or we can't verify it, bail. + if ( ! isset( $_POST['unlist_post_enable_robots_nounce'] ) || ! wp_verify_nonce( $_POST['unlist_post_enable_robots_nounce'], 'unlist_post_enable_robots_nounce' ) ) { + return; + } + + // if our current user can't edit this post, bail. + if ( ! current_user_can( 'edit_posts' ) ) { + return; + } + + // Don't record unlist option for revisions. + if ( false !== wp_is_post_revision( $post_id ) ) { + return; + } + + $enable_robots = get_option( 'unlist_posts_enable_robots', array() ); + + if ( '' === $enable_robots ) { + $enable_robots = array(); + } + + if ( isset( $_POST['unlist_posts_enable_robots'] ) ) { + $enable_robots[] = $post_id; + + // Get only the unique post id's in the option array. + $enable_robots = array_unique( $enable_robots ); + } elseif ( in_array( $post_id, $enable_robots, true ) ) { + + // Get only the unique post id's in the option array. + $enable_robots = array_unique( $enable_robots ); + + $key = array_search( $post_id, $enable_robots, true ); + unset( $enable_robots[ $key ] ); + } + + update_option( 'unlist_posts_enable_robots', $enable_robots ); + } + /** * Add 'Unlisted' post status to post list items. * @@ -255,6 +286,68 @@ function filter_unlisted_posts( $query ) { return $query; } + + /** + * Render Unlist Posts meta field + * + * @param Post $post The current post object. + * @return void + */ + function render_unlist_posts( $post ) { + $hidden_posts = get_option( 'unlist_posts', array() ); + + if ( '' === $hidden_posts ) { + $hidden_posts = array(); + } + + $checked = ''; + + if ( in_array( (int) $post->ID, $hidden_posts, true ) ) { + $checked = 'checked'; + } + + // We'll use this nonce field later on when saving. + wp_nonce_field( 'unlist_post_nounce', 'unlist_post_nounce' ); + ?> +
+ +
++ ID, $enable_robots, true ) ) { + $checked = 'checked'; + } + + // We'll use this nonce field later on when saving. + wp_nonce_field( 'unlist_post_enable_robots_nounce', 'unlist_post_enable_robots_nounce' ); + ?> +
+ +
++ robots_enabled_check( $hidden_posts, $enable_robots ) ) { $robots['index'] = 'noindex'; } return $robots; @@ -175,8 +176,9 @@ public function hide_post_from_searchengines() { } $hidden_posts = get_option( 'unlist_posts', array() ); + $enable_robots = get_option( 'unlist_posts_enable_robots', array() ); - if ( in_array( get_the_ID(), $hidden_posts, true ) && false !== get_the_ID() ) { + if ( $this->robots_enabled_check( $hidden_posts, $enable_robots ) ) { wp_no_robots(); } } @@ -194,8 +196,9 @@ public function no_robots_for_unlisted_posts( $robots ) { } $hidden_posts = get_option( 'unlist_posts', array() ); + $enable_robots = get_option( 'unlist_posts_enable_robots', array() ); - if ( in_array( get_the_ID(), $hidden_posts, true ) && false !== get_the_ID() ) { + if ( $this->robots_enabled_check( $hidden_posts, $enable_robots ) ) { // Disable robots tags from Yoast SEO. add_filter( 'wpseo_robots_array', '__return_empty_array' ); return wp_robots_no_robots( $robots ); @@ -246,6 +249,25 @@ public function hidden_post_string() { private function allow_post_unlist() { return apply_filters( 'unlist_posts_enabled', true ); } + + /** + * Check if robots should be enabled + * + * @param array $hidden_posts Array of hidden post ids. + * @param array $enable_robots Array of post ids that should allow robots + * @return boolean False - This is the default value. This means that robots are disabled. + */ + private function robots_enabled_check( $hidden_posts, $enable_robots ) { + // Check if hidden posts for post id is enabled + if ( in_array( get_the_ID(), $hidden_posts, true ) && false !== get_the_ID() ) { + // Check if enable robots for post id is enabled + if ( !in_array( get_the_ID(), $enable_robots, true ) && false !== get_the_ID() ) { + return true; + } + } + + return false; + } }