PluginProbe ʕ •ᴥ•ʔ
Clear Cache Everywhere / trunk
Clear Cache Everywhere vtrunk
1.2.2.1 trunk 1.1.0 1.1.1 1.2.0 1.2.1 1.2.1.1 1.2.2
clear-cache-everywhere / inc / settings.php
clear-cache-everywhere / inc Last commit date
css 4 months ago js 4 months ago admin-bar.php 4 months ago clear-cache.php 3 weeks ago common.php 8 months ago index.php 1 year ago settings.php 5 hours ago
settings.php
883 lines
1 <?php
2 /**
3 * Plugin settings
4 */
5
6
7 /**
8 * Define Namespaces
9 */
10 namespace Apos37\ClearCache;
11 use Apos37\ClearCache\Clear;
12
13
14 /**
15 * Exit if accessed directly.
16 */
17 if ( !defined( 'ABSPATH' ) ) exit;
18
19
20 /**
21 * Instantiate the class
22 */
23 add_action( 'init', function() {
24 (new Settings())->init();
25 } );
26
27
28 /**
29 * The class
30 */
31 class Settings {
32
33 /**
34 * Nonce
35 *
36 * @var string
37 */
38 private $nonce = 'cceverywhere_nonce';
39
40
41 /**
42 * Load on init
43 */
44 public function init() {
45
46 // Submenu
47 add_action( 'admin_menu', [ $this, 'submenu' ] );
48
49 // Settings fields
50 add_action( 'admin_init', [ $this, 'settings_fields' ] );
51
52 // JQuery and CSS
53 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
54
55 } // End init()
56
57
58 /**
59 * Submenu
60 *
61 * @return void
62 */
63 public function submenu() {
64 add_submenu_page(
65 'tools.php',
66 CCEVERYWHERE_NAME . '' . __( 'Settings', 'clear-cache-everywhere' ),
67 CCEVERYWHERE_NAME,
68 'manage_options',
69 CCEVERYWHERE__TEXTDOMAIN,
70 [ $this, 'page' ]
71 );
72 } // End submenu()
73
74
75 /**
76 * The page
77 *
78 * @return void
79 */
80 public function page() {
81 global $current_screen;
82 if ( $current_screen->id != CCEVERYWHERE_SETTINGS_SCREEN_ID ) {
83 return;
84 }
85 ?>
86 <div class="wrap">
87 <h1><?php echo esc_attr( get_admin_page_title() ) ?></h1>
88
89 <!-- Clear Cache Button -->
90 <br><br>
91 <button id="cce-clear-cache-btn" class="button button-secondary cce-clear-cache-btn">
92 <?php esc_html_e( 'Clear Cache Now', 'clear-cache-everywhere' ); ?>
93 </button>
94
95 <!-- Result Container -->
96 <?php
97 $last_results = get_option( 'clear_cache_everywhere_last_results', [] );
98 $total_elapsed = 0;
99
100 $clearing_actions = ( new Clear() )->get_clearing_actions();
101
102 if ( ! empty( $last_results ) && ! empty( $clearing_actions ) ) {
103 foreach ( $clearing_actions as $action ) {
104 if ( ! empty( $action[ 'enabled' ] ) ) {
105 $key = $action[ 'key' ];
106 if ( isset( $last_results[ $key ] ) ) {
107 $res = $last_results[ $key ];
108 if ( ! empty( $res[ 'start' ] ) && ! empty( $res[ 'end' ] ) ) {
109 $total_elapsed += $res[ 'end' ] - $res[ 'start' ];
110 }
111 }
112 }
113 }
114 }
115 ?>
116 <div id="cce-clear-cache-result">
117 <?php if ( $total_elapsed > 0 ) : ?>
118 <?php
119 echo sprintf(
120 /* translators: %s is total elapsed seconds for clearing actions only */
121 __( 'Total time for clearing all enabled options the last time they were cleared: <strong>%s seconds</strong>.<br><em>Note: This does not include page reload or any recaching by the site or plugins afterwards.</em>', 'clear-cache-everywhere' ),
122 number_format( $total_elapsed, 3 )
123 );
124 ?>
125 <?php else : ?>
126 <?php
127 echo __( 'No enabled clearing actions have been run. You may clear all using the button above, or individually using the buttons below.', 'clear-cache-everywhere' );
128 ?>
129 <?php endif; ?>
130 </div>
131
132 <!-- Settings Form -->
133 <br><br>
134 <h2><?php esc_html_e( 'Choose below which items you want to clear with the Clear Cache Now button and Admin Bar link.', 'clear-cache-everywhere' ); ?></h2>
135 <form method="post" action="options.php">
136 <?php
137 settings_fields( CCEVERYWHERE_TEXTDOMAIN );
138 do_settings_sections( CCEVERYWHERE_TEXTDOMAIN );
139 ?><br><br><?php
140 submit_button();
141 ?>
142 </form>
143 </div>
144 <?php
145 } // End page()
146
147
148 /**
149 * Store the settings fields here
150 *
151 * @return array
152 */
153 public function get_settings_fields( $return_keys_only = false ) {
154 // Defaults and Hosting
155 $fields = [
156 [
157 'key' => 'rewrite_rules',
158 'title' => __( 'Rewrite Rules', 'clear-cache-everywhere' ),
159 'type' => 'checkbox',
160 'sanitize' => 'sanitize_checkbox',
161 'section' => 'defaults',
162 'default' => TRUE,
163 'run_context' => 'ajax',
164 'comments' => sprintf(
165 /* translators: %s: permalink settings page URL */
166 __( 'Flushes WordPress rewrite rules cache. Sometimes this doesn’t fully clear; you can also resave the <a href="%s" target="_blank" rel="noopener noreferrer">Permalinks settings</a> page.', 'clear-cache-everywhere' ),
167 esc_url( admin_url( 'options-permalink.php' ) )
168 ),
169 ],
170 [
171 'key' => 'wp_cache_flush',
172 'title' => __( 'WordPress Object Cache', 'clear-cache-everywhere' ),
173 'type' => 'checkbox',
174 'sanitize' => 'sanitize_checkbox',
175 'section' => 'defaults',
176 'default' => TRUE,
177 'run_context' => 'ajax',
178 'comments' => __( 'Flushes the WordPress object cache.', 'clear-cache-everywhere' ),
179 ],
180 [
181 'key' => 'transients',
182 'title' => __( 'Transients', 'clear-cache-everywhere' ),
183 'type' => 'checkbox',
184 'sanitize' => 'sanitize_checkbox',
185 'section' => 'defaults',
186 'default' => FALSE,
187 'run_context' => 'ajax',
188 'comments' => __( 'Clears all expired and active transients. This is usually not needed if you are trying to see updates on a page. This may impact site performance temporarily as themes and plugins rebuild their caches.', 'clear-cache-everywhere' ),
189 ],
190 [
191 'key' => 'opcache_reset',
192 'title' => __( 'OPcache Reset', 'clear-cache-everywhere' ),
193 'type' => 'checkbox',
194 'sanitize' => 'sanitize_checkbox',
195 'section' => 'defaults',
196 'default' => FALSE,
197 'run_context' => 'ajax',
198 'comments' => __( 'Resets PHP OPcache to clear cached scripts. May take a few seconds on large sites.', 'clear-cache-everywhere' ),
199 ],
200 [
201 'key' => 'apcu',
202 'title' => __( 'APCu Cache', 'clear-cache-everywhere' ),
203 'type' => 'checkbox',
204 'sanitize' => 'sanitize_checkbox',
205 'section' => 'defaults',
206 'default' => FALSE,
207 'run_context' => 'ajax',
208 'comments' => __( 'Clears the APCu user cache. Separate from OPcache and commonly available on shared and managed hosting.', 'clear-cache-everywhere' ),
209 ],
210 [
211 'key' => 'varnish',
212 'title' => __( 'Varnish Cache', 'clear-cache-everywhere' ),
213 'type' => 'checkbox',
214 'sanitize' => 'sanitize_checkbox',
215 'section' => 'defaults',
216 'default' => FALSE,
217 'run_context' => 'ajax',
218 'comments' => __( 'Purges the Varnish cache if detected. Network requests may take a few seconds to propagate.', 'clear-cache-everywhere' ),
219 ],
220 [
221 'key' => 'redis_memcached',
222 'title' => __( 'Redis/Memcached', 'clear-cache-everywhere' ),
223 'type' => 'checkbox',
224 'sanitize' => 'sanitize_checkbox',
225 'section' => 'defaults',
226 'default' => FALSE,
227 'run_context' => 'ajax',
228 'comments' => __( 'Flushes Redis or Memcached object cache. Large caches may take several seconds.', 'clear-cache-everywhere' ),
229 ],
230 [
231 'key' => 'fragment_cache',
232 'title' => __( 'Fragment Cache', 'clear-cache-everywhere' ),
233 'type' => 'checkbox',
234 'sanitize' => 'sanitize_checkbox',
235 'section' => 'defaults',
236 'default' => TRUE,
237 'run_context' => 'ajax',
238 'comments' => __( 'Clears any fragment cache used by themes or plugins. May take a few seconds on large sites.', 'clear-cache-everywhere' ),
239 ],
240 [
241 'key' => 'rest_api_cache',
242 'title' => __( 'REST API Cache', 'clear-cache-everywhere' ),
243 'type' => 'checkbox',
244 'sanitize' => 'sanitize_checkbox',
245 'section' => 'defaults',
246 'default' => TRUE,
247 'run_context' => 'ajax',
248 'comments' => __( 'Clears cached REST API responses.', 'clear-cache-everywhere' ),
249 ],
250 [
251 'key' => 'sessions',
252 'title' => __( 'Sessions', 'clear-cache-everywhere' ),
253 'type' => 'checkbox',
254 'sanitize' => 'sanitize_checkbox',
255 'section' => 'defaults',
256 'default' => FALSE,
257 'run_context' => 'page',
258 'comments' => __( 'Clears active user sessions; users may need to log in again.', 'clear-cache-everywhere' ),
259 ],
260 [
261 'key' => 'cookies',
262 'title' => __( 'Cookies', 'clear-cache-everywhere' ),
263 'type' => 'checkbox',
264 'sanitize' => 'sanitize_checkbox',
265 'section' => 'defaults',
266 'default' => TRUE,
267 'run_context' => 'page',
268 'comments' => __( 'Clears browser cookies related to the site.', 'clear-cache-everywhere' ),
269 ],
270 [
271 'key' => 'browser_cache',
272 'title' => __( 'Browser Cache', 'clear-cache-everywhere' ),
273 'type' => 'checkbox',
274 'sanitize' => 'sanitize_checkbox',
275 'section' => 'defaults',
276 'default' => TRUE,
277 'run_context' => 'page',
278 'comments' => __( 'Clears cached resources stored in the user’s browser.', 'clear-cache-everywhere' ),
279 ],
280 [
281 'key' => 'hosting_cache',
282 'title' => __( 'Hosting Cache', 'clear-cache-everywhere' ),
283 'type' => 'checkbox',
284 'sanitize' => 'sanitize_checkbox',
285 'section' => 'hosting',
286 'default' => FALSE,
287 'run_context' => 'ajax',
288 'comments' => __( 'Clears caching handled by your hosting provider.', 'clear-cache-everywhere' ),
289 ],
290 [
291 'key' => 'hosting_purge_url',
292 'title' => __( 'Hosting Purge URL', 'clear-cache-everywhere' ),
293 'type' => 'text',
294 'sanitize' => 'sanitize_text_field',
295 'section' => 'hosting',
296 ],
297 [
298 'key' => 'cloudflare_cache',
299 'title' => __( 'Cloudflare Cache', 'clear-cache-everywhere' ),
300 'type' => 'checkbox',
301 'sanitize' => 'sanitize_checkbox',
302 'section' => 'hosting',
303 'default' => FALSE,
304 'run_context' => 'ajax',
305 'comments' => __( 'Clears caching handled by Cloudflare.', 'clear-cache-everywhere' ),
306 ],
307 [
308 'key' => 'cloudflare_zone_id',
309 'title' => __( 'Cloudflare Zone ID', 'clear-cache-everywhere' ),
310 'type' => 'text',
311 'sanitize' => 'sanitize_text_field',
312 'section' => 'hosting',
313 'comments' => __( 'Required to clear Cloudflare cache. Find this in your Cloudflare dashboard under Overview > API > Zone ID.', 'clear-cache-everywhere' ),
314 ],
315 [
316 'key' => 'cloudflare_api_token',
317 'title' => __( 'Cloudflare API Token', 'clear-cache-everywhere' ),
318 'type' => 'password',
319 'sanitize' => 'sanitize_text_field',
320 'section' => 'hosting',
321 'comments' => __( 'Required to clear Cloudflare cache. Find this in your Cloudflare dashboard under Overview > API > Get Your Token > Create a New Token with Permission: Zone, Cache Purge, Purge.', 'clear-cache-everywhere' ),
322 ],
323 ];
324
325 // Integrations
326 if ( is_plugin_active( 'cornerstone/cornerstone.php' ) || defined( 'CS_VERSION' ) ) {
327 $fields[] = [
328 'key' => 'cornerstone',
329 'title' => __( 'Cornerstone Cache', 'clear-cache-everywhere' ),
330 'type' => 'checkbox',
331 'sanitize' => 'sanitize_checkbox',
332 'section' => 'integrations',
333 'default' => TRUE,
334 'run_context' => 'ajax',
335 'comments' => __( 'Clears the Cornerstone page builder cache.', 'clear-cache-everywhere' ),
336 ];
337 }
338
339 if ( is_plugin_active( 'elementor/elementor.php' ) ) {
340 $fields[] = [
341 'key' => 'elementor',
342 'title' => __( 'Elementor Cache', 'clear-cache-everywhere' ),
343 'type' => 'checkbox',
344 'sanitize' => 'sanitize_checkbox',
345 'section' => 'integrations',
346 'default' => TRUE,
347 'run_context' => 'ajax',
348 'comments' => __( 'Clears Elementor page builder cache.', 'clear-cache-everywhere' ),
349 ];
350 }
351
352 if ( is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
353 $fields[] = [
354 'key' => 'wp_super_cache',
355 'title' => __( 'WP Super Cache', 'clear-cache-everywhere' ),
356 'type' => 'checkbox',
357 'sanitize' => 'sanitize_checkbox',
358 'section' => 'integrations',
359 'default' => TRUE,
360 'run_context' => 'ajax',
361 'comments' => __( 'Clears the WP Super Cache plugin cache.', 'clear-cache-everywhere' ),
362 ];
363 }
364
365 if ( is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) {
366 $fields[] = [
367 'key' => 'w3_total_cache',
368 'title' => __( 'W3 Total Cache', 'clear-cache-everywhere' ),
369 'type' => 'checkbox',
370 'sanitize' => 'sanitize_checkbox',
371 'section' => 'integrations',
372 'default' => TRUE,
373 'run_context' => 'ajax',
374 'comments' => __( 'Clears W3 Total Cache plugin caches.', 'clear-cache-everywhere' ),
375 ];
376 }
377
378 if ( is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
379 $fields[] = [
380 'key' => 'wp_rocket',
381 'title' => __( 'WP Rocket', 'clear-cache-everywhere' ),
382 'type' => 'checkbox',
383 'sanitize' => 'sanitize_checkbox',
384 'section' => 'integrations',
385 'default' => TRUE,
386 'run_context' => 'ajax',
387 'comments' => __( 'Clears WP Rocket plugin cache.', 'clear-cache-everywhere' ),
388 ];
389 }
390
391 if ( is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
392 $fields[] = [
393 'key' => 'litespeed_cache',
394 'title' => __( 'LiteSpeed Cache', 'clear-cache-everywhere' ),
395 'type' => 'checkbox',
396 'sanitize' => 'sanitize_checkbox',
397 'section' => 'integrations',
398 'default' => TRUE,
399 'run_context' => 'ajax',
400 'comments' => __( 'Clears LiteSpeed Cache plugin cache.', 'clear-cache-everywhere' ),
401 ];
402 }
403
404 if ( is_plugin_active( 'sg-cachepress/sg-cachepress.php' ) ) {
405 $fields[] = [
406 'key' => 'sg_optimizer',
407 'title' => __( 'SiteGround Optimizer', 'clear-cache-everywhere' ),
408 'type' => 'checkbox',
409 'sanitize' => 'sanitize_checkbox',
410 'section' => 'integrations',
411 'default' => TRUE,
412 'run_context' => 'ajax',
413 'comments' => __( 'Clears SiteGround Optimizer cache.', 'clear-cache-everywhere' ),
414 ];
415 }
416
417 if ( is_plugin_active( 'cloudflare/cloudflare.php' ) ) {
418 $fields[] = [
419 'key' => 'cloudflare',
420 'title' => __( 'Cloudflare Cache', 'clear-cache-everywhere' ),
421 'type' => 'checkbox',
422 'sanitize' => 'sanitize_checkbox',
423 'section' => 'integrations',
424 'default' => TRUE,
425 'run_context' => 'ajax',
426 'comments' => __( 'Purges Cloudflare cache.', 'clear-cache-everywhere' ),
427 ];
428 }
429
430 if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) {
431 $fields[] = [
432 'key' => 'autoptimize',
433 'title' => __( 'Autoptimize Cache', 'clear-cache-everywhere' ),
434 'type' => 'checkbox',
435 'sanitize' => 'sanitize_checkbox',
436 'section' => 'integrations',
437 'default' => TRUE,
438 'run_context' => 'ajax',
439 'comments' => __( 'Clears Autoptimize generated CSS/JS cache.', 'clear-cache-everywhere' ),
440 ];
441 }
442
443 if ( is_plugin_active( 'swift-performance-lite/performance.php' ) || is_plugin_active( 'swift-performance/performance.php' ) ) {
444 $fields[] = [
445 'key' => 'swift_performance',
446 'title' => __( 'Swift Performance Cache', 'clear-cache-everywhere' ),
447 'type' => 'checkbox',
448 'sanitize' => 'sanitize_checkbox',
449 'section' => 'integrations',
450 'default' => TRUE,
451 'run_context' => 'ajax',
452 'comments' => __( 'Clears Swift Performance plugin cache.', 'clear-cache-everywhere' ),
453 ];
454 }
455
456 if ( is_plugin_active( 'comet-cache/comet-cache.php' ) ) {
457 $fields[] = [
458 'key' => 'comet_cache',
459 'title' => __( 'Comet Cache', 'clear-cache-everywhere' ),
460 'type' => 'checkbox',
461 'sanitize' => 'sanitize_checkbox',
462 'section' => 'integrations',
463 'default' => TRUE,
464 'run_context' => 'ajax',
465 'comments' => __( 'Clears Comet Cache plugin cache.', 'clear-cache-everywhere' ),
466 ];
467 }
468
469 if ( is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
470 $fields[] = [
471 'key' => 'wp_fastest_cache',
472 'title' => __( 'WP Fastest Cache', 'clear-cache-everywhere' ),
473 'type' => 'checkbox',
474 'sanitize' => 'sanitize_checkbox',
475 'section' => 'integrations',
476 'default' => TRUE,
477 'run_context' => 'ajax',
478 'comments' => __( 'Clears WP Fastest Cache plugin cache.', 'clear-cache-everywhere' ),
479 ];
480 }
481
482 if ( is_plugin_active( 'hummingbird-performance/hummingbird.php' ) ) {
483 $fields[] = [
484 'key' => 'hummingbird_cache',
485 'title' => __( 'Hummingbird Cache', 'clear-cache-everywhere' ),
486 'type' => 'checkbox',
487 'sanitize' => 'sanitize_checkbox',
488 'section' => 'integrations',
489 'default' => TRUE,
490 'run_context' => 'ajax',
491 'comments' => __( 'Clears Hummingbird plugin cache.', 'clear-cache-everywhere' ),
492 ];
493 }
494
495 if ( is_plugin_active( 'nginx-helper/nginx-helper.php' ) ) {
496 $fields[] = [
497 'key' => 'nginx_helper',
498 'title' => __( 'Nginx Helper', 'clear-cache-everywhere' ),
499 'type' => 'checkbox',
500 'sanitize' => 'sanitize_checkbox',
501 'section' => 'integrations',
502 'default' => TRUE,
503 'run_context' => 'ajax',
504 'comments' => __( 'Purges cache managed by Nginx Helper plugin.', 'clear-cache-everywhere' ),
505 ];
506 }
507
508 if ( is_plugin_active( 'wp-optimize/wp-optimize.php' ) ) {
509 $fields[] = [
510 'key' => 'wp_optimize',
511 'title' => __( 'WP-Optimize Cache', 'clear-cache-everywhere' ),
512 'type' => 'checkbox',
513 'sanitize' => 'sanitize_checkbox',
514 'section' => 'integrations',
515 'default' => TRUE,
516 'run_context' => 'ajax',
517 'comments' => __( 'Clears WP-Optimize cache.', 'clear-cache-everywhere' ),
518 ];
519 }
520
521 if ( is_plugin_active( 'breeze/breeze.php' ) ) {
522 $fields[] = [
523 'key' => 'breeze',
524 'title' => __( 'Breeze Cache', 'clear-cache-everywhere' ),
525 'type' => 'checkbox',
526 'sanitize' => 'sanitize_checkbox',
527 'section' => 'integrations',
528 'default' => TRUE,
529 'run_context' => 'ajax',
530 'comments' => __( 'Clears Breeze (Cloudways) plugin cache.', 'clear-cache-everywhere' ),
531 ];
532 }
533
534 if ( defined( 'KINSTAMU_VERSION' ) ) {
535 $fields[] = [
536 'key' => 'kinsta',
537 'title' => __( 'Kinsta Cache', 'clear-cache-everywhere' ),
538 'type' => 'checkbox',
539 'sanitize' => 'sanitize_checkbox',
540 'section' => 'integrations',
541 'default' => TRUE,
542 'run_context' => 'ajax',
543 'comments' => __( 'Clears Kinsta server-level cache via the Kinsta MU plugin.', 'clear-cache-everywhere' ),
544 ];
545 }
546
547 if ( method_exists( 'wpecommon', 'purge_varnish_cache' ) ) {
548 $fields[] = [
549 'key' => 'wp_engine',
550 'title' => __( 'WP Engine Cache', 'clear-cache-everywhere' ),
551 'type' => 'checkbox',
552 'sanitize' => 'sanitize_checkbox',
553 'section' => 'integrations',
554 'default' => TRUE,
555 'run_context' => 'ajax',
556 'comments' => __( 'Clears WP Engine server-level Varnish cache.', 'clear-cache-everywhere' ),
557 ];
558 }
559
560 if ( is_plugin_active( 'nitropack-integration/nitropack.php' ) ) {
561 $fields[] = [
562 'key' => 'nitropack',
563 'title' => __( 'NitroPack Cache', 'clear-cache-everywhere' ),
564 'type' => 'checkbox',
565 'sanitize' => 'sanitize_checkbox',
566 'section' => 'integrations',
567 'default' => TRUE,
568 'run_context' => 'ajax',
569 'comments' => __( 'Clears NitroPack plugin cache.', 'clear-cache-everywhere' ),
570 ];
571 }
572
573 if ( is_plugin_active( 'pantheon-advanced-page-cache/pantheon-advanced-page-cache.php' ) ) {
574 $fields[] = [
575 'key' => 'pantheon',
576 'title' => __( 'Pantheon Edge Cache', 'clear-cache-everywhere' ),
577 'type' => 'checkbox',
578 'sanitize' => 'sanitize_checkbox',
579 'section' => 'integrations',
580 'default' => TRUE,
581 'run_context' => 'ajax',
582 'comments' => __( 'Clears Pantheon edge cache via the Pantheon Advanced Page Cache plugin.', 'clear-cache-everywhere' ),
583 ];
584 }
585
586 // Apply filter to allow developers to add custom fields
587 $fields = apply_filters( 'cceverywhere_custom_settings', $fields );
588
589 // Return
590 if ( $return_keys_only ) {
591 $field_keys = [];
592 foreach ( $fields as $field ) {
593 $field_keys[] = $field[ 'key' ];
594 }
595 return $field_keys;
596 }
597 return $fields;
598 } // End get_settings_fields()
599
600
601 /**
602 * Settings fields
603 *
604 * @return void
605 */
606 public function settings_fields() {
607 // Slug
608 $slug = CCEVERYWHERE_TEXTDOMAIN;
609
610 // Fields
611 $fields = $this->get_settings_fields( false );
612
613 /**
614 * Sections
615 */
616 $settings_sections = [
617 [ 'defaults', __( 'Defaults', 'clear-cache-everywhere' ), '' ],
618 [ 'hosting', __( 'Hosting', 'clear-cache-everywhere' ), '' ],
619 [ 'integrations', __( 'Integrations', 'clear-cache-everywhere' ), '' ],
620 [ 'custom', __( 'Custom', 'clear-cache-everywhere' ), '' ],
621 ];
622
623 // Only include sections with fields
624 $settings_sections_to_add = [];
625 foreach ( $settings_sections as $settings_section ) {
626 $section_key = $settings_section[0];
627
628 // Check if any fields exist for the section
629 $section_fields = array_filter( $fields, function( $field ) use ( $section_key ) {
630 return isset( $field[ 'section' ] ) && $field[ 'section' ] === $section_key;
631 });
632
633 // If there are fields for this section, add the section to the settings sections
634 if ( ! empty( $section_fields ) ) {
635 $settings_sections_to_add[] = $settings_section;
636 }
637 }
638
639 // Iter the filtered sections
640 foreach ( $settings_sections_to_add as $settings_section ) {
641 add_settings_section(
642 $settings_section[0],
643 $settings_section[1] . ':',
644 $settings_section[2],
645 $slug
646 );
647 }
648
649 /**
650 * Fields
651 */
652 // Iter the fields
653 foreach ( $fields as $field ) {
654 $option_name = CCEVERYWHERE__TEXTDOMAIN.'_'.$field[ 'key' ];
655 $callback = 'settings_field_'.$field[ 'type' ];
656 $args = [
657 'id' => $option_name,
658 'class' => $option_name,
659 'name' => $option_name,
660 'key' => $field[ 'key' ],
661 ];
662
663 // Add comments
664 if ( isset( $field[ 'comments' ] ) && $field[ 'comments' ] != '' ) {
665 $comments = '<br><span class="cceverywhere_action_desc">' . $field[ 'comments' ] . '</span>';
666 } else {
667 $comments = '';
668 }
669
670 // Add select options
671 if ( isset( $field[ 'options' ] ) ) {
672 $args[ 'options' ] = $field[ 'options' ];
673 }
674
675 // Add default
676 if ( isset( $field[ 'default' ] ) ) {
677 $args[ 'default' ] = $field[ 'default' ];
678 }
679
680 // Add revert
681 if ( isset( $field[ 'revert' ] ) ) {
682 $args[ 'revert' ] = $field[ 'revert' ];
683 }
684
685 // Add run context
686 if ( isset( $field[ 'run_context' ] ) ) {
687 $args[ 'run_context' ] = $field[ 'run_context' ];
688 }
689
690 // Add the field
691 register_setting( $slug, $option_name, sanitize_key( $field[ 'sanitize' ] ) );
692 add_settings_field( $option_name, $field[ 'title' ] . wp_kses_post( $comments ), [ $this, $callback ], $slug, $field[ 'section' ], $args );
693 }
694 } // End settings_fields()
695
696
697 /**
698 * Custom callback function to print text field
699 *
700 * @param array $args
701 * @return void
702 */
703 public function settings_field_text( $args ) {
704 $width = isset( $args[ 'width' ] ) ? $args[ 'width' ] : '43rem';
705 $default = isset( $args[ 'default' ] ) ? $args[ 'default' ] : '';
706 $value = get_option( $args[ 'name' ], $default );
707 if ( isset( $args[ 'revert' ] ) && $args[ 'revert' ] == true && trim( $value ) == '' ) {
708 $value = $default;
709 }
710 $comments = isset( $args[ 'comments' ] ) ? '<br><p class="description">' . $args[ 'comments' ] . '</p>' : '';
711
712 printf(
713 '<input type="text" id="%s" name="%s" value="%s" style="width: %s;" />%s',
714 esc_attr( $args[ 'id' ] ),
715 esc_attr( $args[ 'name' ] ),
716 esc_html( $value ),
717 esc_attr( $width ),
718 wp_kses_post( $comments )
719 );
720 } // settings_field_text()
721
722
723 /**
724 * Custom callback function to print password field
725 *
726 * @param array $args
727 * @return void
728 */
729 public function settings_field_password( $args ) {
730 $width = isset( $args[ 'width' ] ) ? $args[ 'width' ] : '43rem';
731 $default = isset( $args[ 'default' ] ) ? $args[ 'default' ] : '';
732 $value = get_option( $args[ 'name' ], $default );
733 if ( isset( $args[ 'revert' ] ) && $args[ 'revert' ] == true && trim( $value ) == '' ) {
734 $value = $default;
735 }
736 $comments = isset( $args[ 'comments' ] ) ? '<br><p class="description">' . $args[ 'comments' ] . '</p>' : '';
737
738 printf(
739 '<input type="password" id="%s" name="%s" value="%s" style="width: %s;" />%s',
740 esc_attr( $args[ 'id' ] ),
741 esc_attr( $args[ 'name' ] ),
742 esc_html( $value ),
743 esc_attr( $width ),
744 wp_kses_post( $comments )
745 );
746 } // settings_field_password()
747
748
749 /**
750 * Custom callback function to print checkbox field
751 *
752 * @param array $args
753 * @return void
754 */
755 public function settings_field_checkbox( $args ) {
756 $value = get_option( $args[ 'name' ] );
757 if ( false === $value && isset( $args[ 'default' ] ) ) {
758 $value = $args[ 'default' ];
759 }
760 $value = $this->sanitize_checkbox( $value );
761
762 // Output the checkbox
763 printf(
764 '<div class="cce-action-container"><input type="checkbox" id="%s" name="%s" value="1" %s/>',
765 esc_attr( $args[ 'name' ] ),
766 esc_attr( $args[ 'name' ] ),
767 checked( 1, $value, false )
768 );
769
770 // Add the "Clear" button and result container for actions with run_context
771 if ( isset( $args[ 'run_context' ] ) && isset( $args[ 'key' ] ) ) {
772 $key = $args[ 'key' ];
773
774 printf(
775 ' <button class="button button-small cce-run-action-btn" data-key="%s">%s</button>',
776 esc_attr( $key ),
777 esc_html__( 'Clear', 'clear-cache-everywhere' )
778 );
779
780 // Determine if page query params exist and match this field
781 $show_finalizing = false;
782 if ( isset( $_GET[ 'cce_run_page' ] ) && $args[ 'run_context' ] === 'page' ) {
783 $show_finalizing = true;
784 } elseif ( isset( $_GET[ 'cce_run_single_page' ] ) && $_GET[ 'cce_run_single_page' ] === '1' ) {
785 if ( isset( $_GET[ 'key' ] ) && $_GET[ 'key' ] === $key ) {
786 $show_finalizing = true;
787 }
788 }
789
790 // Result text and class
791 $result_text = '';
792 $result_class = '';
793
794 if ( $show_finalizing ) {
795 $result_text = __( 'Finalizing...', 'clear-cache-everywhere' );
796 } else {
797 $last_results = get_option( 'clear_cache_everywhere_last_results', [] );
798
799 if ( isset( $last_results[ $key ] ) ) {
800 $res = $last_results[ $key ];
801
802 if ( $res[ 'status' ] === 'running' && empty( $res[ 'end' ] ) ) {
803 $result_class = 'fail';
804 $result_text = __( 'Could not complete. Something went wrong.', 'clear-cache-everywhere' );
805 } elseif ( ! empty( $res[ 'end' ] ) ) {
806 $result_class = $res[ 'status' ] ?? 'fail';
807 $date_format = get_option( 'date_format' );
808 $time_format = get_option( 'time_format' );
809
810 $end_ts = (int) floor( $res[ 'end' ] );
811
812 $datetime = wp_date( $date_format . ' \a\t ' . $time_format, $end_ts );
813
814 $label = $res[ 'status' ] === 'success'
815 ? __( 'Last cleared on %s', 'clear-cache-everywhere' )
816 : __( 'Last attempted on %s', 'clear-cache-everywhere' );
817
818 $result_text = sprintf( $label, $datetime );
819
820 // append elapsed seconds if start is available
821 if ( ! empty( $res[ 'start' ] ) ) {
822 $elapsed = $res[ 'end' ] - $res[ 'start' ];
823 $result_text .= ' (' . number_format( $elapsed, 3 ) . 's)';
824 }
825
826 if ( $res[ 'status' ] !== 'info' ) {
827 $result_text .= ' - ' . strtoupper( $res[ 'status' ] );
828 }
829
830 if ( ! empty( $res[ 'error_message' ] ) ) {
831 $result_text .= ' - ' . $res[ 'error_message' ];
832 }
833 }
834 }
835 }
836
837 // Run context class
838 $run_context_class = 'run-context-' . esc_attr( $args[ 'run_context' ] );
839 $enabled_class = $value ? 'enabled' : 'disabled';
840
841 // Result container
842 printf(
843 '<div class="cce-action-result %s %s %s" id="cce-action-result-%s">%s</div>',
844 esc_attr( $result_class ),
845 esc_attr( $run_context_class ),
846 esc_attr( $enabled_class ),
847 esc_attr( $key ),
848 esc_html( $result_text )
849 );
850 }
851
852 echo '</div>';
853 } // End settings_field_checkbox()
854
855
856 /**
857 * Sanitize checkbox
858 *
859 * @param int $value
860 * @return boolean
861 */
862 public function sanitize_checkbox( $value ) {
863 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
864 } // End sanitize_checkbox()
865
866
867 /**
868 * Enqueue javascript
869 *
870 * @return void
871 */
872 public function enqueue_scripts( $hook ) {
873 // Check if we are on the correct admin page
874 if ( $hook !== CCEVERYWHERE_SETTINGS_SCREEN_ID ) {
875 return;
876 }
877
878 // CSS
879 wp_enqueue_style( CCEVERYWHERE_TEXTDOMAIN . '-settings', CCEVERYWHERE_CSS_PATH . 'settings.css', [], CCEVERYWHERE_SCRIPT_VERSION );
880 } // End enqueue_scripts()
881
882 }
883