From 46d3a48c8a61d4c95b42756206b8c8577ba63b22 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Tue, 25 Jun 2024 15:14:21 +0200 Subject: [PATCH 01/39] first modification --- .../APIHandler/AbstractSafeAPIClient.php | 113 ++++++++++++++++++ .../APIHandler/PluginInformationClient.php | 14 +++ .../APIHandler/PluginLicenseClient.php | 1 + .../APIHandler/PluginUpdateClient.php | 15 +++ .../Common/JobManager/ServiceProvider.php | 7 ++ inc/Engine/Plugin/InformationSubscriber.php | 5 +- inc/Engine/Plugin/UpdaterSubscriber.php | 19 ++- 7 files changed, 162 insertions(+), 12 deletions(-) create mode 100644 inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php create mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php create mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginLicenseClient.php create mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php 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 ) { /** From 1bea6d5d6ff0357ee41aa4dad3b93a93f5cfe649 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Tue, 25 Jun 2024 15:42:23 +0200 Subject: [PATCH 02/39] Fix functions / variables names --- .../APIHandler/AbstractSafeAPIClient.php | 82 +++++++++++-------- .../APIHandler/PluginInformationClient.php | 34 ++++++-- .../APIHandler/PluginUpdateClient.php | 33 ++++++-- .../Common/JobManager/ServiceProvider.php | 8 +- inc/Engine/Plugin/InformationSubscriber.php | 7 +- inc/Engine/Plugin/UpdaterSubscriber.php | 6 +- 6 files changed, 111 insertions(+), 59 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index bd50d17377..fb85a67326 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -5,15 +5,29 @@ use WP_Rocket\Admin\Options_Data; /** - * AbstractSafeAPIClient is an abstract class that provides a base for making API requests. + * Class AbstractSafeAPIClient + * + * This abstract class provides a base for making API requests. * It includes methods for sending GET and POST requests, and handles transient caching to mitigate the impact of API failures. + * + * @package WP_Rocket\Engine\Common\JobManager\APIHandler */ -abstract class AbstractSafeAPIClient -{ +abstract class AbstractSafeAPIClient { + + + /** + * Options data. + * + * @var Options_Data $options WP Rocket options data. + */ private $options; - public function __construct(Options_Data $options) - { + /** + * AbstractSafeAPIClient constructor. + * + * @param Options_Data $options WP Rocket options data. + */ + public function __construct( Options_Data $options ) { $this->options = $options; } @@ -22,14 +36,14 @@ public function __construct(Options_Data $options) * * @return string The transient key. */ - abstract protected function getTransientKey(); + abstract protected function get_transient_key(); /** * Get the API URL. * * @return string The API URL. */ - abstract protected function getApiUrl(); + abstract protected function get_api_url(); /** * Send a GET request. @@ -37,9 +51,8 @@ abstract protected function getApiUrl(); * @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); + public function send_get_request( $params ) { + return $this->send_request( 'GET', $params ); } /** @@ -48,46 +61,44 @@ public function send_get_request($params) * @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); + 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. + * @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(); + private function send_request( $method, $params ) { + $transient_key = $this->get_transient_key(); + $api_url = $this->get_api_url(); - if (get_transient($transientKey . '_timeout_active') === true) { + if ( get_transient( $transient_key . '_timeout_active' ) === true ) { return false; } - if (empty($params['body'])) { + if ( empty( $params['body'] ) ) { $params['body'] = []; } $params['body']['credentials'] = [ - 'wpr_email' => $this->options->get('consumer_email', ''), - 'wpr_key' => $this->options->get('consumer_key', ''), + 'wpr_email' => $this->options->get( 'consumer_email', '' ), + 'wpr_key' => $this->options->get( 'consumer_key', '' ), ]; - $params['method'] = strtoupper($method); - $response = wp_remote_request($apiUrl, $params); + $params['method'] = strtoupper( $method ); + $response = wp_remote_request( $api_url, $params ); - if (is_wp_error($response)) { - $this->set_timeout_transients($transientKey); + if ( is_wp_error( $response ) ) { + $this->set_timeout_transients( $transient_key ); return false; } - delete_transient($transientKey . '_timeout_active'); - delete_transient($transientKey . '_timeout'); + delete_transient( $transient_key . '_timeout_active' ); + delete_transient( $transient_key . '_timeout' ); return $response; } @@ -95,19 +106,18 @@ private function send_request($method, $params) /** * Set the timeout transients. * - * @param string $transientKey The transient key. + * @param string $transient_key The transient key. */ - private function set_timeout_transients($transientKey) - { - $timeout = (int) get_transient($transientKey . '_timeout'); - $timeout = (0 === $timeout) + private function set_timeout_transients( $transient_key ) { + $timeout = (int) get_transient( $transient_key . '_timeout' ); + $timeout = ( 0 === $timeout ) ? 300 - : (2 * $timeout <= DAY_IN_SECONDS + : ( 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); + set_transient( $transient_key . '_timeout', $timeout, WEEK_IN_SECONDS ); + set_transient( $transient_key . '_timeout_active', true, $timeout ); } } diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php index 5db58833f2..5641250dac 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php @@ -1,14 +1,36 @@ getContainer()->add('plugin_information_client', PluginInformationClient::class) - ->addArgument( $this->getContainer()->get('options') ); + $this->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') ); + $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 571302fc8b..8e61073c01 100644 --- a/inc/Engine/Plugin/InformationSubscriber.php +++ b/inc/Engine/Plugin/InformationSubscriber.php @@ -2,7 +2,7 @@ namespace WP_Rocket\Engine\Plugin; use WP_Rocket\Event_Management\Subscriber_Interface; -use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginInformationClient; +use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginInformationClient; /** * Manages the plugin information. @@ -129,9 +129,8 @@ private function is_requesting_rocket_info( $action, $args ) { * @return object|\WP_Error */ private function get_plugin_information() { - $client = new PluginInformationClient(); - $response = $client->send_get_request([]); - + $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 7832c2b48e..14d173b6a6 100644 --- a/inc/Engine/Plugin/UpdaterSubscriber.php +++ b/inc/Engine/Plugin/UpdaterSubscriber.php @@ -306,10 +306,10 @@ public function disable_auto_updates( $update, $item ) { * } */ public function get_latest_version_data() { - $client = new PluginUpdateClient(); - $response = $client->send_get_request([]); + $client = new PluginUpdateClient(); + $response = $client->send_get_request( [] ); - if (is_wp_error($response)) { + if ( is_wp_error( $response ) ) { return $this->get_request_error( [ 'error_code' => $response->get_error_code(), From 175cd88b6482f28ee3d37638c3a348b955d0423a Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 26 Jun 2024 03:34:08 +0200 Subject: [PATCH 03/39] Fix endpoints --- .../Common/JobManager/APIHandler/PluginInformationClient.php | 2 +- inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php index 5641250dac..9a852ed3ba 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php @@ -31,6 +31,6 @@ protected function get_transient_key() { * @return string The API URL for plugin information. */ protected function get_api_url() { - return 'https://wp-rocket.me'; + return 'https://wp-rocket.me/plugin_information.php'; } } diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php index fb49367f30..a0b7fa5a41 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php @@ -31,6 +31,6 @@ protected function get_transient_key() { * @return string The API URL for plugin updates. */ protected function get_api_url() { - return 'https://wp-rocket.me'; + return 'https://wp-rocket.me/check_update.php'; } } From c99735496b176e9deb8d1e85575b11598f7ba7f7 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 26 Jun 2024 03:38:53 +0200 Subject: [PATCH 04/39] Adds User Information API --- .../APIHandler/AbstractSafeAPIClient.php | 2 +- .../APIHandler/UserInformationClient.php | 36 +++++++++++++++++++ inc/Engine/License/API/UserClient.php | 8 +++-- 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index fb85a67326..bb13519392 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -61,7 +61,7 @@ public function send_get_request( $params ) { * @param array $params The request parameters. * @return mixed The response from the API. */ - protected function send_post_request( $params ) { + public function send_post_request( $params ) { return $this->send_request( 'POST', $params ); } diff --git a/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php new file mode 100644 index 0000000000..165b5064bc --- /dev/null +++ b/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php @@ -0,0 +1,36 @@ +options->get( 'consumer_email', '' ) : rocket_get_constant( 'WP_ROCKET_EMAIL', '' ); - $response = wp_safe_remote_post( - self::USER_ENDPOINT, + $client = new UserInformationClient( $this->options ); + $response = $client->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), ] ); - if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { + + if ( false === $response ) { return false; } From f4f44a11845bf21a5ab8bc267997467533c3c484 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 26 Jun 2024 03:49:29 +0200 Subject: [PATCH 05/39] Adds Pricing client --- .../APIHandler/PluginInformationClient.php | 2 +- .../APIHandler/PluginPricingClient.php | 36 +++++++++++++++++++ .../APIHandler/PluginUpdateClient.php | 2 +- inc/Engine/License/API/PricingClient.php | 11 +++--- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php index 9a852ed3ba..0fc991bbd5 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php @@ -20,7 +20,7 @@ class PluginInformationClient extends AbstractSafeAPIClient { * @return string The transient key for plugin information. */ protected function get_transient_key() { - return 'plugin_information'; + return 'wp_rocket_plugin_information'; } /** diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php new file mode 100644 index 0000000000..fd9d8304d3 --- /dev/null +++ b/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php @@ -0,0 +1,36 @@ +set_timeout_transients(); + $client = new PluginPricingClient( ); + $response = $client->send_get_request(); + if ( false === $response ) { return false; } From e95309464d1c8e6346f0179f48626e37d55909c0 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 26 Jun 2024 03:51:55 +0200 Subject: [PATCH 06/39] Apply linter --- inc/Engine/License/API/PricingClient.php | 2 +- inc/Engine/License/API/UserClient.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index 58ed3d4de4..8bab964116 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -46,7 +46,7 @@ private function get_raw_pricing_data() { return false; } - $client = new PluginPricingClient( ); + $client = new PluginPricingClient(); $response = $client->send_get_request(); if ( false === $response ) { diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index 36ed831b4e..cefc2366ed 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -66,14 +66,13 @@ private function get_raw_user_data() { ? $this->options->get( 'consumer_email', '' ) : rocket_get_constant( 'WP_ROCKET_EMAIL', '' ); - $client = new UserInformationClient( $this->options ); + $client = new UserInformationClient( $this->options ); $response = $client->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), ] ); - if ( false === $response ) { return false; } From f3175b449efd3abed6b62ad28512fcf87d4da7b6 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 09:07:29 +0300 Subject: [PATCH 07/39] remove not needed headers --- .../APIHandler/AbstractSafeAPIClient.php | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index bb13519392..e5f08f095c 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -14,23 +14,6 @@ */ abstract class AbstractSafeAPIClient { - - /** - * Options data. - * - * @var Options_Data $options WP Rocket options data. - */ - private $options; - - /** - * AbstractSafeAPIClient constructor. - * - * @param Options_Data $options WP Rocket options data. - */ - public function __construct( Options_Data $options ) { - $this->options = $options; - } - /** * Get the transient key. * @@ -84,11 +67,6 @@ private function send_request( $method, $params ) { $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( $api_url, $params ); From f283e08d42f73c2de831c1ffca37bdcea05b56d7 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 09:26:16 +0300 Subject: [PATCH 08/39] adjust information client/subscriber --- .../APIHandler/AbstractSafeAPIClient.php | 10 ++++---- .../Common/JobManager/ServiceProvider.php | 13 ++++------- .../InformationAPIClient.php} | 6 +++-- inc/Engine/Plugin/InformationSubscriber.php | 23 +++++++++++-------- 4 files changed, 28 insertions(+), 24 deletions(-) rename inc/Engine/{Common/JobManager/APIHandler/PluginInformationClient.php => Plugin/InformationAPIClient.php} (83%) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index e5f08f095c..6eaf1ace23 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -2,7 +2,7 @@ namespace WP_Rocket\Engine\Common\JobManager\APIHandler; -use WP_Rocket\Admin\Options_Data; +use WP_Error; /** * Class AbstractSafeAPIClient @@ -34,7 +34,7 @@ abstract protected function get_api_url(); * @param array $params The request parameters. * @return mixed The response from the API. */ - public function send_get_request( $params ) { + public function send_get_request( $params = [] ) { return $this->send_request( 'GET', $params ); } @@ -59,8 +59,8 @@ private function send_request( $method, $params ) { $transient_key = $this->get_transient_key(); $api_url = $this->get_api_url(); - if ( get_transient( $transient_key . '_timeout_active' ) === true ) { - return false; + if ( true === get_transient( $transient_key . '_timeout_active' ) ) { + return new WP_Error( 429, __( 'Too many requests.', 'wp-rocket' ) ); } if ( empty( $params['body'] ) ) { @@ -72,7 +72,7 @@ private function send_request( $method, $params ) { if ( is_wp_error( $response ) ) { $this->set_timeout_transients( $transient_key ); - return false; + return $response; } delete_transient( $transient_key . '_timeout_active' ); diff --git a/inc/Engine/Common/JobManager/ServiceProvider.php b/inc/Engine/Common/JobManager/ServiceProvider.php index dcb69cbe92..bf17157783 100644 --- a/inc/Engine/Common/JobManager/ServiceProvider.php +++ b/inc/Engine/Common/JobManager/ServiceProvider.php @@ -4,17 +4,14 @@ namespace WP_Rocket\Engine\Common\JobManager; use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider; -use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginInformationClient; -use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginUpdateClient; -use WP_Rocket\Engine\Common\JobManager\Strategy\Context\RetryContext; -use WP_Rocket\Engine\Common\JobManager\Strategy\Factory\StrategyFactory; -use WP_Rocket\Engine\Common\JobManager\Strategy\Strategies\DefaultProcess; -use WP_Rocket\Engine\Common\JobManager\Strategy\Strategies\JobSetFail; -use WP_Rocket\Engine\Common\JobManager\Strategy\Strategies\ResetRetryProcess; use WP_Rocket\Engine\Common\Clock\WPRClock; -use WP_Rocket\Engine\Common\JobManager\Queue\Queue; use WP_Rocket\Engine\Common\JobManager\APIHandler\APIClient; +use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginUpdateClient; use WP_Rocket\Engine\Common\JobManager\Cron\Subscriber as CronSubscriber; +use WP_Rocket\Engine\Common\JobManager\Queue\Queue; +use WP_Rocket\Engine\Common\JobManager\Strategy\Context\RetryContext; +use WP_Rocket\Engine\Common\JobManager\Strategy\Factory\StrategyFactory; +use WP_Rocket\Engine\Plugin\PluginInformationClient; class ServiceProvider extends AbstractServiceProvider { diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php b/inc/Engine/Plugin/InformationAPIClient.php similarity index 83% rename from inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php rename to inc/Engine/Plugin/InformationAPIClient.php index 0fc991bbd5..d1a72f883d 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginInformationClient.php +++ b/inc/Engine/Plugin/InformationAPIClient.php @@ -1,6 +1,8 @@ plugin_slug = $this->get_plugin_slug( $args['plugin_file'] ); } if ( isset( $args['api_url'] ) ) { $this->api_url = $args['api_url']; } + + $this->client = $client; } /** @@ -80,12 +86,12 @@ public function exclude_rocket_from_wp_info( $bool, $action, $args ) { // phpcs: } /** - * Insert WP Rocket plugin info. + * Insert WP Rocket plugin info. * - * @param object|\WP_Error $res Response object or WP_Error. + * @param object|WP_Error $res Response object or WP_Error. * @param string $action The type of information being requested from the Plugin Install API. * @param object $args Plugin API arguments. - * @return object|\WP_Error Updated response object or WP_Error. + * @return object|WP_Error Updated response object or WP_Error. */ public function add_rocket_info( $res, $action, $args ) { if ( ! $this->is_requesting_rocket_info( $action, $args ) || empty( $res->external ) ) { @@ -113,7 +119,7 @@ public function add_wp_tested_version( $wp_tested_version ): string { } /** - * Tell if requesting WP Rocket plugin info. + * Tell if requesting WP Rocket plugin info. * * @param string $action The type of information being requested from the Plugin Install API. * @param object $args Plugin API arguments. @@ -126,11 +132,10 @@ private function is_requesting_rocket_info( $action, $args ) { /** * Gets the plugin information data * - * @return object|\WP_Error + * @return object|WP_Error */ private function get_plugin_information() { - $client = new PluginInformationClient(); - $response = $client->send_get_request( [] ); + $response = $this->client->send_get_request(); if ( is_wp_error( $response ) ) { return $this->get_request_error( $response->get_error_message() ); From e6d657ab34ef87f7c9277ec0d05d2f18ba6d4df2 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 09:28:04 +0300 Subject: [PATCH 09/39] adjust the docblock --- .../Common/JobManager/APIHandler/AbstractSafeAPIClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 6eaf1ace23..d082b66dfe 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -53,7 +53,7 @@ public function send_post_request( $params ) { * * @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. + * @return mixed The response from the API, or WP_Error if a timeout is active. */ private function send_request( $method, $params ) { $transient_key = $this->get_transient_key(); From 1477a72889d68b64124faeb1def0e5093ffbbdbb Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 09:32:21 +0300 Subject: [PATCH 10/39] use the client in container --- inc/Engine/Plugin/ServiceProvider.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inc/Engine/Plugin/ServiceProvider.php b/inc/Engine/Plugin/ServiceProvider.php index 398e79ec43..a2d14b3082 100644 --- a/inc/Engine/Plugin/ServiceProvider.php +++ b/inc/Engine/Plugin/ServiceProvider.php @@ -17,6 +17,7 @@ class ServiceProvider extends AbstractServiceProvider { 'plugin_updater_common_subscriber', 'plugin_information_subscriber', 'plugin_updater_subscriber', + 'plugin_information_api_client', ]; /** @@ -55,6 +56,8 @@ public function register(): void { ] ) ->addTag( 'common_subscriber' ); + $this->getContainer()->add( 'plugin_information_api_client', InformationAPIClient::class ); + $this->getContainer()->addShared( 'plugin_information_subscriber', InformationSubscriber::class ) ->addArgument( [ @@ -62,7 +65,9 @@ public function register(): void { 'api_url' => WP_ROCKET_WEB_INFO, ] ) + ->addArgument( $this->getContainer()->get( 'plugin_information_api_client' ) ) ->addTag( 'common_subscriber' ); + $this->getContainer()->addShared( 'plugin_updater_subscriber', UpdaterSubscriber::class ) ->addArgument( $this->getContainer()->get( 'plugin_renewal_notice' ) ) ->addArgument( From a3cb7ba6d597528444e05fca24bd5f6c20632a57 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:02:25 +0300 Subject: [PATCH 11/39] fix pricing client --- .../APIHandler/AbstractSafeAPIClient.php | 20 ++++--- .../APIHandler/PluginPricingClient.php | 36 ------------ inc/Engine/License/API/PricingClient.php | 58 +++++++++---------- 3 files changed, 39 insertions(+), 75 deletions(-) delete mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index d082b66dfe..8242be73ef 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -56,10 +56,9 @@ public function send_post_request( $params ) { * @return mixed The response from the API, or WP_Error if a timeout is active. */ private function send_request( $method, $params ) { - $transient_key = $this->get_transient_key(); $api_url = $this->get_api_url(); - if ( true === get_transient( $transient_key . '_timeout_active' ) ) { + if ( true === get_transient( $this->get_transient_key() . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'wp-rocket' ) ); } @@ -71,22 +70,21 @@ private function send_request( $method, $params ) { $response = wp_remote_request( $api_url, $params ); if ( is_wp_error( $response ) ) { - $this->set_timeout_transients( $transient_key ); + $this->set_timeout_transients(); return $response; } - delete_transient( $transient_key . '_timeout_active' ); - delete_transient( $transient_key . '_timeout' ); + $this->delete_timeout_transients(); return $response; } /** * Set the timeout transients. - * - * @param string $transient_key The transient key. */ - private function set_timeout_transients( $transient_key ) { + protected function set_timeout_transients() { + $transient_key = $this->get_transient_key(); + $timeout = (int) get_transient( $transient_key . '_timeout' ); $timeout = ( 0 === $timeout ) ? 300 @@ -98,4 +96,10 @@ private function set_timeout_transients( $transient_key ) { set_transient( $transient_key . '_timeout', $timeout, WEEK_IN_SECONDS ); set_transient( $transient_key . '_timeout_active', true, $timeout ); } + + protected function delete_timeout_transients() { + $transient_key = $this->get_transient_key(); + delete_transient( $transient_key . '_timeout_active' ); + delete_transient( $transient_key . '_timeout' ); + } } diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php deleted file mode 100644 index fd9d8304d3..0000000000 --- a/inc/Engine/Common/JobManager/APIHandler/PluginPricingClient.php +++ /dev/null @@ -1,36 +0,0 @@ -send_get_request(); - $client = new PluginPricingClient(); - $response = $client->send_get_request(); - - if ( false === $response ) { + if ( is_wp_error( $response ) ) { return false; } @@ -61,29 +78,8 @@ private function get_raw_pricing_data() { return false; } - delete_transient( 'wp_rocket_pricing_timeout' ); - delete_transient( 'wp_rocket_pricing_timeout_active' ); + $this->delete_timeout_transients(); return json_decode( $body ); } - - /** - * Set pricing timeout transients. - * - * @since 3.8.4 - * - * @return void - */ - private function set_timeout_transients() { - $timeout = (int) get_transient( 'wp_rocket_pricing_timeout' ); - $timeout = ( 0 === $timeout ) - ? 300 - : ( 2 * $timeout <= DAY_IN_SECONDS - ? 2 * $timeout : - DAY_IN_SECONDS - ); - - set_transient( 'wp_rocket_pricing_timeout', $timeout, WEEK_IN_SECONDS ); - set_transient( 'wp_rocket_pricing_timeout_active', true, $timeout ); - } } From b74a30235bec84dd175691fac793ed6797d6b573 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:09:26 +0300 Subject: [PATCH 12/39] fix updater API Client --- inc/Engine/Common/JobManager/ServiceProvider.php | 2 +- .../UpdaterAPIClient.php} | 8 +++++--- inc/Engine/Plugin/UpdaterSubscriber.php | 10 ++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) rename inc/Engine/{Common/JobManager/APIHandler/PluginUpdateClient.php => Plugin/UpdaterAPIClient.php} (74%) diff --git a/inc/Engine/Common/JobManager/ServiceProvider.php b/inc/Engine/Common/JobManager/ServiceProvider.php index bf17157783..cad65535f4 100644 --- a/inc/Engine/Common/JobManager/ServiceProvider.php +++ b/inc/Engine/Common/JobManager/ServiceProvider.php @@ -6,12 +6,12 @@ use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider; use WP_Rocket\Engine\Common\Clock\WPRClock; use WP_Rocket\Engine\Common\JobManager\APIHandler\APIClient; -use WP_Rocket\Engine\Common\JobManager\APIHandler\PluginUpdateClient; use WP_Rocket\Engine\Common\JobManager\Cron\Subscriber as CronSubscriber; use WP_Rocket\Engine\Common\JobManager\Queue\Queue; use WP_Rocket\Engine\Common\JobManager\Strategy\Context\RetryContext; use WP_Rocket\Engine\Common\JobManager\Strategy\Factory\StrategyFactory; use WP_Rocket\Engine\Plugin\PluginInformationClient; +use WP_Rocket\Engine\Plugin\PluginUpdateClient; class ServiceProvider extends AbstractServiceProvider { diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php b/inc/Engine/Plugin/UpdaterAPIClient.php similarity index 74% rename from inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php rename to inc/Engine/Plugin/UpdaterAPIClient.php index 93ec0b85d8..bce0a8387e 100644 --- a/inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php +++ b/inc/Engine/Plugin/UpdaterAPIClient.php @@ -1,6 +1,8 @@ $setting = $args[ $setting ]; @@ -102,6 +104,7 @@ public function __construct( RenewalNotice $renewal_notice, $args ) { } $this->renewal_notice = $renewal_notice; + $this->client = $client; } /** @@ -306,8 +309,7 @@ public function disable_auto_updates( $update, $item ) { * } */ public function get_latest_version_data() { - $client = new PluginUpdateClient(); - $response = $client->send_get_request( [] ); + $response = $this->client->send_get_request(); if ( is_wp_error( $response ) ) { return $this->get_request_error( From 784613ada24865d9803cb957db77369d20afa546 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:19:08 +0300 Subject: [PATCH 13/39] add updater API Client to the container --- inc/Engine/Plugin/ServiceProvider.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/Engine/Plugin/ServiceProvider.php b/inc/Engine/Plugin/ServiceProvider.php index a2d14b3082..d557586d21 100644 --- a/inc/Engine/Plugin/ServiceProvider.php +++ b/inc/Engine/Plugin/ServiceProvider.php @@ -18,6 +18,7 @@ class ServiceProvider extends AbstractServiceProvider { 'plugin_information_subscriber', 'plugin_updater_subscriber', 'plugin_information_api_client', + 'plugin_updater_api_client', ]; /** @@ -68,6 +69,8 @@ public function register(): void { ->addArgument( $this->getContainer()->get( 'plugin_information_api_client' ) ) ->addTag( 'common_subscriber' ); + $this->getContainer()->add( 'plugin_updater_api_client', UpdaterAPIClient::class ); + $this->getContainer()->addShared( 'plugin_updater_subscriber', UpdaterSubscriber::class ) ->addArgument( $this->getContainer()->get( 'plugin_renewal_notice' ) ) ->addArgument( @@ -82,6 +85,7 @@ public function register(): void { ], ] ) + ->addArgument( $this->getContainer()->get( 'plugin_updater_api_client' ) ) ->addTag( 'common_subscriber' ); } } From 60fc1527572b66f72e115a2d0f0fac81cfaa86aa Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:26:16 +0300 Subject: [PATCH 14/39] adjust user client --- .../APIHandler/UserInformationClient.php | 36 ------------------- inc/Engine/License/API/UserClient.php | 30 +++++++++++++--- 2 files changed, 26 insertions(+), 40 deletions(-) delete mode 100644 inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php b/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php deleted file mode 100644 index 165b5064bc..0000000000 --- a/inc/Engine/Common/JobManager/APIHandler/UserInformationClient.php +++ /dev/null @@ -1,36 +0,0 @@ -options->get( 'consumer_email', '' ) : rocket_get_constant( 'WP_ROCKET_EMAIL', '' ); - $client = new UserInformationClient( $this->options ); - $response = $client->send_post_request( + $response = $this->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), ] ); - if ( false === $response ) { + if ( is_wp_error( $response ) ) { return false; } From 9e9b535d27df3dec33c06d32901e01f049b6beae Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:27:44 +0300 Subject: [PATCH 15/39] remove not needed use --- inc/Engine/License/API/UserClient.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index ab3e69002c..350ca97b1a 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -4,7 +4,6 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractSafeAPIClient; -use WP_Rocket\Engine\Common\JobManager\APIHandler\UserInformationClient; class UserClient extends AbstractSafeAPIClient { const USER_ENDPOINT = 'https://wp-rocket.me/stat/1.0/wp-rocket/user.php'; From da6b339056c322eac95c932a06ec2925b4a77051 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 10:37:56 +0300 Subject: [PATCH 16/39] refine job manager service provider --- inc/Engine/Common/JobManager/ServiceProvider.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/inc/Engine/Common/JobManager/ServiceProvider.php b/inc/Engine/Common/JobManager/ServiceProvider.php index cad65535f4..d404ef36ff 100644 --- a/inc/Engine/Common/JobManager/ServiceProvider.php +++ b/inc/Engine/Common/JobManager/ServiceProvider.php @@ -10,9 +10,6 @@ use WP_Rocket\Engine\Common\JobManager\Queue\Queue; use WP_Rocket\Engine\Common\JobManager\Strategy\Context\RetryContext; use WP_Rocket\Engine\Common\JobManager\Strategy\Factory\StrategyFactory; -use WP_Rocket\Engine\Plugin\PluginInformationClient; -use WP_Rocket\Engine\Plugin\PluginUpdateClient; - class ServiceProvider extends AbstractServiceProvider { /** @@ -86,10 +83,5 @@ public function register(): void { $factories, ] ); - $this->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' ) ); } } From 43a502725f52500bfacac319df455cbda7a354ca Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Wed, 26 Jun 2024 10:03:28 +0200 Subject: [PATCH 17/39] Apply linter --- .../APIHandler/AbstractSafeAPIClient.php | 14 ++++++++++++-- inc/Engine/Plugin/InformationAPIClient.php | 3 ++- inc/Engine/Plugin/InformationSubscriber.php | 17 ++++++++++------- inc/Engine/Plugin/UpdaterSubscriber.php | 13 +++++++++---- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 8242be73ef..9390a20dde 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -56,10 +56,10 @@ public function send_post_request( $params ) { * @return mixed The response from the API, or WP_Error if a timeout is active. */ private function send_request( $method, $params ) { - $api_url = $this->get_api_url(); + $api_url = $this->get_api_url(); if ( true === get_transient( $this->get_transient_key() . '_timeout_active' ) ) { - return new WP_Error( 429, __( 'Too many requests.', 'wp-rocket' ) ); + return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } if ( empty( $params['body'] ) ) { @@ -97,6 +97,16 @@ protected function set_timeout_transients() { set_transient( $transient_key . '_timeout_active', true, $timeout ); } + /** + * Delete the timeout transients. + * + * This method deletes the timeout transients for the API requests. It uses the transient key obtained from the `get_transient_key` method. + * The transients deleted are: + * - `{transient_key}_timeout_active`: This transient indicates if a timeout is currently active. + * - `{transient_key}_timeout`: This transient stores the timeout duration. + * + * @return void + */ protected function delete_timeout_transients() { $transient_key = $this->get_transient_key(); delete_transient( $transient_key . '_timeout_active' ); diff --git a/inc/Engine/Plugin/InformationAPIClient.php b/inc/Engine/Plugin/InformationAPIClient.php index d1a72f883d..2a6bfa091b 100644 --- a/inc/Engine/Plugin/InformationAPIClient.php +++ b/inc/Engine/Plugin/InformationAPIClient.php @@ -14,6 +14,7 @@ */ class InformationAPIClient extends AbstractSafeAPIClient { + const CLIENT_INFO_ENDPOINT = 'https://wp-rocket.me/plugin_information.php'; /** * Get the transient key for plugin information. * @@ -33,6 +34,6 @@ protected function get_transient_key() { * @return string The API URL for plugin information. */ protected function get_api_url() { - return 'https://wp-rocket.me/plugin_information.php'; + return self::CLIENT_INFO_ENDPOINT; } } diff --git a/inc/Engine/Plugin/InformationSubscriber.php b/inc/Engine/Plugin/InformationSubscriber.php index 8d8afb5f2b..7be04d38d5 100644 --- a/inc/Engine/Plugin/InformationSubscriber.php +++ b/inc/Engine/Plugin/InformationSubscriber.php @@ -31,19 +31,22 @@ class InformationSubscriber implements Subscriber_Interface { */ protected $request_error_id = 'plugins_api_failed'; + /** + * An instance of the InformationAPIClient class. This client is used to send API requests + * for plugin information and other related tasks. + * + * @var InformationAPIClient API Client. + */ private $client; /** * Constructor * - * @param array $args { - * Required arguments to populate the class properties. - * + * @param array $args { Required arguments to populate the class properties. * @type string $plugin_file Full path to the plugin. * @type string $api_url URL to contact to get update info. * } - * @param $client InformationAPIClient API Client. - * + * @param InformationAPIClient $client API Client. */ public function __construct( $args, $client ) { if ( isset( $args['plugin_file'] ) ) { @@ -89,8 +92,8 @@ public function exclude_rocket_from_wp_info( $bool, $action, $args ) { // phpcs: * Insert WP Rocket plugin info. * * @param object|WP_Error $res Response object or WP_Error. - * @param string $action The type of information being requested from the Plugin Install API. - * @param object $args Plugin API arguments. + * @param string $action The type of information being requested from the Plugin Install API. + * @param object $args Plugin API arguments. * @return object|WP_Error Updated response object or WP_Error. */ public function add_rocket_info( $res, $action, $args ) { diff --git a/inc/Engine/Plugin/UpdaterSubscriber.php b/inc/Engine/Plugin/UpdaterSubscriber.php index 4cad10f518..1254ae726c 100644 --- a/inc/Engine/Plugin/UpdaterSubscriber.php +++ b/inc/Engine/Plugin/UpdaterSubscriber.php @@ -80,15 +80,20 @@ class UpdaterSubscriber implements Event_Manager_Aware_Subscriber_Interface { */ private $renewal_notice; + /** + * An instance of the UpdaterAPIClient class. This client is used to send API requests for plugin updates & other related tasks. + * + * @var UpdaterAPIClient $client + */ + private $client; /** * Constructor * - * @param RenewalNotice $renewal_notice RenewalNotice instance. - * @param array $args { - * Required arguments to populate the class properties. + * @param RenewalNotice $renewal_notice RenewalNotice instance. * + * @param array $args { Required arguments to populate the class properties. * @type string $plugin_file Full path to the plugin. * @type string $plugin_version Current version of the plugin. * @type string $vendor_url URL to the plugin provider. @@ -104,7 +109,7 @@ public function __construct( RenewalNotice $renewal_notice, $args, UpdaterAPICli } $this->renewal_notice = $renewal_notice; - $this->client = $client; + $this->client = $client; } /** From 65e347f4b3e3a7b1ad913d92b81c8692abd6bd31 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 11:38:01 +0300 Subject: [PATCH 18/39] license validation API Client --- .../APIHandler/PluginLicenseClient.php | 1 - .../License/API/LicenseValidationClient.php | 37 +++++++++++++++++++ inc/functions/options.php | 6 ++- 3 files changed, 41 insertions(+), 3 deletions(-) delete mode 100644 inc/Engine/Common/JobManager/APIHandler/PluginLicenseClient.php create mode 100644 inc/Engine/License/API/LicenseValidationClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/PluginLicenseClient.php b/inc/Engine/Common/JobManager/APIHandler/PluginLicenseClient.php deleted file mode 100644 index b3d9bbc7f3..0000000000 --- a/inc/Engine/Common/JobManager/APIHandler/PluginLicenseClient.php +++ /dev/null @@ -1 +0,0 @@ -send_get_request( [ 'timeout' => 30, ] From a62d205fb9c4e4e36132991f959f5efba6bc50f8 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 26 Jun 2024 11:41:09 +0300 Subject: [PATCH 19/39] fix phpcs --- inc/Engine/License/API/LicenseValidationClient.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/Engine/License/API/LicenseValidationClient.php b/inc/Engine/License/API/LicenseValidationClient.php index 35c1ea6255..56c9c49332 100644 --- a/inc/Engine/License/API/LicenseValidationClient.php +++ b/inc/Engine/License/API/LicenseValidationClient.php @@ -9,7 +9,6 @@ * * This class extends the AbstractSafeAPIClient class and provides methods for * getting the transient key and API URL specific to plugin information. - * */ class LicenseValidationClient extends AbstractSafeAPIClient { From 2927cce8c3b0097fe5fb9b645479d27fe661008a Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 27 Jun 2024 03:38:35 +0200 Subject: [PATCH 20/39] Fix tests --- .../Common/JobManager/APIHandler/AbstractSafeAPIClient.php | 2 +- inc/Engine/License/API/PricingClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 9390a20dde..0370d48628 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -69,7 +69,7 @@ private function send_request( $method, $params ) { $params['method'] = strtoupper( $method ); $response = wp_remote_request( $api_url, $params ); - if ( is_wp_error( $response ) ) { + if ( is_wp_error( $response ) || ( is_array($response) && $response['response']['code'] !== 200 ) ) { $this->set_timeout_transients(); return $response; } diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index ed496b91dc..c7cc36d5b8 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -66,7 +66,7 @@ public function get_pricing_data() { private function get_raw_pricing_data() { $response = $this->send_get_request(); - if ( is_wp_error( $response ) ) { + if ( is_wp_error( $response ) || ( is_array( $response ) && $response['response']['code'] !== 200 ) ) { return false; } From ccf47a3435ec2093273d49f703e9e17a217a2beb Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Thu, 27 Jun 2024 03:40:54 +0200 Subject: [PATCH 21/39] Fix linter --- .../Common/JobManager/APIHandler/AbstractSafeAPIClient.php | 2 +- inc/Engine/License/API/PricingClient.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 0370d48628..d757ddcb36 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -69,7 +69,7 @@ private function send_request( $method, $params ) { $params['method'] = strtoupper( $method ); $response = wp_remote_request( $api_url, $params ); - if ( is_wp_error( $response ) || ( is_array($response) && $response['response']['code'] !== 200 ) ) { + if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); return $response; } diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index c7cc36d5b8..ab9d3cfc50 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -66,7 +66,7 @@ public function get_pricing_data() { private function get_raw_pricing_data() { $response = $this->send_get_request(); - if ( is_wp_error( $response ) || ( is_array( $response ) && $response['response']['code'] !== 200 ) ) { + if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { return false; } From 69d1ba9b1d082ba7dbd178eafba4dbd182771eeb Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 15:25:41 +0300 Subject: [PATCH 22/39] fix some unit tests --- .../APIHandler/AbstractSafeAPIClient.php | 34 +++++++++++++------ inc/Engine/License/API/PricingClient.php | 2 +- .../API/PricingClient/getPricingData.php | 3 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index d757ddcb36..cbfa5be45d 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -34,8 +34,8 @@ abstract protected function get_api_url(); * @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 ); + public function send_get_request( $params = [], $safe = false ) { + return $this->send_request( 'GET', $params, $safe ); } /** @@ -44,8 +44,8 @@ public function send_get_request( $params = [] ) { * @param array $params The request parameters. * @return mixed The response from the API. */ - public function send_post_request( $params ) { - return $this->send_request( 'POST', $params ); + public function send_post_request( $params = [], $safe = false ) { + return $this->send_request( 'POST', $params, $safe ); } /** @@ -55,19 +55,16 @@ public function send_post_request( $params ) { * @param array $params The request parameters. * @return mixed The response from the API, or WP_Error if a timeout is active. */ - private function send_request( $method, $params ) { + private function send_request( $method, $params = [], $safe = false ) { $api_url = $this->get_api_url(); if ( true === get_transient( $this->get_transient_key() . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } - if ( empty( $params['body'] ) ) { - $params['body'] = []; - } - $params['method'] = strtoupper( $method ); - $response = wp_remote_request( $api_url, $params ); + + $response = $this->send_remote_request( $api_url, $method, $params, $safe ); if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); @@ -112,4 +109,21 @@ protected function delete_timeout_transients() { delete_transient( $transient_key . '_timeout_active' ); delete_transient( $transient_key . '_timeout' ); } + + private function send_remote_request( $api_url, $method, $params, $safe ) { + if ( ! $safe ) { + return wp_remote_request( $api_url, $params ); + } + + unset( $params['method'] ); + + switch ( $method ) { + case 'GET': + return wp_safe_remote_get( $api_url, $params ); + case 'POST': + return wp_safe_remote_post( $api_url, $params ); + } + + new WP_Error( 400, __( 'Not valid request type.', 'rocket' ) ); + } } diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index ab9d3cfc50..0a0601cdbf 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -64,7 +64,7 @@ public function get_pricing_data() { * @return bool|object */ private function get_raw_pricing_data() { - $response = $this->send_get_request(); + $response = $this->send_get_request( [], true ); if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { return false; diff --git a/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php b/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php index 72595b52de..50c9defa49 100644 --- a/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php +++ b/tests/Unit/inc/Engine/License/API/PricingClient/getPricingData.php @@ -17,6 +17,7 @@ class GetPricingData extends TestCase { * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $expected ) { + $this->stubTranslationFunctions(); $client = new PricingClient(); Functions\expect( 'get_transient' ) @@ -48,7 +49,7 @@ public function testShouldReturnExpected( $config, $expected ) { if ( false !== $config['response'] ) { Functions\expect( 'wp_safe_remote_get' ) ->once() - ->with( PricingClient::PRICING_ENDPOINT ) + ->with( PricingClient::PRICING_ENDPOINT, [] ) ->andReturn( $config['response'] ); if ( From 1cb1422400af95f60fc33482662e6b8b708e2366 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 15:44:12 +0300 Subject: [PATCH 23/39] fix pricing client unit tests --- .../JobManager/APIHandler/AbstractSafeAPIClient.php | 6 ++++++ inc/Engine/License/API/PricingClient.php | 12 +----------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index cbfa5be45d..dc8a14c381 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -66,6 +66,12 @@ private function send_request( $method, $params = [], $safe = false ) { $response = $this->send_remote_request( $api_url, $method, $params, $safe ); + $body = wp_remote_retrieve_body( $response ); + if ( empty( $body ) ) { + $this->set_timeout_transients(); + return new WP_Error( 500, __( 'Not valid response body.', 'rocket' ) ); + } + if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); return $response; diff --git a/inc/Engine/License/API/PricingClient.php b/inc/Engine/License/API/PricingClient.php index 0a0601cdbf..6d29d47023 100644 --- a/inc/Engine/License/API/PricingClient.php +++ b/inc/Engine/License/API/PricingClient.php @@ -70,16 +70,6 @@ private function get_raw_pricing_data() { return false; } - $body = wp_remote_retrieve_body( $response ); - - if ( empty( $body ) ) { - $this->set_timeout_transients(); - - return false; - } - - $this->delete_timeout_transients(); - - return json_decode( $body ); + return json_decode( wp_remote_retrieve_body( $response ) ); } } From 649171efa3dfa3a2b379f749db16bcaaa6814613 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 15:55:50 +0300 Subject: [PATCH 24/39] fix user client --- inc/Engine/License/API/UserClient.php | 3 ++- .../inc/Engine/License/API/UserClient/getUserData.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index 350ca97b1a..ce162b25ed 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -91,7 +91,8 @@ private function get_raw_user_data() { $response = $this->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), - ] + ], + true ); if ( is_wp_error( $response ) ) { diff --git a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php index bcf4f1559f..80463e6657 100644 --- a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php @@ -39,6 +39,8 @@ public function setUp(): void { * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $expected ) { + $this->stubTranslationFunctions(); + Functions\expect( 'get_transient' ) ->atLeast()->once() ->with( 'wp_rocket_customer_data' ) @@ -96,6 +98,14 @@ public function testShouldReturnExpected( $config, $expected ) { Functions\expect( 'set_transient' )->never(); } + if ( ! $expected ) { + Functions\expect( 'set_transient' )->with( 'user_information_timeout' )->andReturn(null); + Functions\expect( 'set_transient' )->with( 'user_information_timeout_active' )->andReturn(null); + } else { + Functions\expect( 'delete_transient' )->with( 'user_information_timeout' )->andReturn(null); + Functions\expect( 'delete_transient' )->with( 'user_information_timeout_active' )->andReturn(null); + } + $this->assertEquals( $expected, $this->client->get_user_data() From cff2006663a2ced6271bfc3943c8de4e970efa2b Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 16:22:45 +0300 Subject: [PATCH 25/39] fix all unit tests --- .../APIHandler/AbstractSafeAPIClient.php | 14 +++---- .../License/API/LicenseValidationClient.php | 2 +- inc/functions/options.php | 2 +- .../addPluginsToResult.php | 5 ++- tests/Unit/inc/functions/rocketCheckKey.php | 37 +++++++++++++++---- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index dc8a14c381..c87ce53834 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -66,15 +66,15 @@ private function send_request( $method, $params = [], $safe = false ) { $response = $this->send_remote_request( $api_url, $method, $params, $safe ); - $body = wp_remote_retrieve_body( $response ); - if ( empty( $body ) ) { + if ( is_wp_error( $response ) ) { $this->set_timeout_transients(); - return new WP_Error( 500, __( 'Not valid response body.', 'rocket' ) ); + return $response; } - if ( is_wp_error( $response ) || ( is_array( $response ) && 200 !== $response['response']['code'] ) ) { + $body = wp_remote_retrieve_body( $response ); + if ( empty( $body ) || ( is_array( $response ) && !empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); - return $response; + return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) ); } $this->delete_timeout_transients(); @@ -85,7 +85,7 @@ private function send_request( $method, $params = [], $safe = false ) { /** * Set the timeout transients. */ - protected function set_timeout_transients() { + private function set_timeout_transients() { $transient_key = $this->get_transient_key(); $timeout = (int) get_transient( $transient_key . '_timeout' ); @@ -110,7 +110,7 @@ protected function set_timeout_transients() { * * @return void */ - protected function delete_timeout_transients() { + private function delete_timeout_transients() { $transient_key = $this->get_transient_key(); delete_transient( $transient_key . '_timeout_active' ); delete_transient( $transient_key . '_timeout' ); diff --git a/inc/Engine/License/API/LicenseValidationClient.php b/inc/Engine/License/API/LicenseValidationClient.php index 56c9c49332..06e6bd175b 100644 --- a/inc/Engine/License/API/LicenseValidationClient.php +++ b/inc/Engine/License/API/LicenseValidationClient.php @@ -1,6 +1,6 @@ 'wp-rocket/wp-rocket.php', 'api_url' => 'https://wp-rocket.me', - ] + ], + Mockery::mock( InformationAPIClient::class ) ); } diff --git a/tests/Unit/inc/functions/rocketCheckKey.php b/tests/Unit/inc/functions/rocketCheckKey.php index efc7a39dac..bcb550038f 100644 --- a/tests/Unit/inc/functions/rocketCheckKey.php +++ b/tests/Unit/inc/functions/rocketCheckKey.php @@ -45,13 +45,13 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { ->andReturn( 'wp_rocket_settings' ); Functions\expect( 'rocket_valid_key' )->once()->andReturn( false ); Functions\expect( 'rocket_delete_licence_data_file' )->never(); - Functions\expect( 'wp_remote_get' ) + Functions\expect( 'wp_remote_request' ) ->once() - ->with( 'https://wp-rocket.me/valid_key.php', [ 'timeout' => 30 ] ) + ->with( 'https://wp-rocket.me/valid_key.php', [ 'method' => 'GET', 'timeout' => 30 ] ) ->andReturn( [] ); - Functions\expect( 'is_wp_error' )->once()->andReturn( false ); + Functions\expect( 'is_wp_error' )->twice()->andReturn( false ); Functions\expect( 'wp_remote_retrieve_body' ) - ->once() + ->twice() ->with( [] ) ->andReturn( '{"success": true, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","secret_key":"secret"}}' ); Functions\expect( 'get_rocket_option' )->once()->with( 'license' )->andReturn( true ); @@ -80,6 +80,11 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { 'secret_key' => 'secret', ]; + Functions\expect( 'get_transient' ) + ->once() + ->with( 'wp_rocket_license_validation_timeout_active' ) + ->andReturn( false ); + $this->assertSame( $expected, rocket_check_key() ); } @@ -89,7 +94,7 @@ public function testShouldReturnFalseWhenIsWPError() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_get' )->alias( function() { + Functions\when( 'wp_remote_request' )->alias( function() { $wp_error = \Mockery::mock( \WP_Error::class )->makePartial(); $wp_error->shouldReceive( 'get_error_messages' ) ->withNoArgs() @@ -104,6 +109,11 @@ public function testShouldReturnFalseWhenIsWPError() { Functions\expect('update_option') ->never(); + Functions\expect( 'get_transient' ) + ->once() + ->with( 'wp_rocket_license_validation_timeout_active' ) + ->andReturn( false ); + $this->assertFalse( rocket_check_key() ); } @@ -113,7 +123,7 @@ public function testShouldReturnFalseWhenEmptyResponse() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_get' )->justReturn( [] ); + Functions\when( 'wp_remote_request' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -121,6 +131,11 @@ public function testShouldReturnFalseWhenEmptyResponse() { Functions\expect('update_option') ->never(); + Functions\expect( 'get_transient' ) + ->once() + ->with( 'wp_rocket_license_validation_timeout_active' ) + ->andReturn( false ); + $this->assertFalse( rocket_check_key() ); } @@ -134,7 +149,7 @@ public function testShouldReturnArrayWhenSuccessFalse() { ->with( 'WP_ROCKET_SLUG' ) ->andReturn( 'wp_rocket_settings' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_get' )->justReturn( [] ); + Functions\when( 'wp_remote_request' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '{"success": false, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","reason":"BAD_KEY"}}' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -142,12 +157,20 @@ public function testShouldReturnArrayWhenSuccessFalse() { Functions\expect('update_option') ->never(); + Functions\expect( 'get_transient' ) + ->once() + ->with( 'wp_rocket_license_validation_timeout_active' ) + ->andReturn( false ); + $expected = [ 'consumer_key' => 'ABCDEF', 'consumer_email' => 'example@example.org', 'secret_key' => '', ]; + Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout' )->andReturn(null); + Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout_active' )->andReturn(null); + $this->assertSame( $expected, rocket_check_key() ); } } From b6d8497faec20b9a63e2cc5a305320a59726b7bd Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 16:37:03 +0300 Subject: [PATCH 26/39] fix phpcs --- .../APIHandler/AbstractSafeAPIClient.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index c87ce53834..ccdc21a75a 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -32,6 +32,8 @@ abstract protected function get_api_url(); * Send a GET request. * * @param array $params The request parameters. + * @param bool $safe Send safe request WP functions or not, default to not. + * * @return mixed The response from the API. */ public function send_get_request( $params = [], $safe = false ) { @@ -42,6 +44,8 @@ public function send_get_request( $params = [], $safe = false ) { * Send a POST request. * * @param array $params The request parameters. + * @param bool $safe Send safe request WP functions or not, default to not. + * * @return mixed The response from the API. */ public function send_post_request( $params = [], $safe = false ) { @@ -53,6 +57,7 @@ public function send_post_request( $params = [], $safe = false ) { * * @param string $method The HTTP method (GET or POST). * @param array $params The request parameters. + * @param bool $safe Send safe request WP functions or not, default to not. * @return mixed The response from the API, or WP_Error if a timeout is active. */ private function send_request( $method, $params = [], $safe = false ) { @@ -72,7 +77,7 @@ private function send_request( $method, $params = [], $safe = false ) { } $body = wp_remote_retrieve_body( $response ); - if ( empty( $body ) || ( is_array( $response ) && !empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) { + if ( empty( $body ) || ( is_array( $response ) && ! empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) { $this->set_timeout_transients(); return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) ); } @@ -116,6 +121,15 @@ private function delete_timeout_transients() { delete_transient( $transient_key . '_timeout' ); } + /** + * Decide which WP core function will be used to send the request based on the params. + * + * @param string $api_url API Url. + * @param string $method Request method (GET or POST). + * @param array $params Parameters being sent with the request. + * @param bool $safe Send safe request WP functions or not, default to not. + * @return array|WP_Error + */ private function send_remote_request( $api_url, $method, $params, $safe ) { if ( ! $safe ) { return wp_remote_request( $api_url, $params ); From a372df61219dabfc9b1d3da9dff48f9643c7f13e Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 16:43:28 +0300 Subject: [PATCH 27/39] add missing return --- .../Common/JobManager/APIHandler/AbstractSafeAPIClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index ccdc21a75a..c8ee9a2fd7 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -144,6 +144,6 @@ private function send_remote_request( $api_url, $method, $params, $safe ) { return wp_safe_remote_post( $api_url, $params ); } - new WP_Error( 400, __( 'Not valid request type.', 'rocket' ) ); + return new WP_Error( 400, __( 'Not valid request type.', 'rocket' ) ); } } From 818c4c39364f91e961469c8e1e99b6e69f2d0da2 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 18:44:43 +0300 Subject: [PATCH 28/39] remove not needed code --- .../APIHandler/AbstractSafeAPIClient.php | 2 +- .../License/API/LicenseValidationClient.php | 36 ----------------- inc/Engine/Plugin/InformationAPIClient.php | 39 ------------------- inc/Engine/Plugin/InformationSubscriber.php | 15 +------ inc/Engine/Plugin/ServiceProvider.php | 7 ---- inc/Engine/Plugin/UpdaterAPIClient.php | 38 ------------------ inc/Engine/Plugin/UpdaterSubscriber.php | 29 ++++++-------- inc/functions/options.php | 5 +-- 8 files changed, 17 insertions(+), 154 deletions(-) delete mode 100644 inc/Engine/License/API/LicenseValidationClient.php delete mode 100644 inc/Engine/Plugin/InformationAPIClient.php delete mode 100644 inc/Engine/Plugin/UpdaterAPIClient.php diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index c8ee9a2fd7..a539b9059d 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -63,7 +63,7 @@ public function send_post_request( $params = [], $safe = false ) { private function send_request( $method, $params = [], $safe = false ) { $api_url = $this->get_api_url(); - if ( true === get_transient( $this->get_transient_key() . '_timeout_active' ) ) { + if ( get_transient( $this->get_transient_key() . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } diff --git a/inc/Engine/License/API/LicenseValidationClient.php b/inc/Engine/License/API/LicenseValidationClient.php deleted file mode 100644 index 06e6bd175b..0000000000 --- a/inc/Engine/License/API/LicenseValidationClient.php +++ /dev/null @@ -1,36 +0,0 @@ -plugin_slug = $this->get_plugin_slug( $args['plugin_file'] ); } if ( isset( $args['api_url'] ) ) { $this->api_url = $args['api_url']; } - - $this->client = $client; } /** @@ -138,7 +127,7 @@ private function is_requesting_rocket_info( $action, $args ) { * @return object|WP_Error */ private function get_plugin_information() { - $response = $this->client->send_get_request(); + $response = wp_remote_get( $this->api_url ); if ( is_wp_error( $response ) ) { return $this->get_request_error( $response->get_error_message() ); diff --git a/inc/Engine/Plugin/ServiceProvider.php b/inc/Engine/Plugin/ServiceProvider.php index d557586d21..809bcad86a 100644 --- a/inc/Engine/Plugin/ServiceProvider.php +++ b/inc/Engine/Plugin/ServiceProvider.php @@ -17,8 +17,6 @@ class ServiceProvider extends AbstractServiceProvider { 'plugin_updater_common_subscriber', 'plugin_information_subscriber', 'plugin_updater_subscriber', - 'plugin_information_api_client', - 'plugin_updater_api_client', ]; /** @@ -57,7 +55,6 @@ public function register(): void { ] ) ->addTag( 'common_subscriber' ); - $this->getContainer()->add( 'plugin_information_api_client', InformationAPIClient::class ); $this->getContainer()->addShared( 'plugin_information_subscriber', InformationSubscriber::class ) ->addArgument( @@ -66,11 +63,8 @@ public function register(): void { 'api_url' => WP_ROCKET_WEB_INFO, ] ) - ->addArgument( $this->getContainer()->get( 'plugin_information_api_client' ) ) ->addTag( 'common_subscriber' ); - $this->getContainer()->add( 'plugin_updater_api_client', UpdaterAPIClient::class ); - $this->getContainer()->addShared( 'plugin_updater_subscriber', UpdaterSubscriber::class ) ->addArgument( $this->getContainer()->get( 'plugin_renewal_notice' ) ) ->addArgument( @@ -85,7 +79,6 @@ public function register(): void { ], ] ) - ->addArgument( $this->getContainer()->get( 'plugin_updater_api_client' ) ) ->addTag( 'common_subscriber' ); } } diff --git a/inc/Engine/Plugin/UpdaterAPIClient.php b/inc/Engine/Plugin/UpdaterAPIClient.php deleted file mode 100644 index bce0a8387e..0000000000 --- a/inc/Engine/Plugin/UpdaterAPIClient.php +++ /dev/null @@ -1,38 +0,0 @@ -$setting = $args[ $setting ]; @@ -109,7 +100,6 @@ public function __construct( RenewalNotice $renewal_notice, $args, UpdaterAPICli } $this->renewal_notice = $renewal_notice; - $this->client = $client; } /** @@ -314,19 +304,24 @@ public function disable_auto_updates( $update, $item ) { * } */ public function get_latest_version_data() { - $response = $this->client->send_get_request(); + $request = wp_remote_get( + $this->api_url, + [ + 'timeout' => 30, + ] + ); - if ( is_wp_error( $response ) ) { + if ( is_wp_error( $request ) ) { return $this->get_request_error( [ - 'error_code' => $response->get_error_code(), - 'response' => $response->get_error_message(), + 'error_code' => $request->get_error_code(), + 'response' => $request->get_error_message(), ] ); } - $res = trim( wp_remote_retrieve_body( $response ) ); - $code = wp_remote_retrieve_response_code( $response ); + $res = trim( wp_remote_retrieve_body( $request ) ); + $code = wp_remote_retrieve_response_code( $request ); if ( 200 !== $code ) { /** diff --git a/inc/functions/options.php b/inc/functions/options.php index a7e457689b..d1834acb6c 100755 --- a/inc/functions/options.php +++ b/inc/functions/options.php @@ -461,9 +461,8 @@ function rocket_check_key() { Logger::info( 'LICENSE VALIDATION PROCESS STARTED.', [ 'license validation process' ] ); - $api_client = new LicenseValidationClient(); - - $response = $api_client->send_get_request( + $response = wp_remote_get( + rocket_get_constant( 'WP_ROCKET_WEB_VALID' ), [ 'timeout' => 30, ] From 75734bec73a3c77fb4198aacea3cc617fa82e788 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 18:54:44 +0300 Subject: [PATCH 29/39] fix unit tests --- .../addPluginsToResult.php | 3 +- tests/Unit/inc/functions/rocketCheckKey.php | 37 ++++--------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php index 862e66df4b..a16f8e352a 100644 --- a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php +++ b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php @@ -23,8 +23,7 @@ protected function setUp(): void { [ 'plugin_file' => 'wp-rocket/wp-rocket.php', 'api_url' => 'https://wp-rocket.me', - ], - Mockery::mock( InformationAPIClient::class ) + ] ); } diff --git a/tests/Unit/inc/functions/rocketCheckKey.php b/tests/Unit/inc/functions/rocketCheckKey.php index bcb550038f..efc7a39dac 100644 --- a/tests/Unit/inc/functions/rocketCheckKey.php +++ b/tests/Unit/inc/functions/rocketCheckKey.php @@ -45,13 +45,13 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { ->andReturn( 'wp_rocket_settings' ); Functions\expect( 'rocket_valid_key' )->once()->andReturn( false ); Functions\expect( 'rocket_delete_licence_data_file' )->never(); - Functions\expect( 'wp_remote_request' ) + Functions\expect( 'wp_remote_get' ) ->once() - ->with( 'https://wp-rocket.me/valid_key.php', [ 'method' => 'GET', 'timeout' => 30 ] ) + ->with( 'https://wp-rocket.me/valid_key.php', [ 'timeout' => 30 ] ) ->andReturn( [] ); - Functions\expect( 'is_wp_error' )->twice()->andReturn( false ); + Functions\expect( 'is_wp_error' )->once()->andReturn( false ); Functions\expect( 'wp_remote_retrieve_body' ) - ->twice() + ->once() ->with( [] ) ->andReturn( '{"success": true, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","secret_key":"secret"}}' ); Functions\expect( 'get_rocket_option' )->once()->with( 'license' )->andReturn( true ); @@ -80,11 +80,6 @@ public function testShouldReturnArrayWhenSuccessfulValidation() { 'secret_key' => 'secret', ]; - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertSame( $expected, rocket_check_key() ); } @@ -94,7 +89,7 @@ public function testShouldReturnFalseWhenIsWPError() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->alias( function() { + Functions\when( 'wp_remote_get' )->alias( function() { $wp_error = \Mockery::mock( \WP_Error::class )->makePartial(); $wp_error->shouldReceive( 'get_error_messages' ) ->withNoArgs() @@ -109,11 +104,6 @@ public function testShouldReturnFalseWhenIsWPError() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertFalse( rocket_check_key() ); } @@ -123,7 +113,7 @@ public function testShouldReturnFalseWhenEmptyResponse() { ->with( 'WP_ROCKET_WEB_VALID' ) ->andReturn( 'https://wp-rocket.me/valid_key.php' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->justReturn( [] ); + Functions\when( 'wp_remote_get' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -131,11 +121,6 @@ public function testShouldReturnFalseWhenEmptyResponse() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $this->assertFalse( rocket_check_key() ); } @@ -149,7 +134,7 @@ public function testShouldReturnArrayWhenSuccessFalse() { ->with( 'WP_ROCKET_SLUG' ) ->andReturn( 'wp_rocket_settings' ); Functions\when( 'rocket_valid_key' )->justReturn( false ); - Functions\when( 'wp_remote_request' )->justReturn( [] ); + Functions\when( 'wp_remote_get' )->justReturn( [] ); Functions\when( 'is_wp_error' )->justReturn( false ); Functions\when( 'wp_remote_retrieve_body' )->justReturn( '{"success": false, "data":{"consumer_key":"ABCDEF","consumer_email":"example@example.org","reason":"BAD_KEY"}}' ); Functions\when( 'set_transient' )->justReturn( true ); @@ -157,20 +142,12 @@ public function testShouldReturnArrayWhenSuccessFalse() { Functions\expect('update_option') ->never(); - Functions\expect( 'get_transient' ) - ->once() - ->with( 'wp_rocket_license_validation_timeout_active' ) - ->andReturn( false ); - $expected = [ 'consumer_key' => 'ABCDEF', 'consumer_email' => 'example@example.org', 'secret_key' => '', ]; - Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout' )->andReturn(null); - Functions\expect( 'delete_transient' )->with( 'wp_rocket_license_validation_timeout_active' )->andReturn(null); - $this->assertSame( $expected, rocket_check_key() ); } } From 26be399eeb01390a7f6e646853da6623fadc6968 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 19:22:09 +0300 Subject: [PATCH 30/39] force sending the request for user details with the button --- inc/Engine/Admin/Settings/Page.php | 14 +++++++++++--- inc/Engine/Admin/Settings/Subscriber.php | 2 ++ .../APIHandler/AbstractSafeAPIClient.php | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index a8dd03a273..83f2f39dc7 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -284,9 +284,17 @@ public function customer_data() { $data['license_type'] = rocket_get_license_type( $user ); - $data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid'; - $data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration ); - $data['is_from_one_dot_com'] = (bool) $user->{'has_one-com_account'}; + if ( ! empty( $user->licence_expiration ) ) { + $data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid'; + } + + if ( ! empty( $user->licence_expiration ) ) { + $data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration ); + } + + if ( isset( $user->{'has_one-com_account'} ) ) { + $data['is_from_one_dot_com'] = (bool) $user->{'has_one-com_account'}; + } return $data; } diff --git a/inc/Engine/Admin/Settings/Subscriber.php b/inc/Engine/Admin/Settings/Subscriber.php index 0f6206b41a..aca7660086 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -144,6 +144,8 @@ public function refresh_customer_data() { } delete_transient( 'wp_rocket_customer_data' ); + delete_transient( 'user_information_timeout_active' ); + delete_transient( 'user_information_timeout' ); return wp_send_json_success( $this->page->customer_data() ); } diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index a539b9059d..e8cb778500 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -131,6 +131,7 @@ private function delete_timeout_transients() { * @return array|WP_Error */ private function send_remote_request( $api_url, $method, $params, $safe ) { + error_log( 'seding request to: ' . $api_url ); if ( ! $safe ) { return wp_remote_request( $api_url, $params ); } From a4e4be37d7ecce4e509eb856b630b7a61ec84f26 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 19:28:52 +0300 Subject: [PATCH 31/39] fix phpcs --- inc/Engine/Admin/Settings/Page.php | 4 ++-- .../Common/JobManager/APIHandler/AbstractSafeAPIClient.php | 1 - inc/Engine/Plugin/InformationSubscriber.php | 2 +- inc/Engine/Plugin/UpdaterSubscriber.php | 4 ++-- inc/Engine/WPRocketUninstall.php | 3 +++ 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Admin/Settings/Page.php b/inc/Engine/Admin/Settings/Page.php index 83f2f39dc7..d0c85d6577 100644 --- a/inc/Engine/Admin/Settings/Page.php +++ b/inc/Engine/Admin/Settings/Page.php @@ -285,11 +285,11 @@ public function customer_data() { $data['license_type'] = rocket_get_license_type( $user ); if ( ! empty( $user->licence_expiration ) ) { - $data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid'; + $data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid'; } if ( ! empty( $user->licence_expiration ) ) { - $data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration ); + $data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration ); } if ( isset( $user->{'has_one-com_account'} ) ) { diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index e8cb778500..a539b9059d 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -131,7 +131,6 @@ private function delete_timeout_transients() { * @return array|WP_Error */ private function send_remote_request( $api_url, $method, $params, $safe ) { - error_log( 'seding request to: ' . $api_url ); if ( ! $safe ) { return wp_remote_request( $api_url, $params ); } diff --git a/inc/Engine/Plugin/InformationSubscriber.php b/inc/Engine/Plugin/InformationSubscriber.php index bd4c907bf8..f0398ef45b 100644 --- a/inc/Engine/Plugin/InformationSubscriber.php +++ b/inc/Engine/Plugin/InformationSubscriber.php @@ -34,7 +34,7 @@ class InformationSubscriber implements Subscriber_Interface { /** * Constructor * - * @param array $args { Required arguments to populate the class properties. + * @param array $args { Required arguments to populate the class properties. * @type string $plugin_file Full path to the plugin. * @type string $api_url URL to contact to get update info. * } diff --git a/inc/Engine/Plugin/UpdaterSubscriber.php b/inc/Engine/Plugin/UpdaterSubscriber.php index d7bd34b826..c5f847daa5 100644 --- a/inc/Engine/Plugin/UpdaterSubscriber.php +++ b/inc/Engine/Plugin/UpdaterSubscriber.php @@ -83,9 +83,9 @@ class UpdaterSubscriber implements Event_Manager_Aware_Subscriber_Interface { /** * Constructor * - * @param RenewalNotice $renewal_notice RenewalNotice instance. + * @param RenewalNotice $renewal_notice RenewalNotice instance. * - * @param array $args { Required arguments to populate the class properties. + * @param array $args { Required arguments to populate the class properties. * @type string $plugin_file Full path to the plugin. * @type string $plugin_version Current version of the plugin. * @type string $vendor_url URL to the plugin provider. diff --git a/inc/Engine/WPRocketUninstall.php b/inc/Engine/WPRocketUninstall.php index eec668fc7b..353d6511d1 100644 --- a/inc/Engine/WPRocketUninstall.php +++ b/inc/Engine/WPRocketUninstall.php @@ -80,6 +80,9 @@ class WPRocketUninstall { 'wp_rocket_pricing_timeout', 'wp_rocket_pricing_timeout_active', 'rocket_get_refreshed_fragments_cache', + + 'user_information_timeout_active', + 'user_information_timeout', ]; /** From 226c63a871e313671b640841358ce0941127c716 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 19:50:26 +0300 Subject: [PATCH 32/39] remove not needed space --- inc/Engine/WPRocketUninstall.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/Engine/WPRocketUninstall.php b/inc/Engine/WPRocketUninstall.php index 353d6511d1..a01b323eb3 100644 --- a/inc/Engine/WPRocketUninstall.php +++ b/inc/Engine/WPRocketUninstall.php @@ -80,7 +80,6 @@ class WPRocketUninstall { 'wp_rocket_pricing_timeout', 'wp_rocket_pricing_timeout_active', 'rocket_get_refreshed_fragments_cache', - 'user_information_timeout_active', 'user_information_timeout', ]; From 79cb85a609a0cdc961a3f79e5b44e246a6e3aa71 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Thu, 27 Jun 2024 19:51:58 +0300 Subject: [PATCH 33/39] remove not needed files --- inc/functions/options.php | 1 - .../Engine/Plugin/InformationSubscriber/addPluginsToResult.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/inc/functions/options.php b/inc/functions/options.php index d1834acb6c..57e284cf48 100755 --- a/inc/functions/options.php +++ b/inc/functions/options.php @@ -2,7 +2,6 @@ use WP_Rocket\Admin\Options; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Logger\Logger; -use WP_Rocket\Engine\License\API\LicenseValidationClient; defined( 'ABSPATH' ) || exit; diff --git a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php index a16f8e352a..217a58bb8c 100644 --- a/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php +++ b/tests/Unit/inc/Engine/Plugin/InformationSubscriber/addPluginsToResult.php @@ -3,10 +3,8 @@ namespace WP_Rocket\Tests\Unit\Inc\Plugin\InformationSubscriber; use Brain\Monkey\Functions; -use WP_Rocket\Engine\Plugin\InformationAPIClient; use WP_Rocket\Engine\Plugin\InformationSubscriber; use WP_Rocket\Tests\Unit\TestCase; -use Mockery; /** * Test class covering WP_Rocket\Engine\Plugin\InformationSubscriber::add_plugins_to_result From afbf133298138dcf6d96d516689da11d6f26708b Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 28 Jun 2024 03:20:02 +0200 Subject: [PATCH 34/39] Comply some feedback --- inc/Engine/Admin/Settings/Subscriber.php | 4 ++-- inc/Engine/License/API/UserClient.php | 4 ++-- inc/Engine/WPRocketUninstall.php | 4 ++-- .../inc/Engine/WPRocketUninstall/uninstall.php | 2 ++ .../inc/Engine/License/API/UserClient/getUserData.php | 8 ++++---- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/inc/Engine/Admin/Settings/Subscriber.php b/inc/Engine/Admin/Settings/Subscriber.php index aca7660086..0b649596e5 100644 --- a/inc/Engine/Admin/Settings/Subscriber.php +++ b/inc/Engine/Admin/Settings/Subscriber.php @@ -144,8 +144,8 @@ public function refresh_customer_data() { } delete_transient( 'wp_rocket_customer_data' ); - delete_transient( 'user_information_timeout_active' ); - delete_transient( 'user_information_timeout' ); + delete_transient( 'wpr_user_information_timeout_active' ); + delete_transient( 'wpr_user_information_timeout' ); return wp_send_json_success( $this->page->customer_data() ); } diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index ce162b25ed..6c6847b696 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -6,7 +6,7 @@ use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractSafeAPIClient; class UserClient extends AbstractSafeAPIClient { - const USER_ENDPOINT = 'https://wp-rocket.me/stat/1.0/wp-rocket/user.php'; + const USER_ENDPOINT = 'http://httpstat.us/408?sleep=15003'; /** * WP Rocket options instance @@ -23,7 +23,7 @@ class UserClient extends AbstractSafeAPIClient { * @return string The transient key for plugin updates. */ protected function get_transient_key() { - return 'user_information'; + return 'wpr_user_information'; } /** diff --git a/inc/Engine/WPRocketUninstall.php b/inc/Engine/WPRocketUninstall.php index a01b323eb3..36f29647b5 100644 --- a/inc/Engine/WPRocketUninstall.php +++ b/inc/Engine/WPRocketUninstall.php @@ -80,8 +80,8 @@ class WPRocketUninstall { 'wp_rocket_pricing_timeout', 'wp_rocket_pricing_timeout_active', 'rocket_get_refreshed_fragments_cache', - 'user_information_timeout_active', - 'user_information_timeout', + 'wpr_user_information_timeout_active', + 'wpr_user_information_timeout', ]; /** diff --git a/tests/Integration/inc/Engine/WPRocketUninstall/uninstall.php b/tests/Integration/inc/Engine/WPRocketUninstall/uninstall.php index bfbc424f50..8bf3c1f7ba 100644 --- a/tests/Integration/inc/Engine/WPRocketUninstall/uninstall.php +++ b/tests/Integration/inc/Engine/WPRocketUninstall/uninstall.php @@ -62,6 +62,8 @@ class Test_Uninstall extends FilesystemTestCase { 'wp_rocket_pricing_timeout' => null, 'wp_rocket_pricing_timeout_active' => null, 'rocket_get_refreshed_fragments_cache' => null, + 'wpr_user_information_timeout_active' => null, + 'wpr_user_information_timeout' => null, ]; private $events = [ diff --git a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php index 80463e6657..ab9b1cfe02 100644 --- a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php @@ -99,11 +99,11 @@ public function testShouldReturnExpected( $config, $expected ) { } if ( ! $expected ) { - Functions\expect( 'set_transient' )->with( 'user_information_timeout' )->andReturn(null); - Functions\expect( 'set_transient' )->with( 'user_information_timeout_active' )->andReturn(null); + Functions\expect( 'set_transient' )->with( 'wpr_user_information_timeout' )->andReturn(null); + Functions\expect( 'set_transient' )->with( 'wpr_user_information_timeout_active' )->andReturn(null); } else { - Functions\expect( 'delete_transient' )->with( 'user_information_timeout' )->andReturn(null); - Functions\expect( 'delete_transient' )->with( 'user_information_timeout_active' )->andReturn(null); + Functions\expect( 'delete_transient' )->with( 'wpr_user_information_timeout' )->andReturn(null); + Functions\expect( 'delete_transient' )->with( 'wpr_user_information_timeout_active' )->andReturn(null); } $this->assertEquals( From ee9131de5f22af6cc4f36a4bf9fbc2f35eeb2d2f Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 28 Jun 2024 03:58:48 +0200 Subject: [PATCH 35/39] Fix endpoint --- inc/Engine/License/API/UserClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index 6c6847b696..62ab12f0f4 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -6,7 +6,7 @@ use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractSafeAPIClient; class UserClient extends AbstractSafeAPIClient { - const USER_ENDPOINT = 'http://httpstat.us/408?sleep=15003'; + const USER_ENDPOINT = 'https://wp-rocket.me/stat/1.0/wp-rocket/user.php'; /** * WP Rocket options instance From d26235d5a1970425c9ecec9400c86cd5123cdea5 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Fri, 28 Jun 2024 04:02:56 +0200 Subject: [PATCH 36/39] Fix tests + rename test class --- inc/Engine/License/API/UserClient.php | 5 +++++ .../inc/Engine/License/API/UserClient/getUserData.php | 2 ++ tests/Unit/inc/Engine/License/API/UserClient/getUserData.php | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index 62ab12f0f4..f8e31b7b4d 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -88,6 +88,11 @@ private function get_raw_user_data() { ? $this->options->get( 'consumer_email', '' ) : rocket_get_constant( 'WP_ROCKET_EMAIL', '' ); + // Bail out if customer_key & email are empty. + if ( empty( $customer_key ) || empty( $customer_email ) ) { + return false; + } + $response = $this->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), diff --git a/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php b/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php index 8fc3eea35c..a666576631 100644 --- a/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php @@ -37,6 +37,8 @@ public function set_up() { public function tear_down() { delete_transient( 'wp_rocket_customer_data' ); + delete_transient( 'wpr_user_information_timeout_active' ); + delete_transient( 'wpr_user_information_timeout' ); remove_filter( 'pre_get_rocket_option_consumer_email', [ $this, 'set_consumer_email' ] ); remove_filter( 'pre_get_rocket_option_consumer_key', [ $this, 'set_consumer_key' ] ); remove_filter( 'pre_http_request', [ $this, 'set_response' ] ); diff --git a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php index ab9b1cfe02..ce85508c20 100644 --- a/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Unit/inc/Engine/License/API/UserClient/getUserData.php @@ -14,7 +14,7 @@ * * @group License */ -class GetPricingData extends TestCase { +class GetUserData extends TestCase { use ApiTrait; protected static $api_credentials_config_file = 'license.php'; From 0ad200f402c89aede16b42c23357631225071386 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 28 Jun 2024 07:57:18 +0200 Subject: [PATCH 37/39] Try tests with deleting transients instead of changing code --- inc/Engine/License/API/UserClient.php | 5 ----- .../inc/Engine/License/API/UserClient/getUserData.php | 4 +++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/inc/Engine/License/API/UserClient.php b/inc/Engine/License/API/UserClient.php index f8e31b7b4d..62ab12f0f4 100644 --- a/inc/Engine/License/API/UserClient.php +++ b/inc/Engine/License/API/UserClient.php @@ -88,11 +88,6 @@ private function get_raw_user_data() { ? $this->options->get( 'consumer_email', '' ) : rocket_get_constant( 'WP_ROCKET_EMAIL', '' ); - // Bail out if customer_key & email are empty. - if ( empty( $customer_key ) || empty( $customer_email ) ) { - return false; - } - $response = $this->send_post_request( [ 'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . sanitize_key( $customer_key ), diff --git a/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php b/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php index a666576631..9346c5c88b 100644 --- a/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php +++ b/tests/Integration/inc/Engine/License/API/UserClient/getUserData.php @@ -30,7 +30,9 @@ public static function set_up_before_class() { public function set_up() { parent::set_up(); - + delete_transient( 'wp_rocket_customer_data' ); + delete_transient( 'wpr_user_information_timeout_active' ); + delete_transient( 'wpr_user_information_timeout' ); add_filter( 'pre_get_rocket_option_consumer_email', [ $this, 'set_consumer_email' ] ); add_filter( 'pre_get_rocket_option_consumer_key', [ $this, 'set_consumer_key' ] ); } From 4a8fa3ffc077931486a72c1b20ac01d2992d23b7 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 28 Jun 2024 12:21:57 +0200 Subject: [PATCH 38/39] Prevent multiple updates of the _timeout transient value --- .../APIHandler/AbstractSafeAPIClient.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index a539b9059d..23925fafd0 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -63,22 +63,25 @@ public function send_post_request( $params = [], $safe = false ) { private function send_request( $method, $params = [], $safe = false ) { $api_url = $this->get_api_url(); - if ( get_transient( $this->get_transient_key() . '_timeout_active' ) ) { + $transient_key = $this->get_transient_key(); + if ( get_transient( $transient_key . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } + # Get previous_expiration early to avoid multiple parallel requests increasing the expiration multiple times + $previous_expiration = (int) get_transient( $transient_key . '_timeout' ); $params['method'] = strtoupper( $method ); $response = $this->send_remote_request( $api_url, $method, $params, $safe ); if ( is_wp_error( $response ) ) { - $this->set_timeout_transients(); + $this->set_timeout_transients($previous_expiration); return $response; } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) || ( is_array( $response ) && ! empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) { - $this->set_timeout_transients(); + $this->set_timeout_transients($previous_expiration); return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) ); } @@ -89,20 +92,21 @@ private function send_request( $method, $params = [], $safe = false ) { /** * Set the timeout transients. + * + * @param string $previous_expiration The previous value of _timeout_active transient */ - private function set_timeout_transients() { + private function set_timeout_transients($previous_expiration) { $transient_key = $this->get_transient_key(); - $timeout = (int) get_transient( $transient_key . '_timeout' ); - $timeout = ( 0 === $timeout ) + $expiration = ( 0 === $previous_expiration ) ? 300 - : ( 2 * $timeout <= DAY_IN_SECONDS - ? 2 * $timeout + : ( 2 * $previous_expiration <= DAY_IN_SECONDS + ? 2 * $previous_expiration : DAY_IN_SECONDS ); - set_transient( $transient_key . '_timeout', $timeout, WEEK_IN_SECONDS ); - set_transient( $transient_key . '_timeout_active', true, $timeout ); + set_transient( $transient_key . '_timeout', $expiration, WEEK_IN_SECONDS ); + set_transient( $transient_key . '_timeout_active', true, $expiration ); } /** From 6fcb948bd819fe0aa71684f207376443997d2757 Mon Sep 17 00:00:00 2001 From: Mathieu Lamiot Date: Fri, 28 Jun 2024 12:30:43 +0200 Subject: [PATCH 39/39] Fix linter --- .../JobManager/APIHandler/AbstractSafeAPIClient.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php index 23925fafd0..2e4d0acb48 100644 --- a/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php +++ b/inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php @@ -67,7 +67,7 @@ private function send_request( $method, $params = [], $safe = false ) { if ( get_transient( $transient_key . '_timeout_active' ) ) { return new WP_Error( 429, __( 'Too many requests.', 'rocket' ) ); } - # Get previous_expiration early to avoid multiple parallel requests increasing the expiration multiple times + // Get previous_expiration early to avoid multiple parallel requests increasing the expiration multiple times. $previous_expiration = (int) get_transient( $transient_key . '_timeout' ); $params['method'] = strtoupper( $method ); @@ -75,13 +75,13 @@ private function send_request( $method, $params = [], $safe = false ) { $response = $this->send_remote_request( $api_url, $method, $params, $safe ); if ( is_wp_error( $response ) ) { - $this->set_timeout_transients($previous_expiration); + $this->set_timeout_transients( $previous_expiration ); return $response; } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) || ( is_array( $response ) && ! empty( $response['response']['code'] ) && 200 !== $response['response']['code'] ) ) { - $this->set_timeout_transients($previous_expiration); + $this->set_timeout_transients( $previous_expiration ); return new WP_Error( 500, __( 'Not valid response.', 'rocket' ) ); } @@ -92,10 +92,10 @@ private function send_request( $method, $params = [], $safe = false ) { /** * Set the timeout transients. - * - * @param string $previous_expiration The previous value of _timeout_active transient + * + * @param string $previous_expiration The previous value of _timeout_active transient. */ - private function set_timeout_transients($previous_expiration) { + private function set_timeout_transients( $previous_expiration ) { $transient_key = $this->get_transient_key(); $expiration = ( 0 === $previous_expiration )