diff --git a/inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php b/inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php index 610cf77883..afc85a27e0 100644 --- a/inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php +++ b/inc/Engine/CDN/RocketCDN/DataManagerSubscriber.php @@ -1,6 +1,9 @@ api_client = $api_client; $this->cdn_options = $cdn_options; + $this->options = $options; + $this->options_api = $options_api; } /** @@ -48,6 +71,7 @@ public static function get_subscribed_events() { '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 ], ]; } @@ -315,4 +339,38 @@ private function schedule_subscription_check( $subscription ) { 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 { + 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:\/\/(?[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() ); + } } diff --git a/inc/Engine/CDN/RocketCDN/NoticesSubscriber.php b/inc/Engine/CDN/RocketCDN/NoticesSubscriber.php index a09a968a59..36519b8f46 100644 --- a/inc/Engine/CDN/RocketCDN/NoticesSubscriber.php +++ b/inc/Engine/CDN/RocketCDN/NoticesSubscriber.php @@ -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', @@ -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( '
', $message_lines ), + 'dismiss_button' => 'rocketcdn_change_cname', + 'id' => 'rocketcdn_change_cname_notice', + 'action' => sprintf( '%2$s', $support_url, esc_html__( 'contact support', 'rocket' ) ), + ] + ); + } } diff --git a/inc/Engine/CDN/RocketCDN/ServiceProvider.php b/inc/Engine/CDN/RocketCDN/ServiceProvider.php index 3816aeb95d..220bd65248 100644 --- a/inc/Engine/CDN/RocketCDN/ServiceProvider.php +++ b/inc/Engine/CDN/RocketCDN/ServiceProvider.php @@ -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' ) ) diff --git a/inc/Engine/WPRocketUninstall.php b/inc/Engine/WPRocketUninstall.php index c1a291b644..ae30a0ad06 100644 --- a/inc/Engine/WPRocketUninstall.php +++ b/inc/Engine/WPRocketUninstall.php @@ -40,6 +40,7 @@ class WPRocketUninstall { 'wp_rocket_no_licence', 'wp_rocket_last_option_hash', 'wp_rocket_debug', + 'wp_rocket_rocketcdn_old_url', ]; /**