PluginProbe
Clear Cache for Me / trunk
Clear Cache for Me vtrunk
1.8 2.0 2.0.1 2.1 2.1.1 2.2 2.3 2.4 2.4.1 2.4.2 2.5 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.6.1 0.6.2 0.7 0.7.1 0.8 0.9 0.91 All 38 releases
clear-cache-for-widgets / clear-cache-for-widgets.php

clear-cache-for-widgets.php in Clear Cache for Me trunk, at clear-cache-for-widgets.php

557 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Clear Cache For Me
4 Plugin URI: https://webheadcoder.com/clear-cache-for-me/
5 Description: Purges all cache on WPEngine, W3 Total Cache, WP Super Cache, WP Fastest Cache when updating widgets, menus, settings. Can force a browser to reload a theme's CSS and JS files.
6 Author: Webhead LLC
7 Author URI: https://webheadcoder.com
8 Version: 2.5
9 */
10
11
12 define( 'CCFM_VERSION', '2.5' );
13 define( 'CCFM_PLUGIN', __FILE__ );
14
15 require_once( 'caching-plugins.php' );
16 require_once( 'clear-cache-for-action.php' );
17 require_once( 'options-page.php' );
18
19 // locale
20 function ccfm_plugins_loaded() {
21 load_plugin_textdomain( 'ccfm', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
22
23 if ( apply_filters( 'ccfm_clear_cache_on_wp_activity', true ) ) {
24 add_action( 'activated_plugin', 'ccfm_clear_cache_for_wp_activity' );
25 add_action( 'deactivated_plugin', 'ccfm_clear_cache_for_wp_activity' );
26 add_action( 'upgrader_process_complete', 'ccfm_clear_cache_for_wp_activity' );
27
28 add_action( '_core_updated_successfully', 'ccfm_clear_cache_for_wp_activity' );
29 }
30 }
31 add_action( 'plugins_loaded', 'ccfm_plugins_loaded' );
32
33
34 /**
35 * Add widget save, reorder and delete detection. Thanks to Ov3rfly.
36 */
37 function ccfm_admin_init() {
38 global $pagenow;
39
40 ccfm_handle_requests();
41
42 add_action( 'wp_dashboard_setup', 'ccfm_dashboard_widget' );
43
44 //add styles for dashboard
45 add_action( 'admin_head', 'ccfm_admin_head' );
46
47 $plugin_slugs = ccfm_clear_addtional_cache( false );
48
49 if ( ccfm_supported_caching_exists() || !empty( $plugin_slugs ) ) {
50
51 if ( defined( 'QODE_ROOT' ) ) {
52 if ( 'options.php' == $pagenow ) {
53 //detect when qode options saved.
54 // add_action( 'updated_option', 'ccfm_save_clear_cache_for_qode', 10, 3 );
55 }
56 }
57
58 //detect widget save, reorder and delete detection. Thanks to Ov3rfly.
59 add_action( 'wp_ajax_save-widget', 'ccfm_clear_cache_for_widgets_wp_ajax_action', 1 );
60 add_action( 'wp_ajax_widgets-order', 'ccfm_clear_cache_for_widgets_wp_ajax_action', 1 );
61 add_action( 'sidebar_admin_setup', 'ccfm_clear_cache_for_widgets_sidebar_admin_setup' );
62 //detect customize theme actions.
63 add_action( 'customize_save_after', 'ccfm_clear_cache_for_customized_theme' );
64
65 //detect nav menu changes
66 add_action( 'wp_update_nav_menu', 'ccfm_clear_cache_for_menus' );
67
68 //detect settings page changes
69 add_filter( 'pre_set_transient_settings_errors', 'ccfm_clear_cache_for_settings' );
70
71 //detect ContactForm7 changes
72 add_action( 'wpcf7_save_contact_form', 'ccfm_clear_cache_for_cf7' );
73
74 //detect WooThemes settings changes
75 add_action( 'update_option_woo_options', 'ccfm_clear_cache_for_woo_options' );
76
77
78 if ( class_exists( 'ACF' ) ) {
79 add_action( 'save_post', 'ccfm_acf_update_fields', 10, 2 );
80 }
81
82 //detect TablePress changes
83 if ( class_exists( 'TablePress' ) ) {
84 add_action( 'save_post', 'ccfm_tablepress_update_fields', 10, 2 );
85 }
86
87 //try detect NextGen Gallery changes
88 add_action( 'ngg_update_gallery', 'ccfm_clear_cache_for_ngg' );
89 add_action( 'ngg_delete_gallery', 'ccfm_clear_cache_for_ngg' );
90 add_action( 'ngg_update_album', 'ccfm_clear_cache_for_ngg' );
91 add_action( 'ngg_update_album_sortorder', 'ccfm_clear_cache_for_ngg' );
92 add_action( 'ngg_delete_album', 'ccfm_clear_cache_for_ngg' );
93
94 //detect Formidable changes
95 add_action( 'frm_update_form', 'ccfm_clear_cache_for_formidable' );
96
97 //detect Contact Form by WP Forms changes
98 add_action( 'wpforms_builder_save_form', 'ccfm_clear_cache_for_wpforms' );
99
100 // detect Insert Headers and Footers by WPBeginner changes
101 add_action( 'update_option_ihaf_insert_header', 'ccfm_insert_headers_and_footers' );
102 add_action( 'update_option_ihaf_insert_footer', 'ccfm_insert_headers_and_footers' );
103 add_action( 'update_option_ihaf_insert_body', 'ccfm_insert_headers_and_footers' );
104
105 //trigger functions in supported plugins when Clear Cache button is clicked.
106 add_action( 'ccfm_clear_cache_for_me', 'ccfm_clear_addtional_cache', 1 );
107
108 do_action( 'ccfm_admin_init' );
109 }
110 }
111 add_action( 'admin_init', 'ccfm_admin_init' ); // not 'init'
112
113 /**
114 * Special function to add actions for hooks that are triggered before admin_init.
115 */
116 function ccfm_init_actions() {
117 //detect WooCommerce settings changes
118 add_action( 'woocommerce_settings_saved', 'ccfm_clear_cache_for_woocommerce');
119
120 if ( ccfm_option( 'btn_admin_bar', 1 ) == 1 ) {
121 $needed_cap = ccfm_option( 'btn_cap', 'manage_options' );
122 if ( current_user_can( $needed_cap ) || current_user_can( 'manage_options' ) ) {
123 add_action( 'admin_bar_menu', 'ccfm_toolbar_link', 99 );
124 }
125 }
126
127 do_action( 'ccfm_init_actions' );
128 }
129 add_action( 'init', 'ccfm_init_actions' );
130
131 /**
132 * Enqueue script for admin bar.
133 */
134 function ccfm_enqueue_admin_bar_scripts() {
135 if ( !is_user_logged_in() ) {
136 return;
137 }
138 wp_enqueue_script(
139 'ccfm-admin-bar',
140 plugins_url( '/js/admin-bar.js', CCFM_PLUGIN ),
141 array( 'jquery' ),
142 CCFM_VERSION,
143 true
144 );
145 wp_localize_script( 'ccfm-admin-bar', 'ccfm', array(
146 'ajax_url' => admin_url( 'admin-ajax.php' ),
147 'nonce' => wp_create_nonce( 'ccfm' ),
148 ) );
149 }
150 add_action( 'wp_enqueue_scripts', 'ccfm_enqueue_admin_bar_scripts' );
151 add_action( 'admin_enqueue_scripts', 'ccfm_enqueue_admin_bar_scripts' );
152
153
154 /**
155 * Return true if known caching systems exists.
156 */
157 function ccfm_supported_caching_exists() {
158 $cache_system_key = ccfm_get_caching_system_used();
159 $supported = !empty( $cache_system_key );
160 return apply_filters( 'ccfm_supported_caching_exists', $supported );
161 }
162
163 /**
164 * Set up the cache to be cleared
165 */
166 function ccfm_clear_cache_for_me( $source ) {
167 global $ccfm_source;
168 if ( isset( $ccfm_source ) ) {
169 return;
170 }
171
172 $ccfm_source = $source;
173
174 do_action( 'ccfm_clear_cache_for_me_setup', $source );
175
176 if ( defined( 'LSCWP_V' ) ) {
177 ccfm_clear_cache_for_all();
178 return;
179 }
180
181 add_action( 'shutdown', 'ccfm_clear_cache_for_all' );
182 }
183
184 /**
185 * Clear the caches!
186 * Caution: Any hooks in this function are not guaranteed to run. See notes in readme.
187 */
188 function ccfm_clear_cache_for_all() {
189 global $wp_fastest_cache, $kinsta_cache, $admin, $ccfm_source;
190
191 if ( empty( $ccfm_source ) ) {
192 $ccfm_source = '';
193 }
194
195 do_action( 'ccfm_clear_cache_for_me_before', $ccfm_source );
196
197 // if W3 Total Cache is being used, clear the cache
198 if ( function_exists( 'w3tc_pgcache_flush' ) ) {
199 w3tc_pgcache_flush();
200 }
201 // if WP Super Cache is being used, clear the cache
202 else if ( function_exists( 'wp_cache_clean_cache' ) ) {
203 global $file_prefix, $supercachedir;
204 if ( empty( $supercachedir ) && function_exists( 'get_supercache_dir' ) ) {
205 $supercachedir = get_supercache_dir();
206 }
207 wp_cache_clean_cache( $file_prefix );
208 }
209 else if ( class_exists( 'WpeCommon' ) ) {
210 //be extra careful, just in case 3rd party changes things on us
211 if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) {
212 WpeCommon::purge_memcached();
213 }
214 if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) {
215 WpeCommon::purge_varnish_cache();
216 }
217 if ( method_exists( 'WpeCommon', 'clear_cdn_cache' ) ) {
218 WpeCommon::clear_cdn_cache();
219 }
220 // deprecated
221 else if ( method_exists( 'WpeCommon', 'clear_maxcdn_cache' ) ) {
222 WpeCommon::clear_maxcdn_cache();
223 }
224 }
225 else if ( method_exists( 'WpFastestCache', 'deleteCache' ) && !empty( $wp_fastest_cache ) ) {
226 $wp_fastest_cache->deleteCache( true );
227 }
228 else if ( class_exists( '\Kinsta\Cache' ) && !empty( $kinsta_cache ) ) {
229 $kinsta_cache->kinsta_cache_purge->purge_complete_caches();
230 }
231 else if ( class_exists( '\WPaaS\Cache' ) ) {
232 ccfm_godaddy_purge();
233 }
234 else if ( class_exists( 'WP_Optimize' ) && defined( 'WPO_PLUGIN_MAIN_PATH' ) ) {
235 if (!class_exists('WP_Optimize_Cache_Commands')) include_once(WPO_PLUGIN_MAIN_PATH . 'cache/class-cache-commands.php');
236
237 if ( class_exists( 'WP_Optimize_Cache_Commands' ) ) {
238 $wpoptimize_cache_commands = new WP_Optimize_Cache_Commands();
239 $wpoptimize_cache_commands->purge_page_cache();
240 }
241 }
242 else if ( class_exists( 'Breeze_Admin' ) ) {
243 do_action('breeze_clear_all_cache');
244 }
245 else if ( defined( 'LSCWP_V' ) ) {
246 do_action( 'litespeed_purge_all' );
247 }
248 else if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
249 sg_cachepress_purge_cache();
250 }
251 else if ( class_exists( 'autoptimizeCache' ) ) {
252 autoptimizeCache::clearall();
253 }
254 else if ( class_exists( 'Cache_Enabler' ) ) {
255 Cache_Enabler::clear_total_cache();
256 }
257 else if ( function_exists( 'rocket_clean_domain' ) ) {
258 rocket_clean_domain();
259 if ( function_exists( 'rocket_clean_minify' ) ) {
260 rocket_clean_minify();
261 }
262 }
263 else if ( is_plugin_active( 'wp-cloudflare-page-cache/wp-cloudflare-super-page-cache.php' ) ) {
264 do_action( 'swcfpc_purge_cache' );
265 }
266
267 if ( ccfm_cloudflare_plugin_exists() ) {
268 ccfm_cloudflare_purge_cache();
269 }
270
271 if ( did_action( 'elementor/loaded' ) ) {
272 $elementor_inst = \Elementor\Plugin::instance();
273 if ( !empty( $elementor_inst ) && !empty( $elementor_inst->files_manager ) ) {
274 $elementor_inst->files_manager->clear_cache();
275 }
276 }
277
278 do_action( 'ccfm_clear_cache_for_me', $ccfm_source );
279
280 $clear_assets = ccfm_option( 'clear_assets', 'only' );
281
282 $time = false;
283 if ( $clear_assets == 'always' || ( $clear_assets == 'only' && $ccfm_source == 'admin-bar-button' ) ) {
284 $time = time();
285 update_option( '_ccfm_style_timestamp_theme', $time );
286 }
287
288 do_action( 'ccfm_clear_cache_for_css_js', $time );
289 }
290
291 /**
292 * Add a button to clear the cache on the dashboard.
293 */
294 function ccfm_dashboard_widget() {
295 $needed_cap = ccfm_option( 'btn_cap', 'manage_options' );
296 if ( current_user_can( $needed_cap ) || current_user_can( 'manage_options' ) ) {
297 wp_add_dashboard_widget('dashboard_ccfm_widget', 'Clear Cache for Me', 'ccfm_dashboard_widget_output');
298 }
299 }
300
301 function ccfm_dashboard_widget_output() {
302 $infotext = ccfm_option( 'btn_instructions', __( 'If you\'re not seeing your changes on your public pages, it might be cached. Click the button below to see a fresh version of your pages.', 'ccfm' ) );
303 ?>
304 <div class="ccfm_widget">
305 <form class="ccfm_widget_form" method="post">
306 <?php wp_nonce_field( 'ccfm' ); ?>
307 <?php echo ( $infotext ) ? '<p class="info">' . $infotext . '</p>' : ''; ?><div class="ccfm-button">
308 <input type="submit" name="ccfm" class="button button-primary button-large" value="<?php _e( 'Clear Cache Now!', 'ccfm' ); ?>">
309 </div>
310 </form>
311 </div>
312 <?php
313 if ( current_user_can( 'manage_options' ) ) {
314 global $wp_roles;
315 $roles = $wp_roles->roles;
316 $caps = array();
317 foreach( $roles as $role ) {
318 if ( !empty( $role['capabilities'] ) ) {
319 foreach ( $role['capabilities'] as $capability => $val ) {
320 $caps[ $capability ] = $capability;
321 }
322 }
323 }
324 asort( $caps );
325 ?>
326 <?php
327 }
328 }
329
330 /**
331 * Add some CSS.
332 */
333 function ccfm_admin_head() {
334 ?>
335 <style type="text/css">
336 #dashboard_ccfm_widget .inside {
337 margin: 0;
338 padding: 0;
339 }
340 #dashboard_ccfm_widget .ccfm_widget {
341 border-top: 1px solid #eee;
342 font-size: 13px;
343 padding: 12px;
344 }
345 #dashboard_ccfm_widget .ccfm_widget:first-child {
346 border-top: none;
347 }
348 #dashboard_ccfm_widget h4 {
349 margin-bottom: 4px;
350 }
351 #dashboard_ccfm_widget p {
352 margin: 0 0 8px 0;
353 }
354 #dashboard_ccfm_widget label {
355 display: block;
356 margin: 0 0 4px 0;
357 color: #777;
358 }
359 #dashboard_ccfm_widget .info {
360 display: block;
361 width: 100%;
362 margin-bottom: 15px;
363 }
364
365 #dashboard_ccfm_widget .ccfm-button {
366 display: inline-block;
367 width: auto;
368 text-align: left;
369 }
370
371 #dashboard_ccfm_widget .ccfm-admin-bar-msg {
372 position: absolute;
373 right: 12px;
374 bottom: 12px;
375 border-radius: 5px;
376 line-height: 32px;
377 width: 135px;
378 text-align: center;
379 }
380
381
382 </style>
383 <?php
384 }
385
386
387 /**
388 * Clear the cache if requested.
389 */
390 function ccfm_handle_requests() {
391 if ( isset( $_REQUEST['ccfm'] ) ) {
392 check_admin_referer( 'ccfm' );
393 $needed_cap = ccfm_option( 'btn_cap', 'manage_options' );
394 $is_success = 0;
395 if ( current_user_can( $needed_cap ) ) {
396 ccfm_clear_cache_for_me( 'button' );
397 $is_success = 1;
398 add_action( 'admin_notices', 'ccfm_success' );
399 }
400 else {
401 add_action( 'admin_notices', 'ccfm_error' );
402 }
403 wp_safe_redirect( admin_url() . '?ccfm_success=' . $is_success );
404 exit;
405 }
406
407 if ( isset( $_GET['ccfm_success'] ) ) {
408 if ( !empty( $_GET['ccfm_success'] ) ) {
409 add_action( 'admin_notices', 'ccfm_success' );
410 }
411 else {
412 add_action( 'admin_notices', 'ccfm_error' );
413 }
414 }
415 }
416
417 /**
418 * Show the success notice.
419 */
420 function ccfm_success() { ?>
421 <div class="updated">
422 <p><?php _e( 'Cache cleared!', 'ccfm' ); ?></p>
423 </div>
424 <?php
425 }
426
427 /**
428 * Show the success notice for saving options.
429 */
430 function ccfm_admin_success() { ?>
431 <div class="updated">
432 <p><?php _e( 'Settings Saved!', 'ccfm' ); ?></p>
433 </div>
434 <?php
435 }
436
437 /**
438 * Show the error notice.
439 */
440 function ccfm_error() { ?>
441 <div class="error">
442 <p><?php _e( 'You do not have permission to do that.', 'ccfm' ); ?></p>
443 </div>
444 <?php
445 }
446
447
448
449 /**
450 * If we have urls to uncache, setup filters.
451 * - must be at wp_head priority 1 for style_loader_src to work.
452 */
453 function ccfm_clear_cache_for_custom_styles() {
454 add_filter( 'style_loader_src', 'ccfm_show_src_version', 10, 2 );
455 add_filter( 'script_loader_src', 'ccfm_show_src_version', 10, 2 );
456 }
457 add_action( 'wp_head', 'ccfm_clear_cache_for_custom_styles', 1 );
458
459 /**
460 * Change a src to a key.
461 */
462 function ccfm_custom_src_key( $src ) {
463 $urls = apply_filters( 'ccfm_custom_src_urls', array() );
464 $src_parts = explode( '?', $src );
465 foreach( $urls as $url => $key ) {
466 if ( $src_parts[0] === $url ) {
467 return $key;
468 }
469 }
470 $template = get_template_directory_uri();
471 $child = get_stylesheet_directory_uri();
472 if ( stripos( $src, $template ) === 0 || stripos( $src, $child ) === 0 ) {
473 return 'theme';
474 }
475
476 return '';
477 }
478
479 /**
480 * Filter the version to make sure it's the latest.
481 */
482 function ccfm_show_src_version ( $src, $handle ) {
483 $key = ccfm_custom_src_key( $src );
484 $timestamp = get_option( '_ccfm_style_timestamp_' . $key, '' );
485 $dev_mode_assets = ccfm_option( 'dev_mode_assets', 0 );
486 $clear_assets = ccfm_option( 'clear_assets', 'only' );
487 if ( empty( $timestamp ) && $dev_mode_assets == 0 ) {
488 return $src;
489 }
490 if ( $dev_mode_assets == 1 ) {
491 $timestamp = time();
492 }
493 if ( empty( $timestamp ) || $clear_assets == 'never' ) {
494 return $src;
495 }
496 $src_parts = explode( '?', $src );
497 if ( count( $src_parts ) > 1 ) {
498 $query_string = $src_parts[1];
499 parse_str( $query_string, $query );
500 $query['ver'] = $timestamp;
501 return add_query_arg( $query, $src_parts[0] );
502 }
503 return add_query_arg( 'ver', $timestamp, $src );
504 }
505
506
507
508
509 /**
510 * Get option
511 */
512 function ccfm_option($name, $default='', $options = false) {
513 if (empty($options)) {
514 $options = get_option( 'ccfm_options' );
515 }
516
517 if (!empty($options) && isset($options[$name])) {
518 $ret = $options[$name];
519 }
520 else {
521 $ret = $default;
522 }
523 return $ret;
524 }
525
526 /**
527 * add a link to the WP Toolbar
528 */
529 function ccfm_toolbar_link( $wp_admin_bar ) {
530 $url = add_query_arg( '_wpnonce', wp_create_nonce( 'ccfm' ), admin_url() . '?ccfm=1' );
531 $args = array(
532 'id' => 'ccfm-link',
533 'title' => _x( 'Clear Cache For Me', 'Button in admin bar', 'ccfm' ),
534 'href' => $url
535 );
536 $wp_admin_bar->add_node( $args );
537 }
538
539 /**
540 * Return true if user has the right capabilities
541 */
542 function ccfm_ajax_ccfm() {
543 if ( !check_ajax_referer( 'ccfm', 'nonce', false ) ){
544 wp_die( 'The url you are trying to reach is no longer valid.', 401);
545 }
546 $needed_cap = ccfm_option( 'btn_cap', 'manage_options' );
547 $is_success = false;
548 if ( current_user_can( $needed_cap ) || current_user_can( 'manage_options' ) ) {
549 ccfm_clear_cache_for_me( 'admin-bar-button' );
550 $is_success = true;
551 }
552
553 wp_send_json( ['success' => $is_success ] );
554 }
555 add_action( 'wp_ajax_ccfm-ajax-ccfm', 'ccfm_ajax_ccfm' );
556
557