| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Services\Health_Check; |
| 4 |
|
| 5 |
use WPSEO_Ryte_Option; |
| 6 |
use WPSEO_Utils; |
| 7 |
use Yoast\WP\SEO\Integrations\Admin\Ryte_Integration; |
| 8 |
|
| 9 |
/** |
| 10 |
* Runs the Ryte health check. |
| 11 |
*/ |
| 12 |
class Ryte_Runner implements Runner_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The Ryte_Integration object that the health check uses to check the site's indexability. |
| 16 |
* |
| 17 |
* @var Ryte_Integration |
| 18 |
*/ |
| 19 |
private $ryte; |
| 20 |
|
| 21 |
/** |
| 22 |
* Set to true when the health check gets a valid response from Ryte. |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
private $got_valid_response; |
| 27 |
|
| 28 |
/** |
| 29 |
* The error that is set when the health check gets a response error from Ryte. |
| 30 |
* |
| 31 |
* @var array|null |
| 32 |
*/ |
| 33 |
private $response_error; |
| 34 |
|
| 35 |
/** |
| 36 |
* The Ryte option that represents the site's indexability. |
| 37 |
* |
| 38 |
* @var WPSEO_Ryte_Option |
| 39 |
*/ |
| 40 |
private $ryte_option; |
| 41 |
|
| 42 |
/** |
| 43 |
* The WPSEO_Utils class used to determine whether the site is in development mode. |
| 44 |
* |
| 45 |
* @var WPSEO_Utils |
| 46 |
*/ |
| 47 |
private $utils; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constructor. |
| 51 |
* |
| 52 |
* @param Ryte_Integration $ryte The Ryte_Integration object that the health check uses to check indexability. |
| 53 |
* @param WPSEO_Utils $utils The WPSEO_Utils object used to determine whether the site is in development mode. |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
Ryte_Integration $ryte, |
| 57 |
WPSEO_Utils $utils |
| 58 |
) { |
| 59 |
$this->ryte = $ryte; |
| 60 |
$this->utils = $utils; |
| 61 |
$this->got_valid_response = false; |
| 62 |
$this->ryte_option = $ryte->get_option(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Runs the health check. Checks if Ryte is accessible and whether the site is indexable. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function run() { |
| 71 |
if ( ! $this->should_run() ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$this->fetch_from_ryte(); |
| 76 |
|
| 77 |
if ( ! $this->got_valid_response ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$this->set_ryte_option(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Attempts to get a new indexability status from Ryte. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
private function fetch_from_ryte() { |
| 90 |
$this->ryte->fetch_from_ryte(); |
| 91 |
$response = $this->ryte->get_response(); |
| 92 |
|
| 93 |
if ( is_array( $response ) && isset( $response['is_error'] ) ) { |
| 94 |
$this->got_valid_response = false; |
| 95 |
$this->response_error = $response; |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$this->got_valid_response = true; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Sets the Ryte option based on the response from Ryte. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
private function set_ryte_option() { |
| 108 |
$this->ryte_option = $this->ryte->get_option(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Checks if the site is a live production site that has Ryte enabled. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function should_run() { |
| 117 |
if ( wp_get_environment_type() !== 'production' ) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
if ( wp_debug_mode() || $this->utils->is_development_mode() ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
if ( get_option( 'blog_public' ) === '0' ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
$ryte_option = $this->ryte->get_option(); |
| 130 |
if ( ! $ryte_option->is_enabled() ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Checks if the site is indexable. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
public function is_successful() { |
| 143 |
if ( ! $this->could_fetch() ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
$ryte_status = $this->ryte_option->get_status(); |
| 148 |
|
| 149 |
if ( $ryte_status === WPSEO_Ryte_Option::IS_INDEXABLE ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Checks if the site's indexability is unknown. |
| 158 |
* |
| 159 |
* @return bool Returns true if the site indexability is unknown even though getting a response from Ryte was successful. |
| 160 |
*/ |
| 161 |
public function has_unknown_indexability() { |
| 162 |
if ( ! $this->could_fetch() ) { |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
$ryte_status = $this->ryte_option->get_status(); |
| 167 |
|
| 168 |
if ( $ryte_status === WPSEO_Ryte_Option::IS_INDEXABLE ) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
if ( $ryte_status === WPSEO_Ryte_Option::IS_NOT_INDEXABLE ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
// This return statement should never be reached. |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Checks if the health check was able to get a valid response from Ryte. |
| 182 |
* |
| 183 |
* @return bool |
| 184 |
*/ |
| 185 |
private function could_fetch() { |
| 186 |
if ( ! $this->got_valid_response ) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
$ryte_status = $this->ryte_option->get_status(); |
| 191 |
|
| 192 |
if ( $ryte_status === WPSEO_Ryte_Option::CANNOT_FETCH ) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Checks whether there was a response error when attempting a request to Ryte. |
| 201 |
* |
| 202 |
* @return bool True if the health check got a valid error response. |
| 203 |
*/ |
| 204 |
public function got_response_error() { |
| 205 |
return isset( $this->response_error ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns the error response is there was one. |
| 210 |
* |
| 211 |
* @return array|null |
| 212 |
*/ |
| 213 |
public function get_error_response() { |
| 214 |
if ( ! $this->got_response_error() ) { |
| 215 |
return null; |
| 216 |
} |
| 217 |
|
| 218 |
return $this->response_error; |
| 219 |
} |
| 220 |
} |
| 221 |
|