Skip to content

Commit

Permalink
first modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Jun 25, 2024
1 parent a8da7ae commit 46d3a48
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 12 deletions.
113 changes: 113 additions & 0 deletions inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace WP_Rocket\Engine\Common\JobManager\APIHandler;

use WP_Rocket\Admin\Options_Data;

/**
* AbstractSafeAPIClient is an abstract class that 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.
*/
abstract class AbstractSafeAPIClient
{
private $options;

public function __construct(Options_Data $options)

Check notice on line 15 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L15

Missing doc comment for function __construct()
{
$this->options = $options;
}

/**
* Get the transient key.
*
* @return string The transient key.
*/
abstract protected function getTransientKey();

Check notice on line 25 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L25

Method name "getTransientKey" in class AbstractSafeAPIClient is not in snake case format, try "get_transient_key"

/**
* Get the API URL.
*
* @return string The API URL.
*/
abstract protected function getApiUrl();

Check notice on line 32 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L32

Method name "getApiUrl" in class AbstractSafeAPIClient is not in snake case format, try "get_api_url"

/**
* Send a GET request.
*
* @param array $params The request parameters.
* @return mixed The response from the API.
*/
public function send_get_request($params)

Check notice on line 40 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L40

Expected 1 spaces after opening parenthesis; 0 found
{
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)

Check notice on line 51 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L51

Expected 1 spaces after opening parenthesis; 0 found
{
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();

Check notice on line 65 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L65

Variable "$transientKey" is not in valid snake_case format, try "$transient_key"
$apiUrl = $this->getApiUrl();

Check notice on line 66 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L66

Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

if (get_transient($transientKey . '_timeout_active') === true) {

Check notice on line 68 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L68

Expected 1 spaces after opening parenthesis; 0 found
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', ''),

Check notice on line 78 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L78

Array double arrow not aligned correctly; expected 3 space(s) between "'wpr_key'" and double arrow, but found 1.
];

$params['method'] = strtoupper($method);
$response = wp_remote_request($apiUrl, $params);

Check notice on line 82 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L82

Expected 1 spaces after opening parenthesis; 0 found

if (is_wp_error($response)) {

Check notice on line 84 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L84

Expected 1 spaces after opening parenthesis; 0 found

Check notice on line 84 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L84

No space after opening parenthesis is prohibited
$this->set_timeout_transients($transientKey);

Check notice on line 85 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L85

Expected 1 spaces after opening parenthesis; 0 found
return false;
}

delete_transient($transientKey . '_timeout_active');
delete_transient($transientKey . '_timeout');

Check notice on line 90 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L90

Variable "$transientKey" is not in valid snake_case format, try "$transient_key"

return $response;
}

/**
* Set the timeout transients.
*
* @param string $transientKey The transient key.
*/
private function set_timeout_transients($transientKey)
{
$timeout = (int) get_transient($transientKey . '_timeout');

Check notice on line 102 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L102

Expected 1 spaces after opening parenthesis; 0 found

Check notice on line 102 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L102

Variable "$transientKey" is not in valid snake_case format, try "$transient_key"
$timeout = (0 === $timeout)
? 300
: (2 * $timeout <= DAY_IN_SECONDS

Check notice on line 105 in inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/AbstractSafeAPIClient.php#L105

Expected 1 space after open parenthesis; 0 found
? 2 * $timeout
: DAY_IN_SECONDS
);

set_transient($transientKey . '_timeout', $timeout, WEEK_IN_SECONDS);
set_transient($transientKey . '_timeout_active', true, $timeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace WP_Rocket\Engine\Common\JobManager\APIHandler;
class PluginInformationClient extends AbstractSafeAPIClient
{
protected function getTransientKey()
{
return 'plugin_information';
}

protected function getApiUrl()
{
return 'https://wp-rocket.me';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
15 changes: 15 additions & 0 deletions inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace WP_Rocket\Engine\Common\JobManager\APIHandler;

Check notice on line 3 in inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php#L3

There must be one blank line after the namespace declaration
class PluginUpdateClient extends AbstractSafeAPIClient
{
protected function getTransientKey()

Check notice on line 6 in inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php#L6

Missing doc comment for function getTransientKey()
{
return 'plugin_update';
}

protected function getApiUrl()

Check notice on line 11 in inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/JobManager/APIHandler/PluginUpdateClient.php#L11

Missing doc comment for function getApiUrl()
{
return 'https://wp-rocket.me';
}
}
7 changes: 7 additions & 0 deletions inc/Engine/Common/JobManager/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand Down Expand Up @@ -87,5 +89,10 @@ 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') );
}
}
5 changes: 4 additions & 1 deletion inc/Engine/Plugin/InformationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Check failure on line 132 in inc/Engine/Plugin/InformationSubscriber.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Class WP_Rocket\Engine\Common\JobManager\APIHandler\PluginInformationClient constructor invoked with 0 parameters, 1 required.

Check notice on line 132 in inc/Engine/Plugin/InformationSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/InformationSubscriber.php#L132

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$response = $client->send_get_request([]);

Check notice on line 133 in inc/Engine/Plugin/InformationSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/InformationSubscriber.php#L133

Expected 1 spaces after opening parenthesis; 0 found

Check notice on line 134 in inc/Engine/Plugin/InformationSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/InformationSubscriber.php#L134

Functions must not contain multiple empty lines in a row; found 2 empty lines

if ( is_wp_error( $response ) ) {
return $this->get_request_error( $response->get_error_message() );
Expand Down
19 changes: 8 additions & 11 deletions inc/Engine/Plugin/UpdaterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Check failure on line 309 in inc/Engine/Plugin/UpdaterSubscriber.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Class WP_Rocket\Engine\Common\JobManager\APIHandler\PluginUpdateClient constructor invoked with 0 parameters, 1 required.
$response = $client->send_get_request([]);

Check notice on line 310 in inc/Engine/Plugin/UpdaterSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/UpdaterSubscriber.php#L310

Expected 1 spaces after opening parenthesis; 0 found

if ( is_wp_error( $request ) ) {
if (is_wp_error($response)) {

Check notice on line 312 in inc/Engine/Plugin/UpdaterSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/UpdaterSubscriber.php#L312

No space after opening parenthesis is prohibited
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 ) {
/**
Expand Down

0 comments on commit 46d3a48

Please sign in to comment.