| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Internals |
| 6 |
*/ |
| 7 |
|
| 8 |
use Yoast\WP\SEO\Presenters\Admin\Alert_Presenter; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents the health check for Ryte. |
| 12 |
*/ |
| 13 |
class WPSEO_Health_Check_Ryte extends WPSEO_Health_Check { |
| 14 |
|
| 15 |
/** |
| 16 |
* The name of the test. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $test = 'yoast-health-check-ryte'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Runs the test. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function run() { |
| 28 |
// If Ryte is disabled or the blog is not public or development mode is on, don't run code. |
| 29 |
if ( ! $this->should_run() ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
/* |
| 34 |
* Run the request to fetch the indexability status. Set last fetch time. |
| 35 |
* Update the Ryte option status. Will run a new request only if the last |
| 36 |
* one is not within the `WPSEO_Ryte_Option::FETCH_LIMIT` time interval. |
| 37 |
*/ |
| 38 |
$wpseo_ryte = new WPSEO_Ryte(); |
| 39 |
$wpseo_ryte->fetch_from_ryte(); |
| 40 |
|
| 41 |
// Get the Ryte API response to properly handle errors. |
| 42 |
$response = $wpseo_ryte->get_response(); |
| 43 |
|
| 44 |
if ( is_array( $response ) && isset( $response['is_error'] ) ) { |
| 45 |
$this->response_error( $response ); |
| 46 |
|
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// The request was successful: get the updated Ryte option. |
| 51 |
$ryte_option = $this->get_ryte_option(); |
| 52 |
|
| 53 |
switch ( $ryte_option->get_status() ) { |
| 54 |
case WPSEO_Ryte_Option::IS_NOT_INDEXABLE: |
| 55 |
$this->is_not_indexable_response(); |
| 56 |
break; |
| 57 |
case WPSEO_Ryte_Option::IS_INDEXABLE: |
| 58 |
$this->is_indexable_response(); |
| 59 |
break; |
| 60 |
case WPSEO_Ryte_Option::NOT_FETCHED: |
| 61 |
default: // WPSEO_Ryte_Option::CANNOT_FETCH. |
| 62 |
$this->unknown_indexability_response(); |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Checks whether the Ryte Site Health check should run. |
| 69 |
* |
| 70 |
* Checks for the WordPress environment type, checks if Ryte integration is |
| 71 |
* enabled, the blog is public, and the Yoast SEO environment is not development mode. |
| 72 |
* |
| 73 |
* @return bool Whether the Ryte Site Health check should run. |
| 74 |
*/ |
| 75 |
protected function should_run() { |
| 76 |
if ( wp_get_environment_type() !== 'production' ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
$ryte_option = $this->get_ryte_option(); |
| 81 |
if ( ! $ryte_option->is_enabled() ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
if ( get_option( 'blog_public' ) === '0' ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
return ! $this->is_development_mode(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Checks if debug mode is on but Yoast development mode is not on (i.e. for non-Yoast developers). |
| 94 |
* |
| 95 |
* @return bool True when debug mode is on and Yoast development mode is not on. |
| 96 |
*/ |
| 97 |
protected function is_development_mode() { |
| 98 |
return wp_debug_mode() && ! WPSEO_Utils::is_development_mode(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns a new instance of WPSEO_Ryte_Option. |
| 103 |
* |
| 104 |
* @return WPSEO_Ryte_Option New Ryte Option. |
| 105 |
*/ |
| 106 |
protected function get_ryte_option() { |
| 107 |
return new WPSEO_Ryte_Option(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Adds the content for a failed Ryte API request. |
| 112 |
* |
| 113 |
* @param array $response The error details. |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
protected function response_error( $response ) { |
| 118 |
$this->label = esc_html__( 'An error occurred while checking whether your site can be found by search engines', 'wordpress-seo' ); |
| 119 |
$this->status = self::STATUS_RECOMMENDED; |
| 120 |
$this->badge['color'] = 'red'; |
| 121 |
|
| 122 |
$this->description = sprintf( |
| 123 |
'%s<br><br>%s', |
| 124 |
sprintf( |
| 125 |
/* translators: %1$s: Expands to 'Ryte', %2$s: Expands to 'Yoast SEO'. */ |
| 126 |
esc_html__( '%1$s offers a free indexability check for %2$s users. The request to %1$s to check whether your site can be found by search engines failed due to an error.', 'wordpress-seo' ), |
| 127 |
'Ryte', |
| 128 |
'Yoast SEO' |
| 129 |
), |
| 130 |
sprintf( |
| 131 |
/* translators: 1: The Ryte response raw error code, if any. 2: The error message. 3: The WordPress error code, if any. */ |
| 132 |
__( 'Error details: %1$s %2$s %3$s', 'wordpress-seo' ), |
| 133 |
$response['raw_error_code'], |
| 134 |
$response['message'], |
| 135 |
$response['wp_error_code'] |
| 136 |
) |
| 137 |
); |
| 138 |
|
| 139 |
$this->actions = sprintf( |
| 140 |
/* translators: %1$s: Opening tag of the link to the Yoast help center, %2$s: Link closing tag. */ |
| 141 |
esc_html__( 'If this is a live site, %1$sit is recommended that you figure out why the check failed.%2$s', 'wordpress-seo' ), |
| 142 |
'<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/onpagerequestfailed' ) ) . '" target="_blank">', |
| 143 |
WPSEO_Admin_Utils::get_new_tab_message() . '</a>' |
| 144 |
); |
| 145 |
$this->actions .= '<br /><br />'; |
| 146 |
|
| 147 |
$this->add_ryte_link(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Adds the content for the "Cannot be indexed" response. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
protected function is_not_indexable_response() { |
| 156 |
$this->label = esc_html__( 'Your site cannot be found by search engines', 'wordpress-seo' ); |
| 157 |
$this->status = self::STATUS_CRITICAL; |
| 158 |
$this->badge['color'] = 'red'; |
| 159 |
|
| 160 |
$this->description = sprintf( |
| 161 |
/* translators: %1$s: Expands to 'Ryte', %2$s: Expands to 'Yoast SEO'. */ |
| 162 |
esc_html__( '%1$s offers a free indexability check for %2$s users and it has determined that your site cannot be found by search engines. If this site is live or about to become live, this should be fixed.', 'wordpress-seo' ), |
| 163 |
'Ryte', |
| 164 |
'Yoast SEO' |
| 165 |
); |
| 166 |
|
| 167 |
$this->actions = sprintf( |
| 168 |
/* translators: %1$s: Opening tag of the link to the Yoast help center, %2$s: Link closing tag. */ |
| 169 |
esc_html__( '%1$sRead more about troubleshooting search engine visibility.%2$s', 'wordpress-seo' ), |
| 170 |
'<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/onpageindexerror' ) ) . '" target="_blank">', |
| 171 |
WPSEO_Admin_Utils::get_new_tab_message() . '</a>' |
| 172 |
); |
| 173 |
$this->actions .= '<br /><br />'; |
| 174 |
|
| 175 |
$this->add_ryte_link(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Adds the content for the "Cannot tell if it can be indexed" response. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
protected function unknown_indexability_response() { |
| 184 |
$this->label = sprintf( |
| 185 |
/* translators: %1$s: Expands to 'Ryte'. */ |
| 186 |
esc_html__( '%1$s cannot determine whether your site can be found by search engines', 'wordpress-seo' ), |
| 187 |
'Ryte' |
| 188 |
); |
| 189 |
$this->status = self::STATUS_RECOMMENDED; |
| 190 |
$this->badge['color'] = 'red'; |
| 191 |
|
| 192 |
/* translators: %1$s: Expands to 'Ryte', %2$s: Expands to 'Yoast SEO'. */ |
| 193 |
$description = esc_html__( |
| 194 |
'%1$s offers a free indexability check for %2$s users and right now it has trouble determining |
| 195 |
whether search engines can find your site. This could have several (legitimate) reasons and |
| 196 |
is not a problem in itself. If this is a live site, it is recommended that you figure out why |
| 197 |
the %1$s check failed.', |
| 198 |
'wordpress-seo' |
| 199 |
); |
| 200 |
$this->description = sprintf( |
| 201 |
$description, |
| 202 |
'Ryte', |
| 203 |
'Yoast SEO' |
| 204 |
); |
| 205 |
$this->description .= '<br />'; |
| 206 |
|
| 207 |
/* translators: %1$s: Expands to 'Ryte', %2$s: Link start tag to the Yoast help center, %3$s: Link closing tag. */ |
| 208 |
$alert_text = esc_html__( |
| 209 |
'As the indexability status of your website can only be fetched from %1$s every 15 seconds, |
| 210 |
a first step could be to wait at least 15 seconds and refresh the Site Health page. If that did not help, |
| 211 |
%2$sread more about troubleshooting search engine visibility%3$s.', |
| 212 |
'wordpress-seo' |
| 213 |
); |
| 214 |
$alert_content = sprintf( |
| 215 |
$alert_text, |
| 216 |
'Ryte', |
| 217 |
'<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/onpagerequestfailed' ) ) . '" target="_blank">', |
| 218 |
WPSEO_Admin_Utils::get_new_tab_message() . '</a>' |
| 219 |
); |
| 220 |
|
| 221 |
$alert = new Alert_Presenter( $alert_content, 'info' ); |
| 222 |
$this->description .= $alert->present(); |
| 223 |
|
| 224 |
$this->add_ryte_link(); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Adds the content for the "Can be indexed" response. |
| 229 |
* |
| 230 |
* @return void |
| 231 |
*/ |
| 232 |
protected function is_indexable_response() { |
| 233 |
$this->label = esc_html__( 'Your site can be found by search engines', 'wordpress-seo' ); |
| 234 |
$this->status = self::STATUS_GOOD; |
| 235 |
$this->badge['color'] = 'blue'; |
| 236 |
|
| 237 |
/* translators: %1$s: Expands to 'Ryte', %2$s: Expands to 'Yoast SEO'. */ |
| 238 |
$description = esc_html__( |
| 239 |
'%1$s offers a free indexability check for %2$s users, and it shows that your site can be found by search engines.', |
| 240 |
'wordpress-seo' |
| 241 |
); |
| 242 |
$this->description = sprintf( $description, 'Ryte', 'Yoast SEO' ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Adds the link to the Ryte site to the actions. |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
protected function add_ryte_link() { |
| 251 |
$this->actions .= sprintf( |
| 252 |
/* translators: %1$s: Opening tag of the link to the Yoast Ryte website, %2$s: Expands to 'Ryte', %3$s: Link closing tag. */ |
| 253 |
esc_html__( '%1$sGo to %2$s to analyze your entire site%3$s', 'wordpress-seo' ), |
| 254 |
'<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/rytelp' ) ) . '" target="_blank">', |
| 255 |
'Ryte', |
| 256 |
WPSEO_Admin_Utils::get_new_tab_message() . '</a>' |
| 257 |
); |
| 258 |
} |
| 259 |
} |
| 260 |
|