Changeset 1537017
- Timestamp:
- 11/20/2016 10:28:29 AM (9 years ago)
- Location:
- purge-varnish
- Files:
-
- 26 added
- 3 edited
-
assets/screenshot-6.png (added)
-
tags/1.0.0.3 (added)
-
tags/1.0.0.3/class_purge_varnish.php (added)
-
tags/1.0.0.3/css (added)
-
tags/1.0.0.3/css/purge_varnish.css (added)
-
tags/1.0.0.3/images (added)
-
tags/1.0.0.3/images/error.png (added)
-
tags/1.0.0.3/images/ok.png (added)
-
tags/1.0.0.3/images/purge16x16.png (added)
-
tags/1.0.0.3/includes (added)
-
tags/1.0.0.3/includes/expire.php (added)
-
tags/1.0.0.3/includes/purge_all.php (added)
-
tags/1.0.0.3/includes/purge_urls.php (added)
-
tags/1.0.0.3/includes/terminal.php (added)
-
tags/1.0.0.3/js (added)
-
tags/1.0.0.3/js/purge_varnish.js (added)
-
tags/1.0.0.3/readme.txt (added)
-
tags/1.0.0.3/vcl (added)
-
tags/1.0.0.3/vcl/4.x (added)
-
tags/1.0.0.3/vcl/4.x/default.vcl (added)
-
tags/1.0.0.3/vcl/4.x/varnish (added)
-
trunk/class_purge_varnish.php (modified) (18 diffs)
-
trunk/includes/expire.php (modified) (4 diffs)
-
trunk/includes/purge_all.php (added)
-
trunk/readme.txt (modified) (3 diffs)
-
trunk/vcl (added)
-
trunk/vcl/4.x (added)
-
trunk/vcl/4.x/default.vcl (added)
-
trunk/vcl/4.x/varnish (added)
Legend:
- Unmodified
- Added
- Removed
-
purge-varnish/trunk/class_purge_varnish.php
r1526122 r1537017 12 12 13 13 // Exit if accessed directly 14 if ( ! defined( 'ABSPATH' )) {14 if (!defined('ABSPATH')) { 15 15 exit; 16 16 } … … 31 31 function __construct() { 32 32 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')); 34 34 register_activation_hook(__FILE__, array($this, 'purge_varnish_install')); 35 35 register_deactivation_hook(__FILE__, array($this, 'purge_varnish_uninstall')); … … 41 41 function purge_varnish_add_menu() { 42 42 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')) { 46 46 add_submenu_page('purge-varnish-settings', 'Terminal', 'Terminal', 'manage_options', 'purge-varnish-settings', array( 47 47 __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')); 48 50 49 51 add_submenu_page('purge-varnish-settings', 'Expire', 'Expire', 'manage_options', 'purge-varnish-expire', array( … … 51 53 } 52 54 53 if ( current_user_can('edit_posts')) {55 if (current_user_can('edit_posts')) { 54 56 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')); 56 58 } 57 59 } … … 61 63 */ 62 64 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); 65 67 return $links; 66 68 } 67 69 68 70 /** 69 71 * Implements hook for register styles and scripts. 70 72 */ 71 73 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'); 74 76 } 75 77 … … 83 85 if (strpos($screen->base, 'purge-varnish-settings') !== false) { 84 86 require_once('includes/terminal.php'); 87 } 88 elseif (strpos($screen->base, 'purge-varnish-all') !== false) { 89 require_once('includes/purge_all.php'); 85 90 } 86 91 elseif (strpos($screen->base, 'purge-varnish-expire') !== false) { … … 263 268 264 269 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'); 266 271 } 267 272 if (empty($port) || (int) $port <= 0) { … … 287 292 } 288 293 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) { 290 299 $parse_url = $this->purge_varnish_parse_url($url); 291 300 $host = $parse_url['host']; 292 301 $path = $parse_url['path']; 293 302 $command = "ban req.http.host == \"$host\" && req.url ~ \"^$path$\""; 294 if ($ is_front == true) {303 if ($flag == 'front') { 295 304 $command = "ban req.http.host == \"$host\" && req.url ~ \"^$path/$\""; 296 305 } 306 elseif ($flag == 'purgeall') { 307 $command = "ban req.http.host == \"$host\""; 308 } 297 309 298 310 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; 299 343 } 300 344 … … 305 349 $outout = ''; 306 350 $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 311 355 foreach ($urls as $url) { 312 356 $url = trim(esc_url($url)); … … 354 398 355 399 if ($stats_code == 200) { 356 if (esc_url($parse_url['path']) == '/') {400 if (esc_url($parse_url['path']) == '/') { 357 401 $stats_msg = 'The <a href="' . esc_url($_POST['url']) . '" target="@target" style="color:#228B22;"><i>"front/home page url"</i></a> has been purged.'; 358 402 $msg .= '<li style="color:#228B22;list-style-type: circle;">' . $stats_msg . '</li>'; 359 403 } 360 else {404 else { 361 405 $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.'; 362 406 $msg .= '<li style="color:#228B22;list-style-type: circle;">' . $stats_msg . '</li>'; … … 378 422 function purge_varnish_front_page() { 379 423 $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'); 381 435 $this->purge_varnish_terminal_run(array($command)); 382 436 } … … 631 685 * Callback for generate the logs. 632 686 */ 687 633 688 function purge_varnish_debug($basedir, $logtext) { 634 689 635 690 if (defined('WP_VARNISH_PURGE_DEBUG') && WP_VARNISH_PURGE_DEBUG == true) { 636 691 $filename = $basedir . '/purge_varnish_log.txt'; … … 645 700 } 646 701 } 647 702 648 703 /* 649 704 * Callback to validate post once. 650 705 */ 706 651 707 function purge_varnish_nonce($vp_nonce) { 652 708 $wp_nonce = $_REQUEST['_wpnonce']; 653 709 $referer_nonce = $vp_nonce . '_referer'; 654 710 655 if (!wp_verify_nonce($wp_nonce, $vp_nonce )) {711 if (!wp_verify_nonce($wp_nonce, $vp_nonce)) { 656 712 return '<ul><li style="color:#8B0000;">Sorry! Invalid nonce.</li></ul>'; 657 713 } 658 714 659 715 return true; 660 716 } 661 717 662 /* 663 * Sanitize actions values 664 */ 718 /* 719 * Sanitize actions values 720 */ 721 665 722 function purge_varnish_sanitize_actions($post) { 666 723 $actions = array( … … 671 728 'comment_trash' => 'trash_comment', 672 729 'navmenu_insert_update' => 'wp_update_nav_menu', 730 'theme_switch' => 'after_switch_theme', 673 731 ); 674 732 675 733 $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)) { 678 736 $sanitize[$key] = $value; 679 737 } … … 681 739 return $sanitize; 682 740 } 683 741 684 742 /* 685 743 * Sanitize tiggers values 686 744 */ 745 687 746 function purge_varnish_sanitize_tiggers($post) { 688 747 $tiggers = array( … … 694 753 'navmenu_front_page' => 'front_page', 695 754 'navmenu_menu_link' => 'post_item', 755 'wp_theme_front_page' => 'front_page', 756 'wp_theme_purge_all' => 'purge_all', 696 757 ); 697 758 698 759 $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)) { 701 762 $sanitize[$key] = $value; 702 763 } 703 764 } 704 765 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 } 705 790 } 706 791 … … 718 803 719 804 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'); 727 812 } 728 813 … … 765 850 add_action($action, array($purge_varnish, 'purge_varnish_update_nav_menu_trigger')); 766 851 break; 852 853 case 'after_switch_theme': 854 add_action($action, array($purge_varnish, 'purge_varnish_switch_theme_trigger')); 855 break; 767 856 } 768 857 } -
purge-varnish/trunk/includes/expire.php
r1526122 r1537017 12 12 if (isset($_POST['save_configurations']) && $_POST['save_configurations']) { 13 13 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); 15 16 update_option('purge_varnish_action', serialize($sanitize_actions)); 16 17 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); 18 20 update_option('purge_varnish_expire', serialize($sanitize_tiggers)); 19 21 } … … 21 23 22 24 // 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 = ''; 30 33 if (!empty($purge_varnish_action)) { 31 34 $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'] : ''; 38 42 } 39 43 40 44 // 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 = ''; 49 54 if (!empty($purge_varnish_expire)) { 50 55 $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'] : ''; 58 65 } 59 66 ?> … … 65 72 <li><a href="javascript:void(0)" class="tablinks" onclick="open_menu(event, 'comment_expiration')"><?php esc_html_e('Comment expiration'); ?></a></li> 66 73 <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> 67 75 </ul> 68 76 <form action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post"> … … 200 208 } 201 209 ?> /> <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> 202 249 </p> 203 250 </td> -
purge-varnish/trunk/readme.txt
r1530370 r1537017 3 3 Tags: varnish, purge, cache, caching 4 4 Requires at least: 3.0 5 Tested up to: 4. 6.16 Stable tag: 1.0.0. 25 Tested up to: 4.7 6 Stable tag: 1.0.0.3 7 7 License: GPLv2 or later 8 8 … … 22 22 * Custom URLs 23 23 24 <strong> AvaliableFeatures:</strong>24 <strong>Features:</strong> 25 25 26 26 * admin-socket integration and Varnish admin interface for status etc. 27 27 * Unlimited number of Varnish Cache servers 28 28 * 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. 29 34 * Purge multiple URLs manually from Varnish cache. 35 * Purge whole site cache manually. 30 36 * Debugging 31 37 … … 70 76 5. Purge URLs screen to purge varnish cache object manually. 71 77 78 5. Purge whole site cache. 79 72 80 == ChangeLog == 73 81
Note: See TracChangeset
for help on using the changeset viewer.