diff --git a/Purgeable.php b/Purgeable.php new file mode 100644 index 00000000..683bfba0 --- /dev/null +++ b/Purgeable.php @@ -0,0 +1,41 @@ +'; @@ -114,11 +114,19 @@ function nginx_admin_page_init() { add_action( 'admin_bar_menu', array( &$this, 'nginx_toolbar_purge_item' ), 100 ); } + /** + * Add the Purge Cache link to the top admin bar + * @param \WP_Admin_Bar $admin_bar + */ function nginx_toolbar_purge_item( $admin_bar ) { if ( !current_user_can( 'manage_options' ) ) { return; } - $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'all' ) ); + if (is_multisite()) { + $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'site' ) ); + } else { + $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'all' ) ); + } $nonced_url = wp_nonce_url( $purge_url, 'nginx_helper-purge_all' ); $admin_bar->add_menu( array( 'id' => 'nginx-helper-purge-all', 'title' => __( 'Purge Cache', 'nginx-helper' ), 'href' => $nonced_url, 'meta' => array( 'title' => __( 'Purge Cache', 'nginx-helper' ), ), ) ); } diff --git a/admin/install.php b/admin/install.php index 0882f2c0..257cdeb4 100644 --- a/admin/install.php +++ b/admin/install.php @@ -5,21 +5,21 @@ * Parts of code based off http://wordpress.org/extend/plugins/nginx-manager/ by http://profiles.wordpress.org/hpatoio/ and http://profiles.wordpress.org/rukbat/ */ namespace rtCamp\WP\Nginx { - if ( preg_match( '#' . basename( __FILE__ ) . '#', $_SERVER[ 'PHP_SELF' ] ) ) { + + if ( preg_match( '#' . basename( __FILE__ ) . '#', $_SERVER['PHP_SELF'] ) ) { die( 'You are not allowed to call this page directly.' ); } function rt_wp_nginx_helper_install() { - global $wp_roles, $rt_wp_nginx_helper; - - if ( ! current_user_can( 'activate_plugins' ) ) { - return; - } + if ( ! current_user_can( 'activate_plugins' ) ) { + return; + } $role = get_role( 'administrator' ); if ( empty( $role ) ) { update_site_option( "rt_wp_nginx_helper_init_check", __( 'Sorry, you need to be an administrator to use Nginx Helper', 'nginx-helper' ) ); + return; } @@ -69,31 +69,31 @@ function rt_wp_nginx_helper_remove_capability( $capability ) { } function rt_wp_nginx_helper_get_options() { - $rt_wp_nginx_helper_get_options = array( ); - $rt_wp_nginx_helper_get_options[ 'log_level' ] = 'INFO'; - $rt_wp_nginx_helper_get_options[ 'log_filesize' ] = 5; + $rt_wp_nginx_helper_get_options = array(); + $rt_wp_nginx_helper_get_options['log_level'] = 'INFO'; + $rt_wp_nginx_helper_get_options['log_filesize'] = 5; - $rt_wp_nginx_helper_get_options[ 'enable_purge' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_map' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_log' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_stamp' ] = 0; + $rt_wp_nginx_helper_get_options['enable_purge'] = 0; + $rt_wp_nginx_helper_get_options['enable_map'] = 0; + $rt_wp_nginx_helper_get_options['enable_log'] = 0; + $rt_wp_nginx_helper_get_options['enable_stamp'] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_new' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_edit' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_del' ] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_new'] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_edit'] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_del'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_new' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_edit' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_del' ] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_new'] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_edit'] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_del'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_new_comment' ] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_deleted_comment' ] = 0; + $rt_wp_nginx_helper_get_options['purge_archive_on_new_comment'] = 0; + $rt_wp_nginx_helper_get_options['purge_archive_on_deleted_comment'] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_mod' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_new_comment' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_deleted_comment' ] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_mod'] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_new_comment'] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_deleted_comment'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_method' ] = 'get_request'; + $rt_wp_nginx_helper_get_options['purge_method'] = 'get_request'; return $rt_wp_nginx_helper_get_options; } diff --git a/admin/lib/nginx-general.php b/admin/lib/nginx-general.php index 48440781..ad72e2d0 100644 --- a/admin/lib/nginx-general.php +++ b/admin/lib/nginx-general.php @@ -6,8 +6,6 @@ function nginx_general_options_page() { global $rt_wp_nginx_helper, $rt_wp_nginx_purger; - $update = 0; - $error_time = false; $error_log_filesize = false; $rt_wp_nginx_helper->options['enable_purge'] = (isset( $_POST['enable_purge'] ) and ( $_POST['enable_purge'] == 1) ) ? 1 : 0; $rt_wp_nginx_helper->options['cache_method'] = (isset( $_POST['cache_method'] ) ) ? $_POST['cache_method'] : 'enable_fastcgi'; @@ -61,7 +59,6 @@ function nginx_general_options_page() $rt_wp_nginx_helper->options['redis_prefix'] = ( isset( $_POST['redis_prefix'] ) ) ? $_POST['redis_prefix'] : 'nginx-cache:'; } update_site_option( 'rt_wp_nginx_helper_options', $rt_wp_nginx_helper->options ); - $update = 1; } $rt_wp_nginx_helper->options = get_site_option( 'rt_wp_nginx_helper_options' ); @@ -79,7 +76,6 @@ function nginx_general_options_page() /** * Check for single multiple with subdomain OR multiple with subdirectory site */ - $nginx_setting_link = '#'; if ( is_multisite() ) { if ( SUBDOMAIN_INSTALL == false ) { $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdirectories/fastcgi-cache-with-purging/'; @@ -425,10 +421,10 @@ function nginx_general_options_page() diff --git a/includes/redis-delete.php b/includes/redis-delete.php index 1be1e9ec..2061cc2b 100644 --- a/includes/redis-delete.php +++ b/includes/redis-delete.php @@ -3,7 +3,7 @@ //TODO:: phpRedis based implementation https://github.com/phpredis/phpredis#eval //include predis (php implementation for redis) -global $myredis, $rt_wp_nginx_helper, $redis_api, $lua, $rt_wp_nginx_purger; +global $myredis, $rt_wp_nginx_helper, $redis_api, $rt_wp_nginx_purger; $host = $rt_wp_nginx_helper->options['redis_hostname']; $port = $rt_wp_nginx_helper->options['redis_port']; @@ -41,25 +41,13 @@ } } -//Lua Script -$lua = <<keys( $key ); @@ -74,29 +62,16 @@ function delete_multi_keys( $key ) return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } -} -/* - * Delete all the keys from currently selected database - */ - -function flush_entire_db() -{ - global $myredis, $rt_wp_nginx_purger; - try { - if ( !empty( $myredis ) ) { - return $myredis->flushdb(); - } else { - return false; - } - } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } + return false; } -/* - Single Key Delete Example - e.g. $key can be nginx-cache:httpsGETexample.com/ +/** + * Single Key Delete + * @param string $key can be nginx-cache:httpsGETexample.com/ + * + * @return bool|int|mixed */ - function delete_single_key( $key ) { global $myredis, $redis_api, $rt_wp_nginx_purger; @@ -111,36 +86,42 @@ function delete_single_key( $key ) return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } + + return false; } -/* - Delete Keys by wildcar - e.g. $key can be nginx-cache:httpsGETexample.com* +/** + * Delete Keys by wildcard + * @param string $pattern can be nginx-cache:httpsGETexample.com* + * + * @return mixed What is returned depends on what the LUA script itself returns. */ - function delete_keys_by_wildcard( $pattern ) { - global $myredis, $lua, $redis_api, $rt_wp_nginx_purger; - /* - Lua Script block to delete multiple keys using wildcard - Script will return count i.e. number of keys deleted - if return value is 0, that means no matches were found - */ + global $myredis, $redis_api, $rt_wp_nginx_purger; + + //Lua Script + $lua = <<eval( $lua, 1, $pattern ); } else if( $redis_api == 'php-redis') { - return $myredis->eval( $lua, array( $pattern ), 1 ); + return $myredis->eval( $lua, [ $pattern ], 1 ); } } else { return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } -} -?> \ No newline at end of file + return false; +} \ No newline at end of file diff --git a/nginx-helper.php b/nginx-helper.php index a05905ce..a1a60b06 100644 --- a/nginx-helper.php +++ b/nginx-helper.php @@ -3,12 +3,12 @@ Plugin Name: Nginx Helper Plugin URI: https://rtcamp.com/nginx-helper/ Description: Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things. - Version: 1.9.7 + Version: 1.10.0 Author: rtCamp Author URI: https://rtcamp.com Text Domain: nginx-helper Requires at least: 3.0 - Tested up to: 4.5.2 + Tested up to: 4.6 */ namespace rtCamp\WP\Nginx { @@ -17,18 +17,17 @@ class Helper { - var $minium_WP = '3.0'; - var $options = null; - var $plugin_name = 'nginx-helper'; + private $minium_WP = '3.0'; + private $plugin_name = 'nginx-helper'; + public $options = null; const WP_CLI_COMMAND = 'nginx-helper'; function __construct() { - - if ( !$this->required_wp_version() ) - if ( !$this->required_php_version() ) - return; + if ( ! $this->required_wp_version() && ! version_compare(phpversion(), '5.4.0', '>=') ) { + return; + } // Load Plugin Text Domain add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); @@ -46,25 +45,12 @@ function start_helper() { global $rt_wp_nginx_purger; + + add_action( 'admin_init', array( &$this, 'purge' ) ); add_action( 'shutdown', array( &$this, 'add_timestamps' ), 99999 ); add_action( 'add_init', array( &$this, 'update_map' ) ); - - //add_action( 'save_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // add_action( 'publish_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // add_action( 'publish_page', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); add_action( 'wp_insert_comment', array( &$rt_wp_nginx_purger, 'purgePostOnComment' ), 200, 2 ); add_action( 'transition_comment_status', array( &$rt_wp_nginx_purger, 'purgePostOnCommentChange' ), 200, 3 ); - - // $args = array( '_builtin' => false ); - // $_rt_custom_post_types = get_post_types( $args ); - // if ( isset( $post_types ) && !empty( $post_types ) ) { - // if ( $this->options['rt_wp_custom_post_types'] == true ) { - // foreach ( $_rt_custom_post_types as $post_type ) { - // add_action( 'publish_' . trim( $post_type ), array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // } - // } - // } - add_action( 'transition_post_status', array( &$this, 'set_future_post_option_on_future_status' ), 20, 3 ); add_action( 'delete_post', array( &$this, 'unset_future_post_option_on_delete' ), 20, 1 ); add_action( 'nm_check_log_file_size_daily', array( &$rt_wp_nginx_purger, 'checkAndTruncateLogFile' ), 100, 1 ); @@ -74,10 +60,11 @@ function start_helper() add_action( 'edit_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 ); add_action( 'delete_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 ); add_action( 'check_ajax_referer', array( &$rt_wp_nginx_purger, 'purge_on_check_ajax_referer' ), 20, 2 ); - add_action( 'admin_init', array( &$this, 'purge_all' ) ); - // expose action to allow other plugins to purge the cache - add_action( 'rt_nginx_helper_purge_all', array( &$this, 'true_purge_all' ) ); + // expose actions to allow other plugins to purge the cache + add_action( 'rt_nginx_helper_purge_all', [ $rt_wp_nginx_purger, 'true_purge_all' ] ); + // TODO: Only works in Redis atm. + add_action( 'rt_nginx_helper_purge_wildcard', [ $rt_wp_nginx_purger, 'purgeWildcard'] ); // Load WP-CLI command if ( defined( 'WP_CLI' ) && WP_CLI ) { @@ -288,7 +275,7 @@ function show_notice() echo '

' . __( 'Purge initiated', 'nginx-helper' ) . '

'; } - function purge_all() + function purge() { if ( !isset( $_REQUEST['nginx_helper_action'] ) ) return; @@ -297,6 +284,7 @@ function purge_all() wp_die( 'Sorry, you do not have the necessary privileges to edit these options.' ); $action = $_REQUEST['nginx_helper_action']; + $what_to_purge = $_REQUEST['nginx_helper_urls']; if ( $action == 'done' ) { add_action( 'admin_notices', array( &$this, 'show_notice' ) ); @@ -304,20 +292,26 @@ function purge_all() return; } - check_admin_referer( 'nginx_helper-purge_all' ); + if ( check_admin_referer( 'nginx_helper-purge_all' ) !== 1 ) { + return; + } - switch ( $action ) { - case 'purge': - $this->true_purge_all(); - break; + if ( $action === 'purge' ) { + global $rt_wp_nginx_purger; + + switch ( $what_to_purge ) { + case 'all': + $rt_wp_nginx_purger->true_purge_all(); + break; + case 'site': + $rt_wp_nginx_purger->purgeCurrentSite(); + break; + default: + // unknown action + } } wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) ); - } - function true_purge_all() - { - global $rt_wp_nginx_purger; - $rt_wp_nginx_purger->true_purge_all(); } /** @@ -344,6 +338,7 @@ function load_plugin_textdomain() $rtwpAdminPanel = new \rtCamp\WP\Nginx\Admin(); } + require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'Purgeable.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'purger.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'redis-purger.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'compatibility.php'); @@ -352,7 +347,7 @@ function load_plugin_textdomain() $rt_wp_nginx_helper = new \rtCamp\WP\Nginx\Helper; if ( !empty( $rt_wp_nginx_helper->options['cache_method'] ) && $rt_wp_nginx_helper->options['cache_method'] == "enable_redis" ) { - $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Redispurger; + $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Redis_Purger; } else { $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Purger; } diff --git a/purger.php b/purger.php index 52042f13..45620f66 100644 --- a/purger.php +++ b/purger.php @@ -2,7 +2,7 @@ namespace rtCamp\WP\Nginx { - class Purger { + class Purger implements Purgeable { function purgePostOnComment( $comment_id, $comment ) { $oldstatus = ''; @@ -840,5 +840,22 @@ function purge_urls() { } } - } + + /** + * @inheritdoc + */ + function purgeCurrentSite() { + // TODO: Implement purgeSingleSite() method. + // For now, just purge all + $this->true_purge_all(); + } + + /** + * @inheritdoc + */ + function purgeWildcard( $pattern ) { + // TODO: Implement purgeWildcard() method. + // For now, just purge all + $this->true_purge_all(); + }} } diff --git a/redis-purger.php b/redis-purger.php index 3fbe948a..ca3bdde3 100644 --- a/redis-purger.php +++ b/redis-purger.php @@ -2,7 +2,7 @@ namespace rtCamp\WP\Nginx { - class Redispurger { + class Redis_Purger implements Purgeable { public function __construct() { @@ -38,7 +38,6 @@ function purgePostOnCommentChange( $newstatus, $oldstatus, $comment ) return; } - $_post_id = $comment->comment_post_ID; $_comment_id = $comment->comment_ID; @@ -109,6 +108,7 @@ function purgePost( $_ID ) $this->log( "Function purgePost BEGIN ===" ); if ( $rt_wp_nginx_helper->options['purge_homepage_on_edit'] == 1 ) { + $homepage_url = get_site_url(); $this->log( "Purging homepage '$homepage_url'" ); $this->_purge_homepage(); } @@ -544,7 +544,7 @@ private function _purge_all_daily_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_daily_archives = $wpdb->get_results( $_query_daily_archives ) ) { @@ -570,7 +570,7 @@ private function _purge_all_monthly_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_monthly_archives = $wpdb->get_results( $_query_monthly_archives ) ) { @@ -596,7 +596,7 @@ private function _purge_all_yearly_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_yearly_archives = $wpdb->get_results( $_query_yearly_archives ) ) { @@ -667,20 +667,32 @@ function purge_on_check_ajax_referer( $action, $result ) function true_purge_all() { global $rt_wp_nginx_helper; - $prefix = substr($rt_wp_nginx_helper->options['redis_prefix'],0, -1); - $this->log( "* * * * *" ); - $this->log( "* Purged Everything!" ); - $this->log( "* * * * *" ); - //delete_multi_keys("*"); + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + $this->log( "* * * * * * * * * * * *" ); + $this->log( "* Purged Everything! *" ); + $this->log( "* * * * * * * * * * * *" ); delete_keys_by_wildcard($prefix."*"); } - + + /** + * @inheritdoc + */ + function purgeWildcard($pattern) + { + global $rt_wp_nginx_helper; + $this->log("Purging the pattern: $pattern"); + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + delete_keys_by_wildcard($prefix . $pattern); + } + + /** + * Handles custom urls entered on the settings page under "Custom Purge Urls". + */ function purge_urls() { global $rt_wp_nginx_helper; $parse = parse_url( site_url() ); - $host = $rt_wp_nginx_helper->options['redis_hostname']; $prefix = $rt_wp_nginx_helper->options['redis_prefix']; $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; @@ -712,8 +724,24 @@ function purge_urls() } } } - } + } + } + + /** + * @inheritdoc + */ + function purgeCurrentSite() + { + global $rt_wp_nginx_helper; + + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + $parse = wp_parse_url( get_site_url() ); + // 'path' is needed on multisite-subfolder setups. + delete_keys_by_wildcard($prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*'); + + $this->log( "* * * * * * * * * * * *" ); + $this->log( "* Purged Current Site *" ); + $this->log( "* * * * * * * * * * * *" ); } - } }