Skip to content

Commit

Permalink
adjust information client/subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Jun 26, 2024
1 parent f3175b4 commit f283e08
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WP_Rocket\Engine\Common\JobManager\APIHandler;

use WP_Rocket\Admin\Options_Data;
use WP_Error;

/**
* Class AbstractSafeAPIClient
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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' ) );

Check notice on line 63 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#L63

Mismatched text domain. Expected 'rocket' but got 'wp-rocket'.
}

if ( empty( $params['body'] ) ) {
Expand All @@ -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' );
Expand Down
13 changes: 5 additions & 8 deletions inc/Engine/Common/JobManager/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace WP_Rocket\Engine\Common\JobManager\APIHandler;
namespace WP_Rocket\Engine\Plugin;

use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractSafeAPIClient;

/**
* Class PluginInformationClient
Expand All @@ -10,7 +12,7 @@
*
* @package WP_Rocket\Engine\Common\JobManager\APIHandler
*/
class PluginInformationClient extends AbstractSafeAPIClient {
class InformationAPIClient extends AbstractSafeAPIClient {

/**
* Get the transient key for plugin information.
Expand Down
23 changes: 14 additions & 9 deletions inc/Engine/Plugin/InformationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_Error;

/**
* Manages the plugin information.
Expand Down Expand Up @@ -31,6 +31,8 @@ class InformationSubscriber implements Subscriber_Interface {
*/
protected $request_error_id = 'plugins_api_failed';

private $client;

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/InformationSubscriber.php#L34

Missing member variable doc comment

/**
* Constructor
*
Expand All @@ -40,14 +42,18 @@ class InformationSubscriber implements Subscriber_Interface {
* @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.

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Plugin/InformationSubscriber.php#L45

Missing parameter type
*
*/
public function __construct( $args ) {
public function __construct( $args, $client ) {
if ( isset( $args['plugin_file'] ) ) {
$this->plugin_slug = $this->get_plugin_slug( $args['plugin_file'] );
}
if ( isset( $args['api_url'] ) ) {
$this->api_url = $args['api_url'];
}

$this->client = $client;
}

/**
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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.
Expand All @@ -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() );
Expand Down

0 comments on commit f283e08

Please sign in to comment.