css
3 months ago
js
3 months ago
admin-bar.php
3 months ago
clear-cache.php
3 months ago
common.php
3 months ago
index.php
3 months ago
settings.php
3 months ago
settings.php
814 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 | <button id="cce-clear-cache-btn" 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 | </button> |
| 100 | |
| 101 | <!-- Result Container --> |
| 102 | <?php |
| 103 | $last_results = get_option( 'clear_cache_everywhere_last_results', [] ); |
| 104 | $total_elapsed = 0; |
| 105 | |
| 106 | $clearing_actions = ( new Clear() )->get_clearing_actions(); |
| 107 | |
| 108 | if ( ! empty( $last_results ) && ! empty( $clearing_actions ) ) { |
| 109 | foreach ( $clearing_actions as $action ) { |
| 110 | if ( ! empty( $action[ 'enabled' ] ) ) { |
| 111 | $key = $action[ 'key' ]; |
| 112 | if ( isset( $last_results[ $key ] ) ) { |
| 113 | $res = $last_results[ $key ]; |
| 114 | if ( ! empty( $res[ 'start' ] ) && ! empty( $res[ 'end' ] ) ) { |
| 115 | $total_elapsed += $res[ 'end' ] - $res[ 'start' ]; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | ?> |
| 122 | <div id="cce-clear-cache-result"> |
| 123 | <?php if ( $total_elapsed > 0 ) : ?> |
| 124 | <?php |
| 125 | echo sprintf( |
| 126 | /* translators: %s is total elapsed seconds for clearing actions only */ |
| 127 | __( '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' ), |
| 128 | number_format( $total_elapsed, 3 ) |
| 129 | ); |
| 130 | ?> |
| 131 | <?php else : ?> |
| 132 | <?php |
| 133 | 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' ); |
| 134 | ?> |
| 135 | <?php endif; ?> |
| 136 | </div> |
| 137 | |
| 138 | <!-- Settings Form --> |
| 139 | <br><br> |
| 140 | <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> |
| 141 | <form method="post" action="options.php"> |
| 142 | <?php |
| 143 | settings_fields( CCEVERYWHERE_TEXTDOMAIN ); |
| 144 | do_settings_sections( CCEVERYWHERE_TEXTDOMAIN ); |
| 145 | ?><br><br><?php |
| 146 | submit_button(); |
| 147 | ?> |
| 148 | </form> |
| 149 | </div> |
| 150 | <?php |
| 151 | } // End page() |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Store the settings fields here |
| 156 | * |
| 157 | * @return array |
| 158 | */ |
| 159 | public function get_settings_fields( $return_keys_only = false ) { |
| 160 | // Defaults and Hosting |
| 161 | $fields = [ |
| 162 | [ |
| 163 | 'key' => 'rewrite_rules', |
| 164 | 'title' => __( 'Rewrite Rules', 'clear-cache-everywhere' ), |
| 165 | 'type' => 'checkbox', |
| 166 | 'sanitize' => 'sanitize_checkbox', |
| 167 | 'section' => 'defaults', |
| 168 | 'default' => TRUE, |
| 169 | 'run_context' => 'ajax', |
| 170 | 'comments' => sprintf( |
| 171 | /* translators: %s: permalink settings page URL */ |
| 172 | __( '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' ), |
| 173 | esc_url( admin_url( 'options-permalink.php' ) ) |
| 174 | ), |
| 175 | ], |
| 176 | [ |
| 177 | 'key' => 'wp_cache_flush', |
| 178 | 'title' => __( 'WordPress Object Cache', 'clear-cache-everywhere' ), |
| 179 | 'type' => 'checkbox', |
| 180 | 'sanitize' => 'sanitize_checkbox', |
| 181 | 'section' => 'defaults', |
| 182 | 'default' => TRUE, |
| 183 | 'run_context' => 'ajax', |
| 184 | 'comments' => __( 'Flushes the WordPress object cache.', 'clear-cache-everywhere' ), |
| 185 | ], |
| 186 | [ |
| 187 | 'key' => 'transients', |
| 188 | 'title' => __( 'Transients', 'clear-cache-everywhere' ), |
| 189 | 'type' => 'checkbox', |
| 190 | 'sanitize' => 'sanitize_checkbox', |
| 191 | 'section' => 'defaults', |
| 192 | 'default' => FALSE, |
| 193 | 'run_context' => 'ajax', |
| 194 | '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' ), |
| 195 | ], |
| 196 | [ |
| 197 | 'key' => 'opcache_reset', |
| 198 | 'title' => __( 'OPcache Reset', 'clear-cache-everywhere' ), |
| 199 | 'type' => 'checkbox', |
| 200 | 'sanitize' => 'sanitize_checkbox', |
| 201 | 'section' => 'defaults', |
| 202 | 'default' => FALSE, |
| 203 | 'run_context' => 'ajax', |
| 204 | 'comments' => __( 'Resets PHP OPcache to clear cached scripts. May take a few seconds on large sites.', 'clear-cache-everywhere' ), |
| 205 | ], |
| 206 | [ |
| 207 | 'key' => 'varnish', |
| 208 | 'title' => __( 'Varnish Cache', 'clear-cache-everywhere' ), |
| 209 | 'type' => 'checkbox', |
| 210 | 'sanitize' => 'sanitize_checkbox', |
| 211 | 'section' => 'defaults', |
| 212 | 'default' => FALSE, |
| 213 | 'run_context' => 'ajax', |
| 214 | 'comments' => __( 'Purges the Varnish cache if detected. Network requests may take a few seconds to propagate.', 'clear-cache-everywhere' ), |
| 215 | ], |
| 216 | [ |
| 217 | 'key' => 'redis_memcached', |
| 218 | 'title' => __( 'Redis/Memcached', 'clear-cache-everywhere' ), |
| 219 | 'type' => 'checkbox', |
| 220 | 'sanitize' => 'sanitize_checkbox', |
| 221 | 'section' => 'defaults', |
| 222 | 'default' => FALSE, |
| 223 | 'run_context' => 'ajax', |
| 224 | 'comments' => __( 'Flushes Redis or Memcached object cache. Large caches may take several seconds.', 'clear-cache-everywhere' ), |
| 225 | ], |
| 226 | [ |
| 227 | 'key' => 'fragment_cache', |
| 228 | 'title' => __( 'Fragment Cache', 'clear-cache-everywhere' ), |
| 229 | 'type' => 'checkbox', |
| 230 | 'sanitize' => 'sanitize_checkbox', |
| 231 | 'section' => 'defaults', |
| 232 | 'default' => TRUE, |
| 233 | 'run_context' => 'ajax', |
| 234 | 'comments' => __( 'Clears any fragment cache used by themes or plugins. May take a few seconds on large sites.', 'clear-cache-everywhere' ), |
| 235 | ], |
| 236 | [ |
| 237 | 'key' => 'rest_api_cache', |
| 238 | 'title' => __( 'REST API Cache', 'clear-cache-everywhere' ), |
| 239 | 'type' => 'checkbox', |
| 240 | 'sanitize' => 'sanitize_checkbox', |
| 241 | 'section' => 'defaults', |
| 242 | 'default' => TRUE, |
| 243 | 'run_context' => 'ajax', |
| 244 | 'comments' => __( 'Clears cached REST API responses.', 'clear-cache-everywhere' ), |
| 245 | ], |
| 246 | [ |
| 247 | 'key' => 'sessions', |
| 248 | 'title' => __( 'Sessions', 'clear-cache-everywhere' ), |
| 249 | 'type' => 'checkbox', |
| 250 | 'sanitize' => 'sanitize_checkbox', |
| 251 | 'section' => 'defaults', |
| 252 | 'default' => FALSE, |
| 253 | 'run_context' => 'page', |
| 254 | 'comments' => __( 'Clears active user sessions; users may need to log in again.', 'clear-cache-everywhere' ), |
| 255 | ], |
| 256 | [ |
| 257 | 'key' => 'cookies', |
| 258 | 'title' => __( 'Cookies', 'clear-cache-everywhere' ), |
| 259 | 'type' => 'checkbox', |
| 260 | 'sanitize' => 'sanitize_checkbox', |
| 261 | 'section' => 'defaults', |
| 262 | 'default' => TRUE, |
| 263 | 'run_context' => 'page', |
| 264 | 'comments' => __( 'Clears browser cookies related to the site.', 'clear-cache-everywhere' ), |
| 265 | ], |
| 266 | [ |
| 267 | 'key' => 'browser_cache', |
| 268 | 'title' => __( 'Browser Cache', 'clear-cache-everywhere' ), |
| 269 | 'type' => 'checkbox', |
| 270 | 'sanitize' => 'sanitize_checkbox', |
| 271 | 'section' => 'defaults', |
| 272 | 'default' => TRUE, |
| 273 | 'run_context' => 'page', |
| 274 | 'comments' => __( 'Clears cached resources stored in the user’s browser.', 'clear-cache-everywhere' ), |
| 275 | ], |
| 276 | [ |
| 277 | 'key' => 'hosting_cache', |
| 278 | 'title' => __( 'Hosting Cache', 'clear-cache-everywhere' ), |
| 279 | 'type' => 'checkbox', |
| 280 | 'sanitize' => 'sanitize_checkbox', |
| 281 | 'section' => 'hosting', |
| 282 | 'default' => FALSE, |
| 283 | 'run_context' => 'ajax', |
| 284 | 'comments' => __( 'Clears caching handled by your hosting provider.', 'clear-cache-everywhere' ), |
| 285 | ], |
| 286 | [ |
| 287 | 'key' => 'hosting_purge_url', |
| 288 | 'title' => __( 'Hosting Purge URL', 'clear-cache-everywhere' ), |
| 289 | 'type' => 'text', |
| 290 | 'sanitize' => 'sanitize_text_field', |
| 291 | 'section' => 'hosting', |
| 292 | ], |
| 293 | [ |
| 294 | 'key' => 'cloudflare_cache', |
| 295 | 'title' => __( 'Cloudflare Cache', 'clear-cache-everywhere' ), |
| 296 | 'type' => 'checkbox', |
| 297 | 'sanitize' => 'sanitize_checkbox', |
| 298 | 'section' => 'hosting', |
| 299 | 'default' => FALSE, |
| 300 | 'run_context' => 'ajax', |
| 301 | 'comments' => __( 'Clears caching handled by Cloudflare.', 'clear-cache-everywhere' ), |
| 302 | ], |
| 303 | [ |
| 304 | 'key' => 'cloudflare_zone_id', |
| 305 | 'title' => __( 'Cloudflare Zone ID', 'clear-cache-everywhere' ), |
| 306 | 'type' => 'text', |
| 307 | 'sanitize' => 'sanitize_text_field', |
| 308 | 'section' => 'hosting', |
| 309 | 'comments' => __( 'Required to clear Cloudflare cache. Find this in your Cloudflare dashboard under Overview > API > Zone ID.', 'clear-cache-everywhere' ), |
| 310 | ], |
| 311 | [ |
| 312 | 'key' => 'cloudflare_api_token', |
| 313 | 'title' => __( 'Cloudflare API Token', 'clear-cache-everywhere' ), |
| 314 | 'type' => 'password', |
| 315 | 'sanitize' => 'sanitize_text_field', |
| 316 | 'section' => 'hosting', |
| 317 | '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' ), |
| 318 | ], |
| 319 | ]; |
| 320 | |
| 321 | // Integrations |
| 322 | if ( is_plugin_active( 'cornerstone/cornerstone.php' ) ) { |
| 323 | $fields[] = [ |
| 324 | 'key' => 'cornerstone', |
| 325 | 'title' => __( 'Cornerstone Cache', 'clear-cache-everywhere' ), |
| 326 | 'type' => 'checkbox', |
| 327 | 'sanitize' => 'sanitize_checkbox', |
| 328 | 'section' => 'integrations', |
| 329 | 'default' => TRUE, |
| 330 | 'run_context' => 'ajax', |
| 331 | 'comments' => __( 'Clears the Cornerstone page builder cache.', 'clear-cache-everywhere' ), |
| 332 | ]; |
| 333 | } |
| 334 | |
| 335 | if ( is_plugin_active( 'elementor/elementor.php' ) ) { |
| 336 | $fields[] = [ |
| 337 | 'key' => 'elementor', |
| 338 | 'title' => __( 'Elementor Cache', 'clear-cache-everywhere' ), |
| 339 | 'type' => 'checkbox', |
| 340 | 'sanitize' => 'sanitize_checkbox', |
| 341 | 'section' => 'integrations', |
| 342 | 'default' => TRUE, |
| 343 | 'run_context' => 'ajax', |
| 344 | 'comments' => __( 'Clears Elementor page builder cache.', 'clear-cache-everywhere' ), |
| 345 | ]; |
| 346 | } |
| 347 | |
| 348 | if ( is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) { |
| 349 | $fields[] = [ |
| 350 | 'key' => 'wp_super_cache', |
| 351 | 'title' => __( 'WP Super Cache', 'clear-cache-everywhere' ), |
| 352 | 'type' => 'checkbox', |
| 353 | 'sanitize' => 'sanitize_checkbox', |
| 354 | 'section' => 'integrations', |
| 355 | 'default' => TRUE, |
| 356 | 'run_context' => 'ajax', |
| 357 | 'comments' => __( 'Clears the WP Super Cache plugin cache.', 'clear-cache-everywhere' ), |
| 358 | ]; |
| 359 | } |
| 360 | |
| 361 | if ( is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) { |
| 362 | $fields[] = [ |
| 363 | 'key' => 'w3_total_cache', |
| 364 | 'title' => __( 'W3 Total Cache', 'clear-cache-everywhere' ), |
| 365 | 'type' => 'checkbox', |
| 366 | 'sanitize' => 'sanitize_checkbox', |
| 367 | 'section' => 'integrations', |
| 368 | 'default' => TRUE, |
| 369 | 'run_context' => 'ajax', |
| 370 | 'comments' => __( 'Clears W3 Total Cache plugin caches.', 'clear-cache-everywhere' ), |
| 371 | ]; |
| 372 | } |
| 373 | |
| 374 | if ( is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) { |
| 375 | $fields[] = [ |
| 376 | 'key' => 'wp_rocket', |
| 377 | 'title' => __( 'WP Rocket', 'clear-cache-everywhere' ), |
| 378 | 'type' => 'checkbox', |
| 379 | 'sanitize' => 'sanitize_checkbox', |
| 380 | 'section' => 'integrations', |
| 381 | 'default' => TRUE, |
| 382 | 'run_context' => 'ajax', |
| 383 | 'comments' => __( 'Clears WP Rocket plugin cache.', 'clear-cache-everywhere' ), |
| 384 | ]; |
| 385 | } |
| 386 | |
| 387 | if ( is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) { |
| 388 | $fields[] = [ |
| 389 | 'key' => 'litespeed_cache', |
| 390 | 'title' => __( 'LiteSpeed Cache', 'clear-cache-everywhere' ), |
| 391 | 'type' => 'checkbox', |
| 392 | 'sanitize' => 'sanitize_checkbox', |
| 393 | 'section' => 'integrations', |
| 394 | 'default' => TRUE, |
| 395 | 'run_context' => 'ajax', |
| 396 | 'comments' => __( 'Clears LiteSpeed Cache plugin cache.', 'clear-cache-everywhere' ), |
| 397 | ]; |
| 398 | } |
| 399 | |
| 400 | if ( is_plugin_active( 'sg-cachepress/sg-cachepress.php' ) ) { |
| 401 | $fields[] = [ |
| 402 | 'key' => 'sg_optimizer', |
| 403 | 'title' => __( 'SiteGround Optimizer', 'clear-cache-everywhere' ), |
| 404 | 'type' => 'checkbox', |
| 405 | 'sanitize' => 'sanitize_checkbox', |
| 406 | 'section' => 'integrations', |
| 407 | 'default' => TRUE, |
| 408 | 'run_context' => 'ajax', |
| 409 | 'comments' => __( 'Clears SiteGround Optimizer cache.', 'clear-cache-everywhere' ), |
| 410 | ]; |
| 411 | } |
| 412 | |
| 413 | if ( is_plugin_active( 'cloudflare/cloudflare.php' ) ) { |
| 414 | $fields[] = [ |
| 415 | 'key' => 'cloudflare', |
| 416 | 'title' => __( 'Cloudflare Cache', 'clear-cache-everywhere' ), |
| 417 | 'type' => 'checkbox', |
| 418 | 'sanitize' => 'sanitize_checkbox', |
| 419 | 'section' => 'integrations', |
| 420 | 'default' => TRUE, |
| 421 | 'run_context' => 'ajax', |
| 422 | 'comments' => __( 'Purges Cloudflare cache.', 'clear-cache-everywhere' ), |
| 423 | ]; |
| 424 | } |
| 425 | |
| 426 | if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) { |
| 427 | $fields[] = [ |
| 428 | 'key' => 'autoptimize', |
| 429 | 'title' => __( 'Autoptimize Cache', 'clear-cache-everywhere' ), |
| 430 | 'type' => 'checkbox', |
| 431 | 'sanitize' => 'sanitize_checkbox', |
| 432 | 'section' => 'integrations', |
| 433 | 'default' => TRUE, |
| 434 | 'run_context' => 'ajax', |
| 435 | 'comments' => __( 'Clears Autoptimize generated CSS/JS cache.', 'clear-cache-everywhere' ), |
| 436 | ]; |
| 437 | } |
| 438 | |
| 439 | if ( is_plugin_active( 'swift-performance-lite/performance.php' ) || is_plugin_active( 'swift-performance/performance.php' ) ) { |
| 440 | $fields[] = [ |
| 441 | 'key' => 'swift_performance', |
| 442 | 'title' => __( 'Swift Performance Cache', 'clear-cache-everywhere' ), |
| 443 | 'type' => 'checkbox', |
| 444 | 'sanitize' => 'sanitize_checkbox', |
| 445 | 'section' => 'integrations', |
| 446 | 'default' => TRUE, |
| 447 | 'run_context' => 'ajax', |
| 448 | 'comments' => __( 'Clears Swift Performance plugin cache.', 'clear-cache-everywhere' ), |
| 449 | ]; |
| 450 | } |
| 451 | |
| 452 | if ( is_plugin_active( 'comet-cache/comet-cache.php' ) ) { |
| 453 | $fields[] = [ |
| 454 | 'key' => 'comet_cache', |
| 455 | 'title' => __( 'Comet Cache', 'clear-cache-everywhere' ), |
| 456 | 'type' => 'checkbox', |
| 457 | 'sanitize' => 'sanitize_checkbox', |
| 458 | 'section' => 'integrations', |
| 459 | 'default' => TRUE, |
| 460 | 'run_context' => 'ajax', |
| 461 | 'comments' => __( 'Clears Comet Cache plugin cache.', 'clear-cache-everywhere' ), |
| 462 | ]; |
| 463 | } |
| 464 | |
| 465 | if ( is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) { |
| 466 | $fields[] = [ |
| 467 | 'key' => 'wp_fastest_cache', |
| 468 | 'title' => __( 'WP Fastest Cache', 'clear-cache-everywhere' ), |
| 469 | 'type' => 'checkbox', |
| 470 | 'sanitize' => 'sanitize_checkbox', |
| 471 | 'section' => 'integrations', |
| 472 | 'default' => TRUE, |
| 473 | 'run_context' => 'ajax', |
| 474 | 'comments' => __( 'Clears WP Fastest Cache plugin cache.', 'clear-cache-everywhere' ), |
| 475 | ]; |
| 476 | } |
| 477 | |
| 478 | if ( is_plugin_active( 'hummingbird-performance/hummingbird.php' ) ) { |
| 479 | $fields[] = [ |
| 480 | 'key' => 'hummingbird_cache', |
| 481 | 'title' => __( 'Hummingbird Cache', 'clear-cache-everywhere' ), |
| 482 | 'type' => 'checkbox', |
| 483 | 'sanitize' => 'sanitize_checkbox', |
| 484 | 'section' => 'integrations', |
| 485 | 'default' => TRUE, |
| 486 | 'run_context' => 'ajax', |
| 487 | 'comments' => __( 'Clears Hummingbird plugin cache.', 'clear-cache-everywhere' ), |
| 488 | ]; |
| 489 | } |
| 490 | |
| 491 | if ( is_plugin_active( 'nginx-helper/nginx-helper.php' ) ) { |
| 492 | $fields[] = [ |
| 493 | 'key' => 'nginx_helper', |
| 494 | 'title' => __( 'Nginx Helper', 'clear-cache-everywhere' ), |
| 495 | 'type' => 'checkbox', |
| 496 | 'sanitize' => 'sanitize_checkbox', |
| 497 | 'section' => 'integrations', |
| 498 | 'default' => TRUE, |
| 499 | 'run_context' => 'ajax', |
| 500 | 'comments' => __( 'Purges cache managed by Nginx Helper plugin.', 'clear-cache-everywhere' ), |
| 501 | ]; |
| 502 | } |
| 503 | |
| 504 | if ( is_plugin_active( 'wp-optimize/wp-optimize.php' ) ) { |
| 505 | $fields[] = [ |
| 506 | 'key' => 'wp_optimize', |
| 507 | 'title' => __( 'WP-Optimize Cache', 'clear-cache-everywhere' ), |
| 508 | 'type' => 'checkbox', |
| 509 | 'sanitize' => 'sanitize_checkbox', |
| 510 | 'section' => 'integrations', |
| 511 | 'default' => TRUE, |
| 512 | 'run_context' => 'ajax', |
| 513 | 'comments' => __( 'Clears WP-Optimize cache.', 'clear-cache-everywhere' ), |
| 514 | ]; |
| 515 | } |
| 516 | |
| 517 | // Apply filter to allow developers to add custom fields |
| 518 | $fields = apply_filters( 'cceverywhere_custom_settings', $fields ); |
| 519 | |
| 520 | // Return |
| 521 | if ( $return_keys_only ) { |
| 522 | $field_keys = []; |
| 523 | foreach ( $fields as $field ) { |
| 524 | $field_keys[] = $field[ 'key' ]; |
| 525 | } |
| 526 | return $field_keys; |
| 527 | } |
| 528 | return $fields; |
| 529 | } // End get_settings_fields() |
| 530 | |
| 531 | |
| 532 | /** |
| 533 | * Settings fields |
| 534 | * |
| 535 | * @return void |
| 536 | */ |
| 537 | public function settings_fields() { |
| 538 | // Slug |
| 539 | $slug = CCEVERYWHERE_TEXTDOMAIN; |
| 540 | |
| 541 | // Fields |
| 542 | $fields = $this->get_settings_fields( false ); |
| 543 | |
| 544 | /** |
| 545 | * Sections |
| 546 | */ |
| 547 | $settings_sections = [ |
| 548 | [ 'defaults', __( 'Defaults', 'clear-cache-everywhere' ), '' ], |
| 549 | [ 'hosting', __( 'Hosting', 'clear-cache-everywhere' ), '' ], |
| 550 | [ 'integrations', __( 'Integrations', 'clear-cache-everywhere' ), '' ], |
| 551 | [ 'custom', __( 'Custom', 'clear-cache-everywhere' ), '' ], |
| 552 | ]; |
| 553 | |
| 554 | // Only include sections with fields |
| 555 | $settings_sections_to_add = []; |
| 556 | foreach ( $settings_sections as $settings_section ) { |
| 557 | $section_key = $settings_section[0]; |
| 558 | |
| 559 | // Check if any fields exist for the section |
| 560 | $section_fields = array_filter( $fields, function( $field ) use ( $section_key ) { |
| 561 | return isset( $field[ 'section' ] ) && $field[ 'section' ] === $section_key; |
| 562 | }); |
| 563 | |
| 564 | // If there are fields for this section, add the section to the settings sections |
| 565 | if ( ! empty( $section_fields ) ) { |
| 566 | $settings_sections_to_add[] = $settings_section; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // Iter the filtered sections |
| 571 | foreach ( $settings_sections_to_add as $settings_section ) { |
| 572 | add_settings_section( |
| 573 | $settings_section[0], |
| 574 | $settings_section[1] . ':', |
| 575 | $settings_section[2], |
| 576 | $slug |
| 577 | ); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Fields |
| 582 | */ |
| 583 | // Iter the fields |
| 584 | foreach ( $fields as $field ) { |
| 585 | $option_name = CCEVERYWHERE__TEXTDOMAIN.'_'.$field[ 'key' ]; |
| 586 | $callback = 'settings_field_'.$field[ 'type' ]; |
| 587 | $args = [ |
| 588 | 'id' => $option_name, |
| 589 | 'class' => $option_name, |
| 590 | 'name' => $option_name, |
| 591 | 'key' => $field[ 'key' ], |
| 592 | ]; |
| 593 | |
| 594 | // Add comments |
| 595 | if ( isset( $field[ 'comments' ] ) && $field[ 'comments' ] != '' ) { |
| 596 | $comments = '<br><span class="cceverywhere_action_desc">' . $field[ 'comments' ] . '</span>'; |
| 597 | } else { |
| 598 | $comments = ''; |
| 599 | } |
| 600 | |
| 601 | // Add select options |
| 602 | if ( isset( $field[ 'options' ] ) ) { |
| 603 | $args[ 'options' ] = $field[ 'options' ]; |
| 604 | } |
| 605 | |
| 606 | // Add default |
| 607 | if ( isset( $field[ 'default' ] ) ) { |
| 608 | $args[ 'default' ] = $field[ 'default' ]; |
| 609 | } |
| 610 | |
| 611 | // Add revert |
| 612 | if ( isset( $field[ 'revert' ] ) ) { |
| 613 | $args[ 'revert' ] = $field[ 'revert' ]; |
| 614 | } |
| 615 | |
| 616 | // Add run context |
| 617 | if ( isset( $field[ 'run_context' ] ) ) { |
| 618 | $args[ 'run_context' ] = $field[ 'run_context' ]; |
| 619 | } |
| 620 | |
| 621 | // Add the field |
| 622 | register_setting( $slug, $option_name, sanitize_key( $field[ 'sanitize' ] ) ); |
| 623 | add_settings_field( $option_name, $field[ 'title' ] . wp_kses_post( $comments ), [ $this, $callback ], $slug, $field[ 'section' ], $args ); |
| 624 | } |
| 625 | } // End settings_fields() |
| 626 | |
| 627 | |
| 628 | /** |
| 629 | * Custom callback function to print text field |
| 630 | * |
| 631 | * @param array $args |
| 632 | * @return void |
| 633 | */ |
| 634 | public function settings_field_text( $args ) { |
| 635 | $width = isset( $args[ 'width' ] ) ? $args[ 'width' ] : '43rem'; |
| 636 | $default = isset( $args[ 'default' ] ) ? $args[ 'default' ] : ''; |
| 637 | $value = get_option( $args[ 'name' ], $default ); |
| 638 | if ( isset( $args[ 'revert' ] ) && $args[ 'revert' ] == true && trim( $value ) == '' ) { |
| 639 | $value = $default; |
| 640 | } |
| 641 | $comments = isset( $args[ 'comments' ] ) ? '<br><p class="description">' . $args[ 'comments' ] . '</p>' : ''; |
| 642 | |
| 643 | printf( |
| 644 | '<input type="text" id="%s" name="%s" value="%s" style="width: %s;" />%s', |
| 645 | esc_attr( $args[ 'id' ] ), |
| 646 | esc_attr( $args[ 'name' ] ), |
| 647 | esc_html( $value ), |
| 648 | esc_attr( $width ), |
| 649 | wp_kses_post( $comments ) |
| 650 | ); |
| 651 | } // settings_field_text() |
| 652 | |
| 653 | |
| 654 | /** |
| 655 | * Custom callback function to print password field |
| 656 | * |
| 657 | * @param array $args |
| 658 | * @return void |
| 659 | */ |
| 660 | public function settings_field_password( $args ) { |
| 661 | $width = isset( $args[ 'width' ] ) ? $args[ 'width' ] : '43rem'; |
| 662 | $default = isset( $args[ 'default' ] ) ? $args[ 'default' ] : ''; |
| 663 | $value = get_option( $args[ 'name' ], $default ); |
| 664 | if ( isset( $args[ 'revert' ] ) && $args[ 'revert' ] == true && trim( $value ) == '' ) { |
| 665 | $value = $default; |
| 666 | } |
| 667 | $comments = isset( $args[ 'comments' ] ) ? '<br><p class="description">' . $args[ 'comments' ] . '</p>' : ''; |
| 668 | |
| 669 | printf( |
| 670 | '<input type="password" id="%s" name="%s" value="%s" style="width: %s;" />%s', |
| 671 | esc_attr( $args[ 'id' ] ), |
| 672 | esc_attr( $args[ 'name' ] ), |
| 673 | esc_html( $value ), |
| 674 | esc_attr( $width ), |
| 675 | wp_kses_post( $comments ) |
| 676 | ); |
| 677 | } // settings_field_password() |
| 678 | |
| 679 | |
| 680 | /** |
| 681 | * Custom callback function to print checkbox field |
| 682 | * |
| 683 | * @param array $args |
| 684 | * @return void |
| 685 | */ |
| 686 | public function settings_field_checkbox( $args ) { |
| 687 | $value = get_option( $args[ 'name' ] ); |
| 688 | if ( false === $value && isset( $args[ 'default' ] ) ) { |
| 689 | $value = $args[ 'default' ]; |
| 690 | } |
| 691 | $value = $this->sanitize_checkbox( $value ); |
| 692 | |
| 693 | // Output the checkbox |
| 694 | printf( |
| 695 | '<div class="cce-action-container"><input type="checkbox" id="%s" name="%s" value="1" %s/>', |
| 696 | esc_attr( $args[ 'name' ] ), |
| 697 | esc_attr( $args[ 'name' ] ), |
| 698 | checked( 1, $value, false ) |
| 699 | ); |
| 700 | |
| 701 | // Add the "Clear" button and result container for actions with run_context |
| 702 | if ( isset( $args[ 'run_context' ] ) && isset( $args[ 'key' ] ) ) { |
| 703 | $key = $args[ 'key' ]; |
| 704 | |
| 705 | printf( |
| 706 | ' <button class="button button-small cce-run-action-btn" data-key="%s">%s</button>', |
| 707 | esc_attr( $key ), |
| 708 | esc_html__( 'Clear', 'clear-cache-everywhere' ) |
| 709 | ); |
| 710 | |
| 711 | // Determine if page query params exist and match this field |
| 712 | $show_finalizing = false; |
| 713 | if ( isset( $_GET[ 'cce_run_page' ] ) && $args[ 'run_context' ] === 'page' ) { |
| 714 | $show_finalizing = true; |
| 715 | } elseif ( isset( $_GET[ 'cce_run_single_page' ] ) && $_GET[ 'cce_run_single_page' ] === '1' ) { |
| 716 | if ( isset( $_GET[ 'key' ] ) && $_GET[ 'key' ] === $key ) { |
| 717 | $show_finalizing = true; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | // Result text and class |
| 722 | $result_text = ''; |
| 723 | $result_class = ''; |
| 724 | |
| 725 | if ( $show_finalizing ) { |
| 726 | $result_text = __( 'Finalizing...', 'clear-cache-everywhere' ); |
| 727 | } else { |
| 728 | $last_results = get_option( 'clear_cache_everywhere_last_results', [] ); |
| 729 | |
| 730 | if ( isset( $last_results[ $key ] ) ) { |
| 731 | $res = $last_results[ $key ]; |
| 732 | |
| 733 | if ( $res[ 'status' ] === 'running' && empty( $res[ 'end' ] ) ) { |
| 734 | $result_class = 'fail'; |
| 735 | $result_text = __( 'Could not complete. Something went wrong.', 'clear-cache-everywhere' ); |
| 736 | } elseif ( ! empty( $res[ 'end' ] ) ) { |
| 737 | $result_class = $res[ 'status' ] ?? 'fail'; |
| 738 | $date_format = get_option( 'date_format' ); |
| 739 | $time_format = get_option( 'time_format' ); |
| 740 | |
| 741 | $end_ts = (int) floor( $res[ 'end' ] ); |
| 742 | |
| 743 | $datetime = wp_date( $date_format . ' \a\t ' . $time_format, $end_ts ); |
| 744 | |
| 745 | $label = $res[ 'status' ] === 'success' |
| 746 | ? __( 'Last cleared on %s', 'clear-cache-everywhere' ) |
| 747 | : __( 'Last attempted on %s', 'clear-cache-everywhere' ); |
| 748 | |
| 749 | $result_text = sprintf( $label, $datetime ); |
| 750 | |
| 751 | // append elapsed seconds if start is available |
| 752 | if ( ! empty( $res[ 'start' ] ) ) { |
| 753 | $elapsed = $res[ 'end' ] - $res[ 'start' ]; |
| 754 | $result_text .= ' (' . number_format( $elapsed, 3 ) . 's)'; |
| 755 | } |
| 756 | |
| 757 | if ( $res[ 'status' ] !== 'info' ) { |
| 758 | $result_text .= ' - ' . strtoupper( $res[ 'status' ] ); |
| 759 | } |
| 760 | |
| 761 | if ( ! empty( $res[ 'error_message' ] ) ) { |
| 762 | $result_text .= ' - ' . $res[ 'error_message' ]; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | // Run context class |
| 769 | $run_context_class = 'run-context-' . esc_attr( $args[ 'run_context' ] ); |
| 770 | $enabled_class = $value ? 'enabled' : 'disabled'; |
| 771 | |
| 772 | // Result container |
| 773 | printf( |
| 774 | '<div class="cce-action-result %s %s %s" id="cce-action-result-%s">%s</div>', |
| 775 | esc_attr( $result_class ), |
| 776 | esc_attr( $run_context_class ), |
| 777 | esc_attr( $enabled_class ), |
| 778 | esc_attr( $key ), |
| 779 | esc_html( $result_text ) |
| 780 | ); |
| 781 | } |
| 782 | |
| 783 | echo '</div>'; |
| 784 | } // End settings_field_checkbox() |
| 785 | |
| 786 | |
| 787 | /** |
| 788 | * Sanitize checkbox |
| 789 | * |
| 790 | * @param int $value |
| 791 | * @return boolean |
| 792 | */ |
| 793 | public function sanitize_checkbox( $value ) { |
| 794 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 795 | } // End sanitize_checkbox() |
| 796 | |
| 797 | |
| 798 | /** |
| 799 | * Enqueue javascript |
| 800 | * |
| 801 | * @return void |
| 802 | */ |
| 803 | public function enqueue_scripts( $hook ) { |
| 804 | // Check if we are on the correct admin page |
| 805 | if ( $hook !== CCEVERYWHERE_SETTINGS_SCREEN_ID ) { |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | // CSS |
| 810 | wp_enqueue_style( CCEVERYWHERE_TEXTDOMAIN . '-settings', CCEVERYWHERE_CSS_PATH . 'settings.css', [], CCEVERYWHERE_SCRIPT_VERSION ); |
| 811 | } // End enqueue_scripts() |
| 812 | |
| 813 | } |
| 814 |