PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.1.13
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.1.13
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 / admin / save-settings.php

save-settings.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.1.13, at admin/save-settings.php

893 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use BerqWP\BerqWP;
4
5 if (!defined('ABSPATH'))
6 exit;
7
8 if (isset($_POST['berqwp_save_nonce'])) {
9 if (!wp_verify_nonce($_POST['berqwp_save_nonce'], 'berqwp_save_settings')) {
10 die('Invalid nonce value');
11 }
12
13 $plugin_name = defined('BERQWP_PLUGIN_NAME') ? BERQWP_PLUGIN_NAME : 'BerqWP';
14
15 // if (!empty($_POST['berqwp_intro_page']) && !berqwp_is_license_managed_by_network()) {
16 if (!berqwp_is_license_managed_by_network() && !empty($_POST['berqwp_optimization_method'])) {
17 $optimization_method = sanitize_text_field($_POST['berqwp_optimization_method']);
18
19 if ($optimization_method == 'local') {
20 $email = sanitize_email($_POST['berqwp_local_user_email']);
21
22 wp_remote_post(BerqWP::$endpoint."free-user", [
23 'timeout' => 30,
24 'body' => [
25 'email' => $email,
26 'site_url' => get_option('home'),
27 ]
28 ]);
29
30 $berqconfigs = berqConfigs::getInstance();
31 $berqconfigs->update_configs(['optimization_method' => 'local']);
32
33 ?>
34 <script>
35 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
36 </script>
37 <?php
38 exit();
39 }
40
41 // if ($optimization_method == 'cloud' && !empty($_POST['berqwp_license_key'])) {
42 if (!empty($_POST['berqwp_license_key'])) {
43 $license_key = sanitize_text_field($_POST['berqwp_license_key']);
44 $berqconfigs = berqConfigs::getInstance();
45 $berqwp_configs = $berqconfigs->get_configs();
46 $site_id = $berqwp_configs['site_id'];
47
48 if (empty($site_id)) {
49 $blog_id = get_current_blog_id();
50 $network_id = function_exists('get_current_network_id') ? get_current_network_id() : 1;
51 $siteurl = get_option('siteurl');
52 $site_id = md5("berqwp|$network_id|$blog_id|$siteurl");
53
54 $berqconfigs->update_configs(['site_id' => $site_id]);
55 }
56
57 // $lic_response = wp_remote_post(BerqWP::$endpoint."authenticate", [
58 // 'timeout' => 30,
59 // 'body' => [
60 // 'license_key' => $license_key,
61 // 'site_id' => $site_id,
62 // 'site_url' => home_url(),
63 // ]
64 // ]);
65
66 // if (is_wp_error($lic_response)) {
67 // $error = "Couldn't reach BerqWP server. Please contact support.";
68 // global $berqNotifications;
69 // $berqNotifications->error($error);
70
71 // ?>
72 // <script>
73 // location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
74 // </script>
75 // <?php
76 // exit();
77 // }
78
79 // $lic_body = wp_remote_retrieve_body($lic_response);
80 // $lic_json = json_decode($lic_body);
81
82 $berqWP = \BerqWP::getInstance();
83 $lic_json = $berqWP->authenticate_license($license_key);
84
85 if (empty($lic_json)) {
86 $error = "Couldn't parse BerqWP server response. Please contact support.";
87 global $berqNotifications;
88 $berqNotifications->error($error);
89
90 ?>
91 <script>
92 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
93 </script>
94 <?php
95 exit();
96 }
97
98 if (!empty($lic_json->lic) && !empty($lic_json->secret) && $lic_json->lic->result == 'success' && ($lic_json->lic->message == 'License key activated' || $lic_json->lic->status == 'active')) {
99 berqwp_update_license_key($license_key);
100
101 $berqconfigs->update_configs([
102 'secret' => $lic_json->secret,
103 'optimization_method' => 'cloud'
104 ]);
105
106 berqwp_update_network_option('berqwp_license_cache', $lic_json->lic);
107
108 if ($lic_json->lic->product_ref == 'AppSumo Deal') {
109 update_option('berqwp_can_use_fluid_images', 0, false);
110
111 // sync addons
112 berqwp_sync_addons($license_key, home_url());
113
114 } else {
115 update_option('berqwp_can_use_fluid_images', 1, false);
116 }
117
118 // trigger cache warmup
119 do_action('berqwp_cache_warmup');
120
121 // clear cache from cloud
122 bwp_request_purge_license_key_cache();
123
124 global $berqNotifications;
125 $berqNotifications->success("$plugin_name license key has been activated.");
126
127 ?>
128 <script>
129 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
130 </script>
131 <?php
132 exit();
133
134 } elseif (isset($lic_json->lic->result ) && $lic_json->lic->result == 'error') {
135 $error = $lic_json->lic->message;
136
137 global $berqNotifications;
138 $berqNotifications->error($error);
139
140 ?>
141 <script>
142 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
143 </script>
144 <?php
145 exit();
146 } elseif (isset($lic_json->lic->status) && $lic_json->lic->status == 'expired') {
147 global $berqNotifications;
148 $berqNotifications->error('License key has expired. Please renew your subscription.');
149
150 ?>
151 <script>
152 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
153 </script>
154 <?php
155 exit();
156 }
157
158 global $berqNotifications;
159 $berqNotifications->error('License key verification failed. Please contact support if the issue persists.');
160
161 ?>
162 <script>
163 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
164 </script>
165 <?php
166 exit();
167 }
168 }
169
170 /*if (!empty($_POST['berqwp_license_key']) && !berqwp_is_license_managed_by_network()) {
171 // if (berq_is_localhost() && get_site_url() !== 'http://berq-test.local') {
172 // return;
173 // }
174
175 $key = sanitize_text_field($_POST['berqwp_license_key']);
176 $key_response = $this->verify_license_key($key, 'slm_activate');
177
178 if (!empty($key_response) && $key_response->result == 'success' && ($key_response->message == 'License key activated' || $key_response->status == 'active')) {
179 berqwp_update_license_key($key);
180
181 if ($key_response->product_ref == 'AppSumo Deal') {
182 update_option('berqwp_can_use_fluid_images', 0, false);
183
184 // sync addons
185 berqwp_sync_addons($key, home_url());
186
187 } else {
188 update_option('berqwp_can_use_fluid_images', 1, false);
189 }
190
191 // trigger cache warmup
192 do_action('berqwp_cache_warmup');
193
194 // clear cache from cloud
195 bwp_request_purge_license_key_cache();
196
197 global $berqNotifications;
198 $berqNotifications->success("$plugin_name license key has been activated.");
199
200 ?>
201 <script>
202 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
203 </script>
204 <?php
205 exit();
206 } elseif (isset($key_response->result ) && $key_response->result == 'error') {
207 $error = $key_response->message;
208
209 global $berqNotifications;
210 $berqNotifications->error($error);
211
212 ?>
213 <script>
214 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
215 </script>
216 <?php
217 exit();
218 } elseif (isset($key_response->status) && $key_response->status == 'expired') {
219 global $berqNotifications;
220 $berqNotifications->error('License key has expired. Please renew your subscription.');
221
222 ?>
223 <script>
224 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
225 </script>
226 <?php
227 exit();
228 }
229
230 global $berqNotifications;
231 $berqNotifications->error('License key verification failed. Please contact support if the issue persists.');
232
233 ?>
234 <script>
235 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
236 </script>
237 <?php
238 exit();
239 }*/
240
241 if (!empty($_POST['berq_deactivate_key']) && !berqwp_is_license_managed_by_network()) {
242 $license_key = berqwp_get_license_key();
243 $key_response = $this->verify_license_key($license_key, 'slm_deactivate');
244
245 delete_transient('berq_lic_response_cache');
246 delete_transient('berqwp_lic_response_cache');
247 berqwp_delete_network_option('berqwp_license_cache');
248
249 berqwp_delete_license_key();
250 berqwp_clear_cache_queue();
251
252 global $berqNotifications;
253 $berqNotifications->success("$plugin_name license key has been deactivated.");
254
255 ?>
256 <script>
257 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
258 </script>
259 <?php
260 exit;
261 }
262
263 if (!empty($_POST['bwp_cf_apitoken']) && !empty($_POST['bwp_cf_zoneid']) && !empty($_POST['bwp_cf_email'])) {
264 $apitoken = sanitize_text_field( $_POST['bwp_cf_apitoken'] );
265 $zoneid = sanitize_text_field( $_POST['bwp_cf_zoneid'] );
266 $email = sanitize_text_field( $_POST['bwp_cf_email'] );
267
268 $berqCloudflareAPIHandler = new berqCloudflareAPIHandler($email, $apitoken, $zoneid);
269
270 if ($berqCloudflareAPIHandler->verify_credentials()) {
271 update_option( 'berqwp_cf_creden', [
272 'apitoken' => $apitoken,
273 'zoneid' => $zoneid,
274 'email' => $email,
275 ], false );
276
277 $berqCloudflareAPIHandler->add_rule();
278 $berqCloudflareAPIHandler->purge_all_cache();
279
280 global $berqNotifications;
281 $berqNotifications->success('Cloudflare account connected.');
282
283 ?>
284 <script>
285 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
286 </script>
287 <?php
288 exit();
289
290 } else {
291
292 global $berqNotifications;
293 $berqNotifications->error('Invalid Cloudflare credentials.');
294
295 ?>
296 <script>
297 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
298 </script>
299 <?php
300 exit();
301 }
302 }
303
304 if (!empty($_POST['bwp_disable_cf'])) {
305
306 if (!empty(get_option( 'berqwp_cf_creden' ))) {
307 $email = get_option( 'berqwp_cf_creden' )['email'];
308 $apitoken = get_option( 'berqwp_cf_creden' )['apitoken'];
309 $zoneid = get_option( 'berqwp_cf_creden' )['zoneid'];
310
311 $berqCloudflareAPIHandler = new berqCloudflareAPIHandler($email, $apitoken, $zoneid);
312 $berqCloudflareAPIHandler->purge_all_cache();
313 $berqCloudflareAPIHandler->delete_rule_by_description('BerqWP cache rules');
314
315 delete_option( 'berqwp_cf_creden' );
316 }
317
318 global $berqNotifications;
319 $berqNotifications->success('Cloudflare Edge Cache disabled.');
320
321 ?>
322 <script>
323 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
324 </script>
325 <?php
326 exit();
327 }
328
329 if (!empty($_POST['bwp_disable_page_compression'])) {
330
331 // Update settings
332 $berqconfigs = berqConfigs::getInstance();
333 $berqconfigs->update_configs(['page_compression'=>false]);
334
335 global $berqNotifications;
336 $berqNotifications->success('Page compression has been successfully disabled on your website.');
337
338 ?>
339 <script>
340 location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>';
341 </script>
342 <?php
343 exit();
344 }
345
346 global $berq_log;
347
348 // If the option is changed
349 if (bwp_is_option_updated('berqwp_enable_sandbox') === true) {
350 $berq_log->info('Updated berqwp_enable_sandbox');
351 do_action('berqwp_on_update_sandbox_mode');
352 }
353
354 if (isset($_POST['berqwp_enable_sandbox'])) {
355 update_option('berqwp_enable_sandbox', 1);
356 } else {
357 update_option('berqwp_enable_sandbox', 0);
358 }
359
360 if (isset($_POST['berqwp_cache_lifespan'])) {
361 $val = (int) sanitize_text_field($_POST['berqwp_cache_lifespan']);
362 update_option('berqwp_cache_lifespan', $val);
363 }
364
365 if (isset($_POST['berqwp_webp_max_width'])) {
366 $val = (int) sanitize_text_field($_POST['berqwp_webp_max_width']);
367 update_option('berqwp_webp_max_width', $val);
368 }
369
370 if (isset($_POST['berqwp_webp_quality'])) {
371 $val = (int) sanitize_text_field($_POST['berqwp_webp_quality']);
372 update_option('berqwp_webp_quality', $val);
373 }
374
375 // If the option is changed require flush cache
376 if (bwp_is_option_updated('berqwp_image_lazyloading') === true) {
377 $berq_log->info('Updated berqwp_image_lazyloading');
378 update_option('bwp_require_flush_cache', 1);
379 }
380
381 if (isset($_POST['berqwp_image_lazyloading'])) {
382 update_option('berqwp_image_lazyloading', 1);
383 } else {
384 update_option('berqwp_image_lazyloading', 0);
385 }
386
387 // If the option is changed require flush cache
388 if (bwp_is_option_updated('berqwp_disable_webp')) {
389 $berq_log->info('Updated berqwp_disable_webp');
390 update_option('bwp_require_flush_cache', 1);
391 }
392
393 if (isset($_POST['berqwp_disable_webp'])) {
394 update_option('berqwp_disable_webp', 1);
395 } else {
396 update_option('berqwp_disable_webp', 0);
397 }
398
399 // If the option is changed require flush cache
400 if (bwp_is_option_updated('berqwp_fluid_images')) {
401 $berq_log->info('Updated berqwp_fluid_images');
402 update_option('bwp_require_flush_cache', 1);
403 }
404
405 if (isset($_POST['berqwp_fluid_images'])) {
406 update_option('berqwp_fluid_images', 1);
407 } else {
408 update_option('berqwp_fluid_images', 0);
409 }
410
411 // If the option is changed require flush cache
412 if (bwp_is_option_updated('berqwp_enable_cdn')) {
413 $berq_log->info('Updated berqwp_enable_cdn');
414 update_option('bwp_require_flush_cache', 1);
415 }
416
417 if (isset($_POST['berqwp_enable_cdn'])) {
418 update_option('berqwp_enable_cdn', 1);
419 } else {
420 update_option('berqwp_enable_cdn', 0);
421 }
422
423 // If the option is changed require flush cache
424 if (bwp_is_option_updated('berqwp_enable_cwv')) {
425 $berq_log->info('Updated berqwp_enable_cwv');
426 update_option('bwp_require_flush_cache', 1);
427 }
428
429 if (isset($_POST['berqwp_enable_cwv'])) {
430 update_option('berqwp_enable_cwv', 1);
431 } else {
432 update_option('berqwp_enable_cwv', 0);
433 }
434
435 // If the option is changed require flush cache
436 if (bwp_is_option_updated('berqwp_preload_cookiebanner')) {
437 $berq_log->info('Updated berqwp_preload_cookiebanner');
438 update_option('bwp_require_flush_cache', 1);
439 }
440
441 if (isset($_POST['berqwp_preload_cookiebanner'])) {
442 update_option('berqwp_preload_cookiebanner', 1);
443 } else {
444 update_option('berqwp_preload_cookiebanner', 0);
445 }
446
447 // If the option is changed require flush cache
448 if (bwp_is_option_updated('berqwp_preload_fontfaces')) {
449 $berq_log->info('Updated berqwp_preload_fontfaces');
450 update_option('bwp_require_flush_cache', 1);
451 }
452
453 if (isset($_POST['berqwp_preload_fontfaces'])) {
454 update_option('berqwp_preload_fontfaces', 1);
455 } else {
456 update_option('berqwp_preload_fontfaces', 0);
457 }
458
459 if (isset($_POST['berqwp_disable_emojis'])) {
460 update_option('berqwp_disable_emojis', 1);
461 } else {
462 update_option('berqwp_disable_emojis', 0);
463 }
464
465 if (isset($_POST['berqwp_heartbeat_mode'])) {
466 $heartbeat_mode = sanitize_text_field($_POST['berqwp_heartbeat_mode']);
467 if (!in_array($heartbeat_mode, ['default', 'restrict', 'throttle', 'disable'], true)) {
468 $heartbeat_mode = 'default';
469 }
470 update_option('berqwp_heartbeat_mode', $heartbeat_mode);
471 }
472
473 if (isset($_POST['berqwp_heartbeat_interval'])) {
474 $heartbeat_interval = max(15, min(300, (int) sanitize_text_field($_POST['berqwp_heartbeat_interval'])));
475 update_option('berqwp_heartbeat_interval', $heartbeat_interval);
476 }
477
478 foreach ([
479 'berqwp_disable_embeds',
480 'berqwp_disable_xmlrpc',
481 'berqwp_remove_rest_head_links',
482 'berqwp_remove_head_meta',
483 'berqwp_disable_self_pingbacks',
484 ] as $bwp_debloat_option) {
485 update_option($bwp_debloat_option, isset($_POST[$bwp_debloat_option]) ? 1 : 0);
486 }
487
488 if (isset($_POST['berqwp_db_schedule_frequency'])) {
489 $db_schedule_frequency = sanitize_text_field($_POST['berqwp_db_schedule_frequency']);
490 if (!in_array($db_schedule_frequency, ['daily', 'weekly', 'monthly'], true)) {
491 $db_schedule_frequency = 'weekly';
492 }
493 update_option('berqwp_db_schedule_frequency', $db_schedule_frequency);
494 }
495
496 if (isset($_POST['berqwp_db_revision_limit'])) {
497 update_option('berqwp_db_revision_limit', max(0, (int) sanitize_text_field($_POST['berqwp_db_revision_limit'])));
498 }
499
500 $_POST['berqwp_db_scheduled_tasks'] = $_POST['berqwp_db_scheduled_tasks'] ?? [];
501 if (is_array($_POST['berqwp_db_scheduled_tasks'])) {
502 update_option('berqwp_db_scheduled_tasks', array_map('sanitize_text_field', wp_unslash($_POST['berqwp_db_scheduled_tasks'])));
503 }
504
505 update_option('berqwp_db_schedule_enabled', isset($_POST['berqwp_db_schedule_enabled']) ? 1 : 0);
506
507 do_action('berqwp_reschedule_db_cron');
508
509 // If the option is changed require flush cache
510 if (bwp_is_option_updated('berqwp_lazyload_youtube_embed')) {
511 $berq_log->info('Updated berqwp_lazyload_youtube_embed');
512 update_option('bwp_require_flush_cache', 1);
513 }
514
515 if (isset($_POST['berqwp_lazyload_youtube_embed'])) {
516 update_option('berqwp_lazyload_youtube_embed', 1);
517 } else {
518 update_option('berqwp_lazyload_youtube_embed', 0);
519 }
520
521 // If the option is changed require flush cache
522 if (bwp_is_option_updated('berqwp_preload_yt_poster')) {
523 $berq_log->info('Updated berqwp_preload_yt_poster');
524 update_option('bwp_require_flush_cache', 1);
525 }
526
527 if (isset($_POST['berqwp_preload_yt_poster'])) {
528 update_option('berqwp_preload_yt_poster', 1);
529 } else {
530 update_option('berqwp_preload_yt_poster', 0);
531 }
532
533 // If the option is changed require flush cache
534 if (bwp_is_option_updated('berqwp_enable_critical_css')) {
535 $berq_log->info('Updated berqwp_enable_critical_css');
536 update_option('bwp_require_flush_cache', 1);
537 }
538
539 if (isset($_POST['berqwp_enable_critical_css'])) {
540 update_option('berqwp_enable_critical_css', 1);
541 } else {
542 update_option('berqwp_enable_critical_css', 0);
543 }
544
545 // If the option is changed require flush cache
546 if (bwp_is_option_updated('berqwp_enable_used_css')) {
547 $berq_log->info('Updated berqwp_enable_used_css');
548 update_option('bwp_require_flush_cache', 1);
549 }
550
551 if (isset($_POST['berqwp_enable_used_css'])) {
552 update_option('berqwp_enable_used_css', 1);
553 } else {
554 update_option('berqwp_enable_used_css', 0);
555 }
556
557 if (isset($_POST['berqwp_async_excluded_styles'])) {
558 update_option('berqwp_async_excluded_styles', 1);
559 } else {
560 update_option('berqwp_async_excluded_styles', 0);
561 }
562
563 if (isset($_POST['berqwp_defer_excluded_js'])) {
564 update_option('berqwp_defer_excluded_js', 1);
565 } else {
566 update_option('berqwp_defer_excluded_js', 0);
567 }
568
569 if (isset($_POST['berqwp_delay_third_party_scripts'])) {
570 update_option('berqwp_delay_third_party_scripts', 1);
571 } else {
572 update_option('berqwp_delay_third_party_scripts', 0);
573 }
574
575 if (isset($_POST['berqwp_lazy_render'])) {
576 update_option('berqwp_lazy_render', 1);
577 } else {
578 update_option('berqwp_lazy_render', 0);
579 }
580
581 if (isset($_POST['berqwp_enable_webp'])) {
582 update_option('berqwp_enable_webp', 1);
583 } else {
584 update_option('berqwp_enable_webp', 0);
585 }
586
587 if (isset($_POST['berqwp_prerender_on_hover'])) {
588 update_option('berqwp_prerender_on_hover', 1);
589 } else {
590 update_option('berqwp_prerender_on_hover', 0);
591 }
592
593 if (isset($_POST['berqwp_lazy_load_videos'])) {
594 update_option('berqwp_lazy_load_videos', 1);
595 } else {
596 update_option('berqwp_lazy_load_videos', 0);
597 }
598
599 if (isset($_POST['berqwp_force_include_critical_css'])) {
600 $keywords = sanitize_textarea_field($_POST['berqwp_force_include_critical_css']);
601 $keywords = explode("\n", $keywords);
602
603 $keywords = array_map(function ($kw) {
604 return trim($kw);
605 }, $keywords);
606
607 $keywords = array_filter($keywords, function ($kw) {
608 return !empty($kw);
609 });
610
611 if (bwp_is_option_updated('berqwp_force_include_critical_css')) {
612
613 update_option('bwp_require_flush_cache', 1);
614 }
615
616 update_option('berqwp_force_include_critical_css', $keywords);
617 }
618
619 if (isset($_POST['berqwp_exclude_lazy_load_images'])) {
620 $keywords = sanitize_textarea_field($_POST['berqwp_exclude_lazy_load_images']);
621 $keywords = explode("\n", $keywords);
622
623 $keywords = array_map(function ($kw) {
624 return trim($kw);
625 }, $keywords);
626
627 $keywords = array_filter($keywords, function ($kw) {
628 return !empty($kw);
629 });
630
631 if (bwp_is_option_updated('berqwp_exclude_lazy_load_images')) {
632
633 update_option('bwp_require_flush_cache', 1);
634 }
635
636 update_option('berqwp_exclude_lazy_load_images', $keywords);
637 }
638
639 if (isset($_POST['berqwp_exclude_third_party_js'])) {
640 $keywords = sanitize_textarea_field($_POST['berqwp_exclude_third_party_js']);
641 $keywords = explode("\n", $keywords);
642
643 $keywords = array_map(function ($kw) {
644 return trim($kw);
645 }, $keywords);
646
647 $keywords = array_filter($keywords, function ($kw) {
648 return !empty($kw);
649 });
650
651 if (bwp_is_option_updated('berqwp_exclude_third_party_js')) {
652
653 update_option('bwp_require_flush_cache', 1);
654 }
655
656 update_option('berqwp_exclude_third_party_js', $keywords);
657 }
658
659 if (isset($_POST['berqwp_exclude_js'])) {
660 $keywords = sanitize_textarea_field($_POST['berqwp_exclude_js']);
661 $keywords = explode("\n", $keywords);
662
663 $keywords = array_map(function ($kw) {
664 return trim($kw);
665 }, $keywords);
666
667 $keywords = array_filter($keywords, function ($kw) {
668 return !empty($kw);
669 });
670
671 if (bwp_is_option_updated('berqwp_exclude_js')) {
672
673 update_option('bwp_require_flush_cache', 1);
674 }
675
676 update_option('berqwp_exclude_js', $keywords);
677 }
678
679 if (isset($_POST['berqwp_exclude_css'])) {
680 $keywords = sanitize_textarea_field($_POST['berqwp_exclude_css']);
681 $keywords = explode("\n", $keywords);
682
683 $keywords = array_map(function ($kw) {
684 return trim($kw);
685 }, $keywords);
686
687 $keywords = array_filter($keywords, function ($kw) {
688 return !empty($kw);
689 });
690
691 if (bwp_is_option_updated('berqwp_exclude_css')) {
692
693 update_option('bwp_require_flush_cache', 1);
694 }
695
696 update_option('berqwp_exclude_css', $keywords);
697 }
698
699 // If the option is changed require flush cache
700 if (bwp_is_option_updated('berqwp_javascript_execution_mode')) {
701 $berq_log->info('Updated berqwp_javascript_execution_mode');
702 update_option('bwp_require_flush_cache', 1);
703 }
704
705 if (isset($_POST['berqwp_javascript_execution_mode'])) {
706 $val = (int) sanitize_text_field($_POST['berqwp_javascript_execution_mode']);
707 update_option('berqwp_javascript_execution_mode', $val);
708 }
709
710 $_POST['berqwp_optimize_post_types'] = $_POST['berqwp_optimize_post_types'] ?? [];
711 if (isset($_POST['berqwp_optimize_post_types']) && is_array($_POST['berqwp_optimize_post_types'])) {
712 update_option('berqwp_optimize_post_types', $_POST['berqwp_optimize_post_types']);
713 }
714
715 $_POST['berqwp_optimize_taxonomies'] = $_POST['berqwp_optimize_taxonomies'] ?? [];
716 if (isset($_POST['berqwp_optimize_taxonomies']) && is_array($_POST['berqwp_optimize_taxonomies'])) {
717 update_option('berqwp_optimize_taxonomies', $_POST['berqwp_optimize_taxonomies']);
718 }
719
720 if (isset($_POST['berqwp_interaction_delay'])) {
721 $val = sanitize_text_field($_POST['berqwp_interaction_delay']);
722 update_option('berqwp_interaction_delay', $val);
723 }
724
725 if (isset($_POST['berq_opt_mode'])) {
726
727 $val = sanitize_text_field($_POST['berq_opt_mode']);
728 $current_val = get_option( 'berq_opt_mode' );
729
730 update_option('berq_opt_mode', $val);
731
732 if ($val !== $current_val) {
733 do_action( 'berqwp_on_update_optimization_mode' );
734 delete_option( 'bwp_require_flush_cache' );
735 }
736 }
737
738 if (isset($_POST['berq_exclude_cdn'])) {
739 $keywords = sanitize_textarea_field($_POST['berq_exclude_cdn']);
740 $keywords = explode("\n", $keywords);
741
742 $keywords = array_map(function ($kw) {
743 return trim($kw);
744 }, $keywords);
745
746 $keywords = array_filter($keywords, function ($kw) {
747 return !empty($kw);
748 });
749
750 if (bwp_is_option_updated('berq_exclude_cdn')) {
751 $berq_log->info('Updated berq_exclude_cdn');
752
753 update_option('bwp_require_flush_cache', 1);
754 }
755
756 update_option('berq_exclude_cdn', $keywords);
757 }
758
759 if (isset($_POST['berq_exclude_urls'])) {
760 $urls = sanitize_textarea_field($_POST['berq_exclude_urls']);
761 $urls_array = explode("\n", $urls);
762
763 // Add trailing slash to each URL
764 $urls_array = array_map(function ($url) {
765 if (!empty ($url)) {
766 $url = trim($url);
767
768 // Delete cache for this url
769 // berqCache::delete_page_cache_files(bwp_url_into_path($url));
770 berqCache::purge_page($url);
771
772 return $url;
773 }
774 }, $urls_array);
775
776 $urls_array = array_filter($urls_array, function($item) {
777 return !empty($item);
778 });
779
780 $urls_array = array_unique($urls_array);
781
782 if (empty($urls_array)) {
783 $urls_array = [];
784 }
785
786 $berqconfigs = berqConfigs::getInstance();
787 $berqconfigs->update_configs(['exclude_urls'=>$urls_array]);
788
789 if (isset($urls_array)) {
790 update_option('berq_exclude_urls', $urls_array);
791 }
792
793 }
794
795
796 if (!empty($_POST['berq_ignore_urls_params'])) {
797 $urls = sanitize_textarea_field($_POST['berq_ignore_urls_params']);
798 $urls_array = explode("\n", $urls);
799
800 if (!empty($urls_array)) {
801 update_option('berq_ignore_urls_params', $urls_array);
802 }
803
804 }
805
806 if (isset($_POST['berq_exclude_cookies'])) {
807 $cookie_ids = sanitize_textarea_field($_POST['berq_exclude_cookies']);
808 // $cookie_ids_array = explode("\n", $cookie_ids);
809 $cookie_ids_array = preg_split("/\r\n|\n|\r/", trim($cookie_ids));
810
811 if (isset($cookie_ids_array) && is_array($cookie_ids_array)) {
812 $berqconfigs = berqConfigs::getInstance();
813 $berqconfigs->update_configs(['exclude_cookies'=>$cookie_ids_array]);
814 }
815
816 }
817
818 if (!empty($_POST['berqwp_cache_lifespan'])) {
819 $cache_lifespan = (int) sanitize_textarea_field($_POST['berqwp_cache_lifespan']);
820
821 // Sanitize
822 if (!in_array($cache_lifespan, [MONTH_IN_SECONDS, WEEK_IN_SECONDS, DAY_IN_SECONDS])) {
823 $cache_lifespan = MONTH_IN_SECONDS;
824 }
825
826 // Update settings
827 $berqconfigs = berqConfigs::getInstance();
828 $berqconfigs->update_configs(['cache_lifespan'=>$cache_lifespan]);
829
830 }
831
832 // If the option is changed require flush cache
833 if (bwp_is_option_updated('berq_exclude_js_css')) {
834 $berq_log->info('Updated berq_exclude_js_css');
835 update_option('bwp_require_flush_cache', 1);
836 }
837
838 if (isset($_POST['berq_exclude_js_css'])) {
839 $urls = sanitize_textarea_field($_POST['berq_exclude_js_css']);
840 $urls_array = explode("\n", $urls);
841
842 $urls_array = array_map(function ($kw) {
843 return trim($kw);
844 }, $urls_array);
845
846 $urls_array = array_filter($urls_array, function ($kw) {
847 return !empty($kw);
848 });
849
850 update_option('berq_exclude_js_css', $urls_array);
851
852 }
853
854 // If the option is changed require flush cache
855 if (bwp_is_option_updated('berq_css_optimization')) {
856 $berq_log->info('Updated berq_css_optimization');
857 update_option('bwp_require_flush_cache', 1);
858 }
859
860 if (!empty($_POST['berq_css_optimization'])) {
861 $css_optimization = sanitize_textarea_field($_POST['berq_css_optimization']);
862 update_option('berq_css_optimization', $css_optimization);
863 }
864
865 // If the option is changed require flush cache
866 if (bwp_is_option_updated('berq_js_optimization')) {
867 $berq_log->info('Updated berq_js_optimization');
868 update_option('bwp_require_flush_cache', 1);
869 }
870
871 if (!empty($_POST['berq_js_optimization'])) {
872 $css_optimization = sanitize_textarea_field($_POST['berq_js_optimization']);
873 update_option('berq_js_optimization', $css_optimization);
874 }
875
876 global $berqNotifications;
877 $berqNotifications->success('Changes have been saved.');
878
879 $tab_id = '';
880 if (!empty($_POST['bwp_current_tab_id'])) {
881 $tab_id = "&tab_id=".sanitize_text_field( $_POST['bwp_current_tab_id'] );
882 }
883
884
885 ?>
886 <script>
887 location.href = '<?php echo get_admin_url() . 'admin.php?page=berqwp'.$tab_id; ?>';
888 </script>
889 <?php
890 exit;
891
892 }
893