Advertisement
swte

Clear cache menu bar for editors

Sep 6th, 2018
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. add_action('admin_bar_menu', function ($admin_bar){
  2.     if (current_user_can('edit_posts')){
  3.         $current_page = site_url(str_replace(site_url(), '', 'http'.(isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']));
  4.         list($current_page_nq,) = explode('?',$current_page);
  5.  
  6.         $admin_bar->add_menu(array(
  7.             'id'    => 'swift-performance',
  8.             'title' => SWIFT_PERFORMANCE_PLUGIN_NAME,
  9.             'href'  => '#'
  10.          ));
  11.  
  12.         if(Swift_Performance::check_option('enable-caching', 1)){
  13.             $admin_bar->add_menu(array(
  14.                 'id'    => 'clear-swift-cache',
  15.                 'parent' => 'swift-performance',
  16.                 'title' => esc_html__('Clear All Cache', 'swift-performance'),
  17.                 'href'  => esc_url(wp_nonce_url(add_query_arg('swift-performance-action', 'clear-all-cache', $current_page), 'clear-swift-cache')),
  18.             ));
  19.  
  20.             if (!is_admin() && Swift_Performance_Cache::is_cached($current_page_nq)){
  21.                 $admin_bar->add_menu(array(
  22.                     'id'    => 'swift-cache-single',
  23.                     'parent' => 'swift-performance',
  24.                     'title' => esc_html__('Clear Page Cache', 'swift-performance'),
  25.                     'href'  => esc_url(wp_nonce_url(add_query_arg(array('swift-performance-action' => 'clear-page-cache', 'permalink' => urlencode($current_page_nq)), $current_page), 'clear-swift-cache')),
  26.                 ));
  27.                 $admin_bar->add_menu(array(
  28.                     'id'    => 'swift-view-cached',
  29.                     'parent' => 'swift-performance',
  30.                     'title' => esc_html__('View Cached', 'swift-performance'),
  31.                     'href'  => esc_url(add_query_arg('force-cached', '1', $current_page_nq)),
  32.                 ));
  33.             }
  34.         }
  35.         if(Swift_Performance::check_option('enable-caching', 1, '!=') && (Swift_Performance::check_option('merge-scripts', 1) || Swift_Performance::check_option('merge-styles', 1))){
  36.             $admin_bar->add_menu(array(
  37.                 'id'    => 'clear-swift-assets-cache',
  38.                 'parent' => 'swift-performance',
  39.                 'title' => esc_html__('Clear Assets Cache', 'swift-performance'),
  40.                 'href'  => esc_url(wp_nonce_url(add_query_arg('swift-performance-action', 'clear-assets-cache', $current_page), 'clear-swift-assets-cache')),
  41.             ));
  42.         }
  43.         if (Swift_Performance::check_option('enable-cdn',1) && Swift_Performance::check_option('maxcdn-key','','!=') && Swift_Performance::check_option('maxcdn-secret','','!=')){
  44.             $admin_bar->add_menu(array(
  45.                 'id'    => 'purge-swift-cdn',
  46.                 'parent' => 'swift-performance',
  47.                 'title' => esc_html__('Purge CDN (All zones)', 'swift-performance'),
  48.                 'href'  => esc_url(wp_nonce_url(add_query_arg('swift-performance-action', 'purge-cdn', $current_page), 'purge-swift-cdn')),
  49.             ));
  50.         }
  51.     }
  52. },100);
  53.  
  54. // Clear caches
  55. add_action('init', function(){
  56.     if (!isset($_GET['swift-performance-action'])){
  57.         return;
  58.     }
  59.  
  60.     if ($_GET['swift-performance-action'] == 'clear-all-cache' && current_user_can('edit_posts') && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'clear-swift-cache')){
  61.         Swift_Performance_Cache::clear_all_cache();
  62.         Swift_Performance::add_notice(esc_html__('All cache cleared', 'swift-performance'), 'success');
  63.     }
  64.  
  65.     if ($_GET['swift-performance-action'] == 'clear-assets-cache' && current_user_can('edit_posts') && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'clear-swift-assets-cache')){
  66.         Swift_Performance_Asset_Manager::clear_assets_cache();
  67.         Swift_Performance::add_notice(esc_html__('Assets cache cleared', 'swift-performance'), 'success');
  68.     }
  69.  
  70.     if ($_GET['swift-performance-action'] == 'purge-cdn' && current_user_can('edit_posts') && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'purge-swift-cdn')){
  71.         if (Swift_Performance::check_option('enable-caching', 1)){
  72.             Swift_Performance_Cache::clear_all_cache();
  73.         }
  74.         else if (Swift_Performance::check_option('merge-scripts',1) || Swift_Performance::check_option('merge-styles',1)){
  75.             Swift_Performance_Asset_Manager::clear_assets_cache();
  76.         }
  77.         else {
  78.             Swift_Performance_CDN_Manager::purge_cdn();
  79.         }
  80.     }
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement