PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.18
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.18
4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 All 173 releases
searchpro / inc / cache / class-berqwarmup.php

class-berqwarmup.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.18, at inc/cache/class-berqwarmup.php

354 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH'))
4 exit;
5
6 use BerqWP\BerqWP;
7
8 if (!class_exists('berqWarmup')) {
9 class berqWarmup
10 {
11 public $cache_warmup_fired = false;
12
13 function __construct()
14 {
15
16 // Automatic cache warmup
17 // add_action('init', [$this, 'warmup_home']);
18 add_action('berqwp_flush_all_cache', [$this, 'warmup_home']);
19 add_action('berqwp_flush_all_cache', [$this, 'warmup_sitemap']);
20 add_action('warmup_cache_quickly', 'warmup_cache_by_url');
21 add_action('berqwp_cache_warmup', [$this, 'warmup_sitemap']);
22 // add_action('bwp_warmup_sitemap', [$this, 'warmup_sitemap']);
23 // add_action('berqwp_flush_all_cache', [$this, 'request_warmup_cache']);
24 // add_action('berqwp_cache_warmup', [$this, 'request_warmup_cache']);
25
26 // if (isset($_GET['bwp_preload'])) {
27 // add_action('wp', [$this, 'preload_buffer'], 999);
28 // }
29 }
30
31 function warmup_home()
32 {
33 $home_url = bwp_admin_home_url('/');
34
35 // if (false === as_has_scheduled_action('warmup_cache_quickly') && (bwp_is_home_cached() === false || bwp_is_partial_cache($home_url)) && function_exists('as_enqueue_async_action')) {
36 // as_enqueue_async_action('warmup_cache_quickly', [$home_url]);
37 // }
38
39 warmup_cache_by_url($home_url, true);
40 }
41
42 function warmup_sitemap($async = false)
43 {
44 if (!berqwp_can_use_cloud()) {
45 return;
46 }
47
48 if ($this->cache_warmup_fired) {
49 return;
50 }
51
52 // Debounce across separate requests (e.g. several plugin updates in a row)
53 if (get_transient('berqwp_warmup_running')) {
54 return;
55 }
56
57 set_transient('berqwp_warmup_running', true, 5 * MINUTE_IN_SECONDS);
58
59 $this->cache_warmup_fired = true;
60
61 global $berq_log;
62 $berq_log->info("Warming cache using sitemap.");
63
64 // Remove require cache warning
65 update_option('bwp_require_flush_cache', 0);
66
67 $post_data = [
68 'license_key' => berqwp_get_license_key(),
69 'site_url' => home_url(),
70 'cache_warmup' => true,
71 ];
72 $berqwp = new BerqWP(berqwp_get_license_key(), null, null);
73 $berqwp->request_cache_warmup($post_data, true);
74 }
75
76 function request_warmup_cache()
77 {
78
79 if (berq_is_localhost() && !defined('BERQWP_ALLOW_LOCALHOST')) {
80 return;
81 }
82
83 if (berq_is_localhost() && defined('BERQWP_ALLOW_LOCALHOST') && BERQWP_ALLOW_LOCALHOST !== true) {
84 return;
85 }
86
87 // if (defined('BERQWP_DISABLE_CACHE_WARMUP') && BERQWP_DISABLE_CACHE_WARMUP === true) {
88 // return;
89 // }
90
91 // Remove require cache warning
92 update_option('bwp_require_flush_cache', 0);
93
94 // if (berqwp_is_wp_cron_broken()) {
95 // $this->warmup_sitemap(true);
96 // return;
97 // }
98
99 // if (false === as_has_scheduled_action('bwp_warmup_sitemap') && function_exists('as_enqueue_async_action')) {
100 // as_enqueue_async_action('bwp_warmup_sitemap', []);
101 // }
102
103 // Add homepage
104 $urls[] = home_url('/');
105
106 // Fetch post type URLs
107 // $post_types = get_post_types(['public' => true, 'exclude_from_search' => false]);
108 $post_types = get_option('berqwp_optimize_post_types', []);
109 // foreach ($post_types as $post_type) {
110 // $post_ids = get_posts([
111 // 'post_status' => 'publish',
112 // 'has_password' => false,
113 // 'ignore_sticky_posts' => true,
114 // 'no_found_rows' => true,
115 // 'update_post_meta_cache' => false,
116 // 'update_post_term_cache' => false,
117 // 'order' => 'DESC',
118 // 'orderby' => 'date',
119 // 'post_type' => $post_type,
120 // 'numberposts' => -1, // get all posts
121 // 'fields' => 'ids', // only get post IDs
122 // ]);
123
124 // foreach ($post_ids as $post_id) {
125 // $urls[] = get_permalink($post_id);
126 // }
127 // }
128
129 $args = array(
130 'post_type' => $post_types,
131 'post_status' => array('publish'), // Only published posts
132 'posts_per_page' => -1,
133 'fields' => 'ids', // Only retrieve IDs to save memory
134 );
135
136 // Run the query
137 $query = new WP_Query($args);
138 $total_posts = $query->found_posts;
139
140 if ($query->have_posts()) {
141 while ($query->have_posts()) {
142 $query->the_post();
143 $url = get_permalink();
144
145 $urls[] = $url;
146
147 $translation_urls = apply_filters('berqwp_page_translation_urls', [], $url);
148
149 if (!empty($translation_urls)) {
150 foreach ($translation_urls as $page_url) {
151 if (!bwp_can_optimize_page_url($page_url)) {
152 continue;
153 }
154
155 $urls[] = $page_url;
156 }
157 }
158 }
159
160 wp_reset_postdata();
161 }
162
163 // Fetch author URLs
164 $user_ids = get_users([
165 'role' => 'author',
166 'count_total' => false,
167 'fields' => 'ID',
168 ]);
169
170 foreach ($user_ids as $user_id) {
171 $urls[] = get_author_posts_url($user_id);
172 }
173
174 $urls = berqwp_validate_url_array($urls);
175
176 $urls = array_filter($urls, function ($page_url) {
177
178 try {
179 return !berqwp_is_page_url_excluded($page_url);
180 } catch (Exception $e) {
181 return false;
182 }
183 });
184
185 $urls = array_map(function ($page_url) {
186 return berqwp_remove_ignore_params($page_url);
187 }, $urls);
188
189 $urls = array_filter($urls, function ($page_url) {
190 return strpos($page_url, '?') === false;
191 });
192
193 $queue = [];
194
195 berqwp_clear_cache_queue();
196
197 update_option('berqwp_server_queue', [], false);
198 // update_option('berqwp_uploaded_assets', [], false);
199
200 $urls = array_unique($urls);
201
202 global $berq_log;
203 $berq_log->info("Warming cache for " . count($urls) . " pages.");
204
205 foreach ($urls as $url) {
206 $key = md5($url);
207
208 if (!isset($queue[$key])) {
209 $queue[$key] = [
210 'url' => $url,
211 'added' => time(),
212 'priority' => $url == home_url('/') ? 1 : 5,
213 'attempts' => 0
214 ];
215 }
216 }
217
218 $this->do_preload($queue);
219 }
220
221 function do_preload($queue = null)
222 {
223
224 if ($queue === null) {
225 $queue = get_option('berqwp_optimize_queue', []);
226 }
227
228 // Sort by priority
229 uasort($queue, fn($a, $b) => $a['priority'] - $b['priority']);
230
231 // remove active pages
232 $pending_queue = array_filter($queue, function ($item) {
233 return empty($item['status']) || $item['status'] !== 'active';
234 });
235
236 $queue_values = array_values($pending_queue);
237
238 if (!empty($queue_values[0]) && !empty($queue_values[0]['url'])) {
239 $preload_url = $queue_values[0]['url'];
240
241 // Remove from original queue using the MD5 key
242 $key = array_key_first($pending_queue);
243 // unset($queue[$key_to_remove]);
244
245 $queue[$key]['status'] = 'active';
246 update_option('berqwp_optimize_queue', $queue, false);
247
248 global $berq_log;
249 $berq_log->info("Doing preload request");
250
251
252 if (defined('BERQWP_DEV_MOCKS') && BERQWP_DEV_MOCKS) {
253 $preload_url = str_replace("localhost:9000", "host.docker.internal:9000", $preload_url);
254 }
255
256 // Preload URL
257 wp_remote_get($preload_url . '?bwp_preload=' . time(), [
258 'timeout' => 0.01,
259 'blocking' => false,
260 'sslverify' => false,
261 'httpversion' => '2.0',
262 ]);
263 }
264 }
265
266 function preload_buffer()
267 {
268 if (is_admin() || is_preview() || is_404() || is_search()) {
269 return;
270 }
271
272 ob_start([$this, 'warmup_html']);
273 }
274
275 function warmup_html($html)
276 {
277
278 $current_page_url = bwp_get_request_url();
279 $queue = get_option('berqwp_optimize_queue', []);
280
281 if (defined('BERQWP_DEV_MOCKS') && BERQWP_DEV_MOCKS) {
282 $current_page_url = str_replace("host.docker.internal:9000", "localhost:9000", $current_page_url);
283 }
284
285 if (empty($html)) {
286 global $berq_log;
287 $berq_log->info("empty page html $current_page_url");
288 $this->do_preload();
289 return $html;
290 }
291
292 if (strpos($current_page_url, '?') !== false) {
293 $current_page_url = explode('?', $current_page_url)[0];
294 }
295
296 if (berqwp_is_page_url_excluded($current_page_url)) {
297 $this->do_preload();
298 return;
299 }
300
301 // Remove ignored params from the slug
302 // $slug = berqwp_remove_ignore_params($slug_uri);
303 $page_url = berqwp_remove_ignore_params($current_page_url);
304 $key = md5($page_url);
305
306 $html = preg_replace('/\?bwp_preload=\d+&/', '?', $html);
307 $html = preg_replace('/\?bwp_preload=\d+/', '', $html);
308 $html = preg_replace('/bwp_preload%3D\d+(%26)?/', '', $html);
309
310 try {
311 $result = berqUpload::process_page($page_url, $html);
312
313 if (!$result['success']) {
314 throw new Exception('BerqWP Page Queue Processing failed');
315 }
316
317 unset($queue[$key]);
318
319 } catch (Exception $e) {
320
321 global $berq_log;
322 $berq_log->info("Page $page_url failed, adding back into queue. {$e->getMessage()}");
323
324 $queue[$key]['url'] = $page_url;
325 $queue[$key]['status'] = 'pending'; $queue[$key]['attempts']++;
326
327 } catch (Throwable $e) {
328
329 global $berq_log;
330 $berq_log->info("Page $page_url failed, adding back into queue. {$e}");
331
332 $queue[$key]['url'] = $page_url;
333 $queue[$key]['status'] = 'pending';
334 $queue[$key]['attempts']++;
335 }
336
337 // Remove items with too many attempts
338 $queue = array_filter($queue, fn($item) => $item['attempts'] < 3);
339
340 global $berq_log;
341 $berq_log->info("Queue count: " . count($queue));
342
343 update_option('berqwp_optimize_queue', $queue, false);
344 usleep(0.5 * 1000000);
345
346 $this->do_preload();
347
348 return $html;
349 }
350 }
351
352 new berqWarmup();
353 }
354