-
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
Open
DogByteMarketing
wants to merge
4
commits into
brainstormforce:master
Choose a base branch
from
DogByteMarketing:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−27
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) ); | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 ) { | ||
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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.