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

Closes 5667 detect domain change #5688

Merged
merged 55 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
f5b05b4
Added a way to detect domain change
CrochetFeve0251 Jan 17, 2023
a940c71
Added a way to deleted
CrochetFeve0251 Jan 17, 2023
dc0afaa
Fixed logic and code standards
CrochetFeve0251 Jan 17, 2023
a279b4c
Fixed tests
CrochetFeve0251 Jan 17, 2023
8d0767b
Added block doc for the `rocket_domain_changed` action
CrochetFeve0251 Jan 18, 2023
b6d20bd
Added clear cache
CrochetFeve0251 Jan 19, 2023
af540ee
Merge branch 'develop' into enhancement/5667-detect-domain-change
CrochetFeve0251 Mar 30, 2023
454a7f9
Fixed code standards
CrochetFeve0251 Mar 30, 2023
60d5f57
Fixed Fatal and delete option
CrochetFeve0251 Mar 30, 2023
978a0c7
Added fixes required by QA
CrochetFeve0251 Apr 17, 2023
cf7c437
Fixed issues with the return type from `dirlist`
CrochetFeve0251 Apr 17, 2023
0f88da3
Fixed clearing issue with the old domain
CrochetFeve0251 Apr 17, 2023
1a94506
Merge branch 'develop' into enhancement/5667-detect-domain-change
vmanthos Apr 20, 2023
a4e1808
Added logic to clear cache depending on option change when domain change
CrochetFeve0251 Apr 25, 2023
d978efa
Fixed code standards
CrochetFeve0251 Apr 25, 2023
430f414
Added tests
CrochetFeve0251 Apr 25, 2023
b0f6eda
Fixed code standards
CrochetFeve0251 Apr 27, 2023
847a79e
Fixed unit tests
CrochetFeve0251 May 2, 2023
78d3146
Merge branch 'develop' into enhancement/5667-detect-domain-change
vmanthos May 10, 2023
281b6f4
Merge branch 'develop' into enhancement/5667-detect-domain-change
CrochetFeve0251 Jun 14, 2023
52d8972
Added base for the notice
CrochetFeve0251 Jun 15, 2023
fc94697
Added logic to clean
CrochetFeve0251 Jun 15, 2023
a2cc98c
Added fix for the new logic
CrochetFeve0251 Jun 16, 2023
770df89
Fixed error within the logic
CrochetFeve0251 Jun 16, 2023
98f2fcc
Fixed tests and code standards
CrochetFeve0251 Jun 16, 2023
de14e78
Fixed code standards
CrochetFeve0251 Jun 16, 2023
687b1d9
Added test for AjaxHandler and fixed old tests
CrochetFeve0251 Jun 16, 2023
7fde2fb
Added new logic
CrochetFeve0251 Jun 16, 2023
191be08
Fixed code standards
CrochetFeve0251 Jun 16, 2023
9ae896f
Removed the notice
CrochetFeve0251 Jun 29, 2023
67e9b00
Merge branch 'develop' into enhancement/5667-detect-domain-change
vmanthos Jun 30, 2023
a4d5466
Fixed typo
CrochetFeve0251 Jun 30, 2023
827a25d
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Jun 30, 2023
09c7681
Fixed feedback QA
CrochetFeve0251 Jul 13, 2023
0b923a5
Merge branch 'develop' into enhancement/5667-detect-domain-change
Tabrisrp Jul 13, 2023
5969b3e
Fixed CR
CrochetFeve0251 Jul 17, 2023
8cc238e
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Jul 17, 2023
aab3fa2
Fixed code standards
CrochetFeve0251 Jul 17, 2023
0a3f00e
Merge branch 'develop' into enhancement/5667-detect-domain-change
CrochetFeve0251 Jul 27, 2023
3ad9594
Fixed CR
CrochetFeve0251 Jul 27, 2023
3c77669
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Jul 27, 2023
258e439
Fixed code standards
CrochetFeve0251 Jul 27, 2023
8927ef5
Added back hook removed by error
CrochetFeve0251 Jul 28, 2023
d96e6d2
Merge branch 'develop' into enhancement/5667-detect-domain-change
vmanthos Jul 31, 2023
fc20803
Added a fix to empty current cache and generate configs
CrochetFeve0251 Aug 1, 2023
c3a69bb
Added the infos for the beacon
CrochetFeve0251 Aug 2, 2023
ec37ad3
Fixed beacon links
CrochetFeve0251 Aug 2, 2023
f94f3dc
Merge branch 'develop' into enhancement/5667-detect-domain-change
CrochetFeve0251 Aug 3, 2023
b9777a7
Added back version exclusion
CrochetFeve0251 Aug 3, 2023
429d223
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Aug 3, 2023
7c67bb3
Excluded Notice from multisite
CrochetFeve0251 Aug 3, 2023
5e23f02
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Aug 3, 2023
704e30f
Added back the action
CrochetFeve0251 Aug 4, 2023
734fc77
Removed unused code
CrochetFeve0251 Aug 4, 2023
2fe373d
Merge remote-tracking branch 'origin/enhancement/5667-detect-domain-c…
CrochetFeve0251 Aug 4, 2023
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
10 changes: 10 additions & 0 deletions inc/Engine/Admin/Beacon/Beacon.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@ public function get_suggest( $doc_id ) {
'url' => 'https://fr.docs.wp-rocket.me/article/1327-problemes-critical-css-fouc#critical-path-css-de-secours',
],
],
'domain_change' => [
'en' => [
'id' => '577578b1903360258a10d8ba',
'url' => 'https://docs.wp-rocket.me/article/705-changing-domains-migrating-sites-with-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
'fr' => [
'id' => '57868414c697912dee72a98a',
'url' => 'https://fr.docs.wp-rocket.me/article/837-changer-de-domaine-migrer-un-site-avec-wp-rocket?utm_source=wp_plugin&utm_medium=wp_rocket',
],
],
'rucss_firewall_ips' => [
'en' => [
'id' => '6076083ff8c0ef2d98df1f97',
Expand Down
35 changes: 35 additions & 0 deletions inc/Engine/Admin/DomainChange/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace WP_Rocket\Engine\Admin\DomainChange;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;

class ServiceProvider extends AbstractServiceProvider {

/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'domain_change_subscriber',
'ajax_handler',
];

/**
* Registers items with the container
*
* @return void
*/
public function register() {
$this->getContainer()->add( 'ajax_handler', AjaxHandler::class );
$this->getContainer()->add( 'domain_change_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'ajax_handler' ) )
->addArgument( $this->getContainer()->get( 'beacon' ) );
}
}
225 changes: 225 additions & 0 deletions inc/Engine/Admin/DomainChange/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php

namespace WP_Rocket\Engine\Admin\DomainChange;

use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Engine\Common\Ajax\AjaxHandler;
use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {

/**
* Handle basic ajax operations.
*
* @var AjaxHandler
*/
protected $ajax_handler;

/**
* Beacon instance
*
* @var Beacon
*/
protected $beacon;

/**
* Name of the option saving the last base URL.
*
* @string
*/
const LAST_BASE_URL_OPTION = 'wp_rocket_last_base_url';
const LAST_OPTION_HASH = 'wp_rocket_last_option_hash';

/**
* Instantiate the class.
*
* @param AjaxHandler $ajax_handler Handle basic ajax operations.
* @param Beacon $beacon Beacon instance.
*/
public function __construct( AjaxHandler $ajax_handler, Beacon $beacon ) {
$this->ajax_handler = $ajax_handler;
$this->beacon = $beacon;
}

/**
* Return an array of events that this subscriber wants to listen to.
*
* @return string[]
*/
public static function get_subscribed_events() {
return [
'admin_init' => 'maybe_launch_domain_changed',
'admin_notices' => 'maybe_display_domain_change_notice',
'rocket_domain_changed' => 'maybe_clean_cache_domain_change',
'update_option_' . rocket_get_constant( 'WP_ROCKET_SLUG' ) => [ 'save_hash_on_update_options', 10, 2 ],
'rocket_notice_args' => 'add_regenerate_configuration_action',
'admin_post_rocket_regenerate_configuration' => 'regenerate_configuration',
];
}

/**
* Maybe launch the domain changed event.
*
* @return void
*/
public function maybe_launch_domain_changed() {
$base_url = trailingslashit( home_url() );
$base_url_encoded = base64_encode( $base_url ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

if ( ! get_option( self::LAST_BASE_URL_OPTION ) ) {
update_option( self::LAST_BASE_URL_OPTION, $base_url_encoded );
return;
}

$last_base_url_encoded = get_option( self::LAST_BASE_URL_OPTION );

if ( $base_url_encoded === $last_base_url_encoded ) {
return;
}

update_option( self::LAST_BASE_URL_OPTION, $base_url_encoded );

$last_base_url = base64_decode( $last_base_url_encoded ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode

set_transient( 'rocket_domain_changed', $last_base_url_encoded, 2 * rocket_get_constant( 'WEEK_IN_SECONDS', 604800 ) );

/**
* Fires when the domain of the website has been changed.
*
* @param string $current_url current URL from the website.
* @param string $old_url old URL from the website.
*/
do_action( 'rocket_detected_domain_changed', $base_url, $last_base_url );
}

/**
* Save the hash when options are saved.
*
* @param array $oldvalue old options.
* @param array $value new options.
* @return array|void
*/
public function save_hash_on_update_options( $oldvalue, $value ) {

Check warning on line 102 in inc/Engine/Admin/DomainChange/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Admin/DomainChange/Subscriber.php#L102

Avoid unused parameters such as '$oldvalue'.
if ( ! is_array( $value ) ) {
return;
}

$hash = rocket_create_options_hash( $value );

update_option( self::LAST_OPTION_HASH, $hash );
}

/**
* Maybe clean cache on domain change.
*
* @return void
*/
public function maybe_clean_cache_domain_change() {

$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );

if ( ! $options ) {
return;
}

/**
* Fires after WP Rocket options that require a cache purge have changed
*
* @param array $value An array of submitted values for the settings.
*/
do_action( 'rocket_options_changed', $options );
}

/**
* Maybe display a notice when domain change.
*
* @return void
*/
public function maybe_display_domain_change_notice() {

if ( ! current_user_can( 'rocket_manage_options' ) ) {
return;
}

$notice = get_transient( 'rocket_domain_changed' );

if ( ! $notice || is_multisite() ) {
return;
}

$beacon = $this->beacon->get_suggest( 'domain_change' );

$args = [
'status' => 'warning',
'dismissible' => '',
'dismiss_button' => false,
'message' => sprintf(
// translators: %1$s = <strong>, %2$s = </strong>, %3$s = <a>, %4$s = </a>.
__( '%1$sWP Rocket:%2$s We detected that the website domain has changed. The configuration files must be regenerated for the page cache and all other optimizations to work as intended. %3$sLearn More%4$s', 'rocket' ),
'<strong>',
'</strong>',
'<a href="' . esc_url( $beacon['url'] ) . '" data-beacon-article="' . esc_attr( $beacon['id'] ) . '" target="_blank" rel="noopener noreferrer">',
'</a>'
),
'action' => 'regenerate_configuration',
];

rocket_notice_html( $args );
}

/**
* Add mapping on notice.
*
* @param array $args Arguments from the notice.
*
* @return array
*/
public function add_regenerate_configuration_action( $args ) {
if ( ! key_exists( 'action', $args ) || 'regenerate_configuration' !== $args['action'] ) {
return $args;
}

$params = [
'action' => 'rocket_regenerate_configuration',
];

$args['action'] = '<a class="wp-core-ui button" href="' . add_query_arg( $params, wp_nonce_url( admin_url( 'admin-post.php' ), 'rocket_regenerate_configuration' ) ) . '">' . __( 'Regenerate WP Rocket configuration files now', 'rocket' ) . '</a>';

return $args;
}

/**
* Regenerate configurations.
*
* @return void
*/
public function regenerate_configuration() {
if ( ! $this->ajax_handler->validate_referer(
'rocket_regenerate_configuration',
'rocket_manage_options'
) ) {
return;
}

$last_base_url_encoded = get_transient( 'rocket_domain_changed' );

if ( ! $last_base_url_encoded ) {
return;
}

$last_base_url = base64_decode( $last_base_url_encoded ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
CrochetFeve0251 marked this conversation as resolved.
Show resolved Hide resolved
$base_url = trailingslashit( home_url() );

/**
* Fires when the domain of the website has been changed and user clicked on notice.
*
* @param string $current_url current URL from the website.
* @param string $old_url old URL from the website.
*/
do_action( 'rocket_domain_changed', $base_url, $last_base_url );

delete_transient( 'rocket_domain_changed' );

$this->ajax_handler->redirect();
}
}
Loading
Loading