-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
?> | ||
<p> | ||
<label class="checkbox-inline"> | ||
<input name="unlist_posts" type="checkbox" <?php echo esc_attr( $checked ); ?> value=""><?php esc_html_e( 'Unlist this post?', 'unlist-posts' ); ?> | ||
</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 | ||
$this->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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
// 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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
$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' ); | ||
?> | ||
<p> | ||
<label class="checkbox-inline"> | ||
<input name="unlist_posts" type="checkbox" <?php echo esc_attr( $checked ); ?> value=""><?php esc_html_e( 'Unlist this post?', 'unlist-posts' ); ?> | ||
</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 | ||
} | ||
|
||
/** | ||
* Render enable robots meta field | ||
* | ||
* @param Post $post The current post object. | ||
* @return void | ||
*/ | ||
function render_enable_robots( $post ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
$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 | ||
} | ||
|
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,8 +94,9 @@ 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() ) { | ||
if ( $this->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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
// 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; | ||
} | ||
|
||
} | ||
|
||
|
There was a problem hiding this comment.
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.