PluginProbe
Clear Cache for Me / 2.1
Clear Cache for Me v2.1
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 2.1, at clear-cache-for-widgets.php

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