| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +use BerqWP\BerqWP; | |
| 4 | + | |
| 3 | 5 | if (!defined('ABSPATH')) |
| 4 | 6 | exit; |
| 5 | 7 | |
| 6 | 8 | if (isset($_POST['berqwp_save_nonce'])) { |
| @@ -7,21 +9,190 @@ | ||
| 7 | 9 | if (!wp_verify_nonce($_POST['berqwp_save_nonce'], 'berqwp_save_settings')) { |
| 8 | 10 | die('Invalid nonce value'); |
| 9 | 11 | } |
| 10 | 12 | |
| 11 | - if (!empty($_POST['berqwp_license_key'])) { | |
| 12 | - if (berq_is_localhost() && get_site_url() !== 'http://berq-test.local') { | |
| 13 | - return; | |
| 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(); | |
| 14 | 39 | } |
| 15 | - | |
| 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 | + | |
| 16 | 171 | $key = sanitize_text_field($_POST['berqwp_license_key']); |
| 17 | 172 | $key_response = $this->verify_license_key($key, 'slm_activate'); |
| 18 | 173 | |
| 19 | - if (!empty($key_response) && $key_response->result == 'success') { | |
| 20 | - update_option('berqwp_license_key', $key); | |
| 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); | |
| 21 | 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 | + | |
| 22 | 193 | global $berqNotifications; |
| 23 | - $berqNotifications->success('BerqWP license key has been activated.'); | |
| 194 | + $berqNotifications->success("$plugin_name license key has been activated."); | |
| 24 | 195 | |
| 25 | 196 | ?> |
| 26 | 197 | <script> |
| 27 | 198 | location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>'; |
| @@ -27,9 +198,9 @@ | ||
| 27 | 198 | location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>'; |
| 28 | 199 | </script> |
| 29 | 200 | <?php |
| 30 | 201 | exit(); |
| 31 | - } elseif ($key_response->result == 'error') { | |
| 202 | + } elseif (isset($key_response->result ) && $key_response->result == 'error') { | |
| 32 | 203 | $error = $key_response->message; |
| 33 | 204 | |
| 34 | 205 | global $berqNotifications; |
| 35 | 206 | $berqNotifications->error($error); |
| @@ -39,11 +210,144 @@ | ||
| 39 | 210 | location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>'; |
| 40 | 211 | </script> |
| 41 | 212 | <?php |
| 42 | 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(); | |
| 43 | 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; | |
| 44 | 257 | } |
| 45 | 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 | + | |
| 46 | 350 | if (isset($_POST['berqwp_enable_sandbox'])) { |
| 47 | 351 | update_option('berqwp_enable_sandbox', 1); |
| 48 | 352 | } else { |
| 49 | 353 | update_option('berqwp_enable_sandbox', 0); |
| @@ -48,14 +352,8 @@ | ||
| 48 | 352 | } else { |
| 49 | 353 | update_option('berqwp_enable_sandbox', 0); |
| 50 | 354 | } |
| 51 | 355 | |
| 52 | - if (isset($_POST['berqwp_enable_cache_for_loggedin'])) { | |
| 53 | - update_option('berqwp_enable_cache_for_loggedin', 1); | |
| 54 | - } else { | |
| 55 | - update_option('berqwp_enable_cache_for_loggedin', 0); | |
| 56 | - } | |
| 57 | - | |
| 58 | 356 | if (isset($_POST['berqwp_cache_lifespan'])) { |
| 59 | 357 | $val = (int) sanitize_text_field($_POST['berqwp_cache_lifespan']); |
| 60 | 358 | update_option('berqwp_cache_lifespan', $val); |
| 61 | 359 | } |
| @@ -69,8 +367,14 @@ | ||
| 69 | 367 | $val = (int) sanitize_text_field($_POST['berqwp_webp_quality']); |
| 70 | 368 | update_option('berqwp_webp_quality', $val); |
| 71 | 369 | } |
| 72 | 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 | + | |
| 73 | 377 | if (isset($_POST['berqwp_image_lazyloading'])) { |
| 74 | 378 | update_option('berqwp_image_lazyloading', 1); |
| 75 | 379 | } else { |
| 76 | 380 | update_option('berqwp_image_lazyloading', 0); |
| @@ -75,8 +379,14 @@ | ||
| 75 | 379 | } else { |
| 76 | 380 | update_option('berqwp_image_lazyloading', 0); |
| 77 | 381 | } |
| 78 | 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 | + | |
| 79 | 389 | if (isset($_POST['berqwp_disable_webp'])) { |
| 80 | 390 | update_option('berqwp_disable_webp', 1); |
| 81 | 391 | } else { |
| 82 | 392 | update_option('berqwp_disable_webp', 0); |
| @@ -81,8 +391,26 @@ | ||
| 81 | 391 | } else { |
| 82 | 392 | update_option('berqwp_disable_webp', 0); |
| 83 | 393 | } |
| 84 | 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 | + | |
| 85 | 413 | if (isset($_POST['berqwp_enable_cdn'])) { |
| 86 | 414 | update_option('berqwp_enable_cdn', 1); |
| 87 | 415 | } else { |
| 88 | 416 | update_option('berqwp_enable_cdn', 0); |
| @@ -87,8 +415,38 @@ | ||
| 87 | 415 | } else { |
| 88 | 416 | update_option('berqwp_enable_cdn', 0); |
| 89 | 417 | } |
| 90 | 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 | + | |
| 91 | 449 | if (isset($_POST['berqwp_preload_fontfaces'])) { |
| 92 | 450 | update_option('berqwp_preload_fontfaces', 1); |
| 93 | 451 | } else { |
| 94 | 452 | update_option('berqwp_preload_fontfaces', 0); |
| @@ -99,8 +457,58 @@ | ||
| 99 | 457 | } else { |
| 100 | 458 | update_option('berqwp_disable_emojis', 0); |
| 101 | 459 | } |
| 102 | 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 | + | |
| 103 | 511 | if (isset($_POST['berqwp_lazyload_youtube_embed'])) { |
| 104 | 512 | update_option('berqwp_lazyload_youtube_embed', 1); |
| 105 | 513 | } else { |
| 106 | 514 | update_option('berqwp_lazyload_youtube_embed', 0); |
| @@ -105,8 +513,192 @@ | ||
| 105 | 513 | } else { |
| 106 | 514 | update_option('berqwp_lazyload_youtube_embed', 0); |
| 107 | 515 | } |
| 108 | 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 | + | |
| 109 | 701 | if (isset($_POST['berqwp_javascript_execution_mode'])) { |
| 110 | 702 | $val = (int) sanitize_text_field($_POST['berqwp_javascript_execution_mode']); |
| 111 | 703 | update_option('berqwp_javascript_execution_mode', $val); |
| 112 | 704 | } |
| @@ -126,64 +718,39 @@ | ||
| 126 | 718 | update_option('berqwp_interaction_delay', $val); |
| 127 | 719 | } |
| 128 | 720 | |
| 129 | 721 | if (isset($_POST['berq_opt_mode'])) { |
| 130 | - | |
| 722 | + | |
| 131 | 723 | $val = sanitize_text_field($_POST['berq_opt_mode']); |
| 132 | - | |
| 133 | - if ($val !== get_option( 'berq_opt_mode' )) { | |
| 134 | - do_action( 'berqwp_before_update_optimization_mode' ); | |
| 135 | - } | |
| 724 | + $current_val = get_option( 'berq_opt_mode' ); | |
| 136 | 725 | |
| 137 | 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 | + } | |
| 138 | 732 | } |
| 139 | 733 | |
| 140 | - if (isset($_POST['berqwp_enable_preload_mostly_used_font'])) { | |
| 141 | - update_option('berqwp_enable_preload_mostly_used_font', 1); | |
| 142 | - } else { | |
| 143 | - update_option('berqwp_enable_preload_mostly_used_font', 0); | |
| 144 | - } | |
| 734 | + if (isset($_POST['berq_exclude_cdn'])) { | |
| 735 | + $keywords = sanitize_textarea_field($_POST['berq_exclude_cdn']); | |
| 736 | + $keywords = explode("\n", $keywords); | |
| 145 | 737 | |
| 146 | - /*if (!empty($_POST['berqwp_license_key'])) { | |
| 147 | - if (berq_is_localhost()) { | |
| 148 | - return; | |
| 149 | - } | |
| 150 | - $key = sanitize_text_field($_POST['berqwp_license_key']); | |
| 151 | - $key_response = $this->verify_license_key($key, 'slm_activate'); | |
| 738 | + $keywords = array_map(function ($kw) { | |
| 739 | + return trim($kw); | |
| 740 | + }, $keywords); | |
| 152 | 741 | |
| 153 | - | |
| 154 | - if (!empty($key_response) && $key_response->result == 'success') { | |
| 155 | - update_option('berqwp_license_key', $key); | |
| 156 | - delete_transient( 'berq_lic_response_cache' ); | |
| 157 | - | |
| 158 | - } elseif ($key_response->result == 'error') { | |
| 159 | - $error = $key_response->message; | |
| 160 | - ?> | |
| 161 | - <div class="notice notice-error is-dismissible"> | |
| 162 | - <?php echo $error; ?> | |
| 163 | - </div> | |
| 164 | - <?php | |
| 165 | - } | |
| 166 | - }*/ | |
| 742 | + $keywords = array_filter($keywords, function ($kw) { | |
| 743 | + return !empty($kw); | |
| 744 | + }); | |
| 167 | 745 | |
| 168 | - if (!empty($_POST['berq_deactivate_key'])) { | |
| 169 | - $license_key = get_option('berqwp_license_key'); | |
| 170 | - $key_response = $this->verify_license_key($license_key, 'slm_deactivate'); | |
| 171 | - | |
| 172 | - delete_transient('berq_lic_response_cache'); | |
| 173 | - delete_option('berqwp_license_key'); | |
| 174 | - // if (!empty($key_response) && $key_response->result == 'success') { | |
| 175 | - // } | |
| 746 | + if (bwp_is_option_updated('berq_exclude_cdn')) { | |
| 747 | + $berq_log->info('Updated berq_exclude_cdn'); | |
| 176 | 748 | |
| 177 | - global $berqNotifications; | |
| 178 | - $berqNotifications->success('BerqWP license key has been deactivated.'); | |
| 749 | + update_option('bwp_require_flush_cache', 1); | |
| 750 | + } | |
| 179 | 751 | |
| 180 | - ?> | |
| 181 | - <script> | |
| 182 | - location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>'; | |
| 183 | - </script> | |
| 184 | - <?php | |
| 185 | - exit; | |
| 752 | + update_option('berq_exclude_cdn', $keywords); | |
| 186 | 753 | } |
| 187 | 754 | |
| 188 | 755 | if (isset($_POST['berq_exclude_urls'])) { |
| 189 | 756 | $urls = sanitize_textarea_field($_POST['berq_exclude_urls']); |
| @@ -191,12 +758,31 @@ | ||
| 191 | 758 | |
| 192 | 759 | // Add trailing slash to each URL |
| 193 | 760 | $urls_array = array_map(function ($url) { |
| 194 | 761 | if (!empty ($url)) { |
| 195 | - return trailingslashit(trim($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; | |
| 196 | 769 | } |
| 197 | 770 | }, $urls_array); |
| 198 | 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 | + | |
| 199 | 785 | if (isset($urls_array)) { |
| 200 | 786 | update_option('berq_exclude_urls', $urls_array); |
| 201 | 787 | } |
| 202 | 788 | |
| @@ -212,25 +798,90 @@ | ||
| 212 | 798 | } |
| 213 | 799 | |
| 214 | 800 | } |
| 215 | 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 | + | |
| 216 | 834 | if (isset($_POST['berq_exclude_js_css'])) { |
| 217 | 835 | $urls = sanitize_textarea_field($_POST['berq_exclude_js_css']); |
| 218 | 836 | $urls_array = explode("\n", $urls); |
| 219 | 837 | |
| 220 | - if (isset($urls_array)) { | |
| 221 | - update_option('berq_exclude_js_css', $urls_array); | |
| 222 | - } | |
| 838 | + $urls_array = array_map(function ($kw) { | |
| 839 | + return trim($kw); | |
| 840 | + }, $urls_array); | |
| 223 | 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 | + | |
| 224 | 848 | } |
| 225 | 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 | + | |
| 226 | 872 | global $berqNotifications; |
| 227 | - $berqNotifications->success('Changes have been saved! Please flush the cache to make changes visible for the visitors.'); | |
| 873 | + $berqNotifications->success('Changes have been saved.'); | |
| 228 | 874 | |
| 229 | - | |
| 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 | + | |
| 230 | 881 | ?> |
| 231 | 882 | <script> |
| 232 | - location.href = '<?php echo esc_html(get_admin_url() . 'admin.php?page=berqwp'); ?>'; | |
| 883 | + location.href = '<?php echo get_admin_url() . 'admin.php?page=berqwp'.$tab_id; ?>'; | |
| 233 | 884 | </script> |
| 234 | 885 | <?php |
| 235 | 886 | exit; |
| 236 | 887 | |