Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply RocketCDN CNAME change #7095

Merged
merged 16 commits into from
Nov 19, 2024
Merged
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
60 changes: 59 additions & 1 deletion inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace WP_Rocket\Engine\CDN\RocketCDN;

use WP_Rocket\Admin\Options;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
Expand All @@ -9,6 +12,8 @@
* @since 3.5
*/
class DataManagerSubscriber implements Subscriber_Interface {
use RegexTrait;

const CRON_EVENT = 'rocketcdn_check_subscription_status_event';

/**
Expand All @@ -25,15 +30,33 @@
*/
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.
*/
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 All @@ -48,6 +71,7 @@
'wp_ajax_rocketcdn_process_status' => 'get_process_status',
'wp_ajax_rocketcdn_validate_token_cname' => 'validate_token_cname',
self::CRON_EVENT => 'maybe_disable_cdn',
'wp_rocket_upgrade' => [ 'refresh_cdn_cname', 10, 2 ],
];
}

Expand Down Expand Up @@ -315,4 +339,38 @@
wp_schedule_single_event( $timestamp, self::CRON_EVENT );
}
}

/**
* Upgrade callback.
*
* @param string $new_version Plugin new version.
* @param string $old_version Plugin old version.
* @return void
*/
public function refresh_cdn_cname( $new_version, $old_version ): void {

Check warning on line 350 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#L350

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

$cdn_cnames = $this->options->get( 'cdn_cnames', [] );
if ( empty( $cdn_cnames ) ) {
return;
}

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

$cdn_matches = $this->find( 'https:\/\/(?<cdn_id>[a-zA-Z0-9]{8})\.rocketcdn\.me', $cdn_cnames[0] );
if ( empty( $cdn_matches ) || empty( $cdn_matches[0]['cdn_id'] ) ) {
return;
}

$this->options_api->set( 'rocketcdn_old_url', $cdn_cnames[0] );
$cdn_cnames[0] = str_replace( $cdn_matches[0]['cdn_id'], $cdn_matches[0]['cdn_id'] . '.delivery', $cdn_cnames[0] );
$this->options->set( 'cdn_cnames', $cdn_cnames );

$this->options_api->set( 'settings', $this->options->get_options() );
}
}
57 changes: 57 additions & 0 deletions inc/Engine/CDN/RocketCDN/NoticesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function get_subscribed_events() {
'admin_notices' => [
[ 'promote_rocketcdn_notice' ],
[ 'purge_cache_notice' ],
[ 'change_cname_notice' ],
],
'rocket_before_cdn_sections' => 'display_rocketcdn_cta',
'wp_ajax_toggle_rocketcdn_cta' => 'toggle_cta',
Expand Down Expand Up @@ -353,4 +354,60 @@ public function purge_cache_notice() {
private function is_white_label_account() {
return (bool) rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_ACCOUNT' );
}

/**
* Change CName admin notice contents.
*
* @return void
*/
public function change_cname_notice() {
if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

if ( 'settings_page_wprocket' !== get_current_screen()->id ) {
return;
}

$boxes = get_user_meta( get_current_user_id(), 'rocket_boxes', true );

if ( in_array( 'rocketcdn_change_cname', (array) $boxes, true ) ) {
return;
}

$old_cname = get_option( 'wp_rocket_rocketcdn_old_url' );
if ( empty( $old_cname ) ) {
return;
}

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

$support_url = rocket_get_external_url(
'support',
[
'utm_source' => 'wp_plugin',
'utm_medium' => 'wp_rocket',
]
);

$message_lines = [
// translators: %1$s = Old CName, %2$s = New CName.
sprintf( esc_html__( 'We\'ve updated your RocketCDN CNAME from %1$s to %2$s.', 'rocket' ), $old_cname, $new_subscription['cdn_url'] ),
// translators: %1$s = New CName.
sprintf( esc_html__( 'The change is already applied to the plugin settings. If you were using the CNAME in your code, make sure to update it to: %1$s.', 'rocket' ), $new_subscription['cdn_url'] ),
];

rocket_notice_html(
[
'status' => 'info',
'message' => implode( '<br>', $message_lines ),
'dismiss_button' => 'rocketcdn_change_cname',
'id' => 'rocketcdn_change_cname_notice',
'action' => sprintf( '<a href="%1$s" target="_blank" rel="noopener" class="wpr-button" id="rocketcdn-change-cname-button">%2$s</a>', $support_url, esc_html__( 'contact support', 'rocket' ) ),
]
);
}
}
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
1 change: 1 addition & 0 deletions inc/Engine/WPRocketUninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class WPRocketUninstall {
'wp_rocket_no_licence',
'wp_rocket_last_option_hash',
'wp_rocket_debug',
'wp_rocket_rocketcdn_old_url',
];

/**
Expand Down
Loading