Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Checkbox to allow / disallow robots to index #124

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions class-unlist-posts-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -96,6 +97,31 @@ function metabox_render( $post ) {
</label>
</p>
<p class="description"><?php esc_html_e( 'This will hide the post from your site, The post can only be accessed from direct URL.', 'unlist-posts' ); ?> </p>


<?php
// Enable Robots
$enable_robots = get_option( 'unlist_posts_enable_robots', array() );

if ( '' === $enable_robots ) {
$enable_robots = array();
}

$checked = '';

if ( in_array( (int) $post->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' );
?>
<p>
<label class="checkbox-inline">
<input name="unlist_posts_enable_robots" type="checkbox" <?php echo esc_attr( $checked ); ?> value=""><?php esc_html_e( 'Allow Robots to Crawl?', 'unlist-posts' ); ?>
</label>
</p>
<p class="description"><?php esc_html_e( 'By default, Unlist Posts does not allow indexing of unlisted posts, check this box to enable indexing.', 'unlist-posts' ); ?> </p>
<?php
}

Expand Down Expand Up @@ -150,6 +176,57 @@ public function save_meta( $post_id ) {
update_option( 'unlist_posts', $hidden_posts );
}

/**
* Save meta field.
*
* @param POST $post_id Currennt post object which is being displayed.
*
* @return Void
*/
public function save_meta_enable_robots( $post_id ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function save_meta_enable_robots has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.

// 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.
*
Expand Down
19 changes: 14 additions & 5 deletions class-unlist-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ public function change_robots_for_rankmath( $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() ) {
$robots['index'] = 'noindex';
if ( !in_array( get_the_ID(), $enable_robots, true ) && false !== get_the_ID()) {
$robots['index'] = 'noindex';
}
}
return $robots;
}
Expand Down Expand Up @@ -175,9 +178,12 @@ 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() ) {
wp_no_robots();
if ( !in_array( get_the_ID(), $enable_robots, true ) && false !== get_the_ID()) {
wp_no_robots();
}
}
}

Expand All @@ -194,11 +200,14 @@ 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() ) {
// Disable robots tags from Yoast SEO.
add_filter( 'wpseo_robots_array', '__return_empty_array' );
return wp_robots_no_robots( $robots );
if ( !in_array( get_the_ID(), $enable_robots, true ) && false !== get_the_ID()) {
// Disable robots tags from Yoast SEO.
add_filter( 'wpseo_robots_array', '__return_empty_array' );
return wp_robots_no_robots( $robots );
}
}

return $robots;
Expand Down