Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Nov 14, 2024
1 parent 8607703 commit c058b97
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
44 changes: 33 additions & 11 deletions inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;

use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
Expand All @@ -25,15 +27,33 @@ class DataManagerSubscriber implements Subscriber_Interface {
*/
private $cdn_options;

/**
* WP Options API instance
*
* @var Options
*/
private $options_api;

/**
* WP Rocket Options instance
*
* @var Options_Data
*/
private $options;

/**
* Constructor
*
* @param APIClient $api_client RocketCDN API Client instance.
* @param CDNOptionsManager $cdn_options CDNOptionsManager instance.
* @param Options_Data $options Options instance.
* @param Options $options_api Options API instance.

Check notice on line 50 in inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php#L50

Expected 11 spaces after parameter type; 6 found
*/
public function __construct( APIClient $api_client, CDNOptionsManager $cdn_options ) {
public function __construct( APIClient $api_client, CDNOptionsManager $cdn_options, Options_Data $options, Options $options_api ) {
$this->api_client = $api_client;
$this->cdn_options = $cdn_options;
$this->options = $options;
$this->options_api = $options_api;
}

/**
Expand Down Expand Up @@ -325,24 +345,26 @@ private function schedule_subscription_check( $subscription ) {
* @return void
*/
public function refresh_cdn_cname( $new_version, $old_version ): void {

Check warning on line 347 in inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php#L347

Avoid unused parameters such as '$new_version'.
if ( version_compare( $old_version, '3.17.2', '>' ) ) {
return;
}

$old_subscription_data = $this->api_client->get_subscription_data();

if ( ! $old_subscription_data['is_active'] || empty( $old_subscription_data['cdn_url'] ) ) {
if ( version_compare( $old_version, '3.17.3', '>=' ) ) {
return;
}

delete_transient( 'rocketcdn_status' );

$new_subscription_data = $this->api_client->get_subscription_data();
if ( ! $new_subscription_data['is_active'] || empty( $new_subscription_data['cdn_url'] ) ) {
return;
}

if ( $old_subscription_data['cdn_url'] === $new_subscription_data['cdn_url'] ) {
$cdn_cnames = $this->options->get( 'cdn_cnames', [] );

Check notice on line 359 in inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php#L359

Equals sign not aligned correctly; expected 1 space but found 2 spaces
if ( empty( $cdn_cnames[0] ) || $cdn_cnames[0] === $new_subscription_data['cdn_url'] ) {
return;
}
// Only save the old url when the new url doesn't equal the old one.
update_option( 'rocketcdn_old_url', $old_subscription_data['cdn_url'], false );

$this->options->set( 'rocketcdn_old_url', $cdn_cnames[0] );
$cdn_cnames[0] = $new_subscription_data['cdn_url'];
$this->options->set( 'cdn_cnames', $cdn_cnames );

$this->options_api->set( 'settings', $this->options->get_options() );
}
}
2 changes: 1 addition & 1 deletion inc/Engine/CDN/RocketCDN/NoticesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function change_cname_notice() {
return;
}

$old_cname = get_option( 'rocketcdn_old_url' );
$old_cname = get_rocket_option( 'rocketcdn_old_url' );
if ( empty( $old_cname ) ) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion inc/Engine/CDN/RocketCDN/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function register(): void {
// RocketCDN Data manager subscriber.
$this->getContainer()->addShared( 'rocketcdn_data_manager_subscriber', DataManagerSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_api_client' ) )
->addArgument( $this->getContainer()->get( 'rocketcdn_options_manager' ) );
->addArgument( $this->getContainer()->get( 'rocketcdn_options_manager' ) )
->addArgument( $this->getContainer()->get( 'options' ) )
->addArgument( $this->getContainer()->get( 'options_api' ) );
// RocketCDN REST API Subscriber.
$this->getContainer()->addShared( 'rocketcdn_rest_subscriber', RESTSubscriber::class )
->addArgument( $this->getContainer()->get( 'rocketcdn_options_manager' ) )
Expand Down

0 comments on commit c058b97

Please sign in to comment.