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

Display the "blog not public" notification where relevant #14690

Merged
merged 13 commits into from
Apr 17, 2020
Merged
102 changes: 66 additions & 36 deletions admin/class-admin-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function __construct() {
$this->asset_manager = new WPSEO_Admin_Asset_Manager();

add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_dismissible' ] );
add_action( 'admin_init', [ $this, 'blog_public_notice' ], 15 );
add_action( 'admin_init', [ $this, 'yoast_plugin_suggestions_notification' ], 15 );
add_action( 'admin_init', [ $this, 'recalculate_notice' ], 15 );
add_action( 'admin_init', [ $this, 'unsupported_php_notice' ], 15 );
Expand All @@ -45,6 +44,13 @@ public function __construct() {
add_action( 'admin_init', [ $this, 'handle_notifications' ], 15 );
add_action( 'admin_notices', [ $this, 'permalink_settings_notice' ] );

/*
* The `admin_notices` hook fires on single site admin pages vs.
* `network_admin_notices` which fires on multisite admin pages and
* `user_admin_notices` which fires on multisite user admin pagss.
*/
add_action( 'admin_notices', [ $this, 'search_engines_discouraged_notice' ] );

$health_checks = [
new WPSEO_Health_Check_Page_Comments(),
new WPSEO_Health_Check_Ryte(),
Expand Down Expand Up @@ -104,37 +110,6 @@ public function enqueue_dismissible() {
$this->asset_manager->enqueue_style( 'dismissible' );
}

/**
* Add a notification if the blog is not publicly visible.
*/
public function blog_public_notice() {

$info_message = '<strong>' . __( 'Huge SEO Issue: You\'re blocking access to robots.', 'wordpress-seo' ) . '</strong> ';
$info_message .= sprintf(
/* translators: %1$s resolves to the opening tag of the link to the reading settings, %1$s resolves to the closing tag for the link */
__( 'You must %1$sgo to your Reading Settings%2$s and uncheck the box for Search Engine Visibility.', 'wordpress-seo' ),
'<a href="' . esc_url( admin_url( 'options-reading.php' ) ) . '">',
'</a>'
);

$notification_options = [
'type' => Yoast_Notification::ERROR,
'id' => 'wpseo-dismiss-blog-public-notice',
'priority' => 1.0,
'capabilities' => 'wpseo_manage_options',
];

$notification = new Yoast_Notification( $info_message, $notification_options );

$notification_center = Yoast_Notification_Center::get();
if ( ! $this->is_blog_public() ) {
$notification_center->add_notification( $notification );
}
else {
$notification_center->remove_notification( $notification );
}
}

/**
* Determines whether a suggested plugins notification needs to be displayed.
*
Expand Down Expand Up @@ -416,12 +391,12 @@ private function load_xml_sitemaps_admin() {
}

/**
* Check if the site is set to be publicly visible.
* Checks whether search engines are discouraged from indexing the site.
*
* @return bool
* @return bool Whether search engines are discouraged from indexing the site.
*/
private function is_blog_public() {
return (string) get_option( 'blog_public' ) === '1';
private function are_search_engines_discouraged() {
return (string) get_option( 'blog_public' ) === '0';
}

/**
Expand Down Expand Up @@ -509,6 +484,51 @@ public function permalink_settings_notice() {
}
}

/**
* Determines whether and where the "search engines discouraged" admin notice should be displayed.
*
* @return bool Whether the "search engines discouraged" admin notice should be displayed.
*/
private function should_display_search_engines_discouraged_notice() {
return (
$this->are_search_engines_discouraged()
&& WPSEO_Capability_Utils::current_user_can( 'manage_options' )
&& WPSEO_Options::get( 'ignore_search_engines_discouraged_notice', false ) === false
&& (
$this->on_wpseo_admin_page()
|| in_array( $this->pagenow, [
'index.php',
'plugins.php',
'update-core.php',
], true )
)
);
}

/**
* Displays an admin notice when WordPress is set to discourage search engines from indexing the site.
*
* @return void
*/
public function search_engines_discouraged_notice() {
if ( ! $this->should_display_search_engines_discouraged_notice() ) {
return;
}

printf(
'<div id="robotsmessage" class="notice notice-error"><p><strong>%1$s</strong> %2$s <button type="button" id="robotsmessage-dismiss-button" class="button-link hide-if-no-js" data-nonce="%3$s">%4$s</button></p></div>',
esc_html__( 'Huge SEO Issue: You\'re blocking access to robots.', 'wordpress-seo' ),
sprintf(
/* translators: 1: Link start tag to the WordPress Reading Settings page, 2: Link closing tag. */
esc_html__( 'If you want search engines to show this site in their results, you must %1$sgo to your Reading Settings%2$s and uncheck the box for Search Engine Visibility.', 'wordpress-seo' ),
'<a href="' . esc_url( admin_url( 'options-reading.php' ) ) . '">',
'</a>'
),
esc_js( wp_create_nonce( 'wpseo-ignore' ) ),
esc_html__( 'I don\'t want this site to show in the search results.', 'wordpress-seo' )
);
}

/* ********************* DEPRECATED METHODS ********************* */

/**
Expand Down Expand Up @@ -609,4 +629,14 @@ public function has_default_tagline() {
public function permalink_notice() {
_deprecated_function( __METHOD__, 'WPSEO 13.2' );
}

/**
* Add an alert if the blog is not publicly visible.
*
* @deprecated 14.1
* @codeCoverageIgnore
*/
public function blog_public_notice() {
_deprecated_function( __METHOD__, 'WPSEO 14.1' );
}
}
6 changes: 3 additions & 3 deletions admin/formatter/class-metabox-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ private function get_defaults() {
}

/**
* Returns a link to the settings page, if the user has the right capabilities.
* Returns a link to the General Settings page, if the user has the right capabilities.
* Returns an empty string otherwise.
*
* @return string The settings link.
* @return string The General Settings link.
*/
private function get_settings_link() {
if ( current_user_can( 'manage_options' ) ) {
return admin_url( 'options-general.php' );
return esc_url( admin_url( 'options-general.php' ) );
}

return '';
Expand Down
9 changes: 9 additions & 0 deletions inc/class-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function __construct() {
'12.4-RC0' => 'upgrade_124',
'12.8-RC0' => 'upgrade_128',
'13.2-RC0' => 'upgrade_132',
'14.1-RC0' => 'upgrade_141',
];

array_walk( $routines, [ $this, 'run_upgrade_routine' ], $version );
Expand Down Expand Up @@ -728,6 +729,14 @@ private function upgrade_132() {
WPSEO_Capability_Manager_Factory::get()->add();
}

/**
* Performs the 14.1 upgrade.
*/
private function upgrade_141() {
Yoast_Notification_Center::get()->remove_notification_by_id( 'wpseo-dismiss-blog-public-notice' );
}


/**
* Removes all notifications saved in the database under 'wp_yoast_notifications'.
*
Expand Down
58 changes: 38 additions & 20 deletions inc/options/class-wpseo-option-wpseo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ class WPSEO_Option_Wpseo extends WPSEO_Option {
*/
protected $defaults = [
// Non-form fields, set via (ajax) function.
'ms_defaults_set' => false,
'ms_defaults_set' => false,
'ignore_search_engines_discouraged_notice' => false,
// Non-form field, should only be set via validation routine.
'version' => '', // Leave default as empty to ensure activation/upgrade works.
'version' => '', // Leave default as empty to ensure activation/upgrade works.

// Form fields.
'disableadvanced_meta' => true,
'ryte_indexability' => true,
'baiduverify' => '', // Text field.
'googleverify' => '', // Text field.
'msverify' => '', // Text field.
'yandexverify' => '',
'site_type' => '', // List of options.
'has_multiple_authors' => '',
'environment_type' => '',
'content_analysis_active' => true,
'keyword_analysis_active' => true,
'enable_admin_bar_menu' => true,
'enable_cornerstone_content' => true,
'enable_xml_sitemap' => true,
'enable_text_link_counter' => true,
'show_onboarding_notice' => false,
'first_activated_on' => false,
'myyoast-oauth' => [
'disableadvanced_meta' => true,
'ryte_indexability' => true,
'baiduverify' => '', // Text field.
'googleverify' => '', // Text field.
'msverify' => '', // Text field.
'yandexverify' => '',
'site_type' => '', // List of options.
'has_multiple_authors' => '',
'environment_type' => '',
'content_analysis_active' => true,
'keyword_analysis_active' => true,
'enable_admin_bar_menu' => true,
'enable_cornerstone_content' => true,
'enable_xml_sitemap' => true,
'enable_text_link_counter' => true,
'show_onboarding_notice' => false,
'first_activated_on' => false,
'myyoast-oauth' => [
'config' => [
'clientId' => null,
'secret' => null,
Expand All @@ -63,6 +64,7 @@ class WPSEO_Option_Wpseo extends WPSEO_Option {
* @var array
*/
public $ms_exclude = [
'ignore_search_engines_discouraged_notice',
/* Privacy. */
'baiduverify',
'googleverify',
Expand Down Expand Up @@ -241,6 +243,7 @@ protected function validate_option( $dirty, $clean, $old ) {
* Boolean dismiss warnings - not fields - may not be in form
* (and don't need to be either as long as the default is false).
*/
case 'ignore_search_engines_discouraged_notice':
case 'ms_defaults_set':
if ( isset( $dirty[ $key ] ) ) {
$clean[ $key ] = WPSEO_Utils::validate_bool( $dirty[ $key ] );
Expand Down Expand Up @@ -387,6 +390,21 @@ protected function get_verify_features_default_option_filter_hook() {
* @return array Cleaned option.
*/
protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) {
// Deal with value change from text string to boolean.
$value_change = [
'ignore_search_engines_discouraged_notice',
];

foreach ( $value_change as $key ) {
if ( isset( $option_value[ $key ] ) && in_array( $option_value[ $key ], [
'ignore',
'done',
], true )
) {
$option_value[ $key ] = true;
}
}

return $option_value;
}
}
5 changes: 5 additions & 0 deletions js/src/wp-seo-admin-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
$button.attr( "aria-expanded", ! isPanelVisible );
} );
} );

// Dismiss the "search engines discouraged" admin notice.
jQuery( "#robotsmessage button" ).on( "click", function() {
wpseoSetIgnore( "search_engines_discouraged_notice", "robotsmessage", jQuery( this ).data( "nonce" ) );
} );
} );

window.wpseoSetIgnore = wpseoSetIgnore;
Expand Down