PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.17
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.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 1.9.4 1.9.5 1.9.6 All 170 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.0.17, at inc/cache/class-berqwarmup.php

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