Skip to content

Commit

Permalink
Automatic mobile preload after cache purge (#2350)
Browse files Browse the repository at this point in the history
After purging the cache, the homepage is automatically preloaded. Now it is also preloaded for mobile if the separate mobile cache is enabled.
Closes #2349.
  • Loading branch information
Screenfeed committed Feb 26, 2020
1 parent 5f112e1 commit 8cf329c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
12 changes: 12 additions & 0 deletions inc/classes/preload/class-abstract-preload.php
Expand Up @@ -75,6 +75,18 @@ public function is_process_running() {
return $this->preload_process->is_process_running();
}

/**
* Tell if mobile preload is enabled.
*
* @since 3.5
* @author Grégory Viguier
*
* @return bool
*/
public function is_mobile_preload_enabled() {
return $this->preload_process->is_mobile_preload_enabled();
}

/**
* Get the number of preloaded URLs.
*
Expand Down
35 changes: 31 additions & 4 deletions inc/classes/subscriber/preload/class-preload-subscriber.php
Expand Up @@ -57,17 +57,20 @@ public function __construct( Homepage $homepage_preloader, Options_Data $options
*/
public static function get_subscribed_events() {
return [
'admin_notices' => [
'admin_notices' => [
[ 'notice_preload_triggered' ],
[ 'notice_preload_running' ],
[ 'notice_preload_complete' ],
],
'admin_post_rocket_stop_preload' => [ 'do_admin_post_stop_preload' ],
'pagely_cache_purge_after' => [ 'run_preload', 11 ],
'update_option_' . WP_ROCKET_SLUG => [
'admin_post_rocket_stop_preload' => [ 'do_admin_post_stop_preload' ],
'pagely_cache_purge_after' => [ 'run_preload', 11 ],
'update_option_' . WP_ROCKET_SLUG => [
[ 'maybe_launch_preload', 11, 2 ],
[ 'maybe_cancel_preload', 10, 2 ],
],
'rocket_after_preload_after_purge_cache' => [
[ 'maybe_preload_mobile_homepage', 10, 3 ],
],
];
}

Expand Down Expand Up @@ -184,6 +187,30 @@ public function maybe_launch_preload( $old_value, $value ) {
}
}

/**
* After automatically preloading the homepage (after purging the cache), also preload the homepage for mobile.
*
* @since 3.5
* @author Grégory Viguier
*
* @param string $home_url URL to the homepage being preloaded.
* @param string $lang The lang of the homepage.
* @param array $args Arguments used for the preload request.
*/
public function maybe_preload_mobile_homepage( $home_url, $lang, $args ) {
if ( ! $this->homepage_preloader->is_mobile_preload_enabled() ) {
return;
}

if ( empty( $args['user-agent'] ) ) {
$args['user-agent'] = 'WP Rocket/Homepage_Preload_After_Purge_Cache';
}

$args['user-agent'] .= ' iPhone';

wp_safe_remote_get( $home_url, $args );
}

/**
* This notice is displayed when the preload is triggered from a different page than WP Rocket settings page
*
Expand Down
22 changes: 17 additions & 5 deletions inc/common/purge.php
Expand Up @@ -462,6 +462,8 @@ function do_admin_post_rocket_purge_cache() { // phpcs:ignore WordPress.NamingCo
}

if ( get_rocket_option( 'manual_preload' ) && ( ! defined( 'WP_ROCKET_DEBUG' ) || ! WP_ROCKET_DEBUG ) ) {
$home_url = get_rocket_i18n_home_url( $lang );

/**
* Filters the arguments for the preload request being triggered after clearing the cache.
*
Expand All @@ -470,7 +472,7 @@ function do_admin_post_rocket_purge_cache() { // phpcs:ignore WordPress.NamingCo
*
* @param array $args Request arguments.
*/
$args = apply_filters(
$args = (array) apply_filters(
'rocket_preload_after_purge_cache_request_args',
[
'blocking' => false,
Expand All @@ -479,10 +481,20 @@ function do_admin_post_rocket_purge_cache() { // phpcs:ignore WordPress.NamingCo
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
]
);
wp_safe_remote_get(
home_url( $lang ),
$args
);

wp_safe_remote_get( $home_url, $args );

/**
* Fires after automatically preloading the homepage, which occurs after purging the cache.
*
* @since 3.5
* @author Grégory Viguier
*
* @param string $home_url URL to the homepage being preloaded.
* @param string $lang The lang of the homepage.
* @param array $args Arguments used for the preload request.
*/
do_action( 'rocket_after_preload_after_purge_cache', $home_url, $lang, $args );
}

rocket_dismiss_box( 'rocket_warning_plugin_modification' );
Expand Down

0 comments on commit 8cf329c

Please sign in to comment.