PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / admin / dashboard.php

dashboard.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score trunk, at includes/admin/dashboard.php

1,029 lines 41.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard Page
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache\Admin\Dashboard;
9
10 use PoweredCache\Async\CachePreloader;
11 use PoweredCache\Async\CachePurger;
12 use PoweredCache\Async\DatabaseOptimizer;
13 use PoweredCache\Config;
14 use PoweredCache\Encryption;
15 use PoweredCache\Preloader;
16 use function PoweredCache\Utils\is_dev_mode_active;
17 use function PoweredCache\Utils\mask_string;
18 use const PoweredCache\Constants\ALLOPTIONS_CRITICAL_THRESHOLD;
19 use const PoweredCache\Constants\ALLOPTIONS_WARNING_THRESHOLD;
20 use const PoweredCache\Constants\ICON_BASE64;
21 use const PoweredCache\Constants\MENU_SLUG;
22 use const PoweredCache\Constants\PURGE_CACHE_CRON_NAME;
23 use const PoweredCache\Constants\PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT;
24 use const PoweredCache\Constants\SETTING_OPTION;
25 use function PoweredCache\Utils\can_configure_htaccess;
26 use function PoweredCache\Utils\can_configure_object_cache;
27 use function PoweredCache\Utils\can_control_all_settings;
28 use function PoweredCache\Utils\cdn_zones;
29 use function PoweredCache\Utils\clean_site_cache_dir;
30 use function PoweredCache\Utils\get_available_object_caches;
31 use function PoweredCache\Utils\get_cache_dir;
32 use function PoweredCache\Utils\get_timeout_with_interval;
33 use function PoweredCache\Utils\is_premium;
34 use function PoweredCache\Utils\powered_cache_flush;
35 use function PoweredCache\Utils\remove_dir;
36 use function PoweredCache\Utils\sanitize_css;
37 use const PoweredCache\Constants\UNMASK_CHARACTER_LENGTH;
38
39 // phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found
40 // phpcs:disable Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed
41 // phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment
42
43 /**
44 * Default setup routine
45 *
46 * @return void
47 */
48 function setup() {
49 if ( POWERED_CACHE_IS_NETWORK ) {
50 add_action( 'network_admin_menu', __NAMESPACE__ . '\\admin_menu' );
51 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_message' );
52 } else {
53 add_action( 'admin_menu', __NAMESPACE__ . '\\admin_menu' );
54 }
55
56 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_message' );
57
58 add_action( 'admin_init', __NAMESPACE__ . '\\process_form_submit' );
59 add_filter( 'admin_body_class', __NAMESPACE__ . '\\add_sui_admin_body_class' );
60 add_action( 'admin_bar_menu', __NAMESPACE__ . '\\admin_bar_menu', 999 );
61 add_action( 'admin_bar_menu', __NAMESPACE__ . '\\purge_all_admin_bar_menu' );
62 add_action( 'admin_post_powered_cache_purge_all_cache', __NAMESPACE__ . '\\purge_all_cache_action' );
63 add_action( 'admin_post_powered_cache_download_rewrite_settings', __NAMESPACE__ . '\\download_rewrite_config' );
64 add_action( 'wp_ajax_powered_cache_run_diagnostic', __NAMESPACE__ . '\\run_diagnostic' );
65 add_action( 'wp_ajax_powered_cache_check_alloptions', __NAMESPACE__ . '\\check_alloptions' );
66 add_action( 'admin_post_deactivate_plugin', __NAMESPACE__ . '\\deactivate_plugin' );
67 add_filter( 'plugin_action_links_' . plugin_basename( POWERED_CACHE_PLUGIN_FILE ), __NAMESPACE__ . '\\action_links' );
68 add_filter( 'network_admin_plugin_action_links_' . plugin_basename( POWERED_CACHE_PLUGIN_FILE ), __NAMESPACE__ . '\\action_links' );
69 }
70
71 /**
72 * Add required class for shared UI
73 *
74 * @param string $classes css classes for admin area
75 *
76 * @return string
77 * @see https://wpmudev.github.io/shared-ui/installation/
78 */
79 function add_sui_admin_body_class( $classes ) {
80 $classes .= ' sui-2-12-24 ';
81
82 return $classes;
83 }
84
85 /**
86 * Adds admin menu item
87 *
88 * @since 1.0
89 */
90 function admin_menu() {
91 global $powered_cache_settings_page;
92
93 $capability = 'manage_options';
94
95 if ( POWERED_CACHE_IS_NETWORK ) {
96 $capability = 'manage_network';
97 }
98
99 $powered_cache_settings_page = add_menu_page(
100 esc_html__( 'Powered Cache Settings', 'powered-cache' ),
101 esc_html__( 'Powered Cache', 'powered-cache' ),
102 $capability,
103 MENU_SLUG,
104 __NAMESPACE__ . '\settings_page',
105 ICON_BASE64
106 );
107
108 /**
109 * Different name submenu item, url point same address with parent.
110 */
111 add_submenu_page(
112 MENU_SLUG,
113 esc_html__( 'Powered Cache Settings', 'powered-cache' ),
114 esc_html__( 'Settings', 'powered-cache' ),
115 $capability,
116 MENU_SLUG
117 );
118 }
119
120 /**
121 * Main settings page of the plugin
122 */
123 function settings_page() {
124 include __DIR__ . '/partials/settings-page.php';
125 }
126
127 /**
128 * Process settings form action
129 *
130 * @since 2.0
131 */
132 function process_form_submit() {
133
134 if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) {
135 return;
136 }
137
138 if ( ! current_user_can( 'manage_options' ) ) {
139 return;
140 }
141
142 $nonce = filter_input( INPUT_POST, 'powered_cache_settings_nonce', FILTER_SANITIZE_SPECIAL_CHARS );
143 if ( wp_verify_nonce( $nonce, 'powered_cache_update_settings' ) ) {
144 $action = isset( $_POST['powered_cache_form_action'] ) ? sanitize_text_field( wp_unslash( $_POST['powered_cache_form_action'] ) ) : 'save_settings';
145 $old_options = \PoweredCache\Utils\get_settings();
146 $options = sanitize_options( $_POST );
147 $options = maybe_process_cloudflare_settings( $options );
148
149 switch ( $action ) {
150 case 'reset_settings':
151 if ( POWERED_CACHE_IS_NETWORK ) {
152 delete_site_option( SETTING_OPTION );
153 } else {
154 delete_option( SETTING_OPTION );
155 }
156
157 if ( 'off' !== $old_options['object_cache'] ) {
158 wp_cache_flush();
159 }
160
161 $options = \PoweredCache\Utils\get_settings();
162
163 break;
164 case 'export_settings':
165 $filename = sprintf( 'powered-cache-settings-%s-%s.json', gmdate( 'Y-m-d' ), uniqid() );
166 if ( POWERED_CACHE_IS_NETWORK ) {
167 $options = get_site_option( SETTING_OPTION );
168 } else {
169 $options = get_option( SETTING_OPTION );
170 }
171
172 $sensitive_options = [
173 'cloudflare_email', // PII data
174 'cloudflare_api_key',
175 'cloudflare_api_token',
176 ];
177
178 foreach ( $sensitive_options as $option_key ) {
179 if ( isset( $options[ $option_key ] ) ) {
180 $options[ $option_key ] = '';
181 }
182 }
183
184 $options = wp_json_encode( $options, JSON_PRETTY_PRINT );
185
186 nocache_headers();
187 // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
188 @header( 'Content-Type: application/json' );
189 @header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
190 @header( 'Content-Transfer-Encoding: binary' );
191 @header( 'Content-Length: ' . strlen( $options ) );
192 @header( 'Connection: close' );
193 // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
194 echo $options; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
195 exit;
196 case 'import_settings':
197 if ( $_FILES['import_file'] && ! empty( $_FILES['import_file']['tmp_name'] ) ) { // phpcs:ignore
198 $import_data = file_get_contents( $_FILES['import_file']['tmp_name'] ); // phpcs:ignore
199 $import_settings = json_decode( $import_data, true );
200 $options = sanitize_options( $import_settings );
201 }
202 break;
203 case 'enable_dev_mode':
204 $options['dev_mode'] = true;
205 break;
206 case 'disable_dev_mode':
207 $options['dev_mode'] = false;
208 break;
209 case 'save_settings_and_optimize':
210 db_optimize( $options );
211 break;
212 case 'save_settings_and_clear_cache':
213 purge_all_cache( $options );
214 break;
215 case 'save_settings':
216 default:
217 break;
218 }
219
220 if ( POWERED_CACHE_IS_NETWORK ) {
221 update_site_option( SETTING_OPTION, $options );
222 } else {
223 update_option( SETTING_OPTION, $options );
224 }
225
226 Config::factory()->save_configuration( $options, POWERED_CACHE_IS_NETWORK );
227
228 // drop object cache on backend changes
229 if ( isset( $options['object_cache'] ) && $old_options['object_cache'] !== $options['object_cache'] ) {
230 wp_cache_flush();
231 }
232
233 // Flush cache when Dev Mode is turned OFF
234 if ( ! empty( $old_options['dev_mode'] ) && empty( $options['dev_mode'] ) ) {
235 wp_cache_flush();
236 }
237
238 // maybe cancel preloading process when it turned off
239 if ( $old_options['enable_cache_preload'] && ! $options['enable_cache_preload'] ) {
240 cancel_preloading();
241 }
242
243 // start the preloading process when it is turned on
244 if ( ! $old_options['enable_cache_preload'] && $options['enable_cache_preload'] ) {
245 start_preloading();
246 }
247
248 if ( $old_options['async_cache_cleaning'] && ! $options['async_cache_cleaning'] ) {
249 cancel_async_cache_cleaning();
250 }
251
252 // cleanup existing cache on toggling cache option
253 if ( $old_options['enable_page_cache'] && ! $options['enable_page_cache'] ) {
254 clean_site_cache_dir();
255 }
256
257 // cleanup existing cache due to optimized URL changes
258 if ( $old_options['rewrite_file_optimizer'] && ! $options['rewrite_file_optimizer'] ) {
259 clean_site_cache_dir();
260 }
261
262 if ( $old_options['cache_timeout'] !== $options['cache_timeout'] ) {
263 $timestamp = wp_next_scheduled( PURGE_CACHE_CRON_NAME );
264
265 wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME );
266 }
267
268 /**
269 * Fires after saving configurations.
270 *
271 * @hook powered_cache_settings_saved
272 *
273 * @param {array} $old_options Old settings.
274 * @param {array} $options New settings.
275 *
276 * @since 1.0
277 */
278 do_action( 'powered_cache_settings_saved', $old_options, $options );
279
280 $redirect_url = wp_get_referer();
281
282 if ( empty( $redirect_url ) && isset( $_SERVER['REQUEST_URI'] ) ) {
283 $redirect_url = wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
284 }
285
286 $redirect_url = add_query_arg(
287 [
288 'pc_action' => $action,
289 ],
290 $redirect_url
291 );
292
293 wp_safe_redirect( esc_url_raw( $redirect_url ) );
294 exit;
295 }
296 }
297
298 /**
299 * Sanitize options
300 *
301 * @param array $options Raw input, most likely $_POST request
302 *
303 * @return array|mixed Sanitized options
304 */
305 function sanitize_options( $options ) {
306 $sanitized_options = [];
307
308 if ( isset( $options['object_cache'] ) && in_array( $options['object_cache'], get_available_object_caches(), true ) ) {
309 $sanitized_options['object_cache'] = sanitize_text_field( $options['object_cache'] );
310 } else {
311 $sanitized_options['object_cache'] = 'off';
312 }
313
314 $sanitized_options['enable_page_cache'] = ! empty( $options['enable_page_cache'] );
315 $sanitized_options['cache_mobile'] = ! empty( $options['cache_mobile'] );
316 $sanitized_options['cache_mobile_separate_file'] = ! empty( $options['cache_mobile_separate_file'] );
317 $sanitized_options['loggedin_user_cache'] = ! empty( $options['loggedin_user_cache'] );
318 $sanitized_options['gzip_compression'] = ! empty( $options['gzip_compression'] );
319 $sanitized_options['cache_timeout'] = absint( $options['cache_timeout'] );
320 $sanitized_options['auto_configure_htaccess'] = ! empty( $options['auto_configure_htaccess'] );
321 $sanitized_options['rewrite_file_optimizer'] = ! empty( $options['rewrite_file_optimizer'] );
322 $sanitized_options['rejected_user_agents'] = sanitize_textarea_field( $options['rejected_user_agents'] );
323 $sanitized_options['rejected_cookies'] = sanitize_textarea_field( $options['rejected_cookies'] );
324 $sanitized_options['rejected_referrers'] = sanitize_textarea_field( $options['rejected_referrers'] );
325 $sanitized_options['vary_cookies'] = sanitize_textarea_field( $options['vary_cookies'] );
326 $sanitized_options['rejected_uri'] = sanitize_textarea_field( $options['rejected_uri'] );
327 $sanitized_options['cache_query_strings'] = sanitize_textarea_field( $options['cache_query_strings'] );
328 $sanitized_options['ignored_query_strings'] = sanitize_textarea_field( $options['ignored_query_strings'] );
329 $sanitized_options['purge_additional_pages'] = sanitize_textarea_field( $options['purge_additional_pages'] );
330 $sanitized_options['minify_html'] = ! empty( $options['minify_html'] );
331 $sanitized_options['minify_html_dom_optimization'] = ! empty( $options['minify_html_dom_optimization'] );
332 $sanitized_options['combine_google_fonts'] = ! empty( $options['combine_google_fonts'] );
333 $sanitized_options['swap_google_fonts_display'] = ! empty( $options['swap_google_fonts_display'] );
334 $sanitized_options['use_bunny_fonts'] = ! empty( $options['use_bunny_fonts'] );
335 $sanitized_options['minify_css'] = ! empty( $options['minify_css'] );
336 $sanitized_options['combine_css'] = ! empty( $options['combine_css'] );
337 $sanitized_options['critical_css'] = ! empty( $options['critical_css'] );
338 $sanitized_options['critical_css_additional_files'] = sanitize_textarea_field( $options['critical_css_additional_files'] );
339 $sanitized_options['critical_css_excluded_files'] = sanitize_textarea_field( $options['critical_css_excluded_files'] );
340 $sanitized_options['excluded_css_files'] = sanitize_textarea_field( $options['excluded_css_files'] );
341 $sanitized_options['remove_unused_css'] = ! empty( $options['remove_unused_css'] );
342 $sanitized_options['ucss_safelist'] = sanitize_textarea_field( $options['ucss_safelist'] );
343 $sanitized_options['ucss_excluded_files'] = sanitize_textarea_field( $options['ucss_excluded_files'] );
344 $sanitized_options['minify_js'] = ! empty( $options['minify_js'] );
345 $sanitized_options['combine_js'] = ! empty( $options['combine_js'] );
346 $sanitized_options['excluded_js_files'] = sanitize_textarea_field( $options['excluded_js_files'] );
347 $sanitized_options['js_defer'] = ! empty( $options['js_defer'] );
348 $sanitized_options['js_defer_exclusions'] = sanitize_textarea_field( $options['js_defer_exclusions'] );
349 $sanitized_options['js_delay'] = ! empty( $options['js_delay'] );
350 $sanitized_options['js_delay_exclusions'] = sanitize_textarea_field( $options['js_delay_exclusions'] );
351 $sanitized_options['js_delay_timeout'] = absint( $options['js_delay_timeout'] );
352 $sanitized_options['enable_image_optimization'] = ! empty( $options['enable_image_optimization'] );
353 $sanitized_options['image_optimizer_preferred_format'] = isset( $options['image_optimizer_preferred_format'] ) ? sanitize_text_field( wp_unslash( $options['image_optimizer_preferred_format'] ) ) : '';
354 $sanitized_options['enable_lazy_load'] = ! empty( $options['enable_lazy_load'] );
355 $sanitized_options['lazy_load_post_content'] = ! empty( $options['lazy_load_post_content'] );
356 $sanitized_options['lazy_load_images'] = ! empty( $options['lazy_load_images'] );
357 $sanitized_options['lazy_load_iframes'] = ! empty( $options['lazy_load_iframes'] );
358 $sanitized_options['lazy_load_widgets'] = ! empty( $options['lazy_load_widgets'] );
359 $sanitized_options['lazy_load_post_thumbnail'] = ! empty( $options['lazy_load_post_thumbnail'] );
360 $sanitized_options['lazy_load_avatars'] = ! empty( $options['lazy_load_avatars'] );
361 $sanitized_options['lazy_load_youtube'] = ! empty( $options['lazy_load_youtube'] );
362 $sanitized_options['lazy_load_exclusions'] = sanitize_textarea_field( $options['lazy_load_exclusions'] );
363 $sanitized_options['lazy_load_skip_first_nth_img'] = absint( $options['lazy_load_skip_first_nth_img'] );
364 $sanitized_options['disable_wp_lazy_load'] = ! empty( $options['disable_wp_lazy_load'] );
365 $sanitized_options['add_missing_image_dimensions'] = ! empty( $options['add_missing_image_dimensions'] );
366 $sanitized_options['disable_wp_embeds'] = ! empty( $options['disable_wp_embeds'] );
367 $sanitized_options['disable_emoji_scripts'] = ! empty( $options['disable_emoji_scripts'] );
368 $sanitized_options['enable_cdn'] = ! empty( $options['enable_cdn'] );
369
370 // convert TTL in minute
371 if ( $options['cache_timeout'] > 0 && isset( $options['cache_timeout_interval'] ) ) {
372 switch ( $options['cache_timeout_interval'] ) {
373 case 'DAY':
374 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1440;
375 break;
376 case 'HOUR':
377 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 60;
378 break;
379 case 'MINUTE':
380 default:
381 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1;
382 }
383 }
384
385 $cdn_hostname = [];
386 if ( isset( $options['cdn_hostname'] ) ) {
387 foreach ( (array) $options['cdn_hostname'] as $hostname ) {
388 $hostname = trim( $hostname );
389 if ( filter_var( $hostname, FILTER_VALIDATE_URL ) ) {
390 $cdn_hostname[] = wp_parse_url( $hostname, PHP_URL_HOST );
391 } else {
392 $cdn_hostname[] = $hostname;
393 }
394 }
395 }
396
397 $cdn_zone = [];
398
399 if ( isset( $options['cdn_zone'] ) ) {
400 foreach ( (array) $options['cdn_zone'] as $zone ) {
401 $cdn_zone[] = sanitize_text_field( $zone );
402 }
403 }
404
405 if ( empty( $cdn_hostname ) ) {
406 $cdn_hostname = [ '' ];
407 }
408
409 if ( empty( $cdn_zone ) ) {
410 $cdn_zone = [ '' ];
411 }
412
413 $sanitized_options['cdn_hostname'] = $cdn_hostname;
414 $sanitized_options['cdn_zone'] = $cdn_zone;
415 $sanitized_options['cdn_rejected_files'] = sanitize_textarea_field( $options['cdn_rejected_files'] );
416 $sanitized_options['enable_cache_preload'] = ! empty( $options['enable_cache_preload'] );
417 $sanitized_options['preload_homepage'] = ! empty( $options['preload_homepage'] );
418 $sanitized_options['preload_public_posts'] = ! empty( $options['preload_public_posts'] );
419 $sanitized_options['preload_public_tax'] = ! empty( $options['preload_public_tax'] );
420 $sanitized_options['enable_sitemap_preload'] = ! empty( $options['enable_sitemap_preload'] );
421 $sanitized_options['preload_request_interval'] = absint( $options['preload_request_interval'] );
422 $sanitized_options['preload_sitemap'] = sanitize_textarea_field( $options['preload_sitemap'] );
423 $sanitized_options['prefetch_dns'] = sanitize_textarea_field( $options['prefetch_dns'] );
424 $sanitized_options['preconnect_resource'] = sanitize_textarea_field( $options['preconnect_resource'] );
425 $sanitized_options['enable_lcp_optimization'] = ! empty( $options['enable_lcp_optimization'] );
426 $sanitized_options['prefetch_links'] = ! empty( $options['prefetch_links'] );
427 $sanitized_options['db_cleanup_post_revisions'] = ! empty( $options['db_cleanup_post_revisions'] );
428 $sanitized_options['db_cleanup_auto_drafts'] = ! empty( $options['db_cleanup_auto_drafts'] );
429 $sanitized_options['db_cleanup_trashed_posts'] = ! empty( $options['db_cleanup_trashed_posts'] );
430 $sanitized_options['db_cleanup_spam_comments'] = ! empty( $options['db_cleanup_spam_comments'] );
431 $sanitized_options['db_cleanup_trashed_comments'] = ! empty( $options['db_cleanup_trashed_comments'] );
432 $sanitized_options['db_cleanup_expired_transients'] = ! empty( $options['db_cleanup_expired_transients'] );
433 $sanitized_options['db_cleanup_all_transients'] = ! empty( $options['db_cleanup_all_transients'] );
434 $sanitized_options['db_cleanup_optimize_tables'] = ! empty( $options['db_cleanup_optimize_tables'] );
435 $sanitized_options['enable_scheduled_db_cleanup'] = ! empty( $options['enable_scheduled_db_cleanup'] );
436 $sanitized_options['scheduled_db_cleanup_frequency'] = sanitize_text_field( $options['scheduled_db_cleanup_frequency'] );
437 $sanitized_options['enable_cloudflare'] = ! empty( $options['enable_cloudflare'] );
438 $sanitized_options['cloudflare_email'] = sanitize_email( $options['cloudflare_email'] );
439 $sanitized_options['cloudflare_api_key'] = sanitize_text_field( $options['cloudflare_api_key'] );
440 $sanitized_options['cloudflare_api_token'] = sanitize_text_field( $options['cloudflare_api_token'] );
441 $sanitized_options['cloudflare_zone'] = sanitize_text_field( $options['cloudflare_zone'] );
442 $sanitized_options['enable_heartbeat'] = ! empty( $options['enable_heartbeat'] );
443 $sanitized_options['heartbeat_dashboard_status'] = sanitize_text_field( $options['heartbeat_dashboard_status'] );
444 $sanitized_options['heartbeat_editor_status'] = sanitize_text_field( $options['heartbeat_editor_status'] );
445 $sanitized_options['heartbeat_frontend_status'] = sanitize_text_field( $options['heartbeat_frontend_status'] );
446 $sanitized_options['heartbeat_dashboard_interval'] = absint( $options['heartbeat_dashboard_interval'] );
447 $sanitized_options['heartbeat_editor_interval'] = absint( $options['heartbeat_editor_interval'] );
448 $sanitized_options['heartbeat_frontend_interval'] = absint( $options['heartbeat_frontend_interval'] );
449 $sanitized_options['enable_varnish'] = ! empty( $options['enable_varnish'] );
450 $sanitized_options['varnish_ip'] = sanitize_text_field( $options['varnish_ip'] );
451 $sanitized_options['cache_footprint'] = ! empty( $options['cache_footprint'] );
452 $sanitized_options['async_cache_cleaning'] = ! empty( $options['async_cache_cleaning'] );
453 $sanitized_options['dev_mode'] = ! empty( $options['dev_mode'] );
454 $sanitized_options['enable_google_tracking'] = ! empty( $options['enable_google_tracking'] );
455 $sanitized_options['enable_fb_tracking'] = ! empty( $options['enable_fb_tracking'] );
456
457 if ( isset( $options['critical_css_appended_content'] ) ) {
458 $sanitized_options['critical_css_appended_content'] = sanitize_css( $options['critical_css_appended_content'] );
459 }
460
461 if ( isset( $options['critical_css_fallback'] ) ) {
462 $sanitized_options['critical_css_fallback'] = sanitize_css( $options['critical_css_fallback'] );
463 }
464
465 /**
466 * Filters sanitized options.
467 *
468 * @hook powered_cache_sanitized_options
469 *
470 * @param {array} $sanitized_options Sanitized options.
471 * @param {array} $options raw input.
472 *
473 * @return {array} New value.
474 *
475 * @since 2.0
476 */
477 return apply_filters( 'powered_cache_sanitized_options', $sanitized_options, $options );
478 }
479
480 /**
481 * Add base admin bar menu
482 *
483 * @param object $wp_admin_bar Admin bar object
484 *
485 * @since 2.0
486 */
487 function admin_bar_menu( $wp_admin_bar ) {
488 $href = admin_url( 'admin.php?page=powered-cache' );
489
490 if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) {
491 $href = network_admin_url( 'admin.php?page=powered-cache' );
492 }
493
494 if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) {
495 $href = '#';
496 }
497
498 if ( current_user_can( 'manage_options' ) ) {
499 $wp_admin_bar->add_menu(
500 array(
501 'id' => MENU_SLUG,
502 'title' => __( 'Powered Cache', 'powered-cache' ),
503 'href' => $href,
504 )
505 );
506 }
507 }
508
509 /**
510 * Maybe display feedback messages when certain action is taken
511 *
512 * @since 2.0
513 */
514 function maybe_display_message() {
515 // phpcs:disable WordPress.Security.NonceVerification.Recommended
516 if ( ! isset( $_GET['pc_action'] ) ) {
517 return;
518 }
519
520 /**
521 * Dont display multiple message when saving the options while having the query_params
522 */
523 if ( ! empty( $_POST ) ) {
524 return;
525 }
526
527 $screen = get_current_screen();
528
529 $success_messages = [
530 'purge_image_optimizer_cache' => esc_html__( 'Image optimizer cache purged successfully!', 'powered-cache' ),
531 'flush_page_cache_network' => esc_html__( 'Page cache deleted for all websites!', 'powered-cache' ),
532 'flush_page_cache' => esc_html__( 'Page cache deleted successfully!', 'powered-cache' ),
533 'flush_object_cache' => esc_html__( 'Object cache deleted successfully!', 'powered-cache' ),
534 'flush_all_cache' => esc_html__( 'All cached items flushed successfully!', 'powered-cache' ),
535 'start_preload' => esc_html__( 'The cache preloading has been initialized!', 'powered-cache' ),
536 'generate_critical' => esc_html__( 'The Critical CSS generation process has been initialized!', 'powered-cache' ),
537 'generate_critical_network' => esc_html__( 'The Critical CSS generation process has been initialized for all sites! This might take a while, depending on the network size.', 'powered-cache' ),
538 'generate_ucss' => esc_html__( 'The UCSS generation process has been initialized!', 'powered-cache' ),
539 'generate_ucss_network' => esc_html__( 'The UCSS generation process has been initialized for all sites! This might take a while, depending on the network size.', 'powered-cache' ),
540 'flush_cf_cache' => esc_html__( 'Cloudflare cache flushed, it can take up to 30 seconds to delete all cache from Cloudflare!', 'powered-cache' ),
541 'reset_settings' => esc_html__( 'Settings have been reset!', 'powered-cache' ),
542 'import_settings' => esc_html__( 'Settings have been imported!', 'powered-cache' ),
543 'save_settings_and_optimize' => esc_html__( 'Settings saved and database being optimized...', 'powered-cache' ),
544 'save_settings_and_clear_cache' => esc_html__( 'Settings have been successfully saved, and all cache has been cleared.', 'powered-cache' ),
545 'save_settings' => esc_html__( 'Settings saved.', 'powered-cache' ),
546 ];
547
548 if ( isset( $_GET['language'] ) ) {
549 $success_messages['flush_lang_cache'] = sprintf( esc_html__( 'Page cache for %s language has been deleted!', 'powered-cache' ), esc_attr( urldecode_deep( $_GET['language'] ) ) ); // phpcs:ignore
550 }
551
552 $err_messages = [
553 'purge_image_optimizer_cache_failed' => esc_html__( 'Could not purge image optimizer cache. Please try again later and ensure your license key is activated!', 'powered-cache' ),
554 'generic_permission_err' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
555 'flush_page_cache_network_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
556 'flush_page_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
557 'flush_object_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
558 'flush_all_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
559 'start_preload_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
560 'start_critical_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
561 'start_ucss_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
562 'start_critical_err_license' => esc_html__( 'Your license key does not seem valid. A valid license is required for the Critical CSS!', 'powered-cache' ),
563 'start_ucss_err_license' => esc_html__( 'Your license key does not seem valid. A valid license is required for removing unused CSS!', 'powered-cache' ),
564 'flush_cf_cache_failed' => esc_html__( 'Could not flush Cloudflare cache. Please make sure you entered the correct credentials and zone id!', 'powered-cache' ),
565 ];
566
567 if ( isset( $success_messages[ $_GET['pc_action'] ] ) ) {
568 if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
569 add_settings_error( $screen->parent_file, MENU_SLUG, $success_messages[ $_GET['pc_action'] ], 'success' ); // phpcs:ignore
570
571 return;
572 }
573
574 printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', $success_messages[ $_GET['pc_action'] ] ); // phpcs:ignore
575 }
576
577 if ( isset( $err_messages[ $_GET['pc_action'] ] ) ) {
578 if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
579 add_settings_error( $screen->parent_file, MENU_SLUG, $err_messages[ $_GET['pc_action'] ], 'error' ); // phpcs:ignore
580
581 return;
582 }
583
584 printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', $err_messages[ $_GET['pc_action'] ] ); // phpcs:ignore
585 }
586 // phpcs:enable WordPress.Security.NonceVerification.Recommended
587 }
588
589
590 /**
591 * Adds `Purge All Cache` menu bar item
592 *
593 * @param object $wp_admin_bar Admin bar object
594 *
595 * @since 1.1
596 */
597 function purge_all_admin_bar_menu( $wp_admin_bar ) {
598 // Only available for the network admins on multisite.
599 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
600 return;
601 }
602
603 $wp_admin_bar->add_menu(
604 array(
605 'id' => 'all-cache-purge',
606 'title' => __( 'Purge All Cache', 'powered-cache' ),
607 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ),
608 'parent' => 'powered-cache',
609 )
610 );
611 }
612
613 /**
614 * Purges all cache related things
615 *
616 * @since 1.1
617 */
618 function purge_all_cache_action() {
619
620 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
621 if ( ! wp_verify_nonce( $nonce, 'powered_cache_purge_all_cache' ) ) {
622 wp_nonce_ays( '' );
623 }
624
625 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
626 $redirect_url = add_query_arg( 'pc_action', 'flush_all_cache_err_permission', wp_get_referer() );
627 wp_safe_redirect( esc_url_raw( $redirect_url ) );
628 exit;
629 }
630
631 if ( ! current_user_can( 'manage_options' ) ) {
632 $redirect_url = add_query_arg( 'pc_action', 'flush_all_cache_err_permission', wp_get_referer() );
633 wp_safe_redirect( esc_url_raw( $redirect_url ) );
634 exit;
635 }
636
637 purge_all_cache();
638
639 $redirect_url = add_query_arg( 'pc_action', 'flush_all_cache', wp_get_referer() );
640
641 wp_safe_redirect( esc_url_raw( $redirect_url ) );
642 exit;
643 }
644
645
646 /**
647 * Purge all cache
648 *
649 * @param array $settings plugin settings
650 *
651 * @return void
652 */
653 function purge_all_cache( $settings = array() ) {
654 $cache_purger = CachePurger::factory();
655 if ( empty( $settings ) ) {
656 $settings = \PoweredCache\Utils\get_settings();
657 }
658
659 if ( $settings['async_cache_cleaning'] ) {
660 if ( function_exists( 'wp_cache_flush' ) ) {
661 wp_cache_flush();
662 }
663 $cache_purger->push_to_queue( [ 'call' => 'powered_cache_flush' ] );
664 $cache_purger->save()->dispatch();
665 } else {
666 powered_cache_flush();// cleans object cache + page cache dir
667 }
668
669 /**
670 * Fires after purging all cache
671 *
672 * @hook powered_cache_purge_all_cache
673 * @since 1.1
674 */
675 do_action( 'powered_cache_purge_all_cache' );
676
677 if ( POWERED_CACHE_IS_NETWORK ) {
678 delete_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT );
679 } else {
680 delete_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT );
681 }
682 }
683
684
685 /**
686 * Downloads proper configuration file
687 *
688 * @since 1.1
689 */
690 function download_rewrite_config() {
691 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
692 if ( ! wp_verify_nonce( $nonce, 'powered_cache_download_rewrite' ) ) {
693 wp_nonce_ays( '' );
694 }
695
696 if ( ! can_control_all_settings() ) {
697 $redirect_url = add_query_arg( 'pc_action', 'generic_permission_err', wp_get_referer() );
698 wp_safe_redirect( esc_url_raw( $redirect_url ) );
699 exit;
700 }
701
702 if ( ! empty( $_GET['server'] ) ) {
703 $server = sanitize_text_field( wp_unslash( $_GET['server'] ) );
704 Config::factory()->download_rewrite_rules( $server );
705 }
706
707 wp_safe_redirect( wp_get_referer() );
708 die();
709 }
710
711 /**
712 * Run DB optimization
713 *
714 * @param array $options plugin settings
715 */
716 function db_optimize( $options ) {
717 $powered_cache_db_optimizer = DatabaseOptimizer::factory();
718
719 $supported_options = $powered_cache_db_optimizer->get_supported_options();
720
721 if ( POWERED_CACHE_IS_NETWORK ) {
722 $sites = get_sites();
723 foreach ( $sites as $site ) {
724 switch_to_blog( $site->blog_id );
725 foreach ( $supported_options as $optimization_item ) {
726 if ( $options[ $optimization_item ] ) {
727 $powered_cache_db_optimizer->push_to_queue( $optimization_item );
728 }
729 }
730
731 $powered_cache_db_optimizer->save()->dispatch();
732 restore_current_blog();
733 }
734 } else {
735 foreach ( $supported_options as $optimization_item ) {
736 if ( $options[ $optimization_item ] ) {
737 $powered_cache_db_optimizer->push_to_queue( $optimization_item );
738 }
739 }
740
741 $powered_cache_db_optimizer->save()->dispatch();
742 }
743
744 }
745
746 /**
747 * Cancel preloading process on toggling preload option
748 */
749 function cancel_preloading() {
750 \PoweredCache\Utils\log( 'Cancel preload process - Settings toggle' );
751 $cache_preloader = CachePreloader::factory();
752 $cache_preloader->cancel_process();
753 $cache_preloader->delete_all();
754 }
755
756 /**
757 * Kick-start preloading process
758 *
759 * @return void
760 */
761 function start_preloading() {
762 \PoweredCache\Utils\log( 'Enable Preloader - Settings toggle' );
763 Preloader::factory()->setup_preload_queue();
764 // kickstart the preloading process
765 Preloader::factory()->dispatch_preload_queue();
766 }
767
768 /**
769 * Cancel async cache purging processes
770 *
771 * @since 2.3
772 */
773 function cancel_async_cache_cleaning() {
774 \PoweredCache\Utils\log( 'Cancel CachePurger process' );
775 $cache_preloader = CachePurger::factory();
776 $cache_preloader->cancel_process();
777 }
778
779
780 /**
781 * Perform diagnostic checks
782 */
783 function run_diagnostic() {
784 global $is_apache;
785
786 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
787
788 if ( wp_verify_nonce( $nonce, 'powered_cache_run_diagnostic' ) ) {
789 $settings = \PoweredCache\Utils\get_settings();
790 $checks = array();
791
792 // check config file
793 $config_file = Config::factory()->find_wp_config_file();
794 $config_file_status = is_writeable( $config_file );
795
796 if ( $config_file_status ) {
797 $config_file_desc = esc_html__( 'wp-config.php is writable.', 'powered-cache' );
798 } else {
799 $config_file_desc = sprintf( __( 'wp-config.php is not writable. Please make sure the file writable or you can manually define %s constant.', 'powered-cache' ), '<code>WP_CACHE</code>' );
800 }
801
802 $checks[] = array(
803 'check' => 'config',
804 'status' => $config_file_status,
805 'description' => $config_file_desc,
806 );
807
808 // check cache directory
809 $cache_dir = get_cache_dir();
810 $cache_dir_status = false;
811 if ( ! file_exists( $cache_dir ) ) {
812 $cache_dir_desc = sprintf( __( 'Cache directory %s is not exist!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
813 } elseif ( ! is_writeable( $cache_dir ) ) {
814 $cache_dir_desc = sprintf( __( 'Cache directory %s is not writeable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
815 } else {
816 $cache_dir_status = true;
817 $cache_dir_desc = sprintf( __( 'Cache directory %s exist and writable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
818 }
819
820 $checks[] = array(
821 'check' => 'cache-dir',
822 'status' => $cache_dir_status,
823 'description' => $cache_dir_desc,
824 );
825
826 // check .htaccess file
827 if ( $is_apache && $settings['auto_configure_htaccess'] ) {
828 $htaccess_file = get_home_path() . '.htaccess';
829 $htaccess_file_status = false;
830 if ( ! file_exists( $htaccess_file ) ) {
831 $htaccess_file_desc = sprintf( __( '.htaccess file %s is not exist!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
832 } elseif ( ! is_writeable( $htaccess_file ) ) {
833 $htaccess_file_desc = sprintf( __( '.htaccess file %s is not writeable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
834 } else {
835 $htaccess_file_status = true;
836 $htaccess_file_desc = sprintf( __( '.htaccess file %s exist and writable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
837 }
838
839 $checks[] = array(
840 'check' => 'htaccess',
841 'status' => $htaccess_file_status,
842 'description' => $htaccess_file_desc,
843 );
844 }
845
846 // check page cache
847 if ( $settings['enable_page_cache'] ) {
848 $advanced_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
849 $advanced_cache_file_status = false;
850 if ( ! file_exists( $advanced_cache_file ) ) {
851 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not exist!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
852 } elseif ( ! is_writeable( $advanced_cache_file ) ) {
853 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not writeable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
854 } else {
855 $advanced_cache_file_status = true;
856 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s exist and writable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
857 }
858
859 $checks[] = array(
860 'check' => 'advanced-cache',
861 'status' => $advanced_cache_file_status,
862 'description' => $advanced_cache_file_desc,
863 );
864 }
865
866 // check object cache
867 if ( 'off' !== $settings['object_cache'] ) {
868 $object_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
869 $object_cache_file_status = false;
870 if ( ! file_exists( $object_cache_file ) ) {
871 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not exist!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
872 } elseif ( ! is_writeable( $object_cache_file ) ) {
873 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not writeable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
874 } else {
875 $object_cache_file_status = true;
876 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s exist and writable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
877 }
878
879 $checks[] = array(
880 'check' => 'object-cache',
881 'status' => $object_cache_file_status,
882 'description' => $object_cache_file_desc,
883 );
884 }
885
886 wp_send_json_success( $checks );
887 }
888
889 wp_send_json_error( [ esc_html__( 'Invalid request', 'powered-cache' ) ] );
890 }
891
892 /**
893 * Check alloptions size like performance lab does.
894 * It's critical to know this before enabling memcached based persistent object cache backends.
895 * When alloptions size is too big (1mb in this case), it will cause performance degradation instead of improvement.
896 *
897 * @return void
898 * @since 3.4
899 */
900 function check_alloptions() {
901 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
902 if ( ! wp_verify_nonce( $nonce, 'powered_cache_update_settings' ) || ! can_configure_object_cache() ) {
903 wp_send_json_error( [ 'message' => esc_html__( 'Invalid request', 'powered-cache' ) ] );
904
905 return;
906 }
907
908 $autoload_option_size = \PoweredCache\Utils\autoloaded_options_size();
909 $autoload_option_count = count( wp_load_alloptions() );
910
911 if ( $autoload_option_size > ALLOPTIONS_CRITICAL_THRESHOLD ) {
912 $status = 'critical';
913 } elseif ( $autoload_option_size > ALLOPTIONS_WARNING_THRESHOLD && $autoload_option_size <= ALLOPTIONS_CRITICAL_THRESHOLD ) {
914 $status = 'warning';
915 } else {
916 $status = 'good';
917 }
918
919 $message = '<p><b>' . esc_html__( 'Autoloaded options could affect performance:', 'powered-cache' ) . '</b> ' .
920 sprintf(
921 esc_html__(
922 /* translators: 1: Number of autoloaded options. 2: Size of autoloaded options. */
923 'Your site has %1$s autoloaded options (size: %2$s) in the options table, which could cause your site to be slow. You can reduce the number of autoloaded options by cleaning up your site\'s options table.',
924 'powered-cache'
925 ),
926 esc_html( number_format_i18n( $autoload_option_count ) ),
927 esc_html( size_format( $autoload_option_size ) )
928 ) . '</p>';
929
930 wp_send_json_success(
931 [
932 'status' => $status,
933 'message' => $message,
934 ]
935 );
936 }
937
938
939 /**
940 * Deactivate incompatible plugins
941 *
942 * @since 1.0
943 */
944 function deactivate_plugin() {
945 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
946 if ( ! wp_verify_nonce( $nonce, 'deactivate_plugin' ) ) {
947 wp_nonce_ays( '' );
948 }
949
950 if ( ! current_user_can( 'activate_plugins' ) ) {
951 $redirect_url = add_query_arg( 'pc_action', 'generic_permission_err', wp_get_referer() );
952 wp_safe_redirect( esc_url_raw( $redirect_url ) );
953 exit;
954 }
955
956 if ( isset( $_GET['plugin'] ) ) {
957 $plugin = sanitize_text_field( wp_unslash( $_GET['plugin'] ) );
958 deactivate_plugins( $plugin );
959 }
960
961 wp_safe_redirect( wp_get_referer() );
962 die();
963 }
964
965 /**
966 * Adds settings link to plugin actions
967 *
968 * @param array $actions Plugin actions.
969 *
970 * @return array
971 * @since 1.0
972 */
973 function action_links( $actions ) {
974
975 $settings_url = POWERED_CACHE_IS_NETWORK ? network_admin_url( 'admin.php?page=powered-cache' ) : admin_url( 'admin.php?page=powered-cache' );
976 $powered_cache_url = 'https://poweredcache.com/?utm_source=wp_admin&utm_medium=plugin&utm_campaign=plugin_action_links';
977
978 $actions['powered_settings'] = sprintf( '<a href="%s">%s</a>', esc_url( $settings_url ), esc_html__( 'Settings', 'powered-cache' ) );
979
980 if ( ! is_premium() ) {
981 $actions['get_premium'] = sprintf( '<a href="%s" style="color: red;">%s</a>', esc_url( $powered_cache_url ), esc_html__( 'Get Premium', 'powered-cache' ) );
982 }
983
984 return array_reverse( $actions );
985 }
986
987 /**
988 * Conditionally process Cloudflare API key and token settings based on the form input.
989 *
990 * @param array $options The form options submitted by the user.
991 *
992 * @return array Updated options with processed Cloudflare settings.
993 */
994 function maybe_process_cloudflare_settings( $options ) {
995 // Retrieve the previous Cloudflare API key and token values.
996 $prev_cf_api_key = \PoweredCache\Extensions\Cloudflare\Cloudflare::get_cf_api_key();
997 $prev_cf_api_token = \PoweredCache\Extensions\Cloudflare\Cloudflare::get_cf_api_token();
998
999 // Check if the submitted Cloudflare API key matches the masked version of the previous key.
1000 if ( isset( $options['cloudflare_api_key'] ) && mask_string( $prev_cf_api_key, UNMASK_CHARACTER_LENGTH ) === $options['cloudflare_api_key'] ) {
1001 $options['cloudflare_api_key'] = $prev_cf_api_key; // Use the unmasked value.
1002 }
1003
1004 if ( isset( $options['cloudflare_api_token'] ) && mask_string( $prev_cf_api_token, UNMASK_CHARACTER_LENGTH ) === $options['cloudflare_api_token'] ) {
1005 $options['cloudflare_api_token'] = $prev_cf_api_token; // Use the unmasked value.
1006 }
1007
1008 $encryption = new Encryption();
1009 // Encrypt sensitive data if it's not empty.
1010 if ( ! empty( $options['cloudflare_api_key'] ) ) {
1011 $options['cloudflare_api_key'] = $encryption->encrypt( $options['cloudflare_api_key'] );
1012 }
1013
1014 if ( ! empty( $options['cloudflare_api_token'] ) ) {
1015 $options['cloudflare_api_token'] = $encryption->encrypt( $options['cloudflare_api_token'] );
1016 }
1017
1018 // Override with empty values if constants are defined.
1019 if ( defined( 'POWERED_CACHE_CF_API_KEY' ) && POWERED_CACHE_CF_API_KEY ) {
1020 $options['cloudflare_api_key'] = '';
1021 }
1022
1023 if ( defined( 'POWERED_CACHE_CF_API_TOKEN' ) && POWERED_CACHE_CF_API_TOKEN ) {
1024 $options['cloudflare_api_token'] = '';
1025 }
1026
1027 return $options;
1028 }
1029