diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php new file mode 100644 index 0000000000..bd50d17377 --- /dev/null +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -0,0 +1,113 @@ +options = $options; + } + + /** + * Get the transient key. + * + * @return string The transient key. + */ + abstract protected function getTransientKey(); + + /** + * Get the API URL. + * + * @return string The API URL. + */ + abstract protected function getApiUrl(); + + /** + * Send a GET request. + * + * @param array $params The request parameters. + * @return mixed The response from the API. + */ + public function send_get_request($params) + { + return $this->send_request('GET', $params); + } + + /** + * Send a POST request. + * + * @param array $params The request parameters. + * @return mixed The response from the API. + */ + protected function send_post_request($params) + { + return $this->send_request('POST', $params); + } + + /** + * Send a request to the API. + * + * @param string $method The HTTP method (GET or POST). + * @param array $params The request parameters. + * @return mixed The response from the API, or false if a timeout is active. + */ + private function send_request($method, $params) + { + $transientKey = $this->getTransientKey(); + $apiUrl = $this->getApiUrl(); + + if (get_transient($transientKey . '_timeout_active') === true) { + return false; + } + + if (empty($params['body'])) { + $params['body'] = []; + } + + $params['body']['credentials'] = [ + 'wpr_email' => $this->options->get('consumer_email', ''), + 'wpr_key' => $this->options->get('consumer_key', ''), + ]; + + $params['method'] = strtoupper($method); + $response = wp_remote_request($apiUrl, $params); + + if (is_wp_error($response)) { + $this->set_timeout_transients($transientKey); + return false; + } + + delete_transient($transientKey . '_timeout_active'); + delete_transient($transientKey . '_timeout'); + + return $response; + } + + /** + * Set the timeout transients. + * + * @param string $transientKey The transient key. + */ + private function set_timeout_transients($transientKey) + { + $timeout = (int) get_transient($transientKey . '_timeout'); + $timeout = (0 === $timeout) + ? 300 + : (2 * $timeout <= DAY_IN_SECONDS + ? 2 * $timeout + : DAY_IN_SECONDS + ); + + set_transient($transientKey . '_timeout', $timeout, WEEK_IN_SECONDS); + set_transient($transientKey . '_timeout_active', true, $timeout); + } +} diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php new file mode 100644 index 0000000000..5db58833f2 --- /dev/null +++ b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php @@ -0,0 +1,14 @@ +getContainer()->add('plugin_information_client', PluginInformationClient::class) + ->addArgument( $this->getContainer()->get('options') ); + + $this->getContainer()->add('plugin_update_client', PluginUpdateClient::class) + ->addArgument( $this->getContainer()->get('options') ); } } diff --git a/inc/Engine/Plugin/InformationSubscriber.php b/inc/Engine/Plugin/InformationSubscriber.php index 93f11c3e78..571302fc8b 100644 --- a/inc/Engine/Plugin/InformationSubscriber.php +++ b/inc/Engine/Plugin/InformationSubscriber.php @@ -2,6 +2,7 @@ namespace WP_Rocket\Engine\Plugin; use WP_Rocket\Event_Management\Subscriber_Interface; +use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginInformationClient; /** * Manages the plugin information. @@ -128,7 +129,9 @@ private function is_requesting_rocket_info( $action, $args ) { * @return object|\WP_Error */ private function get_plugin_information() { - $response = wp_remote_get( $this->api_url ); + $client = new PluginInformationClient(); + $response = $client->send_get_request([]); + if ( is_wp_error( $response ) ) { return $this->get_request_error( $response->get_error_message() ); diff --git a/inc/Engine/Plugin/UpdaterSubscriber.php b/inc/Engine/Plugin/UpdaterSubscriber.php index 5224b7aa8c..7832c2b48e 100644 --- a/inc/Engine/Plugin/UpdaterSubscriber.php +++ b/inc/Engine/Plugin/UpdaterSubscriber.php @@ -5,6 +5,7 @@ use Plugin_Upgrader_Skin; use WP_Error; use WP_Rocket\Event_Management\{Event_Manager, Event_Manager_Aware_Subscriber_Interface}; +use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginUpdateClient; /** * Manages the plugin updates. @@ -305,24 +306,20 @@ public function disable_auto_updates( $update, $item ) { * } */ public function get_latest_version_data() { - $request = wp_remote_get( - $this->api_url, - [ - 'timeout' => 30, - ] - ); + $client = new PluginUpdateClient(); + $response = $client->send_get_request([]); - if ( is_wp_error( $request ) ) { + if (is_wp_error($response)) { return $this->get_request_error( [ - 'error_code' => $request->get_error_code(), - 'response' => $request->get_error_message(), + 'error_code' => $response->get_error_code(), + 'response' => $response->get_error_message(), ] ); } - $res = trim( wp_remote_retrieve_body( $request ) ); - $code = wp_remote_retrieve_response_code( $request ); + $res = trim( wp_remote_retrieve_body( $response ) ); + $code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { /**