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

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