| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Services\Health_Check; |
| 4 |
|
| 5 |
use WPSEO_Addon_Manager; |
| 6 |
use Yoast\WP\SEO\Helpers\Curl_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Runs the Curl health check. |
| 10 |
*/ |
| 11 |
class Curl_Runner implements Runner_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* Sets the minimum cURL version for this health check to pass. |
| 15 |
*/ |
| 16 |
const MINIMUM_CURL_VERSION = '7.34.0'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Sets the target URL for testing whether the MyYoast API is reachable. |
| 20 |
*/ |
| 21 |
const MYYOAST_API_REQUEST_URL = 'sites/current'; |
| 22 |
|
| 23 |
/** |
| 24 |
* True when cURL is installed. |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
private $curl_installed = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* True when cURL is equal to or more recent than MINIMUM_CURL_VERSION. |
| 32 |
* |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
private $curl_is_recent = false; |
| 36 |
|
| 37 |
/** |
| 38 |
* True when the health check got a response from the MyYoast API. |
| 39 |
* |
| 40 |
* @var bool |
| 41 |
*/ |
| 42 |
private $got_my_yoast_api_response = false; |
| 43 |
|
| 44 |
/** |
| 45 |
* True when there are Yoast add-ons installed. |
| 46 |
* |
| 47 |
* @var bool |
| 48 |
*/ |
| 49 |
private $has_installed_addons = false; |
| 50 |
|
| 51 |
/** |
| 52 |
* The add-on manger that the health check uses to determine if there are any add-ons installed. |
| 53 |
* |
| 54 |
* @var WPSEO_Addon_Manager |
| 55 |
*/ |
| 56 |
private $addon_manager; |
| 57 |
|
| 58 |
/** |
| 59 |
* Factory for the MyYoast API request object that the health check uses to check if cURL works correctly. |
| 60 |
* |
| 61 |
* @var MyYoast_Api_Request_Factory |
| 62 |
*/ |
| 63 |
private $my_yoast_api_request_factory; |
| 64 |
|
| 65 |
/** |
| 66 |
* The cURL helper from which the health check gets information about the installed cURL version. |
| 67 |
* |
| 68 |
* @var Curl_Helper |
| 69 |
*/ |
| 70 |
private $curl_helper; |
| 71 |
|
| 72 |
/** |
| 73 |
* Constructor. |
| 74 |
* |
| 75 |
* @psalm-suppress InvalidClass MyYoast is a product name, so it's an exception to the class naming conventions. |
| 76 |
* @param WPSEO_Addon_Manager $addon_manager The add-on manager. |
| 77 |
* @param MyYoast_Api_Request_Factory $my_yoast_api_request_factory A MyYoast API request object. |
| 78 |
* @param Curl_Helper $curl_helper A cURL helper object for obtaining cURL installation information. |
| 79 |
*/ |
| 80 |
public function __construct( |
| 81 |
WPSEO_Addon_Manager $addon_manager, |
| 82 |
MyYoast_Api_Request_Factory $my_yoast_api_request_factory, |
| 83 |
Curl_Helper $curl_helper |
| 84 |
) { |
| 85 |
$this->addon_manager = $addon_manager; |
| 86 |
$this->my_yoast_api_request_factory = $my_yoast_api_request_factory; |
| 87 |
$this->curl_helper = $curl_helper; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Runs the health check. Checks if cURL is installed and up to date, and if it's able to reach the MyYoast API |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function run() { |
| 96 |
$this->check_has_installed_addons(); |
| 97 |
$this->check_curl_installed(); |
| 98 |
|
| 99 |
if ( ! $this->curl_installed ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$this->check_curl_is_recent(); |
| 104 |
$this->check_can_reach_my_yoast_api(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Checks if there are installed Yoast add-ons. |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function check_has_installed_addons() { |
| 113 |
$this->has_installed_addons = $this->addon_manager->has_installed_addons(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks if cURL is installed. |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
private function check_curl_installed() { |
| 122 |
$this->curl_installed = $this->curl_helper->is_installed(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Checks if the installed cURL version is equal to or more recent than MINIMUM_CURL_VERSION. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private function check_curl_is_recent() { |
| 131 |
$version = $this->curl_helper->get_version(); |
| 132 |
|
| 133 |
if ( $version === null ) { |
| 134 |
$this->curl_is_recent = false; |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$this->curl_is_recent = version_compare( $version, self::MINIMUM_CURL_VERSION, '>=' ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Checks if cURL is able to reach the MyYoast API. |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
private function check_can_reach_my_yoast_api() { |
| 147 |
$api_request = $this->my_yoast_api_request_factory->create( self::MYYOAST_API_REQUEST_URL ); |
| 148 |
$got_response = $api_request->fire(); |
| 149 |
|
| 150 |
|
| 151 |
$this->got_my_yoast_api_response = $got_response; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns whether the health check was successful. |
| 156 |
* |
| 157 |
* @return bool True if all the routines for this health check were successful. |
| 158 |
*/ |
| 159 |
public function is_successful() { |
| 160 |
return $this->has_installed_addons && $this->curl_is_recent && $this->got_my_yoast_api_response; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Returns whether there are premium plugins installed. |
| 165 |
* |
| 166 |
* @return bool True if there are premium plugins installed. |
| 167 |
*/ |
| 168 |
public function has_premium_plugins_installed() { |
| 169 |
return $this->has_installed_addons; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns whether cURL was able to reach the MyYoast API. |
| 174 |
* |
| 175 |
* @return bool True if cURL was able to reach the MyYoast API. |
| 176 |
*/ |
| 177 |
public function can_reach_my_yoast_api() { |
| 178 |
return $this->got_my_yoast_api_response; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns whether the installed cURL version is recent enough. |
| 183 |
* |
| 184 |
* @return bool True if the installed cURL version is more recent than MINIMUM_CURL_VERSION. |
| 185 |
*/ |
| 186 |
public function has_recent_curl_version_installed() { |
| 187 |
return $this->curl_is_recent; |
| 188 |
} |
| 189 |
} |
| 190 |
|