Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Admin\RocketInsights\APIHandler;

use WP_Rocket\Engine\Common\JobManager\APIHandler\AbstractAPIClient;
use WP_Rocket\Logger\LoggerAware;
use WP_Rocket\Logger\LoggerAwareInterface;

/**
* Rocket Insights API Client
*
* Handles communication with the SaaS Director API for performance testing
*/
class GlobalScoreSaaSAPIClient extends AbstractAPIClient implements LoggerAwareInterface {
use LoggerAware;

/**
* SaaS Director API path for sending global score.
*
* @var string
*/
protected $request_path = 'rocket-insights-global-score/';

/**
* Send the global score to SaaS.
*
* @param array $data Request body data.
* @param array $args Additional request arguments (timeout, headers, etc.).
* @return array|\WP_Error
*/
public function send_to_saas( array $data = [], array $args = [] ) {
$request_body = wp_parse_args(
$data,
[
'domain' => home_url(),
'average_score' => 0,
'blurred' => 0,
'credits_left' => 0,
'license' => 'free',
'automatic_test' => 0,
'wpr_user_id' => 0,
'email' => $this->options->get( 'consumer_email', '' ),
'key' => $this->options->get( 'consumer_key', '' ),
]
);

$args = array_merge(
[
'json_encode' => true,
'body' => $request_body,
'headers' => [
'Content-Type' => 'application/json',
],
],
$args
);

$this->logger::debug(
'Global Score SaaS: API send request',
[
'domain' => $request_body['domain'],
'average_score' => $request_body['average_score'],
]
);

$sent = $this->handle_post( $args );

if ( ! $sent ) {
$error_data = [
'code' => $this->response_code,
'message' => $this->error_message,
];

$this->logger::error(
'Global Score SaaS: API failed',
$error_data
);

return $error_data;
}

$response_data = json_decode( $this->response_body, true );

$this->logger::info( 'Global Score SaaS: Request sent successfully' );

$response_data['code'] = $this->response_code;

return $response_data;
}

/**
* Validate add to queue response if it's valid or not.
*
* @param array $response Response array.
* @return bool
*/
public function validate_add_to_queue_response( array $response ): bool {

Check warning on line 98 in inc/Engine/Admin/RocketInsights/APIHandler/GlobalScoreSaaSAPIClient.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/APIHandler/GlobalScoreSaaSAPIClient.php#L98

Avoid unused parameters such as '$response'.
return false;
}
}
4 changes: 4 additions & 0 deletions inc/Engine/Admin/RocketInsights/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@
$max_urls = $this->user->get_rocket_insights_addon_limit( $this->user->get_rocket_insights_addon_sku_active() );
return $current_url_count < $max_urls;
}

public function get_user_id() {

Check notice on line 127 in inc/Engine/Admin/RocketInsights/Context/Context.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Context/Context.php#L127

Missing doc comment for function get_user_id()
return $this->user->get_user_id();
}
}
58 changes: 55 additions & 3 deletions inc/Engine/Admin/RocketInsights/GlobalScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace WP_Rocket\Engine\Admin\RocketInsights;

use WP_Rocket\Admin\Options;
use WP_Rocket\Engine\Admin\RocketInsights\APIHandler\GlobalScoreSaaSAPIClient;
use WP_Rocket\Engine\Admin\RocketInsights\Database\Queries\RocketInsights as Query;

/**
Expand All @@ -22,20 +24,43 @@
*/
private const CACHE_EXPIRATION = DAY_IN_SECONDS;

/**
* Last sent global score option name.
*/
private const LAST_SENT_OPTION_NAME = 'sent_global_score';

/**
* Rocket Insights Query instance.
*
* @var Query
*/
private $query;

/**
* Global score SaaS API Client instance.
*
* @var GlobalScoreSaaSAPIClient
*/
private $client;

/**
* Options instance.
*
* @var Options
*/
private $options;

/**
* Constructor.
*
* @param Query $query Rocket Insights Query instance.
* @param Query $query Rocket Insights Query instance.
* @param Options $options Options instance.
* @param GlobalScoreSaaSAPIClient $client Global score SaaS API Client instance.
*/
public function __construct( Query $query ) {
$this->query = $query;
public function __construct( Query $query, Options $options, GlobalScoreSaaSAPIClient $client ) {
$this->query = $query;
$this->client = $client;
$this->options = $options;
}

/**
Expand Down Expand Up @@ -177,4 +202,31 @@
// All tests are complete and none are blurred.
return 'complete';
}

private function get_last_sent_global_score() {

Check notice on line 206 in inc/Engine/Admin/RocketInsights/GlobalScore.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/GlobalScore.php#L206

Missing doc comment for function get_last_sent_global_score()
return $this->options->get( static::LAST_SENT_OPTION_NAME );
}

public function maybe_send_request_to_saas(): void {

Check notice on line 210 in inc/Engine/Admin/RocketInsights/GlobalScore.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/GlobalScore.php#L210

Missing doc comment for function maybe_send_request_to_saas()
$global_score = $this->get_global_score_data();
if ( $global_score['score'] === $this->get_last_sent_global_score() ) {
return;
}

/**
* Filters Rocket Insights global score SaaS args.
*
* @param array $args Array of args.
*/
$args = wpm_apply_filters_typed( 'array', 'rocket_insights_global_score_saas_args', [

Check notice on line 221 in inc/Engine/Admin/RocketInsights/GlobalScore.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/GlobalScore.php#L221

Opening parenthesis of a multi-line function call must be the last content on the line
'average_score' => $global_score['score'],
'blurred' => 'blurred' === $global_score['status'],
] );

Check notice on line 224 in inc/Engine/Admin/RocketInsights/GlobalScore.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/GlobalScore.php#L224

Closing parenthesis of a multi-line function call must be on a line by itself

$sent = $this->client->send_to_saas( $args );
if ( is_wp_error( $sent ) || empty( $sent ) || 200 !== $sent['code'] ) {
return;
}
$this->options->set( static::LAST_SENT_OPTION_NAME, $global_score['score'] );
}
}
10 changes: 7 additions & 3 deletions inc/Engine/Admin/RocketInsights/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use WP_Rocket\Dependencies\League\Container\Argument\Literal\StringArgument;
use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Admin\RocketInsights\{
use WP_Rocket\Engine\Admin\RocketInsights\{APIHandler\GlobalScoreSaaSAPIClient,
Database\Tables\RocketInsights as RITable,
Database\Queries\RocketInsights as RIQuery,
APIHandler\APIClient as RIAPIClient,
Expand All @@ -18,8 +18,7 @@
URLLimit\Subscriber as URLLimitSubscriber,
Settings\Controller as SettingsController,
Settings\Subscriber as SettingsSubscriber,
PostListing\Subscriber as PostListingSubscriber,
};
PostListing\Subscriber as PostListingSubscriber};
use WP_Rocket\Engine\Common\JobManager\Queue\Queue as JobManagerQueue;

class ServiceProvider extends AbstractServiceProvider {
Expand All @@ -36,6 +35,7 @@ class ServiceProvider extends AbstractServiceProvider {
'ri_table',
'ri_query',
'ri_api_client',
'ri_global_score_saas_api_client',
'ri_context',
'ri_saas_context',
'ri_manager',
Expand Down Expand Up @@ -104,6 +104,9 @@ public function register(): void {
$this->getContainer()->add( 'ri_api_client', RIAPIClient::class )
->addArgument( 'options' );

$this->getContainer()->add( 'ri_global_score_saas_api_client', GlobalScoreSaaSAPIClient::class )
->addArgument( 'options' );

$this->getContainer()->add( 'ri_plan', Plan::class )
->addArguments(
[
Expand All @@ -129,6 +132,7 @@ public function register(): void {
->addArguments(
[
'ri_query',
'ri_global_score_saas_api_client',
]
);

Expand Down
27 changes: 27 additions & 0 deletions inc/Engine/Admin/RocketInsights/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@
'wp_rocket_upgrade' => [
[ 'on_update_reset_credit', 10, 2 ],
[ 'on_update_cancel_old_as_jobs', 10, 2 ],
[ 'on_update_send_global_score', 10, 2 ],
],
'admin_notices' => 'maybe_display_rocket_insights_promotion_notice',
'rest_api_init' => [ 'register_routes' ],
'rocket_insights_global_score_saas_args' => 'add_global_score_saas_args',
'set_transient_wpr_global_score_data' => 'send_global_score_saas_request',

Check notice on line 173 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L173

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

Expand Down Expand Up @@ -522,6 +525,20 @@
$this->queue->deprecate_old_actions();
}

/**
* Callback for the wp_rocket_upgrade action to send global score to saas with upgrading from a version before 3.20.3.
*
* @param string $new_version New plugin version.
* @param string $old_version Previous plugin version.
* @return void
*/
public function on_update_send_global_score( $new_version, $old_version ) {

Check warning on line 535 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L535

Avoid unused parameters such as '$new_version'.
if ( version_compare( $old_version, '3.20.3', '>=' ) ) {
return;
}
$this->global_score->maybe_send_request_to_saas();
}

/**
* Displays a promotion notice for Rocket Insights on the admin dashboard.
*
Expand Down Expand Up @@ -586,4 +603,14 @@
]
);
}
public function add_global_score_saas_args( $args ) {

Check notice on line 606 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L606

Missing doc comment for function add_global_score_saas_args()
$args['credits_left'] = $this->plan->get_credit();
$args['license'] = $this->context->is_free_user() ? 'free' : 'paid';

Check notice on line 608 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L608

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$args['wpr_user_id'] = $this->context->get_user_id();

Check notice on line 609 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L609

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
return $args;
}

public function send_global_score_saas_request() {

Check notice on line 613 in inc/Engine/Admin/RocketInsights/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/RocketInsights/Subscriber.php#L613

Missing doc comment for function send_global_score_saas_request()
$this->global_score->maybe_send_request_to_saas();
}
}
7 changes: 7 additions & 0 deletions inc/Engine/License/API/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,11 @@
}
return null;
}

public function get_user_id() {

Check notice on line 532 in inc/Engine/License/API/User.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/License/API/User.php#L532

Missing doc comment for function get_user_id()
if ( empty( $this->user->id ) ) {
return null;
}
return $this->user->id;
}
}