Plugin Directory

Changeset 1537017


Ignore:
Timestamp:
11/20/2016 10:28:29 AM (9 years ago)
Author:
devavi
Message:

Add following new features

Purge entire site varnish cache.
Purge front page/entire site when theme is switched.

Location:
purge-varnish
Files:
26 added
3 edited

Legend:

Unmodified
Added
Removed
  • purge-varnish/trunk/class_purge_varnish.php

    r1526122 r1537017  
    1212
    1313// Exit if accessed directly
    14 if ( ! defined( 'ABSPATH' ) ) {
     14if (!defined('ABSPATH')) {
    1515  exit;
    1616}
     
    3131  function __construct() {
    3232    add_action('admin_menu', array($this, 'purge_varnish_add_menu'));
    33     add_filter('plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'purge_varnish_settings_link'));
     33    add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'purge_varnish_settings_link'));
    3434    register_activation_hook(__FILE__, array($this, 'purge_varnish_install'));
    3535    register_deactivation_hook(__FILE__, array($this, 'purge_varnish_uninstall'));
     
    4141  function purge_varnish_add_menu() {
    4242    add_menu_page('Purge Varnish', 'Purge Varnish', 'manage_options', 'purge-varnish-settings', array(
    43       __CLASS__,'purge_varnish_page_file_path'), plugins_url('images/purge16x16.png', __FILE__), '2.2.9');
    44    
    45     if ( current_user_can('manage_options') ) {
     43      __CLASS__, 'purge_varnish_page_file_path'), plugins_url('images/purge16x16.png', __FILE__), '2.2.9');
     44
     45    if (current_user_can('manage_options')) {
    4646      add_submenu_page('purge-varnish-settings', 'Terminal', 'Terminal', 'manage_options', 'purge-varnish-settings', array(
    4747        __CLASS__, 'purge_varnish_page_file_path'));
     48      add_submenu_page('purge-varnish-settings', 'Purge all', 'Purge all', 'manage_options', 'purge-varnish-all', array(
     49        __CLASS__, 'purge_varnish_page_file_path'));
    4850
    4951      add_submenu_page('purge-varnish-settings', 'Expire', 'Expire', 'manage_options', 'purge-varnish-expire', array(
     
    5153    }
    5254
    53     if ( current_user_can('edit_posts') ) {
     55    if (current_user_can('edit_posts')) {
    5456      add_submenu_page('purge-varnish-settings', 'Purge URLs', 'Purge URLs', 'edit_posts', 'purge-varnish-urls', array(
    55         __CLASS__, 'purge_varnish_page_file_path' ));
     57        __CLASS__, 'purge_varnish_page_file_path'));
    5658    }
    5759  }
     
    6163   */
    6264  function purge_varnish_settings_link($links) {
    63     $settings_link = '<a href="'.esc_url( admin_url( '/admin.php?page=purge-varnish-settings' )).'">' . __( 'Settings', 'Purge Varnish' ) . '</a>';
    64     array_unshift( $links, $settings_link );
     65    $settings_link = '<a href="' . esc_url(admin_url('/admin.php?page=purge-varnish-settings')) . '">' . __('Settings', 'Purge Varnish') . '</a>';
     66    array_unshift($links, $settings_link);
    6567    return $links;
    6668  }
    67    
     69
    6870  /**
    6971   * Implements hook for register styles and scripts.
    7072   */
    7173  function purge_varnish_register_styles() {
    72     wp_enqueue_style('purge-varnish',  plugins_url( '/css/purge_varnish.css', __FILE__ ), false, '1.0');
    73     wp_enqueue_script('purge-varnish', plugins_url( '/js/purge_varnish.js', __FILE__ ), false, '1.0');
     74    wp_enqueue_style('purge-varnish', plugins_url('/css/purge_varnish.css', __FILE__), false, '1.0');
     75    wp_enqueue_script('purge-varnish', plugins_url('/js/purge_varnish.js', __FILE__), false, '1.0');
    7476  }
    7577
     
    8385    if (strpos($screen->base, 'purge-varnish-settings') !== false) {
    8486      require_once('includes/terminal.php');
     87    }
     88    elseif (strpos($screen->base, 'purge-varnish-all') !== false) {
     89      require_once('includes/purge_all.php');
    8590    }
    8691    elseif (strpos($screen->base, 'purge-varnish-expire') !== false) {
     
    263268
    264269        if (isset($server) && (filter_var($server, FILTER_VALIDATE_IP) === false)) {
    265           $message['error']['terminal_' . $server] = $server .' '. esc_html_e('is not a valid IP address');
     270          $message['error']['terminal_' . $server] = $server . ' ' . esc_html_e('is not a valid IP address');
    266271        }
    267272        if (empty($port) || (int) $port <= 0) {
     
    287292  }
    288293
    289   function purge_varnish_get_command($url, $is_front = NULL) {
     294  /**
     295   * Build command to purge the cache.
     296   */
     297 
     298  function purge_varnish_get_command($url, $flag = NULL) {
    290299    $parse_url = $this->purge_varnish_parse_url($url);
    291300    $host = $parse_url['host'];
    292301    $path = $parse_url['path'];
    293302    $command = "ban req.http.host == \"$host\" && req.url ~ \"^$path$\"";
    294     if ($is_front == true) {
     303    if ($flag == 'front') {
    295304      $command = "ban req.http.host == \"$host\" && req.url ~ \"^$path/$\"";
    296305    }
     306    elseif ($flag == 'purgeall') {
     307      $command = "ban req.http.host == \"$host\"";
     308    }
    297309
    298310    return $command;
     311  }
     312
     313  /**
     314   * Helper function to clear all varnish cache.
     315   */
     316  function purge_varnish_all_cache_manually() {
     317    $url = get_home_url();
     318    $parse_url = $this->purge_varnish_parse_url($url);
     319    $host = $parse_url['host'];
     320   
     321    $command = "ban req.http.host == \"$host\"";
     322    $response = $this->purge_varnish_terminal_run(array($command));
     323   
     324    $varnish_control_terminal = get_option('varnish_control_terminal', '');
     325    $terminals = explode(' ', $varnish_control_terminal);
     326   
     327    $msg = '';
     328    foreach ($terminals as $terminal) {
     329      $resp_stats = $response[$terminal][$command];
     330      $stats_code = $resp_stats['code'];
     331      $stats_msg = $resp_stats['msg'];
     332     
     333      if ($stats_code == 200) {
     334        $stats_msg = 'The page url <a href="' . esc_url($_POST['url']) . '" target="@target" style="color:#228B22;"><i>"' . esc_url($parse_url['path']) . '"</i></a> has been purged.';
     335        $msg .= '<li style="color:#228B22;list-style-type: circle;">All Varnish cache has been purged successfuly!</li>';
     336      }
     337      else {
     338        $msg .= '<li style="color:#8B0000;list-style-type: circle;">There is an error, Please try later!</li>';
     339      }
     340    }
     341
     342    return $msg;
    299343  }
    300344
     
    305349    $outout = '';
    306350    $purge_msg = '';
    307     if(!count($urls)) {
    308       return '<ul><li style="color:#8B0000;">' . esc_html_e('Please enter at leat one url for purge.') .'</li></ul>';
    309     }
    310    
     351    if (!count($urls)) {
     352      return '<ul><li style="color:#8B0000;">' . esc_html_e('Please enter at leat one url for purge.') . '</li></ul>';
     353    }
     354
    311355    foreach ($urls as $url) {
    312356      $url = trim(esc_url($url));
     
    354398
    355399      if ($stats_code == 200) {
    356         if(esc_url($parse_url['path']) == '/') {
     400        if (esc_url($parse_url['path']) == '/') {
    357401          $stats_msg = 'The <a href="' . esc_url($_POST['url']) . '" target="@target" style="color:#228B22;"><i>"front/home page url"</i></a> has been purged.';
    358402          $msg .= '<li style="color:#228B22;list-style-type: circle;">' . $stats_msg . '</li>';
    359403        }
    360         else{
     404        else {
    361405          $stats_msg = 'The page url <a href="' . esc_url($_POST['url']) . '" target="@target" style="color:#228B22;"><i>"' . esc_url($parse_url['path']) . '"</i></a> has been purged.';
    362406          $msg .= '<li style="color:#228B22;list-style-type: circle;">' . $stats_msg . '</li>';
     
    378422  function purge_varnish_front_page() {
    379423    $url = get_home_url();
    380     $command = $this->purge_varnish_get_command($url, true);
     424    $command = $this->purge_varnish_get_command($url, 'front');
     425    $this->purge_varnish_terminal_run(array($command));
     426  }
     427 
     428  /*
     429   * Purges entire cache.
     430   */
     431
     432  function purge_varnish_all_cache_automatically() {
     433    $url = get_home_url();
     434    $command = $this->purge_varnish_get_command($url, 'front');
    381435    $this->purge_varnish_terminal_run(array($command));
    382436  }
     
    631685   * Callback for generate the logs.
    632686   */
     687
    633688  function purge_varnish_debug($basedir, $logtext) {
    634    
     689
    635690    if (defined('WP_VARNISH_PURGE_DEBUG') && WP_VARNISH_PURGE_DEBUG == true) {
    636691      $filename = $basedir . '/purge_varnish_log.txt';
     
    645700    }
    646701  }
    647  
     702
    648703  /*
    649704   * Callback to validate post once.
    650705   */
     706
    651707  function purge_varnish_nonce($vp_nonce) {
    652708    $wp_nonce = $_REQUEST['_wpnonce'];
    653709    $referer_nonce = $vp_nonce . '_referer';
    654710
    655     if (!wp_verify_nonce($wp_nonce, $vp_nonce ) ) {
     711    if (!wp_verify_nonce($wp_nonce, $vp_nonce)) {
    656712      return '<ul><li style="color:#8B0000;">Sorry! Invalid nonce.</li></ul>';
    657713    }
    658    
     714
    659715    return true;
    660716  }
    661717
    662    /*
    663     * Sanitize actions values
    664     */
     718  /*
     719   * Sanitize actions values
     720   */
     721
    665722  function purge_varnish_sanitize_actions($post) {
    666723    $actions = array(
     
    671728      'comment_trash' => 'trash_comment',
    672729      'navmenu_insert_update' => 'wp_update_nav_menu',
     730      'theme_switch' => 'after_switch_theme',
    673731    );
    674732
    675733    $sanitize = array();
    676     foreach($post as $key => $value) {
    677       if(array_key_exists($key, $actions) && ($actions[$key] == $value)) {
     734    foreach ($post as $key => $value) {
     735      if (array_key_exists($key, $actions) && ($actions[$key] == $value)) {
    678736        $sanitize[$key] = $value;
    679737      }
     
    681739    return $sanitize;
    682740  }
    683  
     741
    684742  /*
    685743   * Sanitize tiggers values
    686744   */
     745
    687746  function purge_varnish_sanitize_tiggers($post) {
    688747    $tiggers = array(
     
    694753      'navmenu_front_page' => 'front_page',
    695754      'navmenu_menu_link' => 'post_item',
     755      'wp_theme_front_page' => 'front_page',
     756      'wp_theme_purge_all' => 'purge_all',
    696757    );
    697    
     758
    698759    $sanitize = array();
    699     foreach($post as $key => $value) {
    700       if(array_key_exists($key, $tiggers) && ($tiggers[$key] == $value)) {
     760    foreach ($post as $key => $value) {
     761      if (array_key_exists($key, $tiggers) && ($tiggers[$key] == $value)) {
    701762        $sanitize[$key] = $value;
    702763      }
    703764    }
    704765    return $sanitize;
     766  }
     767 
     768  /*
     769   * Actions perform on activation of plugin
     770   */
     771
     772  function purge_varnish_switch_theme_trigger($theme) {
     773    $purge_varnish_expire = get_option('purge_varnish_expire', '');
     774    if (!empty($purge_varnish_expire)) {
     775      $expire = unserialize($purge_varnish_expire);
     776      if (is_array($expire)) {
     777        foreach ($expire as $page) {
     778          switch ($page) {
     779            case 'front_page':
     780              $this->purge_varnish_front_page();
     781              break;
     782
     783            case 'purge_all':
     784              $this->purge_varnish_all_cache_automatically();
     785              break;
     786          }
     787        }
     788      }
     789    }
    705790  }
    706791
     
    718803
    719804  function purge_varnish_uninstall() {
    720     delete_option( 'varnish_version' );
    721     delete_option( 'varnish_control_terminal' );
    722     delete_option( 'varnish_control_key' );
    723     delete_option( 'varnish_socket_timeout' );
    724     delete_option( 'varnish_bantype' );
    725     delete_option( 'purge_varnish_action' );
    726     delete_option( 'purge_varnish_expire' );
     805    delete_option('varnish_version');
     806    delete_option('varnish_control_terminal');
     807    delete_option('varnish_control_key');
     808    delete_option('varnish_socket_timeout');
     809    delete_option('varnish_bantype');
     810    delete_option('purge_varnish_action');
     811    delete_option('purge_varnish_expire');
    727812  }
    728813
     
    765850          add_action($action, array($purge_varnish, 'purge_varnish_update_nav_menu_trigger'));
    766851          break;
     852       
     853        case 'after_switch_theme':
     854          add_action($action, array($purge_varnish, 'purge_varnish_switch_theme_trigger'));
     855          break;
    767856      }
    768857    }
  • purge-varnish/trunk/includes/expire.php

    r1526122 r1537017  
    1212if (isset($_POST['save_configurations']) && $_POST['save_configurations']) {
    1313  if ($purge_varnish->purge_varnish_nonce('pvEsetting') == true) {
    14     $sanitize_actions = $purge_varnish->purge_varnish_sanitize_actions($_POST['purge_varnish_action']);
     14    $post_actions                   = (array) $_POST['purge_varnish_action'];
     15    $sanitize_actions               = $purge_varnish->purge_varnish_sanitize_actions($post_actions);
    1516    update_option('purge_varnish_action', serialize($sanitize_actions));
    1617
    17     $sanitize_tiggers = $purge_varnish->purge_varnish_sanitize_tiggers($_POST['purge_varnish_expire']);
     18    $post_expire                    = (array) $_POST['purge_varnish_expire'];
     19    $sanitize_tiggers               = $purge_varnish->purge_varnish_sanitize_tiggers($post_expire);   
    1820    update_option('purge_varnish_expire', serialize($sanitize_tiggers));
    1921  }
     
    2123
    2224// Get the post action options value
    23 $purge_varnish_action   = get_option('purge_varnish_action', '');
    24 $post_insert_update     = '';
    25 $post_status            = '';
    26 $post_trash             = '';
    27 $comment_insert_update  = '';
    28 $comment_trash          = '';
    29 $navmenu_insert_update  = '';
     25$purge_varnish_action               = get_option('purge_varnish_action', '');
     26$post_insert_update                 = '';
     27$post_status                        = '';
     28$post_trash                         = '';
     29$comment_insert_update              = '';
     30$comment_trash                      = '';
     31$navmenu_insert_update              = '';
     32$after_switch_theme                 = '';
    3033if (!empty($purge_varnish_action)) {
    3134  $action = unserialize($purge_varnish_action);
    32   $post_insert_update     = isset($action['post_insert_update']) ? $action['post_insert_update'] : '';
    33   $post_status            = isset($action['post_status']) ? $action['post_status'] : '';
    34   $post_trash             = isset($action['post_trash']) ? $action['post_trash'] : '';
    35   $comment_insert_update  = isset($action['comment_insert_update']) ? $action['comment_insert_update'] : '';
    36   $comment_trash          = isset($action['comment_trash']) ? $action['comment_trash'] : '';;
    37   $navmenu_insert_update  = isset($action['navmenu_insert_update']) ? $action['navmenu_insert_update'] : '';
     35  $post_insert_update               = isset($action['post_insert_update']) ? $action['post_insert_update'] : '';
     36  $post_status                      = isset($action['post_status']) ? $action['post_status'] : '';
     37  $post_trash                       = isset($action['post_trash']) ? $action['post_trash'] : '';
     38  $comment_insert_update            = isset($action['comment_insert_update']) ? $action['comment_insert_update'] : '';
     39  $comment_trash                    = isset($action['comment_trash']) ? $action['comment_trash'] : '';;
     40  $navmenu_insert_update            = isset($action['navmenu_insert_update']) ? $action['navmenu_insert_update'] : '';
     41  $switch_theme                     = isset($action['theme_switch']) ? $action['theme_switch'] : '';
    3842}
    3943
    4044// Get the post expire options value
    41 $purge_varnish_expire = get_option('purge_varnish_expire', '');
    42 $expire_post_front_page       = '';
    43 $expire_post_post_item        = '';
    44 $expire_post_category_page    = '';
    45 $expire_comment_front_page    = '';
    46 $expire_comment_post_item     = '';
    47 $expire_navmenu_front_page    = '';
    48 $expire_navmenu_link          = '';
     45$purge_varnish_expire               = get_option('purge_varnish_expire', '');
     46$expire_post_front_page             = '';
     47$expire_post_post_item              = '';
     48$expire_post_category_page          = '';
     49$expire_comment_front_page          = '';
     50$expire_comment_post_item           = '';
     51$expire_navmenu_front_page          = '';
     52$expire_navmenu_link                = '';
     53$purge_all                          = '';
    4954if (!empty($purge_varnish_expire)) {
    5055  $expire = unserialize($purge_varnish_expire);
    51   $expire_post_front_page     = isset($expire['post_front_page']) ? $action['post_front_page'] : '';
    52   $expire_post_post_item      = isset($expire['post_post_item']) ? $action['post_post_item'] : '';
    53   $expire_post_category_page  = isset($expire['post_category_page']) ? $action['post_category_page'] : '';
    54   $expire_comment_front_page  = isset($expire['comment_front_page']) ? $action['comment_front_page'] : '';
    55   $expire_comment_post_item   = isset($expire['comment_post_item']) ? $action['comment_post_item'] : '';
    56   $expire_navmenu_front_page  = isset($expire['navmenu_front_page']) ? $action['navmenu_front_page'] : '';
    57   $expire_navmenu_link        = isset($expire['navmenu_menu_link']) ? $action['navmenu_menu_link'] : '';
     56  $expire_post_front_page         = isset($expire['post_front_page']) ? $expire['post_front_page'] : '';
     57  $expire_post_post_item          = isset($expire['post_post_item']) ? $expire['post_post_item'] : '';
     58  $expire_post_category_page      = isset($expire['post_category_page']) ? $expire['post_category_page'] : '';
     59  $expire_comment_front_page      = isset($expire['comment_front_page']) ? $expire['comment_front_page'] : '';
     60  $expire_comment_post_item       = isset($expire['comment_post_item']) ? $expire['comment_post_item'] : '';
     61  $expire_navmenu_front_page      = isset($expire['navmenu_front_page']) ? $expire['navmenu_front_page'] : '';
     62  $expire_navmenu_link            = isset($expire['navmenu_menu_link']) ? $expire['navmenu_menu_link'] : '';
     63  $expire_wp_theme_front_page     = isset($expire['wp_theme_front_page']) ? $expire['wp_theme_front_page'] : '';
     64  $expire_wp_theme_purge_all      = isset($expire['wp_theme_purge_all']) ? $expire['wp_theme_purge_all'] : '';
    5865}
    5966?>
     
    6572      <li><a href="javascript:void(0)" class="tablinks" onclick="open_menu(event, 'comment_expiration')"><?php esc_html_e('Comment expiration'); ?></a></li>
    6673      <li><a href="javascript:void(0)" class="tablinks" onclick="open_menu(event, 'menu_expiration')"><?php esc_html_e('Menu links expiration'); ?></a></li>
     74      <li><a href="javascript:void(0)" class="tablinks" onclick="open_menu(event, 'switch_theme')"><?php esc_html_e('Switch Theme'); ?></a></li>
    6775    </ul>
    6876    <form action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
     
    200208                  }
    201209                  ?> /> <label><?php esc_html_e('Menu links'); ?></label> <br /> <span class="desc"><?php esc_html_e('Expire url of menu links'); ?></span><br />
     210                </p>
     211              </td>
     212            </tr>
     213          </tbody>
     214        </table>
     215      </div>
     216     
     217      <div id="switch_theme" class="tabcontent">
     218        <h3><?php esc_html_e('Switch theme expiration'); ?></h3>
     219        <table cellpadding="5">
     220          <tbody>
     221            <tr>
     222              <th width="20%"></th>
     223              <td width="80%">
     224                <b><?php esc_html_e('Theme actions') ?></b>
     225                <p>
     226                  <input type="checkbox" name="purge_varnish_action[theme_switch]" value="after_switch_theme" <?php
     227                  if ($switch_theme == 'after_switch_theme') {
     228                    print 'checked="checked"';
     229                  }
     230                  ?> /> <label><?php esc_html_e('switch theme'); ?></label> <br /><span class="desc"><?php esc_html_e('Trigger when theme is switched.'); ?></span><br />
     231                </p>
     232              </td>
     233            </tr>
     234            <tr>
     235              <th> </th>
     236              <td>
     237                <b><?php esc_html_e('What URLs should be expired when theme switch action is triggered?') ?></b>
     238                <p>
     239                  <input type="checkbox" name="purge_varnish_expire[wp_theme_front_page]" value="front_page" <?php
     240                  if ($expire_wp_theme_front_page == 'front_page') {
     241                    print 'checked="checked"';
     242                  }
     243                  ?> /> <label><?php esc_html_e('Front page') ?></label> <br /><span class="desc"><?php esc_html_e('Expire url of the site front page'); ?></span><br />
     244                  <input type="checkbox" name="purge_varnish_expire[wp_theme_purge_all]" value="purge_all" <?php
     245                  if ($expire_wp_theme_purge_all == 'purge_all') {
     246                    print 'checked="checked"';
     247                  }
     248                  ?> /> <label>All varnish cache</label> <br /> <span class="desc"><?php esc_html_e('Expire/Purge all varnish cache'); ?></span>
    202249                </p>
    203250              </td>
  • purge-varnish/trunk/readme.txt

    r1530370 r1537017  
    33Tags: varnish, purge, cache, caching
    44Requires at least: 3.0
    5 Tested up to: 4.6.1
    6 Stable tag: 1.0.0.2
     5Tested up to: 4.7
     6Stable tag: 1.0.0.3
    77License: GPLv2 or later
    88
     
    2222*   Custom URLs
    2323
    24 <strong>Avaliable Features:</strong>
     24<strong>Features:</strong>
    2525
    2626*   admin-socket integration and Varnish admin interface for status etc.
    2727*   Unlimited number of Varnish Cache servers
    2828*   Configurable actions upon events that will expire URLs from varnish cache like reverse proxy caches.
     29    *   The front page.
     30    *   The post/page created/updated/status changed.
     31    *   Any categories or tags associated with the page.
     32    *   The menu created/updated.
     33    *   Changing theme.
    2934*   Purge multiple URLs manually from Varnish cache.
     35*   Purge whole site cache manually.
    3036*   Debugging
    3137
     
    70765. Purge URLs screen to purge varnish cache object manually.
    7177
     785. Purge whole site cache.
     79
    7280== ChangeLog ==
    7381
Note: See TracChangeset for help on using the changeset viewer.