PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.6.3
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.6.3
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
← All changes | includes/classes/Preloader.php +137 -16 2.23.6.3 View file →
@@ -9,8 +9,9 @@
9 9
10 10 use PoweredCache\Async\CachePreloader;
11 11 use function PoweredCache\Utils\permalink_structure_has_trailingslash;
12 12 use function PoweredCache\Utils\site_cache_dir;
13 +use const PoweredCache\Constants\DEFERRED_PRELOAD_QUEUE_CRON_NAME;
13 14
14 15 /**
15 16 * Class Preloader
16 17 */
@@ -29,8 +30,15 @@
29 30 */
30 31 private $cache_preloader;
31 32
32 33 /**
34 + * Tracks whether we’ve added any URLs this request
35 + *
36 + * @var $queue_dirty
37 + */
38 + private $queue_dirty = false;
39 +
40 + /**
33 41 * Return an instance of the current class
34 42 *
35 43 * @return Preloader
36 44 * @since 1.0
@@ -59,9 +67,9 @@
59 67 if ( ! $this->settings['enable_cache_preload'] ) {
60 68 return;
61 69 }
62 70
63 - $this->cache_preloader = CachePreloader::factory();
71 + $this->get_preloader();
64 72
65 73 /**
66 74 * Page cache needs to be activated to get benefits of preloading
67 75 */
@@ -72,10 +80,12 @@
72 80 add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ] );
73 81 add_action( 'admin_post_powered_cache_preload_cache', [ $this, 'start_preload' ] );
74 82 add_action( 'powered_cache_purge_all_cache', [ $this, 'setup_preload_queue' ] );
75 83 add_action( 'powered_cache_clean_site_cache_dir', [ $this, 'setup_preload_queue' ] );
76 - add_action( 'powered_cache_advanced_cache_purge_post', [ $this, 'add_purged_urls_to_preload_queue' ], 10, 2 );
84 + add_action( 'powered_cache_advanced_cache_purge_post', [ $this, 'deferred_preload_queue' ], 10, 2 );
77 85 add_action( 'powered_cache_expired_files_deleted', [ $this, 'add_expired_urls_to_preload_queue' ], 10, 2 );
86 + add_action( DEFERRED_PRELOAD_QUEUE_CRON_NAME, [ $this, 'add_purged_urls_to_preload_queue' ], 10, 2 );
87 + add_action( 'shutdown', [ $this, 'dispatch_preload_queue' ], 0 );
78 88 }
79 89
80 90 /**
81 91 * Preload Admin bar menu
@@ -102,13 +112,27 @@
102 112 )
103 113 );
104 114 }
105 115
116 +
106 117 /**
118 + * Get the instance of CachePreloader
119 + *
120 + * @return CachePreloader
121 + */
122 + private function get_preloader() {
123 + if ( ! $this->cache_preloader ) {
124 + $this->cache_preloader = CachePreloader::factory();
125 + }
126 +
127 + return $this->cache_preloader;
128 + }
129 +
130 + /**
107 131 * Add preloading items to queue
108 132 */
109 133 public function start_preload() {
110 - if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_preload_cache' ) ) {
134 + if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_preload_cache' ) ) { // phpcs:ignore
111 135 wp_nonce_ays( '' );
112 136 }
113 137
114 138 if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) {
@@ -122,8 +146,10 @@
122 146 wp_safe_redirect( esc_url_raw( $redirect_url ) );
123 147 exit;
124 148 }
125 149
150 + \PoweredCache\Utils\log( sprintf( 'Preload triggered from admin bar.' ) );
151 +
126 152 $this->setup_preload_queue();
127 153
128 154 $redirect_url = add_query_arg( 'pc_action', 'start_preload', wp_get_referer() );
129 155 wp_safe_redirect( esc_url_raw( $redirect_url ) );
@@ -134,9 +160,9 @@
134 160 * Setup preload
135 161 */
136 162 public function setup_preload_queue() {
137 163 // cancel existing process before populating
138 - $this->cache_preloader->cancel_process();
164 + $this->get_preloader()->cancel_process();
139 165
140 166 if ( POWERED_CACHE_IS_NETWORK ) {
141 167 $sites = get_sites( [ 'fields' => 'ids' ] );
142 168 foreach ( $sites as $site_id ) {
@@ -148,10 +174,8 @@
148 174 } else {
149 175 $this->populate_preload_queue();
150 176 }
151 177
152 - $this->cache_preloader->save()->dispatch();
153 -
154 178 if ( $this->settings['enable_sitemap_preload'] && function_exists( '\PoweredCachePremium\Utils\preload_sitemap' ) ) {
155 179 \PoweredCachePremium\Utils\preload_sitemap();
156 180 }
157 181 }
@@ -197,14 +221,46 @@
197 221 $preload_urls = array_merge( $preload_urls, $public_tax_term_urls );
198 222 \PoweredCache\Utils\log( sprintf( 'Public tax terms added to preload queue. ' ) );
199 223 }
200 224
225 + /**
226 + * Filters preload urls before sending to queue
227 + *
228 + * @hook populate_preload_queue_urls
229 + *
230 + * @param {array} $preload_urls The list of preload urls
231 + *
232 + * @return {array} New value.
233 + * @since 2.4
234 + */
235 + $preload_urls = apply_filters( 'populate_preload_queue_urls', $preload_urls );
236 +
201 237 foreach ( $preload_urls as $url ) {
202 - $this->cache_preloader->push_to_queue( $url );
238 + $this->add_url_to_preload_queue( $url );
203 239 }
204 240 }
205 241
242 + /**
243 + * Add URLs to preload queue with a delay
244 + *
245 + * @param int $post_id Post ID
246 + * @param array $urls The URL list of the related pages that will be preloaded
247 + *
248 + * @since 3.6
249 + */
250 + public function deferred_preload_queue( $post_id, $urls ) {
251 + \PoweredCache\Utils\log( sprintf( 'Post ID %d purged from cache, adding related URLs to preload queue with a delay.', $post_id ) );
252 + wp_schedule_single_event(
253 + time() + 10,
254 + DEFERRED_PRELOAD_QUEUE_CRON_NAME,
255 + [
256 + 'post_id' => $post_id,
257 + 'urls' => $urls,
258 + ]
259 + );
260 + }
206 261
262 +
207 263 /**
208 264 * Add related pages to preload queue when the cache got cleared
209 265 *
210 266 * @param int $post_id Post ID
@@ -212,18 +268,23 @@
212 268 *
213 269 * @since 2.0
214 270 */
215 271 public function add_purged_urls_to_preload_queue( $post_id, $urls ) {
272 + $post_url = get_permalink( $post_id );
273 +
274 + // include post itself all the time
275 + if ( ! empty( $post_url ) ) {
276 + $this->add_url_to_preload_queue( $post_url );
277 + }
278 +
216 279 if ( ! $urls ) {
217 280 return;
218 281 }
219 282
220 283 foreach ( $urls as $url ) {
221 - $this->cache_preloader->push_to_queue( $url );
222 - \PoweredCache\Utils\log( sprintf( 'Pushed to preload queue [add_purged_urls_to_preload_queue]: %s', $url ) );
284 + $this->add_url_to_preload_queue( $url );
223 285 }
224 286
225 - $this->cache_preloader->save()->dispatch();
226 287 }
227 288
228 289 /**
229 290 * Requeue deleted URLs
@@ -267,13 +328,11 @@
267 328 foreach ( $expired_urls as $url ) {
268 329 if ( $has_trailingslash ) {
269 330 $url = trailingslashit( $url );
270 331 }
271 - $this->cache_preloader->push_to_queue( $url );
272 - \PoweredCache\Utils\log( sprintf( 'Pushed to preload queue [add_expired_urls_to_preload_queue]: %s', $url ) );
332 + $this->add_url_to_preload_queue( $url );
273 333 }
274 334
275 - $this->cache_preloader->save()->dispatch();
276 335 }
277 336
278 337 /**
279 338 * Prep. public post urls for preload queue
@@ -584,9 +643,9 @@
584 643 * @since 2.0
585 644 */
586 645 do_action( 'powered_cache_preload_http_request', $url, $args );
587 646
588 - \PoweredCache\Utils\log( sprintf( 'Preloading...%s: %s', $url, print_r( $args, true ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
647 + \PoweredCache\Utils\log( sprintf( 'Processing..: %s', $url ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
589 648
590 649 return wp_remote_get( esc_url_raw( $url ), $args );
591 650 }
592 651
@@ -600,14 +659,29 @@
600 659 * Filters wait time between preload requests.
601 660 *
602 661 * @hook powered_cache_preload_request_interval
603 662 *
604 - * @param {int} $delay Delay time in milliseconds.
663 + * @param {int} $delay Delay time in microseconds.
605 664 *
606 665 * @return {int} New value.
607 666 * @since 2.0
608 667 */
609 - usleep( absint( apply_filters( 'powered_cache_preload_request_interval', $delay ) ) );
668 + $delay = absint( apply_filters( 'powered_cache_preload_request_interval', $delay ) );
669 +
670 + // Convert the delay to seconds and microseconds
671 + $seconds = intdiv( $delay, 1000000 ); // Get full seconds
672 + $microseconds = $delay % 1000000; // Get remaining microseconds
673 +
674 + // Use sleep() for full second delays
675 + if ( $seconds > 0 ) {
676 + sleep( $seconds );
677 + }
678 +
679 + // Use usleep() for remaining microseconds
680 + // sleeping more than 1 seconds may not be supported by the operating system with usleep
681 + if ( $microseconds > 0 ) {
682 + usleep( $microseconds );
683 + }
610 684 }
611 685
612 686
613 687 /**
@@ -626,7 +700,54 @@
626 700 * @return {string} New value.
627 701 * @since 2.0
628 702 */
629 703 return apply_filters( 'powered_cache_preload_mobile_user_agent', 'Powered Cache Preloader mobile iPhone' );
704 + }
705 +
706 + /**
707 + * Add a URL to the preload queue with optional filtering.
708 + *
709 + * @param string $url The URL to add to the preload queue.
710 + *
711 + * @since 3.4
712 + */
713 + protected function add_url_to_preload_queue( $url ) {
714 + /**
715 + * Filters whether a URL should be added to the preload queue.
716 + *
717 + * @hook powered_cache_preload_add_url_to_queue
718 + *
719 + * @param {boolean} $preload Whether to preload the URL. Default true.
720 + * @param {string} $url The URL to be preloaded.
721 + *
722 + * @return {boolean} Whether to preload the URL.
723 + * @since 3.4
724 + */
725 + $do_preload = apply_filters( 'powered_cache_preload_add_url_to_queue', true, $url );
726 +
727 + if ( $do_preload ) {
728 + $this->get_preloader()->push_to_queue( $url );
729 + $this->queue_dirty = true;
730 + \PoweredCache\Utils\log( sprintf( 'URL added to preload queue : %s', $url ) );
731 + } else {
732 + \PoweredCache\Utils\log( sprintf( 'URL skipped from preload queue: %s', $url ) );
733 + }
734 + }
735 +
736 + /**
737 + * Dispatch the preload queue if it has been modified.
738 + *
739 + * @return void
740 + * @since 3.6
741 + */
742 + public function dispatch_preload_queue() {
743 + if ( ! $this->queue_dirty ) {
744 + return;
745 + }
746 +
747 + // This will serialize the queue into the DB and fire off the async request
748 + $this->get_preloader()->save()->dispatch();
749 +
750 + \PoweredCache\Utils\log( 'Dispatched preload queue.' );
630 751 }
631 752
632 753 }