PluginProbe ʕ •ᴥ•ʔ
Clear Cache Everywhere / 1.1.1
Clear Cache Everywhere v1.1.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 8 months ago js 8 months ago admin-bar.php 8 months ago clear-cache.php 8 months ago common.php 8 months ago index.php 8 months ago settings.php 8 months ago
settings.php
541 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 <?php
92 $clear_cache_url = add_query_arg( [
93 (new Clear())->action_param => 1,
94 '_wpnonce' => wp_create_nonce( $this->nonce )
95 ] );
96 ?>
97 <a class="button button-secondary cce-clear-cache-btn" href="<?php echo esc_url( $clear_cache_url ); ?>">
98 <?php esc_html_e( 'Clear Cache Now', 'clear-cache-everywhere' ); ?>
99 </a>
100
101 <!-- Settings Form -->
102 <br><br>
103 <h2><?php esc_html_e( 'Choose below which items you want to clear.', 'clear-cache-everywhere' ); ?></h2>
104 <form method="post" action="options.php">
105 <?php
106 settings_fields( CCEVERYWHERE_TEXTDOMAIN );
107 do_settings_sections( CCEVERYWHERE_TEXTDOMAIN );
108 ?><br><br><?php
109 submit_button();
110 ?>
111 </form>
112 </div>
113 <?php
114 } // End page()
115
116
117 /**
118 * Store the settings fields here
119 *
120 * @return array
121 */
122 public function get_settings_fields( $return_keys_only = false ) {
123 // Defaults and Hosting
124 $fields = [
125 [
126 'key' => 'rewrite_rules',
127 'title' => __( 'Rewrite Rules', 'clear-cache-everywhere' ),
128 'type' => 'checkbox',
129 'sanitize' => 'sanitize_checkbox',
130 'section' => 'defaults',
131 'default' => TRUE,
132 ],
133 [
134 'key' => 'wp_cache_flush',
135 'title' => __( 'WordPress Object Cache', 'clear-cache-everywhere' ),
136 'type' => 'checkbox',
137 'sanitize' => 'sanitize_checkbox',
138 'section' => 'defaults',
139 'default' => TRUE,
140 ],
141 [
142 'key' => 'transients',
143 'title' => __( 'Transients', 'clear-cache-everywhere' ),
144 'type' => 'checkbox',
145 'sanitize' => 'sanitize_checkbox',
146 'section' => 'defaults',
147 'default' => TRUE,
148 ],
149 [
150 'key' => 'sessions',
151 'title' => __( 'Sessions', 'clear-cache-everywhere' ),
152 'type' => 'checkbox',
153 'sanitize' => 'sanitize_checkbox',
154 'section' => 'defaults',
155 'default' => TRUE,
156 ],
157 [
158 'key' => 'cookies',
159 'title' => __( 'Cookies', 'clear-cache-everywhere' ),
160 'type' => 'checkbox',
161 'sanitize' => 'sanitize_checkbox',
162 'section' => 'defaults',
163 'default' => TRUE,
164 ],
165 [
166 'key' => 'browser_cache',
167 'title' => __( 'Browser Cache', 'clear-cache-everywhere' ),
168 'type' => 'checkbox',
169 'sanitize' => 'sanitize_checkbox',
170 'section' => 'defaults',
171 'default' => TRUE,
172 ],
173 [
174 'key' => 'hosting_cache',
175 'title' => __( 'Hosting Cache', 'clear-cache-everywhere' ),
176 'type' => 'checkbox',
177 'sanitize' => 'sanitize_checkbox',
178 'section' => 'hosting',
179 'default' => FALSE,
180 'comments' => __( 'If your hosting provides you with a cache purge URL, you can enter it below and enable clearing it here.', 'clear-cache-everywhere' )
181 ],
182 [
183 'key' => 'hosting_purge_url',
184 'title' => __( 'Hosting Purge URL', 'clear-cache-everywhere' ),
185 'type' => 'text',
186 'sanitize' => 'sanitize_text_field',
187 'section' => 'hosting',
188 ],
189 ];
190
191 // Integrations
192 if ( is_plugin_active( 'cornerstone/cornerstone.php' ) ) {
193 $fields[] = [
194 'key' => 'cornerstone',
195 'title' => __( 'Cornerstone Cache', 'clear-cache-everywhere' ),
196 'type' => 'checkbox',
197 'sanitize' => 'sanitize_checkbox',
198 'section' => 'integrations',
199 'default' => TRUE,
200 ];
201 }
202
203 if ( is_plugin_active( 'elementor/elementor.php' ) ) {
204 $fields[] = [
205 'key' => 'elementor',
206 'title' => __( 'Elementor Cache', 'clear-cache-everywhere' ),
207 'type' => 'checkbox',
208 'sanitize' => 'sanitize_checkbox',
209 'section' => 'integrations',
210 'default' => TRUE,
211 ];
212 }
213
214 if ( is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
215 $fields[] = [
216 'key' => 'wp_super_cache',
217 'title' => __( 'WP Super Cache', 'clear-cache-everywhere' ),
218 'type' => 'checkbox',
219 'sanitize' => 'sanitize_checkbox',
220 'section' => 'integrations',
221 'default' => TRUE,
222 ];
223 }
224
225 if ( is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) {
226 $fields[] = [
227 'key' => 'w3_total_cache',
228 'title' => __( 'W3 Total Cache', 'clear-cache-everywhere' ),
229 'type' => 'checkbox',
230 'sanitize' => 'sanitize_checkbox',
231 'section' => 'integrations',
232 'default' => TRUE,
233 ];
234 }
235
236 if ( is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
237 $fields[] = [
238 'key' => 'wp_rocket',
239 'title' => __( 'WP Rocket', 'clear-cache-everywhere' ),
240 'type' => 'checkbox',
241 'sanitize' => 'sanitize_checkbox',
242 'section' => 'integrations',
243 'default' => TRUE,
244 ];
245 }
246
247 if ( is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
248 $fields[] = [
249 'key' => 'litespeed_cache',
250 'title' => __( 'LiteSpeed Cache', 'clear-cache-everywhere' ),
251 'type' => 'checkbox',
252 'sanitize' => 'sanitize_checkbox',
253 'section' => 'integrations',
254 'default' => TRUE,
255 ];
256 }
257
258 if ( is_plugin_active( 'sg-cachepress/sg-cachepress.php' ) ) {
259 $fields[] = [
260 'key' => 'sg_optimizer',
261 'title' => __( 'SiteGround Optimizer', 'clear-cache-everywhere' ),
262 'type' => 'checkbox',
263 'sanitize' => 'sanitize_checkbox',
264 'section' => 'integrations',
265 'default' => TRUE,
266 ];
267 }
268
269 if ( is_plugin_active( 'cloudflare/cloudflare.php' ) ) {
270 $fields[] = [
271 'key' => 'cloudflare',
272 'title' => __( 'Cloudflare Cache', 'clear-cache-everywhere' ),
273 'type' => 'checkbox',
274 'sanitize' => 'sanitize_checkbox',
275 'section' => 'integrations',
276 'default' => TRUE,
277 ];
278 }
279
280 if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) {
281 $fields[] = [
282 'key' => 'autoptimize',
283 'title' => __( 'Autoptimize Cache', 'clear-cache-everywhere' ),
284 'type' => 'checkbox',
285 'sanitize' => 'sanitize_checkbox',
286 'section' => 'integrations',
287 'default' => TRUE,
288 ];
289 }
290
291 if ( is_plugin_active( 'swift-performance-lite/performance.php' ) || is_plugin_active( 'swift-performance/performance.php' ) ) {
292 $fields[] = [
293 'key' => 'swift_performance',
294 'title' => __( 'Swift Performance Cache', 'clear-cache-everywhere' ),
295 'type' => 'checkbox',
296 'sanitize' => 'sanitize_checkbox',
297 'section' => 'integrations',
298 'default' => TRUE,
299 ];
300 }
301
302 if ( is_plugin_active( 'comet-cache/comet-cache.php' ) ) {
303 $fields[] = [
304 'key' => 'comet_cache',
305 'title' => __( 'Comet Cache', 'clear-cache-everywhere' ),
306 'type' => 'checkbox',
307 'sanitize' => 'sanitize_checkbox',
308 'section' => 'integrations',
309 'default' => TRUE,
310 ];
311 }
312
313 if ( is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
314 $fields[] = [
315 'key' => 'wp_fastest_cache',
316 'title' => __( 'WP Fastest Cache', 'clear-cache-everywhere' ),
317 'type' => 'checkbox',
318 'sanitize' => 'sanitize_checkbox',
319 'section' => 'integrations',
320 'default' => TRUE,
321 ];
322 }
323
324 if ( is_plugin_active( 'hummingbird-performance/hummingbird.php' ) ) {
325 $fields[] = [
326 'key' => 'hummingbird_cache',
327 'title' => __( 'Hummingbird Cache', 'clear-cache-everywhere' ),
328 'type' => 'checkbox',
329 'sanitize' => 'sanitize_checkbox',
330 'section' => 'integrations',
331 'default' => TRUE,
332 ];
333 }
334
335 if ( is_plugin_active( 'nginx-helper/nginx-helper.php' ) ) {
336 $fields[] = [
337 'key' => 'nginx_helper',
338 'title' => __( 'Nginx Helper', 'clear-cache-everywhere' ),
339 'type' => 'checkbox',
340 'sanitize' => 'sanitize_checkbox',
341 'section' => 'integrations',
342 'default' => TRUE,
343 ];
344 }
345
346 if ( is_plugin_active( 'wp-optimize/wp-optimize.php' ) ) {
347 $fields[] = [
348 'key' => 'wp_optimize',
349 'title' => __( 'WP-Optimize Cache', 'clear-cache-everywhere' ),
350 'type' => 'checkbox',
351 'sanitize' => 'sanitize_checkbox',
352 'section' => 'integrations',
353 'default' => TRUE,
354 ];
355 }
356
357 // Apply filter to allow developers to add custom fields
358 $fields = filter_var_array( apply_filters( 'cceverywhere_custom_settings', $fields ), FILTER_SANITIZE_FULL_SPECIAL_CHARS );
359
360 // Return
361 if ( $return_keys_only ) {
362 $field_keys = [];
363 foreach ( $fields as $field ) {
364 $field_keys[] = $field[ 'key' ];
365 }
366 return $field_keys;
367 }
368 return $fields;
369 } // End get_settings_fields()
370
371
372 /**
373 * Settings fields
374 *
375 * @return void
376 */
377 public function settings_fields() {
378 // Slug
379 $slug = CCEVERYWHERE_TEXTDOMAIN;
380
381 // Fields
382 $fields = $this->get_settings_fields( false );
383
384 /**
385 * Sections
386 */
387 $settings_sections = [
388 [ 'defaults', __( 'Defaults', 'clear-cache-everywhere' ), '' ],
389 [ 'hosting', __( 'Hosting', 'clear-cache-everywhere' ), '' ],
390 [ 'integrations', __( 'Integrations', 'clear-cache-everywhere' ), '' ],
391 [ 'custom', __( 'Custom', 'clear-cache-everywhere' ), '' ],
392 ];
393
394 // Only include sections with fields
395 $settings_sections_to_add = [];
396 foreach ( $settings_sections as $settings_section ) {
397 $section_key = $settings_section[0];
398
399 // Check if any fields exist for the section
400 $section_fields = array_filter( $fields, function( $field ) use ( $section_key ) {
401 return isset( $field['section'] ) && $field['section'] === $section_key;
402 });
403
404 // If there are fields for this section, add the section to the settings sections
405 if ( ! empty( $section_fields ) ) {
406 $settings_sections_to_add[] = $settings_section;
407 }
408 }
409
410 // Iter the filtered sections
411 foreach ( $settings_sections_to_add as $settings_section ) {
412 add_settings_section(
413 $settings_section[0],
414 $settings_section[1] . ':',
415 $settings_section[2],
416 $slug
417 );
418 }
419
420 /**
421 * Fields
422 */
423 // Iter the fields
424 foreach ( $fields as $field ) {
425 $option_name = CCEVERYWHERE__TEXTDOMAIN.'_'.$field[ 'key' ];
426 $callback = 'settings_field_'.$field[ 'type' ];
427 $args = [
428 'id' => $option_name,
429 'class' => $option_name,
430 'name' => $option_name,
431 ];
432
433 // Add comments
434 if ( isset( $field[ 'comments' ] ) ) {
435 $args[ 'comments' ] = $field[ 'comments' ];
436 }
437
438 // Add select options
439 if ( isset( $field[ 'options' ] ) ) {
440 $args[ 'options' ] = $field[ 'options' ];
441 }
442
443 // Add default
444 if ( isset( $field[ 'default' ] ) ) {
445 $args[ 'default' ] = $field[ 'default' ];
446 }
447
448 // Add revert
449 if ( isset( $field[ 'revert' ] ) ) {
450 $args[ 'revert' ] = $field[ 'revert' ];
451 }
452
453 // Add the field
454 register_setting( $slug, $option_name, sanitize_key( $field[ 'sanitize' ] ) );
455 add_settings_field( $option_name, $field[ 'title' ], [ $this, $callback ], $slug, $field[ 'section' ], $args );
456 }
457 } // End settings_fields()
458
459
460 /**
461 * Custom callback function to print text field
462 *
463 * @param array $args
464 * @return void
465 */
466 public function settings_field_text( $args ) {
467 $width = isset( $args[ 'width' ] ) ? $args[ 'width' ] : '43rem';
468 $default = isset( $args[ 'default' ] ) ? $args[ 'default' ] : '';
469 $value = get_option( $args[ 'name' ], $default );
470 if ( isset( $args[ 'revert' ] ) && $args[ 'revert' ] == true && trim( $value ) == '' ) {
471 $value = $default;
472 }
473 $comments = isset( $args[ 'comments' ] ) ? '<br><p class="description">' . $args[ 'comments' ] . '</p>' : '';
474
475 printf(
476 '<input type="text" id="%s" name="%s" value="%s" style="width: %s;" />%s',
477 esc_attr( $args[ 'id' ] ),
478 esc_attr( $args[ 'name' ] ),
479 esc_html( $value ),
480 esc_attr( $width ),
481 wp_kses_post( $comments )
482 );
483 } // settings_field_text()
484
485
486 /**
487 * Custom callback function to print checkbox field
488 *
489 * @param array $args
490 * @return void
491 */
492 public function settings_field_checkbox( $args ) {
493 $value = get_option( $args[ 'name' ] );
494 if ( false === $value && isset( $args[ 'default' ] ) ) {
495 $value = $args[ 'default' ];
496 }
497 $value = $this->sanitize_checkbox( $value );
498 $comments = isset( $args[ 'comments' ] ) ? ' <p class="description">' . $args[ 'comments' ] . '</p>' : '';
499
500 printf(
501 '<input type="checkbox" id="%s" name="%s" value="1" %s/>%s',
502 esc_attr( $args[ 'name' ] ),
503 esc_attr( $args[ 'name' ] ),
504 checked( 1, $value, false ),
505 wp_kses_post( $comments )
506 );
507 } // End settings_field_checkbox()
508
509
510 /**
511 * Sanitize checkbox
512 *
513 * @param int $value
514 * @return boolean
515 */
516 public function sanitize_checkbox( $value ) {
517 return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
518 } // End sanitize_checkbox()
519
520
521 /**
522 * Enqueue javascript
523 *
524 * @return void
525 */
526 public function enqueue_scripts( $hook ) {
527 // JavaScript
528 wp_register_script( CCEVERYWHERE_TEXTDOMAIN, CCEVERYWHERE_JS_PATH.'back-notice.js', [ 'jquery' ], CCEVERYWHERE_VERSION, true );
529 wp_enqueue_script( CCEVERYWHERE_TEXTDOMAIN );
530
531 // Check if we are on the correct admin page
532 if ( $hook !== 'appearance_page_'.CCEVERYWHERE_TEXTDOMAIN ) {
533 return;
534 }
535
536 // CSS
537 wp_enqueue_style( CCEVERYWHERE_TEXTDOMAIN . '-styles', CCEVERYWHERE_CSS_PATH . 'settings.css', [], CCEVERYWHERE_VERSION );
538 } // End enqueue_scripts()
539
540 }
541