PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | includes/class-wp-optimize-preloader.php +78 -35 3.5.0 → 4.7.0 View file →
@@ -8,8 +8,15 @@
8 8
9 9 protected $options;
10 10
11 11 /**
12 + * If set to true, the preloader will not close the browser connection when running via AJAX.
13 + *
14 + * @var bool
15 + */
16 + protected $dont_close_browser_connection = false;
17 +
18 + /**
12 19 * WP_Optimize_Page_Cache_Preloader constructor.
13 20 */
14 21 public function __construct() {
15 22 parent::__construct();
@@ -15,9 +22,13 @@
15 22 parent::__construct();
16 23
17 24 $this->options = WP_Optimize()->get_options();
18 25 // setup loggers
19 - $this->set_loggers(WP_Optimize()->wpo_loggers());
26 + if (did_action('init')) {
27 + $this->setup_loggers();
28 + } else {
29 + add_action('init', array($this, 'setup_loggers'));
30 + }
20 31
21 32 add_action('wpo_' . $this->preload_type . '_preload_continue', array($this, 'process_tasks_queue'));
22 33 add_filter('updraft_interrupt_tasks_queue_'.$this->task_type, array($this, 'maybe_interrupt_queue'), 20);
23 34 }
@@ -22,8 +33,17 @@
22 33 add_filter('updraft_interrupt_tasks_queue_'.$this->task_type, array($this, 'maybe_interrupt_queue'), 20);
23 34 }
24 35
25 36 /**
37 + * Setup loggers
38 + *
39 + * @return void
40 + */
41 + public function setup_loggers() {
42 + $this->set_loggers(WP_Optimize()->wpo_loggers());
43 + }
44 +
45 + /**
26 46 * Get a schedule interval
27 47 *
28 48 * @param string $schedule_key The schedule to check
29 49 * @return integer
@@ -33,9 +53,9 @@
33 53 if (!isset($schedules[$schedule_key])) {
34 54 $this->log('Could not get interval for event of type '.$schedule_key);
35 55 return 0;
36 56 }
37 - return isset($schedules[$schedule_key]['interval']) ? $schedules[$schedule_key]['interval'] : 0;
57 + return (int) $schedules[$schedule_key]['interval'] ?? 0;
38 58 }
39 59
40 60 /**
41 61 * Get the interval to continuing a preload task
@@ -45,9 +65,10 @@
45 65 public function get_continue_preload_cron_interval() {
46 66 /**
47 67 * Filters the interval between each preload attempt, in seconds.
48 68 */
49 - return (int) apply_filters('wpo_' . $this->preload_type . '_preload_continue_interval', 600);
69 + $filtered_interval = apply_filters('wpo_' . $this->preload_type . '_preload_continue_interval', 600);
70 + return is_int($filtered_interval) ? $filtered_interval : 600;
50 71 }
51 72
52 73 /**
53 74 * Schedule action for continuously preload.
@@ -56,9 +77,9 @@
56 77 $continue_in = wp_next_scheduled('wpo_' . $this->preload_type .'_preload_continue');
57 78
58 79 // Action is still scheduled
59 80 if ($continue_in && $continue_in > 0) return;
60 - // Action is overdue, delete it and re schedule it
81 + // Action is overdue, delete it and re-schedule it
61 82 if ($continue_in && $continue_in < 0) $this->delete_preload_continue_action();
62 83
63 84 wp_schedule_event(time() + $this->get_schedule_interval('wpo_' . $this->preload_type . '_preload_continue_interval'), 'wpo_' . $this->preload_type . '_preload_continue_interval', 'wpo_' . $this->preload_type . '_preload_continue');
64 85 }
@@ -73,9 +94,9 @@
73 94 /**
74 95 * Run preload. If task queue is empty it creates tasks for site urls.
75 96 *
76 97 * @param string $type - The preload type (schedule | manual)
77 - * @param array $response - Specific response for echo into output thread when browser connection closing.
98 + * @param ?array $response - Specific response for echo into output thread when browser connection closing.
78 99 * @param bool $silent - If DOING_AJAX, close the connection without sending any additional data (default: false)
79 100 * @return array|void - Void when closing the browser connection
80 101 */
81 102 public function run($type = 'scheduled', $response = null, $silent = false) {
@@ -103,24 +124,27 @@
103 124
104 125 $is_wp_cli = defined('WP_CLI') && WP_CLI;
105 126
106 127 // close browser connection and continue work for ajax actions.
107 - if (defined('DOING_AJAX') && DOING_AJAX) {
128 + if (defined('DOING_AJAX') && DOING_AJAX && !$this->dont_close_browser_connection) {
108 129 if (true === $silent) {
109 130 $output = '';
110 131 } else {
111 - $output = json_encode($response);
132 + $output = wp_json_encode($response);
112 133 }
113 134
114 135 WP_Optimize()->close_browser_connection($output);
115 136 }
116 137
138 + // Reset the flag to not close the browser connection for the next runs.
139 + $this->dont_close_browser_connection = false;
140 +
117 141 // trying to change time limit.
118 142 WP_Optimize()->change_time_limit();
119 143
120 144 $status = $this->get_status($this->task_type);
121 145
122 - if (0 == $status['all_tasks'] && $lock) {
146 + if (0 === (int) $status['all_tasks'] && $lock) {
123 147 if (is_multisite()) {
124 148 $sites = WP_Optimize()->get_sites();
125 149
126 150 foreach ($sites as $site) {
@@ -143,8 +167,24 @@
143 167 }
144 168 }
145 169
146 170 /**
171 + * Set the flag to not close the browser connection when running via AJAX.
172 + *
173 + * @return void
174 + */
175 + public function maybe_dont_close_browser_connection() {
176 +
177 + if ($this->dont_close_browser_connection) return;
178 +
179 + $dont_close_browser_connection = apply_filters('wpo_' . $this->preload_type . '_preloader_dont_close_browser_connection', $this->dont_close_browser_connection);
180 +
181 + if ($dont_close_browser_connection) {
182 + $this->dont_close_browser_connection = true;
183 + }
184 + }
185 +
186 + /**
147 187 * Process tasks queue.
148 188 */
149 189 public function process_tasks_queue() {
150 190 // schedule continue preload action.
@@ -175,9 +215,9 @@
175 215
176 216 if ($interrupt) return $interrupt;
177 217
178 218 static $memory_threshold = null;
179 - if (null == $memory_threshold) {
219 + if (null === $memory_threshold) {
180 220 /**
181 221 * Filters the minimum memory required before stopping a queue. Default: 10MB
182 222 */
183 223 $memory_threshold = apply_filters('wpo_' . $this->preload_type . '_preload_memory_threshold', 10485760);
@@ -214,9 +254,9 @@
214 254 *
215 255 * @return bool
216 256 */
217 257 public function is_cancelled() {
218 - return $this->options->get_option("last_{$this->preload_type}_preload_cancel", false);
258 + return (bool) $this->options->get_option("last_{$this->preload_type}_preload_cancel", false);
219 259 }
220 260
221 261 /**
222 262 * Check if preloading queue is processing.
@@ -229,9 +269,9 @@
229 269
230 270 /**
231 271 * Get current status of preloading urls.
232 272 *
233 - * @return array
273 + * @return mixed
234 274 */
235 275 public function get_status_info() {
236 276
237 277 $status = $this->get_status($this->task_type);
@@ -239,9 +279,9 @@
239 279
240 280 if ($this->is_semaphore_locked('wpo_' . $this->preload_type . '_preloader_creating_tasks') && !$this->is_cancelled()) {
241 281 // we are still creating tasks.
242 282 return $this->get_preloading_message($preload_data);
243 - } elseif ($status['complete_tasks'] == $status['all_tasks']) {
283 + } elseif ((int) $status['complete_tasks'] === (int) $status['all_tasks']) {
244 284 $gmt_offset = (int) (3600 * get_option('gmt_offset'));
245 285
246 286 $last_preload_time = $this->options->get_option('wpo_last_' . $this->preload_type . '_preload');
247 287
@@ -253,10 +293,11 @@
253 293 }
254 294 } else {
255 295 $preload_resuming_time = wp_next_scheduled('wpo_' . $this->preload_type . '_preload_continue');
256 296 $preload_resuming_in = $preload_resuming_time ? $preload_resuming_time - time() : 0;
297 + // translators: %1$s: number of preloaded urls, %2$s: total number of urls
257 298 $preloaded_message = sprintf(_n('%1$s out of %2$s URL preloaded', '%1$s out of %2$s URLs preloaded', $status['all_tasks'], 'wp-optimize'), $status['complete_tasks'], $status['all_tasks']);
258 - if ('sitemap' == $this->options->get_option('wpo_last_' . $this->preload_type . '_preload_type', '')) {
299 + if ('sitemap' === $this->options->get_option('wpo_last_' . $this->preload_type . '_preload_type', '')) {
259 300 $preloaded_message = __('Preloading posts found in sitemap:', 'wp-optimize') .' '. $preloaded_message;
260 301 }
261 302 $return = $this->get_preload_progress_message($preload_data, $preloaded_message, $preload_resuming_in);
262 303 if (defined('DOING_AJAX') && DOING_AJAX) {
@@ -261,9 +302,9 @@
261 302 $return = $this->get_preload_progress_message($preload_data, $preloaded_message, $preload_resuming_in);
262 303 if (defined('DOING_AJAX') && DOING_AJAX) {
263 304 // if no cron was found or cron is overdue more than 20s, trigger it
264 305 if (!$preload_resuming_time || $preload_resuming_in < -20) {
265 - $this->run($return);
306 + $this->run('scheduled', $return);
266 307 }
267 308 }
268 309 return $return;
269 310 }
@@ -276,9 +317,9 @@
276 317 */
277 318 public function is_running() {
278 319 $status = $this->get_status($this->task_type);
279 320
280 - if ($status['all_tasks'] > 0) return true;
321 + return $status['all_tasks'] > 0;
281 322 }
282 323
283 324 /**
284 325 * Preload desktop version from url.
@@ -289,9 +330,9 @@
289 330 */
290 331 public function preload_desktop($url) {
291 332 $desktop_args = array(
292 333 'httpversion' => '1.1',
293 - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
334 + 'user-agent' => WP_Optimize_Utils::get_user_agent(),
294 335 'timeout' => 10,
295 336 'headers' => apply_filters('wpo_preload_headers', array()),
296 337 );
297 338
@@ -313,9 +354,9 @@
313 354 if (!apply_filters('wpo_should_preload_amp', false, $url)) return;
314 355
315 356 $amp_args = array(
316 357 'httpversion' => '1.1',
317 - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
358 + 'user-agent' => WP_Optimize_Utils::get_user_agent(),
318 359 'timeout' => 10,
319 360 'headers' => apply_filters('wpo_preload_headers', array()),
320 361 );
321 362
@@ -333,8 +374,9 @@
333 374 *
334 375 * @return string
335 376 */
336 377 protected function get_sitemap_filename() {
378 + $sitemap_filename = 'sitemap.xml';
337 379 /**
338 380 * Filter the sitemap file used to collect the URLs to preload
339 381 *
340 382 * @param string $filename - The sitemap name
@@ -339,9 +381,10 @@
339 381 *
340 382 * @param string $filename - The sitemap name
341 383 * @default sitemap.xml
342 384 */
343 - return apply_filters('wpo_cache_preload_sitemap_filename', 'sitemap.xml');
385 + $filtered_sitemap_filename = apply_filters('wpo_cache_preload_sitemap_filename', $sitemap_filename);
386 + return is_string($filtered_sitemap_filename) ? $filtered_sitemap_filename : $sitemap_filename;
344 387 }
345 388
346 389 /**
347 390 * Check if sitemap exists then returns list of urls from sitemap file otherwise returns all posts urls.
@@ -358,8 +401,9 @@
358 401 $urls = $this->get_post_urls();
359 402 $this->options->update_option('wpo_last_' . $this->preload_type . '_preload_type', 'posts');
360 403 }
361 404
405 + // translators: %d: number of urls
362 406 $this->log(sprintf(_n('%d url found.', '%d urls found.', count($urls), 'wp-optimize'), count($urls)));
363 407
364 408 /**
365 409 * Filter the URLs which will be preloaded
@@ -366,9 +410,10 @@
366 410 *
367 411 * @param array $urls
368 412 * @return array
369 413 */
370 - return apply_filters('wpo_preload_get_site_urls', $urls);
414 + $filtered_url = apply_filters('wpo_preload_get_site_urls', $urls);
415 + return is_array($filtered_url) ? $filtered_url : array();
371 416 }
372 417
373 418 /**
374 419 * Loads sitemap file and returns list of urls.
@@ -374,9 +419,9 @@
374 419 * Loads sitemap file and returns list of urls.
375 420 *
376 421 * @param string $sitemap_url
377 422 *
378 - * @return array|bool
423 + * @return mixed
379 424 */
380 425 public function get_sitemap_urls($sitemap_url = '') {
381 426
382 427 $urls = array();
@@ -396,9 +441,9 @@
396 441 if (is_wp_error($response)) {
397 442 $response = file_get_contents($sitemap_url);
398 443
399 444 // if response is empty then try load from file.
400 - if (empty($response) && '' == $sitemap_url) {
445 + if (empty($response) && '' === $sitemap_url) {
401 446 $sitemap_file = $this->get_local_sitemap_file();
402 447
403 448 $response = file_get_contents($sitemap_file);
404 449 }
@@ -411,9 +456,9 @@
411 456 $xml = @simplexml_load_string(wp_remote_retrieve_body($response)); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- suppress warnings when error found in XML data
412 457 }
413 458
414 459 // xml file has not valid xml content then return false.
415 - if (false === $xml) return false;
460 + if (false === $xml) return array();
416 461
417 462 // if exists urls then return them.
418 463 if (isset($xml->url)) {
419 464 foreach ($xml->url as $element) {
@@ -420,17 +465,14 @@
420 465 if (!isset($element->loc)) continue;
421 466 $urls[] = (string) $element->loc;
422 467 }
423 468 } elseif (isset($xml->sitemap)) {
424 - // if has links to other sitemap files then get urls from them.
469 + // if it has links to other sitemap files then get urls from them.
425 470 foreach ($xml->sitemap as $element) {
426 471 if (!isset($element->loc)) continue;
427 472
428 473 $sitemap_urls = $this->get_sitemap_urls($element->loc);
429 -
430 - if (is_array($sitemap_urls)) {
431 - $urls = array_merge($urls, $sitemap_urls);
432 - }
474 + $urls = array_merge($urls, $sitemap_urls);
433 475 }
434 476 }
435 477
436 478 return $urls;
@@ -483,9 +525,9 @@
483 525 $urls[] = $permalink;
484 526
485 527 // check page separators in the post content
486 528 preg_match_all('/\<\!--nextpage--\>/', $post->post_content, $matches);
487 - // if there any separators add urls for each page
529 + // if there are any separators add urls for each page
488 530 if (count($matches[0])) {
489 531 $prefix = strpos($permalink, '?') ? '&page=' : '';
490 532 for ($page = 0; $page < count($matches[0]); $page++) {
491 533 if ('' != $prefix) {
@@ -527,9 +569,9 @@
527 569 * @return bool
528 570 */
529 571 public function is_domain_mapping_enabled() {
530 572 // SUNRISE constant is defined with installation WordPress MU Domain Mapping plugin.
531 - $enabled = is_multisite() && defined('SUNRISE') && 'on' == strtolower(SUNRISE);
573 + $enabled = is_multisite() && defined('SUNRISE') && 'on' === strtolower(SUNRISE);
532 574
533 575 /**
534 576 * Filters if Multisite Domain mapping is enabled.
535 577 * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use.
@@ -534,9 +576,9 @@
534 576 * Filters if Multisite Domain mapping is enabled.
535 577 * Currently, we can only detect if the WordPress MU Domain Mapping plugin is in use.
536 578 * Using the WP Core functionality should not require this, unless if the domain name is set somewhere else but in the site url option.
537 579 */
538 - return apply_filters('wpo_is_domain_mapping_enabled', $enabled);
580 + return (bool) apply_filters('wpo_is_domain_mapping_enabled', $enabled);
539 581 }
540 582
541 583 /**
542 584 * Return mapped domain by $blog_id.
@@ -550,11 +592,11 @@
550 592
551 593 $domain = '';
552 594 $multisite_plugin_table_name = $wpdb->base_prefix.'domain_mapping';
553 595 // Check if table exists
554 - if ($wpdb->get_var("SHOW TABLES LIKE '$multisite_plugin_table_name'") != $multisite_plugin_table_name) {
596 + if ($wpdb->get_var("SHOW TABLES LIKE '" . esc_sql($multisite_plugin_table_name) . "'") != $multisite_plugin_table_name) {
555 597 // This table created in WordPress MU Domain Mapping plugin.
556 - $row = $wpdb->get_row("SELECT `domain` FROM {$multisite_plugin_table_name} WHERE `blog_id` = {$blog_id} AND `active` = 1", ARRAY_A);
598 + $row = $wpdb->get_row($wpdb->prepare("SELECT `domain` FROM " . esc_sql($multisite_plugin_table_name) . " WHERE `blog_id` = %d AND `active` = %d", $blog_id, 1), ARRAY_A);
557 599 if (!empty($row)) {
558 600 $domain = $row['domain'];
559 601 }
560 602 } else {
@@ -567,16 +609,17 @@
567 609 *
568 610 * @param string $domain The domain name
569 611 * @param integer $blog_id The blog ID
570 612 */
571 - return apply_filters('wpo_get_mapped_domain', $domain, $blog_id);
613 + $filtered_domain = apply_filters('wpo_get_mapped_domain', $domain, $blog_id);
614 + return is_string($filtered_domain) ? $filtered_domain : $domain;
572 615 }
573 616
574 617 /**
575 618 * Captures and logs any interesting messages
576 619 *
577 - * @param String $message - the error message
578 - * @param String $error_type - the error type
620 + * @param string $message - the error message
621 + * @param string $error_type - the error type
579 622 */
580 623 public function log($message, $error_type = 'info') {
581 624
582 625 if (isset($this->loggers)) {