diff --git a/.gitignore b/.gitignore index d2169487..8eaa26b1 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ tests/functional/node_modules/ /tests/functional/reports/ selenium-*.jar vendor +.vscode/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..28c34e27 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,42 @@ +sudo: required + +language: node_js +node_js: +- "0.10" + + +before_install: +- rm -rf ~/.gnupg + + +before_script: +- export PLUGIN_DIR=$(pwd) +- ls -l +- sudo rm -rf /etc/mysql/ +- sudo apt-get -qq purge mysql* graphviz* +- sudo apt-get -qq autoremove +- sudo apt-get update +- sudo bash -c 'echo -e "[user]\n\tname = abc\n\temail = root@localhost.com" > /home/travis/.gitconfig' +- sudo wget -qO ee rt.cx/ee && sudo bash ee +- source /etc/bash_completion.d/ee_auto.rc +- sudo ee -v +- sudo lsb_release -a +- sudo ee site create wp.localtest.me --wpfc --user=ADMINUSER --email=prabuddha.chakraborty@rtcamp.com --pass=ADMINPASS +- sudo chmod 777 -R /var/www +- sudo nginx -t +- sudo service nginx reload +- cd /var/www/wp.localtest.me/htdocs/wp-content/plugins/ && ls +- wp --allow-root plugin deactivate w3-total-cache +- wp --allow-root plugin delete nginx-helper +- mkdir nginx-helper +- cd $PLUGIN_DIR +- cp -Rf * /var/www/wp.localtest.me/htdocs/wp-content/plugins/nginx-helper/ +- cd /var/www/wp.localtest.me/htdocs/ +- wp plugin activate --all +- cd $PLUGIN_DIR +- cd tests/functional/ +- npm install -g nightwatch +- npm install + +script: +- nightwatch diff --git a/README.md b/README.md index e328e9b7..651a7add 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,15 @@ # Nginx Helper # -**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123 +**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint + **Tags:** nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks **Requires at least:** 3.0 -**Tested up to:** 4.2.2 -**Stable tag:** 1.9.3 +**Tested up to:** 4.9.5 +**Stable tag:** 1.9.11 **License:** GPLv2 or later (of-course) **License URI:** http://www.gnu.org/licenses/gpl-2.0.html **Donate Link:** http://rtcamp.com/donate/ -Cleans nginx's fastcgi/proxy cache or redis-cahce whenever a post is edited/published. Also does few more things. +Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things. ## Description ## @@ -21,11 +22,11 @@ Cleans nginx's fastcgi/proxy cache or redis-cahce whenever a post is edited/publ You will need to follow one or more tutorials below to get desired functionality: -* [Nginx Map + WordPress-Multisite + Static Files Handling](http://rtcamp.com/tutorials/nginx-maps-wordpress-multisite-static-files-handling/) -* [Nginx + WordPress + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-nginx-fastcgi-cache-purge-conditional/) -* [Nginx + WordPress-Multisite (Subdirectories) + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-multisite-subdirectories-nginx-fastcgi-cache-purge/) -* [Nginx + WordPress-Multisite (Subdomains/domain-mapping) + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-multisite-subdomains-domain-mapping-nginx-fastcgi-cache-purge/) -* [Other WordPress-Nginx Tutorials](http://rtcamp.com/wordpress-nginx/tutorials/) +* [Nginx Map + WordPress-Multisite + Static Files Handling](https://easyengine.io/wordpress-nginx/tutorials/multisite/static-files-handling/) +* [Nginx + WordPress + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/) +* [Nginx + WordPress-Multisite (Subdirectories) + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/multisite/subdirectories/fastcgi-cache-with-purging/) +* [Nginx + WordPress-Multisite (Subdomains/domain-mapping) + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/multisite/subdomains/fastcgi-cache-with-purging/) +* [Other WordPress-Nginx Tutorials](https://easyengine.io/wordpress-nginx/tutorials/) ## Installation ## @@ -45,13 +46,13 @@ For proper configuration, check out our **tutorial list** in the [Description ta ## Frequently Asked Questions ## -**Important** - Please refer to [http://rtcamp.com/nginx-helper/faq](http://rtcamp.com/nginx-helper/faq) for up-to-date FAQs. +**Important** - Please refer to [https://easyengine.io/nginx-helper/faq](https://easyengine.io/nginx-helper/faq) for up-to-date FAQs. ### FAQ - Installation/Comptability ### **Q. Will this work out of the box?** -No. You need to make some changes at the Nginx end. Please check our [tutorial list](http://rtcamp.com/wordpress-nginx/tutorials). +No. You need to make some changes at the Nginx end. Please check our [tutorial list](https://easyengine.io/wordpress-nginx/tutorials/). ### FAQ - Nginx Fastcgi Cache Purge ### @@ -109,15 +110,48 @@ Most likely yes. A wordpress plugin, if not using explicitly any Apache-only mod Please post your problem in [our free support forum](http://community.rtcamp.com/c/wordpress-nginx). ## Screenshots ## -###1. Nginx plugin settings### +### 1. Nginx plugin settings ### ![Nginx plugin settings](https://ps.w.org/nginx-helper/assets/screenshot-1.png) -###2. Remaining settings### +### 2. Remaining settings ### ![Remaining settings](https://ps.w.org/nginx-helper/assets/screenshot-2.png) ## Changelog ## +### 1.9.11 ### +* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick +* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor) + +### 1.9.10 ### +* Fixed issue where Nginx cache folders deleted on purge. [#123](https://github.com/rtCamp/nginx-helper/pull/123) - by [johan-chassaing](https://github.com/johan-chassaing) +* Fixed Redis purge all feature for installation where WordPress lives in a separate folder. [#130](https://github.com/rtCamp/nginx-helper/pull/130) - by [pySilver](https://github.com/pySilver) + +### 1.9.9 ### +* Fix wp_redirect issue. [#131](https://github.com/rtCamp/nginx-helper/pull/131) - by [matt-h](https://github.com/matt-h) + +### 1.9.8 ### +* Fixed homepage cache cleared when WPML plugin used [#116](https://github.com/rtCamp/nginx-helper/pull/116) - by [Niwreg](https://profiles.wordpress.org/niwreg/) +* Fixed Purge Cache clears the whole Redis cache [#113](https://github.com/rtCamp/nginx-helper/issues/113) - by HansVanEijsden +* One log file for all site in WPMU. +* Single site Redis cache purge when click on Purge Cache button in WPMU [#122](https://github.com/rtCamp/nginx-helper/pull/122) - by Lars Støttrup Nielsen +* Fixed notices and warnings. + +### 1.9.7 ### +* Remove timestamp if cron or wp-cli [#114](https://github.com/rtCamp/nginx-helper/pull/114) - by [samedwards](https://profiles.wordpress.org/samedwards/) +* Fixed notices and warnings. + +### 1.9.6 ### +* Fixed cache purging on post publish. +* Error fixed when redis server not installed. + +### 1.9.5 ### +Added custom purge URL option. + +### 1.9.4 ### +* Added redis server connection timeout. +* Added RedisException handling. + ### 1.9.3 ### * Added PhpRedis API support. * Added redis-lua script support to purge complete cache very fast. @@ -315,6 +349,6 @@ Fix url escaping [#82](https://github.com/rtCamp/nginx-helper/pull/82) - by ## Upgrade Notice ## -### 1.9.3 ### -* Added PhpRedis API support. -* Fixed cache purging link in admin bar. +### 1.9.11 ### +* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick +* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor) diff --git a/admin/class-fastcgi-purger.php b/admin/class-fastcgi-purger.php index 3f382298..d93cdf16 100644 --- a/admin/class-fastcgi-purger.php +++ b/admin/class-fastcgi-purger.php @@ -19,104 +19,108 @@ */ class FastCGI_Purger extends Purger { - public function purgeUrl( $url, $feed = true ) { - global $nginx_helper_admin; - - $this->log( "- Purging URL | " . $url ); - - $parse = parse_url( $url ); - - switch ( $nginx_helper_admin->options['purge_method'] ) { - case 'unlink_files': - $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . $parse['path']; - $_url_purge = $_url_purge_base; - - if ( isset( $parse['query']) && $parse['query'] != '' ) { - $_url_purge .= '?' . $parse['query']; - } - - $this->_delete_cache_file_for( $_url_purge ); - - if ( $feed ) { - $feed_url = rtrim( $_url_purge_base, '/' ) . '/feed/'; - $this->_delete_cache_file_for( $feed_url ); - $this->_delete_cache_file_for( $feed_url . 'atom/' ); - $this->_delete_cache_file_for( $feed_url . 'rdf/' ); - } - break; - case 'get_request': - // Go to default case - default: - $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge' . $parse['path']; - $_url_purge = $_url_purge_base; - - if ( isset( $parse['query']) && '' != $parse['query'] ) { - $_url_purge .= '?' . $parse['query']; - } - - $this->_do_remote_get( $_url_purge ); - - if ( $feed ) { - $feed_url = rtrim($_url_purge_base, '/' ) . '/feed/'; - $this->_do_remote_get( $feed_url ); - $this->_do_remote_get( $feed_url . 'atom/' ); - $this->_do_remote_get( $feed_url . 'rdf/' ); - } - break; - } - } - - public function customPurgeUrls() { - global $nginx_helper_admin; - - $parse = parse_url( site_url() ); - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, false ); - - switch ( $nginx_helper_admin->options['purge_method'] ) { - case 'unlink_files': - $_url_purge_base = $parse['scheme'] . '://' . $parse['host']; - - if( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - foreach ( $purge_urls as $purge_url ) { - $purge_url = trim( $purge_url ); - - if ( strpos($purge_url, '*' ) === false ) { - $purge_url = $_url_purge_base . $purge_url; - $this->log( "- Purging URL | " . $url ); - $this->_delete_cache_file_for( $purge_url ); - } - } - } - break; - case 'get_request': - // Go to default case - default: - $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge'; - - if( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - foreach ( $purge_urls as $purge_url ) { - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - $purge_url = $_url_purge_base . $purge_url; - $this->log( "- Purging URL | " . $url ); - $this->_do_remote_get( $purge_url ); - } - } - } - break; - } - } - - public function purgeAll() { - $this->unlinkRecursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); - $this->log( "* * * * *" ); - $this->log( "* Purged Everything!" ); - $this->log( "* * * * *" ); - } + public function purgeUrl( $url, $feed = true ) { + global $nginx_helper_admin; + + $this->log( '- Purging URL | ' . $url ); + + $parse = parse_url( $url ); + + if ( ! isset( $parse['path'] ) ) { + $parse['path'] = ''; + } + + switch ( $nginx_helper_admin->options['purge_method'] ) { + case 'unlink_files': + $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . $parse['path']; + $_url_purge = $_url_purge_base; + + if ( isset( $parse['query'] ) && $parse['query'] != '' ) { + $_url_purge .= '?' . $parse['query']; + } + + $this->_delete_cache_file_for( $_url_purge ); + + if ( $feed ) { + $feed_url = rtrim( $_url_purge_base, '/' ) . '/feed/'; + $this->_delete_cache_file_for( $feed_url ); + $this->_delete_cache_file_for( $feed_url . 'atom/' ); + $this->_delete_cache_file_for( $feed_url . 'rdf/' ); + } + break; + case 'get_request': + // Go to default case + default: + $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge' . $parse['path']; + $_url_purge = $_url_purge_base; + + if ( isset( $parse['query'] ) && '' != $parse['query'] ) { + $_url_purge .= '?' . $parse['query']; + } + + $this->_do_remote_get( $_url_purge ); + + if ( $feed ) { + $feed_url = rtrim( $_url_purge_base, '/' ) . '/feed/'; + $this->_do_remote_get( $feed_url ); + $this->_do_remote_get( $feed_url . 'atom/' ); + $this->_do_remote_get( $feed_url . 'rdf/' ); + } + break; + } + } + + public function customPurgeUrls() { + global $nginx_helper_admin; + + $parse = parse_url( site_url() ); + + $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? + explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); + + // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed + $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, false ); + + switch ( $nginx_helper_admin->options['purge_method'] ) { + case 'unlink_files': + $_url_purge_base = $parse['scheme'] . '://' . $parse['host']; + + if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { + foreach ( $purge_urls as $purge_url ) { + $purge_url = trim( $purge_url ); + + if ( strpos( $purge_url, '*' ) === false ) { + $purge_url = $_url_purge_base . $purge_url; + $this->log( '- Purging URL | ' . $url ); + $this->_delete_cache_file_for( $purge_url ); + } + } + } + break; + case 'get_request': + // Go to default case + default: + $_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge'; + + if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { + foreach ( $purge_urls as $purge_url ) { + $purge_url = trim( $purge_url ); + + if ( strpos( $purge_url, '*' ) === false ) { + $purge_url = $_url_purge_base . $purge_url; + $this->log( '- Purging URL | ' . $url ); + $this->_do_remote_get( $purge_url ); + } + } + } + break; + } + } + + public function purgeAll() { + $this->unlinkRecursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); + $this->log( '* * * * *' ); + $this->log( '* Purged Everything!' ); + $this->log( '* * * * *' ); + } } diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index ba0ffb7e..5e06365f 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -40,7 +40,7 @@ class Nginx_Helper_Admin { */ private $version; - /** + /** * Various settings tabs. * * @since 2.0.0 @@ -49,7 +49,7 @@ class Nginx_Helper_Admin { */ private $settings_tabs; - /** + /** * Purge options. * * @since 2.0.0 @@ -58,7 +58,7 @@ class Nginx_Helper_Admin { */ public $options; - /** + /** * WP-CLI Command. * * @since 2.0.0 @@ -71,28 +71,30 @@ class Nginx_Helper_Admin { * Initialize the class and set its properties. * * @since 2.0.0 - * @param string $plugin_name The name of this plugin. - * @param string $version The version of this plugin. + * @param string $plugin_name The name of this plugin. + * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; - $this->version = $version; - /** - * Define settings tabs - */ - $this->settings_tabs = apply_filters( 'rt_nginx_helper_settings_tabs', array( - 'general' => array( - 'menu_title' => __( 'General', 'nginx-helper' ), - 'menu_slug' => 'general' - ), - 'support' => array( - 'menu_title' => __( 'Support', 'nginx-helper' ), - 'menu_slug' => 'support' - ) ) - ); - - $this->options = $this->nginx_helper_settings(); + $this->version = $version; + /** + * Define settings tabs + */ + $this->settings_tabs = apply_filters( + 'rt_nginx_helper_settings_tabs', array( + 'general' => array( + 'menu_title' => __( 'General', 'nginx-helper' ), + 'menu_slug' => 'general', + ), + 'support' => array( + 'menu_title' => __( 'Support', 'nginx-helper' ), + 'menu_slug' => 'support', + ), + ) + ); + + $this->options = $this->nginx_helper_settings(); } /** @@ -102,7 +104,7 @@ public function __construct( $plugin_name, $version ) { */ public function enqueue_styles( $hook ) { - /** + /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function @@ -114,10 +116,10 @@ public function enqueue_styles( $hook ) { * class. */ - if ( 'settings_page_nginx' != $hook ) { - return; - } - wp_enqueue_style( $this->plugin_name.'-icons', plugin_dir_url( __FILE__ ) . 'icons/css/nginx-fontello.css', array(), $this->version, 'all' ); + if ( 'settings_page_nginx' != $hook ) { + return; + } + wp_enqueue_style( $this->plugin_name . '-icons', plugin_dir_url( __FILE__ ) . 'icons/css/nginx-fontello.css', array(), $this->version, 'all' ); wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/nginx-helper-admin.css', array(), $this->version, 'all' ); } @@ -140,160 +142,168 @@ public function enqueue_scripts( $hook ) { * class. */ - if ( 'settings_page_nginx' != $hook ) { - return; - } - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/nginx-helper-admin.js', array( 'jquery' ), $this->version, false ); - } + if ( 'settings_page_nginx' != $hook ) { + return; + } + wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/nginx-helper-admin.js', array( 'jquery' ), $this->version, false ); + } - /** + /** * Add admin menu. * * @since 2.0.0 */ - public function nginx_helper_admin_menu() { - - if ( is_multisite() ) { - add_submenu_page( - 'settings.php', - __( 'Nginx Helper', 'nginx-helper' ), - __( 'Nginx Helper', 'nginx-helper' ), - 'manage_options', - 'nginx', - array( &$this, 'nginx_helper_setting_page' ) - ); - } else { - add_submenu_page( - 'options-general.php', - __( 'Nginx Helper', 'nginx-helper' ), - __( 'Nginx Helper', 'nginx-helper' ), - 'manage_options', - 'nginx', - array( &$this, 'nginx_helper_setting_page' ) - ); - } - } - - public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) { - if ( ! current_user_can( 'manage_options' ) ) { - return; - } - - $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' ); - $wp_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' ) ), - ) - ); - } - - /** - * Display settings. - * @global $string $pagenow Contain current admin page. - * - * @since 2.0.0 - */ - public function nginx_helper_setting_page() { - include 'partials/nginx-helper-admin-display.php'; - } - - /** - * Default settings. - * - * @since 2.0.0 - * @return array - */ - public function nginx_helper_default_settings() { - return array( - 'enable_purge' => 0, - 'cache_method' => '', - 'purge_method' => '', - 'enable_map' => 0, - 'enable_log' => 0, - 'log_level' => 'INFO', - 'log_filesize' => '5', - 'enable_stamp' => 0, - 'purge_homepage_on_new' => 0, - 'purge_homepage_on_edit' => 0, - 'purge_homepage_on_del' => 0, - 'purge_archive_on_new' => 0, - 'purge_archive_on_edit' => 0, - 'purge_archive_on_del' => 0, - 'purge_archive_on_new_comment' => 0, - 'purge_archive_on_deleted_comment' => 0, - 'purge_page_on_mod' => 0, - 'purge_page_on_new_comment' => 0, - 'purge_page_on_deleted_comment' => 0, - 'redis_hostname' => '127.0.0.1', - 'redis_port' => '6379', - 'redis_prefix' => 'nginx-cache:', - 'purge_url' => '', - ); - } - - /** - * Get settings. - * - * @since 2.0.0 - */ - public function nginx_helper_settings() { - return wp_parse_args( - get_site_option( 'rt_wp_nginx_helper_options', array() ), - $this->nginx_helper_default_settings() - ); - } - - public function nginx_helper_settings_link( $links ) { - - if ( is_network_admin() ) { - $setting_page = 'settings.php'; - } else { - $setting_page = 'options-general.php'; - } - - $settings_link = '' . __( 'Settings', 'nginx-helper' ) . ''; - array_unshift( $links, $settings_link ); - return $links; - } - - /** + public function nginx_helper_admin_menu() { + + if ( is_multisite() ) { + add_submenu_page( + 'settings.php', + __( 'Nginx Helper', 'nginx-helper' ), + __( 'Nginx Helper', 'nginx-helper' ), + 'manage_options', + 'nginx', + array( &$this, 'nginx_helper_setting_page' ) + ); + } else { + add_submenu_page( + 'options-general.php', + __( 'Nginx Helper', 'nginx-helper' ), + __( 'Nginx Helper', 'nginx-helper' ), + 'manage_options', + 'nginx', + array( &$this, 'nginx_helper_setting_page' ) + ); + } + } + + public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) { + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + $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' ); + $wp_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' ) ), + ) + ); + } + + /** + * Display settings. + * + * @global $string $pagenow Contain current admin page. + * + * @since 2.0.0 + */ + public function nginx_helper_setting_page() { + include 'partials/nginx-helper-admin-display.php'; + } + + /** + * Default settings. + * + * @since 2.0.0 + * @return array + */ + public function nginx_helper_default_settings() { + return array( + 'enable_purge' => 0, + 'cache_method' => '', + 'purge_method' => '', + 'enable_map' => 0, + 'enable_log' => 0, + 'log_level' => 'INFO', + 'log_filesize' => '5', + 'enable_stamp' => 0, + 'purge_homepage_on_new' => 0, + 'purge_homepage_on_edit' => 0, + 'purge_homepage_on_del' => 0, + 'purge_archive_on_new' => 0, + 'purge_archive_on_edit' => 0, + 'purge_archive_on_del' => 0, + 'purge_archive_on_new_comment' => 0, + 'purge_archive_on_deleted_comment' => 0, + 'purge_page_on_mod' => 0, + 'purge_page_on_new_comment' => 0, + 'purge_page_on_deleted_comment' => 0, + 'redis_hostname' => '127.0.0.1', + 'redis_port' => '6379', + 'redis_prefix' => 'nginx-cache:', + 'purge_url' => '', + ); + } + + /** + * Get settings. + * + * @since 2.0.0 + */ + public function nginx_helper_settings() { + return wp_parse_args( + get_site_option( 'rt_wp_nginx_helper_options', array() ), + $this->nginx_helper_default_settings() + ); + } + + public function nginx_helper_settings_link( $links ) { + + if ( is_network_admin() ) { + $setting_page = 'settings.php'; + } else { + $setting_page = 'options-general.php'; + } + + $settings_link = '' . __( 'Settings', 'nginx-helper' ) . ''; + array_unshift( $links, $settings_link ); + return $links; + } + + /** * Retrieve the asset path. * * @since 2.0.0 * @return string asset path of the plugin. */ public function functional_asset_path() { - $log_dir = wp_upload_dir(); - $log_path = $log_dir['basedir'] . '/nginx-helper/'; - return apply_filters( 'nginx_asset_path', $log_path ); + $log_path = WP_CONTENT_DIR . '/uploads/nginx-helper/'; + + return apply_filters( 'nginx_asset_path', $log_path ); + } - /** + /** * Retrieve the asset url. * * @since 2.0.0 * @return string asset url of the plugin. */ public function functional_asset_url() { - $log_dir = wp_upload_dir(); - $log_url = $log_dir['baseurl'] . '/nginx-helper/'; - return apply_filters( 'nginx_asset_url', $log_url ); + $log_url = WP_CONTENT_URL . '/uploads/nginx-helper/'; + + return apply_filters( 'nginx_asset_url', $log_url ); + } - /** - * Get latest news. - * - * @since 2.0.0 - */ - public function nginx_helper_get_feeds() { - // Get RSS Feed(s) - require_once( ABSPATH . WPINC . '/feed.php' ); + /** + * Get latest news. + * + * @since 2.0.0 + */ + public function nginx_helper_get_feeds() { + // Get RSS Feed(s) + require_once ABSPATH . WPINC . '/feed.php'; $maxitems = 0; // Get a SimplePie feed object from the specified feed source. $rss = fetch_feed( 'http://rtcamp.com/blog/feed/' ); @@ -305,247 +315,262 @@ public function nginx_helper_get_feeds() { } ?> - options['enable_purge'] != 1 || $this->options['enable_stamp'] != 1 ) { - return; - } - - foreach ( headers_list() as $header ) { - list( $key, $value ) = explode( ':', $header, 2 ); - if ( 'Content-Type' == $key && strpos( trim( $value ), 'text/html' ) !== 0 ) { - return; - } - if ( 'Content-Type' == $key ) { - break; - } - } - - if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { - return; - } - - $timestamps = "\n\n" . - ""; - - echo $timestamps; - } - - /** - * Get map - * @global type $wpdb - * @return string - */ - public function get_map() { - - if ( !$this->options['enable_map'] ) { - return; - } - - if ( is_multisite() ) { - global $wpdb; - - $rt_all_blogs = $wpdb->get_results( - $wpdb->prepare( - "SELECT blog_id, domain, path FROM " . $wpdb->blogs . " WHERE site_id = %d AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", - $wpdb->siteid - ) - ); - - $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping'; - - $rt_domain_map_sites = ''; - if ( $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->dmtable}'") == $wpdb->dmtable ) { - $rt_domain_map_sites = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY id DESC" ); - } - - $rt_nginx_map = ""; - $rt_nginx_map_array = array(); - - if ( $rt_all_blogs ) - foreach ( $rt_all_blogs as $blog ) { - if ( "yes" == SUBDOMAIN_INSTALL ) { - $rt_nginx_map_array[ $blog->domain ] = $blog->blog_id; - } else { - if ( 1 != $blog->blog_id ) { - $rt_nginx_map_array[ $blog->path ] = $blog->blog_id; - } - } - } - - if ( $rt_domain_map_sites ) { - foreach ( $rt_domain_map_sites as $site ) { - $rt_nginx_map_array[ $site->domain ] = $site->blog_id; - } - } - - foreach ( $rt_nginx_map_array as $domain => $domain_id ) { - $rt_nginx_map .= "\t" . $domain . "\t" . $domain_id . ";\n"; - } - - return $rt_nginx_map; - } - } - - /** - * Update map - */ - public function update_map() { - - if ( is_multisite() ) { - $rt_nginx_map = $this->get_map(); - - if ( $fp = fopen( $this->functional_asset_path() . 'map.conf', 'w+' ) ) { - fwrite( $fp, $rt_nginx_map ); - fclose( $fp ); - } - } - } - - /** - * Purge url when post status is changed. - * - * @global type $blog_id - * @global object $nginx_purger - * @param string $new_status - * @param string $old_status - * @param object $post - */ - public function set_future_post_option_on_future_status( $new_status, $old_status, $post ) { - global $blog_id, $nginx_purger; - - if ( !$this->options['enable_purge'] ) { - return; - } - - $purge_status = array( 'publish', 'future' ); - - if ( in_array( $old_status, $purge_status ) || in_array( $new_status, $purge_status ) ) { - $nginx_purger->log( "Purge post on transition post STATUS from " . $old_status . " to " . $new_status ); - $nginx_purger->purgePost( $post->ID ); - } - - if ( 'future' == $new_status ) { - if ( $post && 'future' == $post->post_status && - ( ( 'post' == $post->post_type || 'page' == $post->post_type ) || - ( isset( $this->options['custom_post_types_recognized'] ) && - in_array( $post->post_type, $this->options['custom_post_types_recognized'] ) ) ) ) { - - $nginx_purger->log( "Set/update future_posts option ( post id = " . $post->ID . " and blog id = " . $blog_id . " )" ); - $this->options['future_posts'][ $blog_id ][ $post->ID ] = strtotime( $post->post_date_gmt ) + 60; - update_site_option( "rt_wp_nginx_helper_global_options", $this->options ); - } - } - } - - /** - * Unset future post option on delete - * @global type $blog_id - * @global type $nginx_purger - * @param type $post_id - */ - public function unset_future_post_option_on_delete( $post_id ) { - global $blog_id, $nginx_purger; - - if ( !$this->options['enable_purge'] ) { - return; - } - - if ( $post_id && !wp_is_post_revision( $post_id ) ) { - if ( isset( $this->options['future_posts'][ $blog_id ][ $post_id ] ) && - count( $this->options['future_posts'][ $blog_id ][ $post_id] ) ) { - - $nginx_purger->log( "Unset future_posts option ( post id = " . $post_id . " and blog id = " . $blog_id . " )" ); - unset( $this->options['future_posts'][ $blog_id ][ $post_id ] ); - update_site_option( "rt_wp_nginx_helper_global_options", $this->options ); - - if ( !count( $this->options['future_posts'][ $blog_id ] ) ) { - unset( $this->options['future_posts'][ $blog_id ] ); - update_site_option( "rt_wp_nginx_helper_global_options", $this->options ); - } - } - } - } - - /** - * Update map when new blog added in multisite. - * @global type $nginx_purger - * @param type $blog_id - */ - public function update_new_blog_options( $blog_id ) { - global $rt_wp_nginx_purger; - - $nginx_purger->log( "New site added ( id $blog_id )" ); - $this->update_map(); - $nginx_purger->log( "New site added to nginx map ( id $blog_id )" ); - $helper_options = $this->nginx_helper_default_settings(); - update_blog_option( $blog_id, "rt_wp_nginx_helper_options", $helper_options ); - $nginx_purger->log( "Default options updated for the new blog ( id $blog_id )" ); - } - - /** - * Purge all urls. - * @global type $nginx_purger - */ - public function purge_all() { - global $nginx_purger; - - if ( !isset( $_REQUEST['nginx_helper_action'] ) ) { - return; - } - - if ( !current_user_can( 'manage_options' ) ) { - wp_die( 'Sorry, you do not have the necessary privileges to edit these options.' ); - } - - $action = $_REQUEST['nginx_helper_action']; - - if ( 'done' == $action ) { - add_action( 'admin_notices', array( &$this, 'display_notices' ) ); - add_action( 'network_admin_notices', array( &$this, 'display_notices' ) ); - return; - } - - check_admin_referer( 'nginx_helper-purge_all' ); - - switch ( $action ) { - case 'purge': - $nginx_purger->purgeAll(); - break; - } - - wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) ); - } - - /** - * Dispay plugin notices. - */ - public function display_notices() { - echo '

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

'; - } + options['enable_purge'] != 1 || $this->options['enable_stamp'] != 1 ) { + return; + } + + foreach ( headers_list() as $header ) { + list( $key, $value ) = explode( ':', $header, 2 ); + if ( 'Content-Type' === $key && strpos( trim( $value ), 'text/html' ) !== 0 ) { + return; + } + if ( 'Content-Type' === $key ) { + break; + } + } + + /** + * Don't add timestamp if run from ajax, cron or wpcli. + */ + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + return; + } + if ( defined( 'DOING_CRON' ) && DOING_CRON ) { + return; + } + if ( defined( 'WP_CLI' ) && WP_CLI ) { + return; + } + + $timestamps = "\n\n" . + ''; + + echo $timestamps; + } + + /** + * Get map + * + * @global type $wpdb + * @return string + */ + public function get_map() { + + if ( ! $this->options['enable_map'] ) { + return; + } + + if ( is_multisite() ) { + global $wpdb; + + $rt_all_blogs = $wpdb->get_results( + $wpdb->prepare( + 'SELECT blog_id, domain, path FROM ' . $wpdb->blogs . " WHERE site_id = %d AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", + $wpdb->siteid + ) + ); + + $wpdb->dmtable = $wpdb->base_prefix . 'domain_mapping'; + + $rt_domain_map_sites = ''; + if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->dmtable}'" ) == $wpdb->dmtable ) { + $rt_domain_map_sites = $wpdb->get_results( "SELECT blog_id, domain FROM {$wpdb->dmtable} ORDER BY id DESC" ); + } + + $rt_nginx_map = ''; + $rt_nginx_map_array = array(); + + if ( $rt_all_blogs ) { + foreach ( $rt_all_blogs as $blog ) { + if ( 'yes' === SUBDOMAIN_INSTALL ) { + $rt_nginx_map_array[ $blog->domain ] = $blog->blog_id; + } else { + if ( 1 !== $blog->blog_id ) { + $rt_nginx_map_array[ $blog->path ] = $blog->blog_id; + } + } + } + } + + if ( $rt_domain_map_sites ) { + foreach ( $rt_domain_map_sites as $site ) { + $rt_nginx_map_array[ $site->domain ] = $site->blog_id; + } + } + + foreach ( $rt_nginx_map_array as $domain => $domain_id ) { + $rt_nginx_map .= "\t" . $domain . "\t" . $domain_id . ";\n"; + } + + return $rt_nginx_map; + } + } + + /** + * Update map + */ + public function update_map() { + + if ( is_multisite() ) { + $rt_nginx_map = $this->get_map(); + + if ( $fp = fopen( $this->functional_asset_path() . 'map.conf', 'w+' ) ) { + fwrite( $fp, $rt_nginx_map ); + fclose( $fp ); + } + } + } + + /** + * Purge url when post status is changed. + * + * @global type $blog_id + * @global object $nginx_purger + * @param string $new_status + * @param string $old_status + * @param object $post + */ + public function set_future_post_option_on_future_status( $new_status, $old_status, $post ) { + global $blog_id, $nginx_purger; + + if ( ! $this->options['enable_purge'] ) { + return; + } + + $purge_status = array( 'publish', 'future' ); + + if ( in_array( $old_status, $purge_status ) || in_array( $new_status, $purge_status ) ) { + $nginx_purger->log( 'Purge post on transition post STATUS from ' . $old_status . ' to ' . $new_status ); + $nginx_purger->purgePost( $post->ID ); + } + + if ( 'future' == $new_status ) { + if ( $post && 'future' == $post->post_status && + ( ( 'post' == $post->post_type || 'page' == $post->post_type ) || + ( isset( $this->options['custom_post_types_recognized'] ) && + in_array( $post->post_type, $this->options['custom_post_types_recognized'] ) ) ) ) { + + $nginx_purger->log( 'Set/update future_posts option ( post id = ' . $post->ID . ' and blog id = ' . $blog_id . ' )' ); + $this->options['future_posts'][ $blog_id ][ $post->ID ] = strtotime( $post->post_date_gmt ) + 60; + update_site_option( 'rt_wp_nginx_helper_global_options', $this->options ); + } + } + } + + /** + * Unset future post option on delete + * + * @global type $blog_id + * @global type $nginx_purger + * @param type $post_id + */ + public function unset_future_post_option_on_delete( $post_id ) { + global $blog_id, $nginx_purger; + + if ( ! $this->options['enable_purge'] ) { + return; + } + + if ( $post_id && ! wp_is_post_revision( $post_id ) ) { + if ( isset( $this->options['future_posts'][ $blog_id ][ $post_id ] ) && + count( $this->options['future_posts'][ $blog_id ][ $post_id ] ) ) { + + $nginx_purger->log( 'Unset future_posts option ( post id = ' . $post_id . ' and blog id = ' . $blog_id . ' )' ); + unset( $this->options['future_posts'][ $blog_id ][ $post_id ] ); + update_site_option( 'rt_wp_nginx_helper_global_options', $this->options ); + + if ( ! count( $this->options['future_posts'][ $blog_id ] ) ) { + unset( $this->options['future_posts'][ $blog_id ] ); + update_site_option( 'rt_wp_nginx_helper_global_options', $this->options ); + } + } + } + } + + /** + * Update map when new blog added in multisite. + * + * @global type $nginx_purger + * @param type $blog_id + */ + public function update_new_blog_options( $blog_id ) { + global $rt_wp_nginx_purger; + + $nginx_purger->log( "New site added ( id $blog_id )" ); + $this->update_map(); + $nginx_purger->log( "New site added to nginx map ( id $blog_id )" ); + $helper_options = $this->nginx_helper_default_settings(); + update_blog_option( $blog_id, 'rt_wp_nginx_helper_options', $helper_options ); + $nginx_purger->log( "Default options updated for the new blog ( id $blog_id )" ); + } + + /** + * Purge all urls. + * + * @global type $nginx_purger + */ + public function purge_all() { + global $nginx_purger; + + if ( ! isset( $_REQUEST['nginx_helper_action'] ) ) { + return; + } + + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( 'Sorry, you do not have the necessary privileges to edit these options.' ); + } + + $action = $_REQUEST['nginx_helper_action']; + + if ( 'done' == $action ) { + add_action( 'admin_notices', array( &$this, 'display_notices' ) ); + add_action( 'network_admin_notices', array( &$this, 'display_notices' ) ); + return; + } + + check_admin_referer( 'nginx_helper-purge_all' ); + + switch ( $action ) { + case 'purge': + $nginx_purger->purgeAll(); + break; + } + + wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) ); + exit(); + } + + /** + * Dispay plugin notices. + */ + public function display_notices() { + echo '

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

'; + } } diff --git a/admin/class-phpredis-purger.php b/admin/class-phpredis-purger.php index 7d8d23e2..fd8e5a17 100644 --- a/admin/class-phpredis-purger.php +++ b/admin/class-phpredis-purger.php @@ -18,8 +18,8 @@ * @author rtCamp */ class PhpRedis_Purger extends Purger { - - /** + + /** * PHP Redis api object. * * @since 2.0.0 @@ -27,123 +27,128 @@ class PhpRedis_Purger extends Purger { * @var string $redis_object PHP Redis api object. */ public $redis_object; - - /** + + /** * Initialize the class and set its properties. * * @since 2.0.0 */ public function __construct() { - global $nginx_helper_admin; - - try { - $this->redis_object = new Redis(); - $this->redis_object->connect( - $nginx_helper_admin->options['redis_hostname'], - $nginx_helper_admin->options['redis_port'], - 5 - ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } - - public function purgeAll() { - global $nginx_helper_admin; - - $this->log( "* * * * *" ); - $this->log( "* Purged Everything!" ); - $total_keys_purged = $this->delete_keys_by_wildcard( "*" ); - if( $total_keys_purged ) { - $this->log( "Total {$total_keys_purged} urls purged." ); - } else { - $this->log( "No Cache found." ); - } - $this->log( "* * * * *" ); - } - - public function purgeUrl( $url, $feed = true ) { - global $nginx_helper_admin; - - $parse = parse_url( $url ); - $host = $nginx_helper_admin->options['redis_hostname']; - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; - $is_purged = $this->delete_single_key( $_url_purge_base ); - - if( $is_purged ) { - $this->log( "- Purged URL | " . $url ); - } else { - $this->log( "- Cache Not Found | " . $url, 'ERROR' ); - } - $this->log( "* * * * *" ); - } - - public function customPurgeUrls() { - global $nginx_helper_admin; - - $parse = parse_url( site_url() ); - $host = $nginx_helper_admin->options['redis_hostname']; - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); - - if( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - foreach ( $purge_urls as $purge_url ) { - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_single_key( $purge_url ); - if ( $status ) { - $this->log( "- Purge URL | " . $purge_url ); - } else { - $this->log( "- Cache Not Found | " . $purge_url, 'ERROR' ); - } - } else { - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_keys_by_wildcard( $purge_url ); - if ( $status ) { - $this->log( "- Purge Wild Card URL | " . $purge_url . " | " . $status . " url purged" ); - } else { - $this->log( "- Cache Not Found | " . $purge_url, 'ERROR' ); - } - } - } - } - } - - /** - * Single Key Delete Example - * e.g. $key can be nginx-cache:httpGETexample.com/ - */ - public function delete_single_key( $key ) { - try { - return $this->redis_object->del( $key ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } - - /** - * Delete Keys by wildcar - * e.g. $key can be nginx-cache:httpGETexample.com* - * - * Lua Script block to delete multiple keys using wildcard + global $nginx_helper_admin; + + try { + $this->redis_object = new Redis(); + $this->redis_object->connect( + $nginx_helper_admin->options['redis_hostname'], + $nginx_helper_admin->options['redis_port'], + 5 + ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } + + public function purgeAll() { + global $nginx_helper_admin; + + $this->log( '* * * * *' ); + $this->log( '* Purged Everything!' ); + $total_keys_purged = $this->delete_keys_by_wildcard( '*' ); + if ( $total_keys_purged ) { + $this->log( "Total {$total_keys_purged} urls purged." ); + } else { + $this->log( 'No Cache found.' ); + } + $this->log( '* * * * *' ); + } + + public function purgeUrl( $url, $feed = true ) { + global $nginx_helper_admin; + + $parse = parse_url( $url ); + + if ( ! isset( $parse['path'] ) ) { + $parse['path'] = ''; + } + + $host = $nginx_helper_admin->options['redis_hostname']; + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; + $is_purged = $this->delete_single_key( $_url_purge_base ); + + if ( $is_purged ) { + $this->log( '- Purged URL | ' . $url ); + } else { + $this->log( '- Cache Not Found | ' . $url, 'ERROR' ); + } + $this->log( '* * * * *' ); + } + + public function customPurgeUrls() { + global $nginx_helper_admin; + + $parse = parse_url( site_url() ); + $host = $nginx_helper_admin->options['redis_hostname']; + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; + + $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? + explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); + + // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed + $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); + + if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { + foreach ( $purge_urls as $purge_url ) { + $purge_url = trim( $purge_url ); + + if ( strpos( $purge_url, '*' ) === false ) { + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_single_key( $purge_url ); + if ( $status ) { + $this->log( '- Purge URL | ' . $purge_url ); + } else { + $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); + } + } else { + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_keys_by_wildcard( $purge_url ); + if ( $status ) { + $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); + } else { + $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); + } + } + } + } + } + + /** + * Single Key Delete Example + * e.g. $key can be nginx-cache:httpGETexample.com/ + */ + public function delete_single_key( $key ) { + try { + return $this->redis_object->del( $key ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } + + /** + * Delete Keys by wildcar + * e.g. $key can be nginx-cache:httpGETexample.com* + * + * 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 - * - * Call redis eval and return value from lua script - */ - public function delete_keys_by_wildcard( $pattern ) { - -//Lua Script -$lua = <<redis_object->eval( $lua, array( $pattern ), 1 ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } + try { + return $this->redis_object->eval( $lua, array( $pattern ), 1 ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } } diff --git a/admin/class-predis-purger.php b/admin/class-predis-purger.php index 7bf4aa66..75bacb12 100644 --- a/admin/class-predis-purger.php +++ b/admin/class-predis-purger.php @@ -1,13 +1,8 @@ redis_object = new Predis\Client([ - 'host' => $nginx_helper_admin->options['redis_hostname'], - 'port' => $nginx_helper_admin->options['redis_port'], - ]); - - try { - $this->redis_object->connect(); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } - - public function purgeAll() { - global $nginx_helper_admin; - - $this->log("* * * * *"); - $this->log("* Purged Everything!"); - $this->log("* * * * *"); - $this->delete_keys_by_wildcard("*"); - } - - public function purgeUrl( $url, $feed = true ) { - global $nginx_helper_admin; - - $this->log( "- Purging URL | " . $url ); - - $parse = parse_url( $url ); - $host = $nginx_helper_admin->options['redis_hostname']; - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; - $this->delete_single_key( $_url_purge_base ); - } - - public function customPurgeUrls() { - global $nginx_helper_admin; - - $parse = parse_url( site_url() ); - $host = $nginx_helper_admin->options['redis_hostname']; - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); - - if( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - foreach ( $purge_urls as $purge_url ) { - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_single_key( $purge_url ); - if ( $status ) { - $this->log( "- Purge URL | " . $purge_url ); - } else { - $this->log( "- Not Found | " . $purge_url, 'ERROR' ); - } - } else { - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_keys_by_wildcard( $purge_url ); - if ( $status ) { - $this->log( "- Purge Wild Card URL | " . $purge_url . " | " . $status . " url purged" ); - } else { - $this->log( "- Not Found | " . $purge_url, 'ERROR' ); - } - } - } - } - } - - /** - * Single Key Delete Example - * e.g. $key can be nginx-cache:httpGETexample.com/ - */ - public function delete_single_key( $key ) { - - try { - return $this->redis_object->executeRaw( ['DEL', $key ] ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } - - /** - * Delete Keys by wildcar - * e.g. $key can be nginx-cache:httpGETexample.com* - * - * Lua Script block to delete multiple keys using wildcard + global $nginx_helper_admin; + + if ( ! class_exists( 'Predis\Autoloader' ) ) { + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/predis.php'; + } + Predis\Autoloader::register(); + + // redis server parameter. + $this->redis_object = new Predis\Client( + [ + 'host' => $nginx_helper_admin->options['redis_hostname'], + 'port' => $nginx_helper_admin->options['redis_port'], + ] + ); + + try { + $this->redis_object->connect(); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } + + public function purgeAll() { + global $nginx_helper_admin; + + $prefix = trim( $rt_wp_nginx_helper->options['redis_prefix'] ); + + $this->log( '* * * * *' ); + + // If Purge Cache link click from network admin then purge all. + if( is_network_admin() ) { + delete_keys_by_wildcard( $prefix . '*' ); + $this->log( '* Purged Everything! * ' ); + } else { // Else purge only site specific cache + $parse = wp_parse_url( get_home_url() ); + $parse['path'] = empty( $parse['path'] ) ? '/' : $parse['path']; + delete_keys_by_wildcard( $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*' ); + $this->log( '* ' . get_home_url() . ' Purged! * ' ); + } + + $this->log( '* * * * *' ); + } + + public function purgeUrl( $url, $feed = true ) { + global $nginx_helper_admin; + + $this->log( '- Purging URL | ' . $url ); + + $parse = parse_url( $url ); + + if ( ! isset( $parse['path'] ) ) { + $parse['path'] = ''; + } + + $host = $nginx_helper_admin->options['redis_hostname']; + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; + $this->delete_single_key( $_url_purge_base ); + } + + public function customPurgeUrls() { + global $nginx_helper_admin; + + $parse = parse_url( site_url() ); + $host = $nginx_helper_admin->options['redis_hostname']; + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; + + $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? + explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); + + // Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed + $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); + + if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { + foreach ( $purge_urls as $purge_url ) { + $purge_url = trim( $purge_url ); + + if ( strpos( $purge_url, '*' ) === false ) { + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_single_key( $purge_url ); + if ( $status ) { + $this->log( '- Purge URL | ' . $purge_url ); + } else { + $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); + } + } else { + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_keys_by_wildcard( $purge_url ); + if ( $status ) { + $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); + } else { + $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); + } + } + } + } + } + + /** + * Single Key Delete Example + * e.g. $key can be nginx-cache:httpGETexample.com/ + */ + public function delete_single_key( $key ) { + + try { + return $this->redis_object->executeRaw( [ 'DEL', $key ] ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } + + /** + * Delete Keys by wildcar + * e.g. $key can be nginx-cache:httpGETexample.com* + * + * 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 - * - * Call redis eval and return value from lua script - */ - public function delete_keys_by_wildcard( $pattern ) { + * + * Call redis eval and return value from lua script + */ + public function delete_keys_by_wildcard( $pattern ) { -//Lua Script -$lua = <<redis_object->eval( $lua, 1, $pattern ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - } -} \ No newline at end of file + try { + return $this->redis_object->eval( $lua, 1, $pattern ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + } +} diff --git a/admin/class-purger.php b/admin/class-purger.php index ac8e54a7..70adead2 100644 --- a/admin/class-purger.php +++ b/admin/class-purger.php @@ -3,11 +3,7 @@ /** * The admin-specific functionality of the plugin. * - * @link https://rtcamp.com/nginx-helper/ - * @since 2.0.0 - * * @package nginx-helper - * @subpackage nginx-helper/admin */ /** @@ -19,697 +15,734 @@ */ abstract class Purger { - abstract public function purgeUrl( $url, $feed ); - - abstract public function purgeAll(); - - public function purgePostOnComment( $comment_id, $comment ) { - $oldstatus = ''; - $approved = $comment->comment_approved; - - if ($approved == null) { - $newstatus = false; - } elseif ($approved == '1') { - $newstatus = 'approved'; - } elseif ($approved == '0') { - $newstatus = 'unapproved'; - } elseif ($approved == 'spam') { - $newstatus = 'spam'; - } elseif ($approved == 'trash') { - $newstatus = 'trash'; - } else { - $newstatus = false; - } - - $this->purgePostOnCommentChange($newstatus, $oldstatus, $comment); - } - - public function purgePostOnCommentChange( $newstatus, $oldstatus, $comment ) { - global $nginx_helper_admin, $blog_id; - - if (!$nginx_helper_admin->options['enable_purge']) { - return; - } - - $_post_id = $comment->comment_post_ID; - $_comment_id = $comment->comment_ID; - - $this->log("* * * * *"); - $this->log("* Blog :: " . addslashes(get_bloginfo('name')) . " ( " . $blog_id . " ). "); - $this->log("* Post :: " . get_the_title($_post_id) . " ( " . $_post_id . " ) "); - $this->log("* Comment :: $_comment_id."); - $this->log("* Status Changed from $oldstatus to $newstatus"); - - switch ($newstatus) { - case 'approved': - if (1 == $nginx_helper_admin->options['purge_page_on_new_comment']) { - $this->log("* Comment ( " . $_comment_id . " ) approved. Post ( " . $_post_id . " ) purging..."); - $this->log("* * * * *"); - $this->purgePost($_post_id); - } - break; - case 'spam': - case 'unapproved': - case 'trash': - if ('approve' == $oldstatus) { - if (1 == $nginx_helper_admin->options['purge_page_on_deleted_comment']) { - $this->log("* Comment ( " . $_comment_id . " ) removed as ( " . $newstatus . " ). Post ( " . $_post_id . " ) purging..."); - $this->log("* * * * *"); - $this->purgePost($_post_id); - } - } - break; - } - } - - public function purgePost( $_ID ) { - global $nginx_helper_admin, $blog_id; - - if ( !$nginx_helper_admin->options['enable_purge'] ) { - return; - } - - switch (current_filter()) { - case 'publish_post': - $this->log("* * * * *"); - $this->log("* Blog :: " . addslashes(get_bloginfo('name')) . " ( " . $blog_id . " )."); - $this->log("* Post :: " . get_the_title($_ID) . " ( " . $_ID . " )."); - $this->log("* Post ( " . $_ID . " ) published or edited and its status is published"); - $this->log("* * * * *"); - break; - - case 'publish_page': - $this->log("* * * * *"); - $this->log("* Blog :: " . addslashes(get_bloginfo('name')) . " ( " . $blog_id . " )."); - $this->log("* Page :: " . get_the_title($_ID) . " ( " . $_ID . " )."); - $this->log("* Page ( " . $_ID . " ) published or edited and its status is published"); - $this->log("* * * * *"); - break; - - case 'comment_post': - case 'wp_set_comment_status': - break; - - default: - $_post_type = get_post_type($_ID); - $this->log("* * * * *"); - $this->log("* Blog :: " . addslashes(get_bloginfo('name')) . " ( " . $blog_id . " )."); - $this->log("* Custom post type '" . $_post_type . "' :: " . get_the_title($_ID) . " ( " . $_ID . " )."); - $this->log("* CPT '" . $_post_type . "' ( " . $_ID . " ) published or edited and its status is published"); - $this->log("* * * * *"); - break; - } - - $this->log("Function purgePost BEGIN ==="); - - if ( 1 == $nginx_helper_admin->options['purge_homepage_on_edit']) { - $homepage_url = trailingslashit(home_url()); - $this->log("Purging homepage '" . $homepage_url . "'"); - $this->purgeUrl($homepage_url); - } - - if ( 'comment_post' == current_filter() || 'wp_set_comment_status' == current_filter()) { - $this->_purge_by_options( - $_ID, $blog_id, $nginx_helper_admin->options['purge_page_on_new_comment'], $nginx_helper_admin->options['purge_archive_on_new_comment'], $nginx_helper_admin->options['purge_archive_on_new_comment'] - ); - } else { - $this->_purge_by_options( - $_ID, $blog_id, $nginx_helper_admin->options['purge_page_on_mod'], $nginx_helper_admin->options['purge_archive_on_edit'], $nginx_helper_admin->options['purge_archive_on_edit'] - ); - } - - $this->customPurgeUrls(); - - $this->log("Function purgePost END ^^^"); - } - - private function _purge_by_options( $_post_ID, $blog_id, $_purge_page, $_purge_archive, $_purge_custom_taxa ) { - global $nginx_helper_admin; - - $_post_type = get_post_type( $_post_ID ); - - if ( $_purge_page ) { - if ( 'post' == $_post_type || 'page' == $_post_type ) { - $this->log( "Purging " . $_post_type. " ( id " . $_post_ID . ", blog id " . $blog_id . " ) " ); - } else { - $this->log( "Purging custom post type '" . $_post_type . "' ( id " . $_post_ID . ", blog id " . $blog_id . " )" ); - } - - $this->purgeUrl( get_permalink( $_post_ID ) ); - } - - if ( $_purge_archive ) { - - if ( function_exists( 'get_post_type_archive_link' ) && ( $_post_type_archive_link = get_post_type_archive_link( $_post_type ) ) ) { - $this->log( "Purging post type archive ( " . $_post_type . " )" ); - $this->purgeUrl( $_post_type_archive_link ); - } - - if ( 'post' == $_post_type ) { - $this->log( "Purging date" ); - - $day = get_the_time( 'd', $_post_ID ); - $month = get_the_time( 'm', $_post_ID ); - $year = get_the_time( 'Y', $_post_ID ); - - if ( $year ) { - $this->purgeUrl( get_year_link( $year ) ); - if ( $month ) { - $this->purgeUrl( get_month_link( $year, $month ) ); - if ( $day ) { - $this->purgeUrl( get_day_link( $year, $month, $day ) ); - } - } - } - } - - if ( $categories = wp_get_post_categories( $_post_ID ) ) { - $this->log( "Purging category archives" ); - - foreach ( $categories as $category_id ) { - $this->log( "Purging category " . $category_id ); - $this->purgeUrl( get_category_link( $category_id ) ); - } - } - - if ( $tags = get_the_tags( $_post_ID ) ) { - $this->log( "Purging tag archives" ); - - foreach ( $tags as $tag ) { - $this->log( "Purging tag " . $tag->term_id ); - $this->purgeUrl( get_tag_link( $tag->term_id ) ); - } - } - - if ( $author_id = get_post( $_post_ID )->post_author ) { - $this->log( "Purging author archive" ); - $this->purgeUrl( get_author_posts_url( $author_id ) ); - } - } - - if ( $_purge_custom_taxa ) { - if ( $custom_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ) ) { - $this->log( "Purging custom taxonomies related" ); - foreach ( $custom_taxonomies as $taxon ) { - - if ( ! in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) { - - if ( $terms = get_the_terms( $_post_ID, $taxon ) ) { - foreach ( $terms as $term ) { - $this->purgeUrl( get_term_link( $term, $taxon ) ); - } - } - } else { - $this->log( "Your built-in taxonomy '" . $taxon . "' has param '_builtin' set to false.", "WARNING" ); - } - } - } - } - } - - /* - * Deletes local cache files without a 3rd party nginx module. - * Does not require any other modules. Requires that the cache be stored on the same server as WordPress. You must also be using the default nginx cache options (levels=1:2) and (fastcgi_cache_key "$scheme$request_method$host$request_uri"). - * Read more on how this works here: - * https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps#purging-the-cache - */ - private function _delete_cache_file_for( $url ) { - - // Verify cache path is set - if ( ! defined( 'RT_WP_NGINX_HELPER_CACHE_PATH' ) ) { - $this->log( 'Error purging because RT_WP_NGINX_HELPER_CACHE_PATH was not defined. URL: ' . $url, 'ERROR' ); - return false; - } - - // Verify URL is valid - $url_data = parse_url( $url ); - if ( ! $url_data ) { - $this->log( 'Error purging because specified URL did not appear to be valid. URL: ' . $url, 'ERROR' ); - return false; - } - - // Build a hash of the URL - $hash = md5( $url_data['scheme'] . 'GET' . $url_data['host'] . $url_data['path'] ); - - // Ensure trailing slash - $cache_path = RT_WP_NGINX_HELPER_CACHE_PATH; - $cache_path = ( '/' == substr( $cache_path, -1 ) ) ? $cache_path : $cache_path . '/'; - - // Set path to cached file - $cached_file = $cache_path . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash; - - // Verify cached file exists - if ( ! file_exists( $cached_file ) ) { - $this->log( "- - " . $url . " is currently not cached ( checked for file: " . $cached_file . " )" ); - return false; - } - - // Delete the cached file - if ( unlink( $cached_file ) ) { - $this->log( "- - " . $url . " *** PURGED ***" ); - } else { - $this->log( "- - An error occurred deleting the cache file. Check the server logs for a PHP warning.", "ERROR" ); - } - } - - private function _do_remote_get( $url ) { - - $response = wp_remote_get( $url ); - - if ( is_wp_error( $response ) ) { - $_errors_str = implode( " - ", $response->get_error_messages() ); - $this->log( "Error while purging URL. " . $_errors_str, "ERROR" ); - } else { - if ( $response['response']['code'] ) { - switch ( $response['response']['code'] ) { - case 200: - $this->log( "- - " . $url . " *** PURGED ***" ); - break; - case 404: - $this->log( "- - " . $url . " is currently not cached" ); - break; - default: - $this->log( "- - " . $url . " not found ( " . $response['response']['code'] . " )", "WARNING" ); - } - } - } - } - - public function checkHttpConnection() { - - $purgeURL = plugins_url( "nginx-manager/check-proxy.php" ); - $response = wp_remote_get( $purgeURL ); - - if ( ! is_wp_error( $response ) && ( 'HTTP Connection OK' == $response['body'] ) ) { - return "OK"; - } - - return "KO"; - } - - public function log( $msg, $level = 'INFO' ) { - global $nginx_helper_admin; - - if ( !$nginx_helper_admin->options['enable_log'] ) { - return; - } - - $log_levels = array( "INFO" => 0, "WARNING" => 1, "ERROR" => 2, "NONE" => 3 ); - - if ( $log_levels[ $level ] >= $log_levels[ $nginx_helper_admin->options['log_level'] ] ) { - if ( $fp = fopen( $nginx_helper_admin->functional_asset_path() . 'nginx.log', "a+" ) ) { - fwrite( $fp, "\n" . gmdate( "Y-m-d H:i:s " ) . " | " . $level . " | " . $msg ); - fclose( $fp ); - } - } - - return true; - } - - public function checkAndTruncateLogFile() { - global $nginx_helper_admin; - - $maxSizeAllowed = ( is_numeric( $nginx_helper_admin->options['log_filesize'] ) ) ? $nginx_helper_admin->options['log_filesize'] * 1048576 : 5242880; - - $fileSize = filesize( $nginx_helper_admin->functional_asset_path() . 'nginx.log' ); - - if ( $fileSize > $maxSizeAllowed ) { - $offset = $fileSize - $maxSizeAllowed; - if ( $file_content = file_get_contents( $nginx_helper_admin->functional_asset_path() . 'nginx.log', NULL, NULL, $offset ) ) { - if ( $file_content = strstr( $file_content, "\n" ) ) { - if ( $fp = fopen( $nginx_helper_admin->functional_asset_path() . 'nginx.log', "w+" ) ) { - fwrite( $fp, $file_content ); - fclose( $fp ); - } - } - } - } - } - - public function purgeImageOnEdit( $attachment_id ) { - - $this->log( "Purging media on edit BEGIN ===" ); - - if ( wp_attachment_is_image( $attachment_id ) ) { - $this->purgeUrl( wp_get_attachment_url( $attachment_id ), false ); - $attachment = wp_get_attachment_metadata( $attachment_id ); - - if ( ! empty( $attachment['sizes'] ) && is_array( $attachment['sizes'] ) ) { - foreach ($attachment['sizes'] as $size_name => $size) { - $resize_image = wp_get_attachment_image_src( $attachment_id, $size_name ); - if ( $resize_image ) - $this->purgeUrl( $resize_image[0], false ); - } - } - $this->purgeURL( get_attachment_link( $attachment_id ) ); - } else { - $this->log( "Media ( id " . $attachment_id . ") edited: no image", "WARNING" ); - } - - $this->log( "Purging media on edit END ^^^" ); - } - - public function purge_on_post_moved_to_trash( $new_status, $old_status, $post ) { - global $nginx_helper_admin, $blog_id; - - if ( ! $nginx_helper_admin->options['enable_purge'] ) { - return; - } - if ( 'trash' == $new_status ) { - - $this->log( "# # # # #" ); - $this->log( "# Post '$post->post_title' ( id " . $post->ID . " ) moved to the trash." ); - $this->log( "# # # # #" ); - $this->log( "Function purge_on_post_moved_to_trash ( post id " . $post->ID . " ) BEGIN ===" ); - - if ( 1 == $nginx_helper_admin->options['purge_homepage_on_del'] ) { - $this->_purge_homepage(); - } - - $this->_purge_by_options( - $post->ID, - $blog_id, - false, - $nginx_helper_admin->options['purge_archive_on_del'], - $nginx_helper_admin->options['purge_archive_on_del'] - ); - - $this->log( "Function purge_on_post_moved_to_trash ( post id " . $post->ID . " ) END ===" ); - } - - return true; - } - - private function _purge_homepage() { - - $homepage_url = trailingslashit( home_url() ); - - $this->log( sprintf( __( "Purging homepage '%s'", "nginx-helper" ), $homepage_url ) ); - $this->purgeUrl( $homepage_url ); - - return true; - } - - private function _purge_personal_urls() { - global $nginx_helper_admin; - - $this->log( __( "Purging personal urls", "nginx-helper" ) ); - - if ( isset( $nginx_helper_admin->options['purgeable_url']['urls'] ) ) { - foreach ( $nginx_helper_admin->options['purgeable_url']['urls'] as $u ) { - $this->purgeUrl( $u, false ); - } - } else { - $this->log( "- " . __( "No personal urls available", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_post_categories( $_post_id ) { - - $this->log( __( "Purging category archives", "nginx-helper" ) ); - - if ( $categories = wp_get_post_categories( $_post_id ) ) { - foreach ( $categories as $category_id ) { - $this->log( sprintf( __( "Purging category '%d'", "nginx-helper" ), $category_id ) ); - $this->purgeUrl( get_category_link( $category_id ) ); - } - } - - return true; - } - - private function _purge_post_tags( $_post_id ) { - - $this->log( __( "Purging tags archives", "nginx-helper" ) ); - - if ( $tags = get_the_tags( $_post_id ) ) { - foreach ( $tags as $tag ) { - $this->log( sprintf( __( "Purging tag '%s' ( id %d )", "nginx-helper" ), $tag->name, $tag->term_id ) ); - $this->purgeUrl( get_tag_link( $tag->term_id ) ); - } - } - - return true; - } - - private function _purge_post_custom_taxa( $_post_id ) { - - $this->log( __( "Purging post custom taxonomies related", "nginx-helper" ) ); - - if ( $custom_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ) ) { - foreach ( $custom_taxonomies as $taxon ) { - $this->log( sprintf( "+ " . __( "Purging custom taxonomy '%s'", "nginx-helper" ), $taxon ) ); - if ( ! in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) { - if ( $terms = get_the_terms( $_post_id, $taxon ) ) { - foreach ( $terms as $term ) { - $this->purgeUrl( get_term_link( $term, $taxon ) ); - } - } - } else { - $this->log( sprintf( "- " . __( "Your built-in taxonomy '%s' has param '_builtin' set to false.", "nginx-helper" ), $taxon ), "WARNING" ); - } - } - } else { - $this->log( "- " . __( "No custom taxonomies", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_all_categories() { - - $this->log( __( "Purging all categories", "nginx-helper" ) ); - - if ( $_categories = get_categories() ) { - foreach ( $_categories as $c ) { - $this->log( sprintf( __( "Purging category '%s' ( id %d )", "nginx-helper" ), $c->name, $c->term_id ) ); - $this->purgeUrl( get_category_link( $c->term_id ) ); - } - } else { - $this->log( __( "No categories archives", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_all_posttags() { - - $this->log( __( "Purging all tags", "nginx-helper" ) ); - - if ( $_posttags = get_tags() ) { - foreach ( $_posttags as $t ) { - $this->log( sprintf( __( "Purging tag '%s' ( id %d )", "nginx-helper" ), $t->name, $t->term_id ) ); - $this->purgeUrl( get_tag_link( $t->term_id ) ); - } - } else { - $this->log( __( "No tags archives", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_all_customtaxa() { - - $this->log( __( "Purging all custom taxonomies", "nginx-helper" ) ); - - if ( $custom_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ) ) { - foreach ( $custom_taxonomies as $taxon ) { - $this->log( sprintf( "+ " . __( "Purging custom taxonomy '%s'", "nginx-helper" ), $taxon ) ); - if ( !in_array( $taxon, array( 'category', 'post_tag', 'link_category') ) ) { - if ( $terms = get_terms( $taxon ) ) { - foreach ( $terms as $term ) { - $this->purgeUrl( get_term_link( $term, $taxon ) ); - } - } - } else { - $this->log( sprintf( "- " . __( "Your built-in taxonomy '%s' has param '_builtin' set to false.", "nginx-helper" ), $taxon ), "WARNING" ); - } - } - } else { - $this->log( "- " . __( "No custom taxonomies", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_all_taxonomies() { - - $this->_purge_all_categories(); - $this->_purge_all_posttags(); - $this->_purge_all_customtaxa(); - - return true; - } - - private function _purge_all_posts() { - - $this->log( __( "Purging all posts, pages and custom post types.", "nginx-helper" ) ); - - $args = array( - 'numberposts' => 0, - 'post_type' => 'any', - 'post_status' => 'publish' - ); - - if ( $_posts = get_posts( $args ) ) { - foreach ( $_posts as $p ) { - $this->log( sprintf( "+ " . __( "Purging post id '%d' ( post type '%s' )", "nginx-helper"), $p->ID, $p->post_type ) ); - $this->purgeUrl( get_permalink( $p->ID ) ); - } - } else { - $this->log( "- " . __( "No posts", "nginx-helper" ) ); - } - - return true; - } - - private function _purge_all_date_archives() { - - $this->log( __( "Purging all date-based archives.", "nginx-helper" ) ); + abstract public function purgeUrl( $url, $feed ); + + abstract public function purgeAll(); + + public function purgePostOnComment( $comment_id, $comment ) { + $oldstatus = ''; + $approved = $comment->comment_approved; + + if ( $approved === null ) { + $newstatus = false; + } elseif ( $approved === '1' ) { + $newstatus = 'approved'; + } elseif ( $approved === '0' ) { + $newstatus = 'unapproved'; + } elseif ( $approved === 'spam' ) { + $newstatus = 'spam'; + } elseif ( $approved === 'trash' ) { + $newstatus = 'trash'; + } else { + $newstatus = false; + } + + $this->purgePostOnCommentChange( $newstatus, $oldstatus, $comment ); + } + + public function purgePostOnCommentChange( $newstatus, $oldstatus, $comment ) { + global $nginx_helper_admin, $blog_id; + + if ( ! $nginx_helper_admin->options['enable_purge'] ) { + return; + } + + $_post_id = $comment->comment_post_ID; + $_comment_id = $comment->comment_ID; + + $this->log( '* * * * *' ); + $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ). ' ); + $this->log( '* Post :: ' . get_the_title( $_post_id ) . ' ( ' . $_post_id . ' ) ' ); + $this->log( "* Comment :: $_comment_id." ); + $this->log( "* Status Changed from $oldstatus to $newstatus" ); + + switch ( $newstatus ) { + case 'approved': + if ( 1 == $nginx_helper_admin->options['purge_page_on_new_comment'] ) { + $this->log( '* Comment ( ' . $_comment_id . ' ) approved. Post ( ' . $_post_id . ' ) purging...' ); + $this->log( '* * * * *' ); + $this->purgePost( $_post_id ); + } + break; + case 'spam': + case 'unapproved': + case 'trash': + if ( 'approve' == $oldstatus ) { + if ( 1 == $nginx_helper_admin->options['purge_page_on_deleted_comment'] ) { + $this->log( '* Comment ( ' . $_comment_id . ' ) removed as ( ' . $newstatus . ' ). Post ( ' . $_post_id . ' ) purging...' ); + $this->log( '* * * * *' ); + $this->purgePost( $_post_id ); + } + } + break; + } + } + + public function purgePost( $_ID ) { + global $nginx_helper_admin, $blog_id; + + if ( ! $nginx_helper_admin->options['enable_purge'] ) { + return; + } + + switch ( current_filter() ) { + case 'publish_post': + $this->log( '* * * * *' ); + $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ).' ); + $this->log( '* Post :: ' . get_the_title( $_ID ) . ' ( ' . $_ID . ' ).' ); + $this->log( '* Post ( ' . $_ID . ' ) published or edited and its status is published' ); + $this->log( '* * * * *' ); + break; + + case 'publish_page': + $this->log( '* * * * *' ); + $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ).' ); + $this->log( '* Page :: ' . get_the_title( $_ID ) . ' ( ' . $_ID . ' ).' ); + $this->log( '* Page ( ' . $_ID . ' ) published or edited and its status is published' ); + $this->log( '* * * * *' ); + break; + + case 'comment_post': + case 'wp_set_comment_status': + break; + + default: + $_post_type = get_post_type( $_ID ); + $this->log( '* * * * *' ); + $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ).' ); + $this->log( "* Custom post type '" . $_post_type . "' :: " . get_the_title( $_ID ) . ' ( ' . $_ID . ' ).' ); + $this->log( "* CPT '" . $_post_type . "' ( " . $_ID . ' ) published or edited and its status is published' ); + $this->log( '* * * * *' ); + break; + } + + $this->log( 'Function purgePost BEGIN ===' ); + + if ( 1 == $nginx_helper_admin->options['purge_homepage_on_edit'] ) { + $this->_purge_homepage(); + } + + if ( 'comment_post' == current_filter() || 'wp_set_comment_status' == current_filter() ) { + $this->_purge_by_options( + $_ID, $blog_id, $nginx_helper_admin->options['purge_page_on_new_comment'], $nginx_helper_admin->options['purge_archive_on_new_comment'], $nginx_helper_admin->options['purge_archive_on_new_comment'] + ); + } else { + $this->_purge_by_options( + $_ID, $blog_id, $nginx_helper_admin->options['purge_page_on_mod'], $nginx_helper_admin->options['purge_archive_on_edit'], $nginx_helper_admin->options['purge_archive_on_edit'] + ); + } + + $this->customPurgeUrls(); + + $this->log( 'Function purgePost END ^^^' ); + } + + private function _purge_by_options( $_post_ID, $blog_id, $_purge_page, $_purge_archive, $_purge_custom_taxa ) { + global $nginx_helper_admin; + + $_post_type = get_post_type( $_post_ID ); + + if ( $_purge_page ) { + if ( 'post' == $_post_type || 'page' == $_post_type ) { + $this->log( 'Purging ' . $_post_type . ' ( id ' . $_post_ID . ', blog id ' . $blog_id . ' ) ' ); + } else { + $this->log( "Purging custom post type '" . $_post_type . "' ( id " . $_post_ID . ', blog id ' . $blog_id . ' )' ); + } + + $this->purgeUrl( get_permalink( $_post_ID ) ); + } + + if ( $_purge_archive ) { + + if ( function_exists( 'get_post_type_archive_link' ) && ( $_post_type_archive_link = get_post_type_archive_link( $_post_type ) ) ) { + $this->log( 'Purging post type archive ( ' . $_post_type . ' )' ); + $this->purgeUrl( $_post_type_archive_link ); + } + + if ( 'post' == $_post_type ) { + $this->log( 'Purging date' ); + + $day = get_the_time( 'd', $_post_ID ); + $month = get_the_time( 'm', $_post_ID ); + $year = get_the_time( 'Y', $_post_ID ); + + if ( $year ) { + $this->purgeUrl( get_year_link( $year ) ); + if ( $month ) { + $this->purgeUrl( get_month_link( $year, $month ) ); + if ( $day ) { + $this->purgeUrl( get_day_link( $year, $month, $day ) ); + } + } + } + } + + if ( $categories = wp_get_post_categories( $_post_ID ) ) { + $this->log( 'Purging category archives' ); + + foreach ( $categories as $category_id ) { + $this->log( 'Purging category ' . $category_id ); + $this->purgeUrl( get_category_link( $category_id ) ); + } + } + + if ( $tags = get_the_tags( $_post_ID ) ) { + $this->log( 'Purging tag archives' ); + + foreach ( $tags as $tag ) { + $this->log( 'Purging tag ' . $tag->term_id ); + $this->purgeUrl( get_tag_link( $tag->term_id ) ); + } + } + + if ( $author_id = get_post( $_post_ID )->post_author ) { + $this->log( 'Purging author archive' ); + $this->purgeUrl( get_author_posts_url( $author_id ) ); + } + } + + if ( $_purge_custom_taxa ) { + if ( $custom_taxonomies = get_taxonomies( + array( + 'public' => true, + '_builtin' => false, + ) + ) ) { + $this->log( 'Purging custom taxonomies related' ); + foreach ( $custom_taxonomies as $taxon ) { + + if ( ! in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) { + + if ( $terms = get_the_terms( $_post_ID, $taxon ) ) { + foreach ( $terms as $term ) { + $this->purgeUrl( get_term_link( $term, $taxon ) ); + } + } + } else { + $this->log( "Your built-in taxonomy '" . $taxon . "' has param '_builtin' set to false.", 'WARNING' ); + } + } + } + } + } + + /* + * Deletes local cache files without a 3rd party nginx module. + * Does not require any other modules. Requires that the cache be stored on the same server as WordPress. You must also be using the default nginx cache options (levels=1:2) and (fastcgi_cache_key "$scheme$request_method$host$request_uri"). + * Read more on how this works here: + * https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps#purging-the-cache + */ + private function _delete_cache_file_for( $url ) { + + // Verify cache path is set + if ( ! defined( 'RT_WP_NGINX_HELPER_CACHE_PATH' ) ) { + $this->log( 'Error purging because RT_WP_NGINX_HELPER_CACHE_PATH was not defined. URL: ' . $url, 'ERROR' ); + return false; + } + + // Verify URL is valid + $url_data = parse_url( $url ); + if ( ! $url_data ) { + $this->log( 'Error purging because specified URL did not appear to be valid. URL: ' . $url, 'ERROR' ); + return false; + } + + // Build a hash of the URL + $hash = md5( $url_data['scheme'] . 'GET' . $url_data['host'] . $url_data['path'] ); + + // Ensure trailing slash + $cache_path = RT_WP_NGINX_HELPER_CACHE_PATH; + $cache_path = ( '/' == substr( $cache_path, -1 ) ) ? $cache_path : $cache_path . '/'; + + // Set path to cached file + $cached_file = $cache_path . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash; + + // Verify cached file exists + if ( ! file_exists( $cached_file ) ) { + $this->log( '- - ' . $url . ' is currently not cached ( checked for file: ' . $cached_file . ' )' ); + return false; + } + + // Delete the cached file + if ( unlink( $cached_file ) ) { + $this->log( '- - ' . $url . ' *** PURGED ***' ); + } else { + $this->log( '- - An error occurred deleting the cache file. Check the server logs for a PHP warning.', 'ERROR' ); + } + } + + private function _do_remote_get( $url ) { + + $response = wp_remote_get( $url ); + + if ( is_wp_error( $response ) ) { + $_errors_str = implode( ' - ', $response->get_error_messages() ); + $this->log( 'Error while purging URL. ' . $_errors_str, 'ERROR' ); + } else { + if ( $response['response']['code'] ) { + switch ( $response['response']['code'] ) { + case 200: + $this->log( '- - ' . $url . ' *** PURGED ***' ); + break; + case 404: + $this->log( '- - ' . $url . ' is currently not cached' ); + break; + default: + $this->log( '- - ' . $url . ' not found ( ' . $response['response']['code'] . ' )', 'WARNING' ); + } + } + } + } + + public function checkHttpConnection() { + + $purgeURL = plugins_url( 'nginx-manager/check-proxy.php' ); + $response = wp_remote_get( $purgeURL ); + + if ( ! is_wp_error( $response ) && ( 'HTTP Connection OK' == $response['body'] ) ) { + return 'OK'; + } + + return 'KO'; + } + + public function log( $msg, $level = 'INFO' ) { + global $nginx_helper_admin; + + if ( ! $nginx_helper_admin->options['enable_log'] ) { + return; + } + + $log_levels = array( + 'INFO' => 0, + 'WARNING' => 1, + 'ERROR' => 2, + 'NONE' => 3, + ); + + if ( $log_levels[ $level ] >= $log_levels[ $nginx_helper_admin->options['log_level'] ] ) { + if ( $fp = fopen( $nginx_helper_admin->functional_asset_path() . 'nginx.log', 'a+' ) ) { + fwrite( $fp, "\n" . gmdate( 'Y-m-d H:i:s ' ) . ' | ' . $level . ' | ' . $msg ); + fclose( $fp ); + } + } + + return true; + } + + public function checkAndTruncateLogFile() { + global $nginx_helper_admin; + + $maxSizeAllowed = ( is_numeric( $nginx_helper_admin->options['log_filesize'] ) ) ? $nginx_helper_admin->options['log_filesize'] * 1048576 : 5242880; + + $fileSize = filesize( $nginx_helper_admin->functional_asset_path() . 'nginx.log' ); + + if ( $fileSize > $maxSizeAllowed ) { + $offset = $fileSize - $maxSizeAllowed; + if ( $file_content = file_get_contents( $nginx_helper_admin->functional_asset_path() . 'nginx.log', null, null, $offset ) ) { + if ( $file_content = strstr( $file_content, "\n" ) ) { + if ( $fp = fopen( $nginx_helper_admin->functional_asset_path() . 'nginx.log', 'w+' ) ) { + fwrite( $fp, $file_content ); + fclose( $fp ); + } + } + } + } + } + + public function purgeImageOnEdit( $attachment_id ) { + + $this->log( 'Purging media on edit BEGIN ===' ); + + if ( wp_attachment_is_image( $attachment_id ) ) { + $this->purgeUrl( wp_get_attachment_url( $attachment_id ), false ); + $attachment = wp_get_attachment_metadata( $attachment_id ); + + if ( ! empty( $attachment['sizes'] ) && is_array( $attachment['sizes'] ) ) { + foreach ( $attachment['sizes'] as $size_name => $size ) { + $resize_image = wp_get_attachment_image_src( $attachment_id, $size_name ); + if ( $resize_image ) { + $this->purgeUrl( $resize_image[0], false ); + } + } + } + $this->purgeURL( get_attachment_link( $attachment_id ) ); + } else { + $this->log( 'Media ( id ' . $attachment_id . ') edited: no image', 'WARNING' ); + } - $this->_purge_all_daily_archives(); - $this->_purge_all_monthly_archives(); - $this->_purge_all_yearly_archives(); + $this->log( 'Purging media on edit END ^^^' ); + } + + public function purge_on_post_moved_to_trash( $new_status, $old_status, $post ) { + global $nginx_helper_admin, $blog_id; + + if ( ! $nginx_helper_admin->options['enable_purge'] ) { + return; + } + if ( 'trash' == $new_status ) { + + $this->log( '# # # # #' ); + $this->log( "# Post '$post->post_title' ( id " . $post->ID . ' ) moved to the trash.' ); + $this->log( '# # # # #' ); + $this->log( 'Function purge_on_post_moved_to_trash ( post id ' . $post->ID . ' ) BEGIN ===' ); + + if ( 1 == $nginx_helper_admin->options['purge_homepage_on_del'] ) { + $this->_purge_homepage(); + } + + $this->_purge_by_options( + $post->ID, + $blog_id, + false, + $nginx_helper_admin->options['purge_archive_on_del'], + $nginx_helper_admin->options['purge_archive_on_del'] + ); + + $this->log( 'Function purge_on_post_moved_to_trash ( post id ' . $post->ID . ' ) END ===' ); + } + + return true; + } + + private function _purge_homepage() { + + // WPML installetd?. + if ( function_exists( 'icl_get_home_url' ) ) { + + $homepage_url = trailingslashit( icl_get_home_url() ); + $this->log( sprintf( __( 'Purging homepage (WPML) ', 'nginx-helper' ) . '%s', $homepage_url ) ); + + } else { + + $homepage_url = trailingslashit( home_url() ); + $this->log( sprintf( __( 'Purging homepage ', 'nginx-helper' ) . '%s', $homepage_url ) ); + + } + + $this->purgeUrl( $homepage_url ); + + return true; + } + + private function _purge_personal_urls() { + global $nginx_helper_admin; + + $this->log( __( 'Purging personal urls', 'nginx-helper' ) ); + + if ( isset( $nginx_helper_admin->options['purgeable_url']['urls'] ) ) { + foreach ( $nginx_helper_admin->options['purgeable_url']['urls'] as $u ) { + $this->purgeUrl( $u, false ); + } + } else { + $this->log( '- ' . __( 'No personal urls available', 'nginx-helper' ) ); + } + + return true; + } + + private function _purge_post_categories( $_post_id ) { + + $this->log( __( 'Purging category archives', 'nginx-helper' ) ); + + if ( $categories = wp_get_post_categories( $_post_id ) ) { + foreach ( $categories as $category_id ) { + $this->log( sprintf( __( "Purging category '%d'", 'nginx-helper' ), $category_id ) ); + $this->purgeUrl( get_category_link( $category_id ) ); + } + } + + return true; + } + + private function _purge_post_tags( $_post_id ) { + + $this->log( __( 'Purging tags archives', 'nginx-helper' ) ); + + if ( $tags = get_the_tags( $_post_id ) ) { + foreach ( $tags as $tag ) { + $this->log( sprintf( __( "Purging tag '%1\$s' ( id %2\$d )", 'nginx-helper' ), $tag->name, $tag->term_id ) ); + $this->purgeUrl( get_tag_link( $tag->term_id ) ); + } + } + + return true; + } + + private function _purge_post_custom_taxa( $_post_id ) { + + $this->log( __( 'Purging post custom taxonomies related', 'nginx-helper' ) ); + + if ( $custom_taxonomies = get_taxonomies( + array( + 'public' => true, + '_builtin' => false, + ) + ) ) { + foreach ( $custom_taxonomies as $taxon ) { + $this->log( sprintf( '+ ' . __( "Purging custom taxonomy '%s'", 'nginx-helper' ), $taxon ) ); + if ( ! in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) { + if ( $terms = get_the_terms( $_post_id, $taxon ) ) { + foreach ( $terms as $term ) { + $this->purgeUrl( get_term_link( $term, $taxon ) ); + } + } + } else { + $this->log( sprintf( '- ' . __( "Your built-in taxonomy '%s' has param '_builtin' set to false.", 'nginx-helper' ), $taxon ), 'WARNING' ); + } + } + } else { + $this->log( '- ' . __( 'No custom taxonomies', 'nginx-helper' ) ); + } + + return true; + } - return true; - } + private function _purge_all_categories() { + + $this->log( __( 'Purging all categories', 'nginx-helper' ) ); - private function _purge_all_daily_archives() { - global $wpdb; + if ( $_categories = get_categories() ) { + foreach ( $_categories as $c ) { + $this->log( sprintf( __( "Purging category '%1\$s' ( id %2\$d )", 'nginx-helper' ), $c->name, $c->term_id ) ); + $this->purgeUrl( get_category_link( $c->term_id ) ); + } + } else { + $this->log( __( 'No categories archives', 'nginx-helper' ) ); + } + + return true; + } + + private function _purge_all_posttags() { + + $this->log( __( 'Purging all tags', 'nginx-helper' ) ); + + if ( $_posttags = get_tags() ) { + foreach ( $_posttags as $t ) { + $this->log( sprintf( __( "Purging tag '%1\$s' ( id %2\$d )", 'nginx-helper' ), $t->name, $t->term_id ) ); + $this->purgeUrl( get_tag_link( $t->term_id ) ); + } + } else { + $this->log( __( 'No tags archives', 'nginx-helper' ) ); + } + + return true; + } + + private function _purge_all_customtaxa() { + + $this->log( __( 'Purging all custom taxonomies', 'nginx-helper' ) ); + + if ( $custom_taxonomies = get_taxonomies( + array( + 'public' => true, + '_builtin' => false, + ) + ) ) { + foreach ( $custom_taxonomies as $taxon ) { + $this->log( sprintf( '+ ' . __( "Purging custom taxonomy '%s'", 'nginx-helper' ), $taxon ) ); + if ( ! in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) { + if ( $terms = get_terms( $taxon ) ) { + foreach ( $terms as $term ) { + $this->purgeUrl( get_term_link( $term, $taxon ) ); + } + } + } else { + $this->log( sprintf( '- ' . __( "Your built-in taxonomy '%s' has param '_builtin' set to false.", 'nginx-helper' ), $taxon ), 'WARNING' ); + } + } + } else { + $this->log( '- ' . __( 'No custom taxonomies', 'nginx-helper' ) ); + } + + return true; + } + + private function _purge_all_taxonomies() { + + $this->_purge_all_categories(); + $this->_purge_all_posttags(); + $this->_purge_all_customtaxa(); + + return true; + } + + private function _purge_all_posts() { + + $this->log( __( 'Purging all posts, pages and custom post types.', 'nginx-helper' ) ); + + $args = array( + 'numberposts' => 0, + 'post_type' => 'any', + 'post_status' => 'publish', + ); + + if ( $_posts = get_posts( $args ) ) { + foreach ( $_posts as $p ) { + $this->log( sprintf( '+ ' . __( "Purging post id '%1\$d' ( post type '%2\$s' )", 'nginx-helper' ), $p->ID, $p->post_type ) ); + $this->purgeUrl( get_permalink( $p->ID ) ); + } + } else { + $this->log( '- ' . __( 'No posts', 'nginx-helper' ) ); + } + + return true; + } + + private function _purge_all_date_archives() { + + $this->log( __( 'Purging all date-based archives.', 'nginx-helper' ) ); - $this->log( __( "Purging all daily archives.", "nginx-helper" ) ); + $this->_purge_all_daily_archives(); + $this->_purge_all_monthly_archives(); + $this->_purge_all_yearly_archives(); - $_query_daily_archives = $wpdb->prepare( - "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', DAYOFMONTH(post_date) AS 'dayofmonth', count(ID) as posts + return true; + } + + private function _purge_all_daily_archives() { + global $wpdb; + + $this->log( __( 'Purging all daily archives.', 'nginx-helper' ) ); + + $_query_daily_archives = $wpdb->prepare( + "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', DAYOFMONTH(post_date) AS 'dayofmonth', count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" - ); - - if ( $_daily_archives = $wpdb->get_results( $_query_daily_archives ) ) { - foreach ( $_daily_archives as $_da ) { - $this->log( - sprintf( - "+ " . __( "Purging daily archive '%s/%s/%s'", "nginx-helper" ), $_da->year, $_da->month, $_da->dayofmonth ) ); - $this->purgeUrl( get_day_link( $_da->year, $_da->month, $_da->dayofmonth ) ); - } - } else { - $this->log("- " . __( "No daily archives", "nginx-helper" ) ); - } - } - - private function _purge_all_monthly_archives() { - global $wpdb; - - $this->log( __( "Purging all monthly archives.", "nginx-helper" ) ); - - $_query_monthly_archives = $wpdb->prepare( - "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) as posts + ); + + if ( $_daily_archives = $wpdb->get_results( $_query_daily_archives ) ) { + foreach ( $_daily_archives as $_da ) { + $this->log( + sprintf( + '+ ' . __( "Purging daily archive '%1\$s/%2\$s/%3\$s'", 'nginx-helper' ), $_da->year, $_da->month, $_da->dayofmonth + ) + ); + $this->purgeUrl( get_day_link( $_da->year, $_da->month, $_da->dayofmonth ) ); + } + } else { + $this->log( '- ' . __( 'No daily archives', 'nginx-helper' ) ); + } + } + + private function _purge_all_monthly_archives() { + global $wpdb; + + $this->log( __( 'Purging all monthly archives.', 'nginx-helper' ) ); + + $_query_monthly_archives = $wpdb->prepare( + "SELECT YEAR(post_date) AS 'year', MONTH(post_date) AS 'month', count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" - ); + ); - if ( $_monthly_archives = $wpdb->get_results( $_query_monthly_archives ) ) { - foreach ( $_monthly_archives as $_ma ) { - $this->log( sprintf( "+ " . __( "Purging monthly archive '%s/%s'", "nginx-helper" ), $_ma->year, $_ma->month ) ); - $this->purgeUrl( get_month_link( $_ma->year, $_ma->month ) ); - } - } else { - $this->log( "- " . __( "No monthly archives", "nginx-helper" ) ); - } - } + if ( $_monthly_archives = $wpdb->get_results( $_query_monthly_archives ) ) { + foreach ( $_monthly_archives as $_ma ) { + $this->log( sprintf( '+ ' . __( "Purging monthly archive '%1\$s/%2\$s'", 'nginx-helper' ), $_ma->year, $_ma->month ) ); + $this->purgeUrl( get_month_link( $_ma->year, $_ma->month ) ); + } + } else { + $this->log( '- ' . __( 'No monthly archives', 'nginx-helper' ) ); + } + } - private function _purge_all_yearly_archives() { - global $wpdb; + private function _purge_all_yearly_archives() { + global $wpdb; - $this->log( __( "Purging all yearly archives.", "nginx-helper" ) ); + $this->log( __( 'Purging all yearly archives.', 'nginx-helper' ) ); - $_query_yearly_archives = $wpdb->prepare( - "SELECT YEAR(post_date) AS 'year', count(ID) as posts + $_query_yearly_archives = $wpdb->prepare( + "SELECT YEAR(post_date) AS 'year', count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date) ORDER BY post_date DESC" - ); - - if ( $_yearly_archives = $wpdb->get_results( $_query_yearly_archives ) ) { - foreach ($_yearly_archives as $_ya ) { - $this->log( sprintf( "+ " . __( "Purging yearly archive '%s'", "nginx-helper" ), $_ya->year ) ); - $this->purgeUrl(get_year_link($_ya->year)); - } - } else { - $this->log( "- " . __( "No yearly archives", "nginx-helper" ) ); - } - } - - public function purge_them_all() { - - $this->log( __( "Let's purge everything!", "nginx-helper" ) ); - $this->_purge_homepage(); - $this->_purge_personal_urls(); - $this->_purge_all_posts(); - $this->_purge_all_taxonomies(); - $this->_purge_all_date_archives(); - $this->log( __( "Everthing purged!", "nginx-helper" ) ); - return true; - } - - public function purge_on_term_taxonomy_edited( $term_id, $tt_id, $taxon ) { - - $this->log( __( "Term taxonomy edited or deleted", "nginx-helper" ) ); - - if ( 'edit_term' == current_filter() && $term = get_term( $term_id, $taxon ) ) { - $this->log( sprintf( __( "Term taxonomy '%s' edited, (tt_id '%d', term_id '%d', taxonomy '%s')", "nginx-helper" ), $term->name, $tt_id, $term_id, $taxon ) ); - } else if ( 'delete_term' == current_filter() ) { - $this->log( sprintf( __( "A term taxonomy has been deleted from taxonomy '%s', (tt_id '%d', term_id '%d')", "nginx-helper" ), $taxon, $term_id, $tt_id ) ); - } - - $this->_purge_homepage(); - - return true; - } - - public function purge_on_check_ajax_referer( $action, $result ) { - - switch ( $action ) { - case 'save-sidebar-widgets' : - $this->log( __( "Widget saved, moved or removed in a sidebar", "nginx-helper" ) ); - $this->_purge_homepage(); - break; - default : - break; - } - return true; - } - - /* - * Source - http://stackoverflow.com/a/1360437/156336 - */ - public function unlinkRecursive( $dir, $deleteRootToo ) { - if ( ! $dh = opendir( $dir ) ) { - return; - } - while ( false !== ($obj = readdir( $dh )) ) { - if ( $obj == '.' || $obj == '..' ) { - continue; - } - - if ( ! @unlink( $dir . '/' . $obj ) ) { - $this->unlinkRecursive( $dir . '/' . $obj, true ); - } - } - - if ($deleteRootToo){ - rmdir($dir); - } - - closedir( $dh ); - - return; - } + ); + + if ( $_yearly_archives = $wpdb->get_results( $_query_yearly_archives ) ) { + foreach ( $_yearly_archives as $_ya ) { + $this->log( sprintf( '+ ' . __( "Purging yearly archive '%s'", 'nginx-helper' ), $_ya->year ) ); + $this->purgeUrl( get_year_link( $_ya->year ) ); + } + } else { + $this->log( '- ' . __( 'No yearly archives', 'nginx-helper' ) ); + } + } + + public function purge_them_all() { + + $this->log( __( "Let's purge everything!", 'nginx-helper' ) ); + $this->_purge_homepage(); + $this->_purge_personal_urls(); + $this->_purge_all_posts(); + $this->_purge_all_taxonomies(); + $this->_purge_all_date_archives(); + $this->log( __( 'Everthing purged!', 'nginx-helper' ) ); + return true; + } + + public function purge_on_term_taxonomy_edited( $term_id, $tt_id, $taxon ) { + + $this->log( __( 'Term taxonomy edited or deleted', 'nginx-helper' ) ); + + if ( 'edit_term' == current_filter() && $term = get_term( $term_id, $taxon ) ) { + $this->log( sprintf( __( "Term taxonomy '%1\$s' edited, (tt_id '%2\$d', term_id '%3\$d', taxonomy '%4\$s')", 'nginx-helper' ), $term->name, $tt_id, $term_id, $taxon ) ); + } elseif ( 'delete_term' == current_filter() ) { + $this->log( sprintf( __( "A term taxonomy has been deleted from taxonomy '%1\$s', (tt_id '%2\$d', term_id '%3\$d')", 'nginx-helper' ), $taxon, $term_id, $tt_id ) ); + } + + $this->_purge_homepage(); + + return true; + } + + public function purge_on_check_ajax_referer( $action, $result ) { + + switch ( $action ) { + case 'save-sidebar-widgets': + $this->log( __( 'Widget saved, moved or removed in a sidebar', 'nginx-helper' ) ); + $this->_purge_homepage(); + break; + default: + break; + } + return true; + } + + /* + * Source - http://stackoverflow.com/a/1360437/156336 + */ + public function unlinkRecursive( $dir, $deleteRootToo ) { + + if ( ! is_dir( $dir ) ) { + return; + } + + if ( ! $dh = opendir( $dir ) ) { + return; + } + + while ( false !== ( $obj = readdir( $dh ) ) ) { + if ( $obj == '.' || $obj == '..' ) { + continue; + } + + if ( ! @unlink( $dir . '/' . $obj ) ) { + $this->unlinkRecursive( $dir . '/' . $obj, false ); + } + } + + if ( $deleteRootToo ) { + rmdir( $dir ); + } + + closedir( $dh ); + + return; + } } diff --git a/admin/partials/nginx-helper-admin-display.php b/admin/partials/nginx-helper-admin-display.php index 1d2865e6..5d7d7cb9 100644 --- a/admin/partials/nginx-helper-admin-display.php +++ b/admin/partials/nginx-helper-admin-display.php @@ -17,38 +17,38 @@
-

- -

-
-
-
- '; - foreach ( $this->settings_tabs as $setting_tab => $setting_name ) { - $class = ( $setting_tab == $current_setting_tab ) ? ' nav-tab-active' : ''; - echo '' . $setting_name['menu_title'] . ''; - } - echo ''; - - switch ( $current_setting_tab ) { - case 'general' : - include 'nginx-helper-general-options.php'; - break; - case 'support' : - include 'nginx-helper-support-options.php'; - break; - } - ?> -
-
- -
-
-
-
\ No newline at end of file +

+ +

+
+
+
+ '; + foreach ( $this->settings_tabs as $setting_tab => $setting_name ) { + $class = ( $setting_tab === $current_setting_tab ) ? ' nav-tab-active' : ''; + echo '' . esc_html( $setting_name['menu_title'] ) . ''; + } + echo ''; + + switch ( $current_setting_tab ) { + case 'general': + include 'nginx-helper-general-options.php'; + break; + case 'support': + include 'nginx-helper-support-options.php'; + break; + } + ?> +
+
+ +
+
+
+ diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index 84ff30c1..ae1ebfd8 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -4,588 +4,590 @@ * * This file is used to markup the admin-facing aspects of the plugin. * - * @link http://example.com * @since 2.0.0 * * @package nginx-helper * @subpackage nginx-helper/admin/partials */ + global $nginx_helper_admin; $error_log_filesize = false; -if( isset( $_POST['smart_http_expire_save'] ) ) { - unset( $_POST['smart_http_expire_save'] ); - unset( $_POST['is_submit'] ); +if ( isset( $_POST['smart_http_expire_save'] ) ) { + unset( $_POST['smart_http_expire_save'] ); + unset( $_POST['is_submit'] ); - $nginx_settings = wp_parse_args( - $_POST, - $nginx_helper_admin->nginx_helper_default_settings() - ); + $nginx_settings = wp_parse_args( + $_POST, + $nginx_helper_admin->nginx_helper_default_settings() + ); - if ( ( ! is_numeric( $nginx_settings['log_filesize'] ) ) || ( empty( $nginx_settings['log_filesize'] ) ) ) { - $error_log_filesize = __( 'Log file size must be a number.', 'nginx-helper' ); - unset( $nginx_settings['log_filesize'] ); - } + if ( ( ! is_numeric( $nginx_settings['log_filesize'] ) ) || ( empty( $nginx_settings['log_filesize'] ) ) ) { + $error_log_filesize = __( 'Log file size must be a number.', 'nginx-helper' ); + unset( $nginx_settings['log_filesize'] ); + } - if ( $nginx_settings['enable_map'] ) { - $nginx_helper_admin->update_map(); - } + if ( $nginx_settings['enable_map'] ) { + $nginx_helper_admin->update_map(); + } - update_site_option( 'rt_wp_nginx_helper_options', $nginx_settings ); + update_site_option( 'rt_wp_nginx_helper_options', $nginx_settings ); - echo '

' . __( 'Settings saved.', 'nginx-helper' ) . '

'; + echo '

' . esc_html__( 'Settings saved.', 'nginx-helper' ) . '

'; } $nginx_helper_settings = $nginx_helper_admin->nginx_helper_settings(); -$log_path = $nginx_helper_admin->functional_asset_path(); -$log_url = $nginx_helper_admin->functional_asset_url(); +$log_path = $nginx_helper_admin->functional_asset_path(); +$log_url = $nginx_helper_admin->functional_asset_url(); /** * Get setting url 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/'; - } else { - $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdomains/fastcgi-cache-with-purging/'; - } + if ( SUBDOMAIN_INSTALL === false ) { + $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdirectories/fastcgi-cache-with-purging/'; + } else { + $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdomains/fastcgi-cache-with-purging/'; + } } else { - $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/'; + $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/'; } ?> - +
-
-

- -

-
- - - - -
- /> - -
-
-
+
+

+ +

+
+ + + + +
+ /> + +
+
+
- - -
- - -
- -
-

- -

-
- - - - - - - - - - - - - -
- /> - -
- /> - -
- /> - -
-
-
- + +
+ + +
+ +
+

+ +

+
+ + + + + + + + + + + + + +
+ /> + +
+ /> + +
+ /> + +
+
+
+ - - -
+ + diff --git a/admin/partials/nginx-helper-sidebar-display.php b/admin/partials/nginx-helper-sidebar-display.php index fb0214ea..8a0c4317 100644 --- a/admin/partials/nginx-helper-sidebar-display.php +++ b/admin/partials/nginx-helper-sidebar-display.php @@ -4,81 +4,88 @@ * * This file is used to markup the admin-facing aspects of the plugin. * - * @link http://example.com * @since 2.0.0 * * @package nginx-helper * @subpackage nginx-helper/admin/partials */ -$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'); + +$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' ); ?>
- - - + + +
-

- -

-
-

- free support forum.', - 'nginx-helper'), - 'http://rtcamp.com/support/forum/wordpress-nginx/' - ); - ?> -

-
+

+ +

+
+

+ free support forum.', + 'nginx-helper' + ), + 'http://rtcamp.com/support/forum/wordpress-nginx/' + ); + ?> +

+
-

- - - -

-
- - - - -
+

+ + + +

+
+ + + + +
-

-

-
-
\ No newline at end of file +

+

+
+ diff --git a/admin/partials/nginx-helper-support-options.php b/admin/partials/nginx-helper-support-options.php index 4989fbc5..5e04ec61 100644 --- a/admin/partials/nginx-helper-support-options.php +++ b/admin/partials/nginx-helper-support-options.php @@ -4,41 +4,41 @@ * * This file is used to markup the admin-facing aspects of the plugin. * - * @link http://example.com * @since 2.0.0 * * @package nginx-helper * @subpackage nginx-helper/admin/partials */ + ?>
-

- -

-
- - - - - - - - - -
- - - - - -
- - - - - -
-
-
\ No newline at end of file +

+ +

+
+ + + + + + + + + +
+ + + + + +
+ + + + + +
+
+ diff --git a/admin/predis.php b/admin/predis.php index 48399dff..040065c6 100644 --- a/admin/predis.php +++ b/admin/predis.php @@ -15200,4 +15200,3 @@ protected function getReadOnlyOperations() } /* --------------------------------------------------------------------------- */ - diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..f215446d --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "rtcamp/nginx-helper", + "description": "Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things.", + "keywords": ["wordpress", "plugin", "nginx", "nginx-helper", "fastcgi", "redis-cache", "redis", "cache"], + "homepage": "https://rtcamp.com/nginx-helper/", + "license": "GPL-2.0+", + "authors": [{ + "name": "rtCamp", + "email": "support@rtcamp.com", + "homepage": "https://rtcamp.com" + }], + "minimum-stability": "dev", + "prefer-stable": true, + "type": "wordpress-plugin", + "support": { + "issues": "https://github.com/rtCamp/nginx-helper/issues", + "forum": "https://wordpress.org/support/plugin/nginx-helper", + "wiki": "https://github.com/rtCamp/nginx-helper/wiki", + "source": "https://github.com/rtCamp/nginx-helper/" + }, + "require": { + "php": ">=5.3.2", + "composer/installers": "^1.0" + }, + "require-dev": { + "wpreadme2markdown/wpreadme2markdown": "*" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 00000000..589b0711 --- /dev/null +++ b/composer.lock @@ -0,0 +1,200 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "f8ee8d46fadaee8c9cc194ef126e7404", + "packages": [ + { + "name": "composer/installers", + "version": "v1.0.6", + "source": { + "type": "git", + "url": "https://github.com/composer/installers.git", + "reference": "b3bd071ea114a57212c75aa6a2eef5cfe0cc798f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/installers/zipball/b3bd071ea114a57212c75aa6a2eef5cfe0cc798f", + "reference": "b3bd071ea114a57212c75aa6a2eef5cfe0cc798f", + "shasum": "" + }, + "replace": { + "shama/baton": "*" + }, + "require-dev": { + "composer/composer": "1.0.*@dev", + "phpunit/phpunit": "3.7.*" + }, + "type": "composer-installer", + "extra": { + "class": "Composer\\Installers\\Installer", + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-0": { + "Composer\\Installers\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Kyle Robinson Young", + "email": "kyle@dontkry.com", + "homepage": "https://github.com/shama", + "role": "Developer" + } + ], + "description": "A multi-framework Composer library installer", + "homepage": "http://composer.github.com/installers/", + "keywords": [ + "TYPO3 CMS", + "TYPO3 Flow", + "TYPO3 Neos", + "agl", + "cakephp", + "codeigniter", + "drupal", + "fuelphp", + "installer", + "joomla", + "kohana", + "laravel", + "li3", + "lithium", + "mako", + "modulework", + "phpbb", + "ppi", + "silverstripe", + "symfony", + "wordpress", + "zend" + ], + "time": "2013-08-20 04:37:09" + } + ], + "packages-dev": [ + { + "name": "symfony/console", + "version": "v2.7.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/Console.git", + "reference": "564398bc1f33faf92fc2ec86859983d30eb81806" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Console/zipball/564398bc1f33faf92fc2ec86859983d30eb81806", + "reference": "564398bc1f33faf92fc2ec86859983d30eb81806", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/event-dispatcher": "~2.1", + "symfony/phpunit-bridge": "~2.7", + "symfony/process": "~2.1" + }, + "suggest": { + "psr/log": "For using the console logger", + "symfony/event-dispatcher": "", + "symfony/process": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Console\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Console Component", + "homepage": "https://symfony.com", + "time": "2015-06-10 15:30:22" + }, + { + "name": "wpreadme2markdown/wpreadme2markdown", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/benbalter/WP-Readme-to-Github-Markdown.git", + "reference": "dceae108111232949affc9107c98276c6fa6c98f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/benbalter/WP-Readme-to-Github-Markdown/zipball/dceae108111232949affc9107c98276c6fa6c98f", + "reference": "dceae108111232949affc9107c98276c6fa6c98f", + "shasum": "" + }, + "require": { + "php": ">= 5.3.3", + "symfony/console": "~2.4" + }, + "bin": [ + "bin/wp2md" + ], + "type": "library", + "autoload": { + "psr-4": { + "WPReadme2Markdown\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Archer", + "email": "chrstnarchr@aol.com" + }, + { + "name": "Benjamin J. Balter" + } + ], + "description": "Convert WordPress Plugin readme.txt to Markdown", + "keywords": [ + "converter", + "markdown", + "readme", + "wordpress" + ], + "time": "2014-05-28 21:28:31" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.2" + }, + "platform-dev": [] +} diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 00000000..53cdbb05 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,95 @@ +#! /bin/bash +# A modification of Dean Clatworthy's deploy script as found here: https://github.com/deanc/wordpress-plugin-git-svn +# The difference is that this script lives in the plugin's git repo & doesn't require an existing SVN repo. + +# main config +PLUGINSLUG="nginx-helper" +MAINFILE="nginx-helper.php" # this should be the name of your main php file in the wordpress plugin +#SVNUSER="rtcamp" # your svn username + + +##### YOU CAN STOP EDITING HERE ##### +CURRENTDIR=`pwd` + +# git config +GITPATH="$CURRENTDIR/" # this file should be in the base of your git repository + +# svn config +SVNPATH="/tmp/$PLUGINSLUG" # path to a temp SVN repo. No trailing slash required and don't add trunk. +SVNURL="https://plugins.svn.wordpress.org/$PLUGINSLUG/" # Remote SVN repo on wordpress.org, with no trailing slash + +# Detect svn username based on url +SVNUSER=$(cat ~/.subversion/auth/svn.simple/* | grep -A4 $(echo $SVNURL | awk -F// '{print $2}' | cut -d'/' -f1) | tail -n1) +if [ -z "$SVNUSER" ] +then + SVNUSER="chandrapatel" +fi + + +# Let's begin... +echo ".........................................." +echo +echo "Preparing to deploy WordPress plugin" +echo +echo ".........................................." +echo + +# Check version in readme.txt is the same as plugin file +NEWVERSION1=`grep "^Stable tag" $GITPATH/readme.txt | awk -F' ' '{print $3}'` +echo "readme version: $NEWVERSION1" +#NEWVERSION2=`grep "^Version" $GITPATH/$MAINFILE | awk -F' ' '{print $2}'` +NEWVERSION2=`grep -i "Version" $GITPATH/$MAINFILE | head -n1 | awk -F':' '{print $2}' | awk -F' ' '{print $1}'` +echo "$MAINFILE version: $NEWVERSION2" + +if [ "$NEWVERSION1" != "$NEWVERSION2" ]; then echo "Versions don't match. Exiting...."; exit 1; fi + +echo "Versions match in readme.txt and PHP file. Let's proceed..." + +cd $GITPATH +bash readme.sh $SVNURL +git add README.md +echo -e "Enter a commit message for this new version: \c" +read COMMITMSG +git commit -am "$COMMITMSG" + +echo "Tagging new version in git" +git tag -a "$NEWVERSION1" -m "Tagging version $NEWVERSION1" + +echo "Pushing latest commit to origin, with tags" +git push origin master +git push origin master --tags + +echo +echo "Creating local copy of SVN repo ..." +svn co $SVNURL $SVNPATH + +echo "Exporting the HEAD of master from git to the trunk of SVN" +git checkout-index -a -f --prefix=$SVNPATH/trunk/ + +echo "Ignoring github specific files and deployment script" +svn propset svn:ignore "deploy.sh +readme.sh +README.md +.git +.gitattributes +.gitignore +tests +map.conf +nginx.log" "$SVNPATH/trunk/" + +echo "Changing directory to SVN and committing to trunk" +cd $SVNPATH/trunk/ +# Add all new files that are not set to be ignored +svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add +svn commit --username=$SVNUSER -m "$COMMITMSG" + +echo "Creating new SVN tag & committing it" +cd $SVNPATH +svn copy trunk/ tags/$NEWVERSION1/ +cd $SVNPATH/tags/$NEWVERSION1 +svn commit --username=$SVNUSER -m "Tagging version $NEWVERSION1" + +echo "Removing temporary directory $SVNPATH" +rm -fr $SVNPATH/ + +echo "*** FIN ***" diff --git a/includes/class-nginx-helper-activator.php b/includes/class-nginx-helper-activator.php index f7186dab..c6e0ebd7 100644 --- a/includes/class-nginx-helper-activator.php +++ b/includes/class-nginx-helper-activator.php @@ -1,58 +1,50 @@ functional_asset_path(); - if ( !is_dir( $path ) ) { - mkdir( $path ); - } + $path = $nginx_helper_admin->functional_asset_path(); + if ( ! is_dir( $path ) ) { + mkdir( $path ); + } - 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' ) - ); + 'rt_wp_nginx_helper_init_check', + __( 'Sorry, you need to be an administrator to use Nginx Helper', 'nginx-helper' ) + ); return; } $role->add_cap( 'Nginx Helper | Config' ); $role->add_cap( 'Nginx Helper | Purge cache' ); - wp_schedule_event( time(), 'daily', 'rt_wp_nginx_helper_check_log_file_size_daily' ); - } + wp_schedule_event( time(), 'daily', 'rt_wp_nginx_helper_check_log_file_size_daily' ); + } } diff --git a/includes/class-nginx-helper-deactivator.php b/includes/class-nginx-helper-deactivator.php index 0e1dadf9..c767f92f 100644 --- a/includes/class-nginx-helper-deactivator.php +++ b/includes/class-nginx-helper-deactivator.php @@ -1,13 +1,8 @@ remove_cap( 'Nginx Helper | Config' ); + + wp_clear_scheduled_hook( 'rt_wp_nginx_helper_check_log_file_size_daily' ); + + $role = get_role( 'administrator' ); + $role->remove_cap( 'Nginx Helper | Config' ); $role->remove_cap( 'Nginx Helper | Purge cache' ); } diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index ed5efe13..09061426 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -56,8 +56,8 @@ class Nginx_Helper { * @var string $version The current version of the plugin. */ protected $version; - - /** + + /** * Minimum WordPress Version Required. * * @since 2.0.0 @@ -78,21 +78,21 @@ class Nginx_Helper { public function __construct() { $this->plugin_name = 'nginx-helper'; - $this->version = '2.0.0'; - $this->minimum_WP = '3.0'; - - if ( !$this->required_wp_version() ) { - return; - } - - if ( !defined( 'RT_WP_NGINX_HELPER_CACHE_PATH' ) ) { - define( 'RT_WP_NGINX_HELPER_CACHE_PATH', '/var/run/nginx-cache' ); - } - + $this->version = '2.0.0'; + $this->minimum_WP = '3.0'; + + if ( ! $this->required_wp_version() ) { + return; + } + + if ( ! defined( 'RT_WP_NGINX_HELPER_CACHE_PATH' ) ) { + define( 'RT_WP_NGINX_HELPER_CACHE_PATH', '/var/run/nginx-cache' ); + } + $this->load_dependencies(); $this->set_locale(); $this->define_admin_hooks(); - //$this->define_public_hooks(); + // $this->define_public_hooks(); } /** @@ -124,12 +124,12 @@ private function load_dependencies() { * of the plugin. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-nginx-helper-i18n.php'; - - /** + + /** * The class responsible for defining all actions that required for purging urls. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-purger.php'; - + /** * The class responsible for defining all actions that occur in the admin area. */ @@ -139,8 +139,7 @@ private function load_dependencies() { * The class responsible for defining all actions that occur in the public-facing * side of the site. */ - //require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-nginx-helper-public.php'; - + // require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-nginx-helper-public.php'; $this->loader = new Nginx_Helper_Loader(); } @@ -171,57 +170,57 @@ private function set_locale() { * @access private */ private function define_admin_hooks() { - global $nginx_helper_admin, $nginx_purger; - + global $nginx_helper_admin, $nginx_purger; + $nginx_helper_admin = new Nginx_Helper_Admin( $this->get_plugin_name(), $this->get_version() ); - - if ( !empty( $nginx_helper_admin->options['cache_method'] ) && $nginx_helper_admin->options['cache_method'] == "enable_redis") { - if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-phpredis-purger.php'; - $nginx_purger = new PhpRedis_Purger(); - } else { - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-predis-purger.php'; - $nginx_purger = new Predis_Purger(); - } - } else { - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-fastcgi-purger.php'; - $nginx_purger = new FastCGI_Purger(); - } - - $this->loader->add_action( 'admin_enqueue_scripts', $nginx_helper_admin, 'enqueue_styles' ); + + if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && $nginx_helper_admin->options['cache_method'] == 'enable_redis' ) { + if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-phpredis-purger.php'; + $nginx_purger = new PhpRedis_Purger(); + } else { + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-predis-purger.php'; + $nginx_purger = new Predis_Purger(); + } + } else { + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-fastcgi-purger.php'; + $nginx_purger = new FastCGI_Purger(); + } + + $this->loader->add_action( 'admin_enqueue_scripts', $nginx_helper_admin, 'enqueue_styles' ); $this->loader->add_action( 'admin_enqueue_scripts', $nginx_helper_admin, 'enqueue_scripts' ); - - if ( is_multisite() ) { - $this->loader->add_action( 'network_admin_menu', $nginx_helper_admin, 'nginx_helper_admin_menu' ); - $this->loader->add_filter( "network_admin_plugin_action_links_" . NGINX_HELPER_BASENAME, $nginx_helper_admin, 'nginx_helper_settings_link' ); - } else { - $this->loader->add_action( 'admin_menu', $nginx_helper_admin, 'nginx_helper_admin_menu' ); - $this->loader->add_filter( "plugin_action_links_" . NGINX_HELPER_BASENAME, $nginx_helper_admin, 'nginx_helper_settings_link' ); - } - - $this->loader->add_action( 'admin_bar_menu', $nginx_helper_admin, 'nginx_helper_toolbar_purge_link', 100 ); - $this->loader->add_action( 'wp_ajax_rt_get_feeds', $nginx_helper_admin, 'nginx_helper_get_feeds' ); - - $this->loader->add_action( 'shutdown', $nginx_helper_admin, 'add_timestamps', 99999 ); - $this->loader->add_action( 'add_init', $nginx_helper_admin, 'update_map' ); - - // Add actions to purge. - $this->loader->add_action( 'wp_insert_comment', $nginx_purger, 'purgePostOnComment', 200, 2 ); - $this->loader->add_action( 'transition_comment_status', $nginx_purger, 'purgePostOnCommentChange', 200, 3 ); - $this->loader->add_action( 'transition_post_status', $nginx_helper_admin, 'set_future_post_option_on_future_status', 20, 3 ); - $this->loader->add_action( 'delete_post', $nginx_helper_admin, 'unset_future_post_option_on_delete', 20, 1 ); - $this->loader->add_action( 'nm_check_log_file_size_daily', $nginx_purger, 'checkAndTruncateLogFile', 100, 1 ); - $this->loader->add_action( 'edit_attachment', $nginx_purger, 'purgeImageOnEdit', 100, 1 ); - $this->loader->add_action( 'wpmu_new_blog', $nginx_helper_admin, 'update_new_blog_options', 10, 1 ); - $this->loader->add_action( 'transition_post_status', $nginx_purger, 'purge_on_post_moved_to_trash', 20, 3 ); - $this->loader->add_action( 'edit_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 ); - $this->loader->add_action( 'delete_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 ); - $this->loader->add_action( 'check_ajax_referer', $nginx_purger, 'purge_on_check_ajax_referer', 20, 2 ); - $this->loader->add_action( 'admin_init', $nginx_helper_admin, 'purge_all' ); - - // expose action to allow other plugins to purge the cache - $this->loader->add_action( 'rt_nginx_helper_purge_all', $nginx_purger, 'purgeAll' ); - } + + if ( is_multisite() ) { + $this->loader->add_action( 'network_admin_menu', $nginx_helper_admin, 'nginx_helper_admin_menu' ); + $this->loader->add_filter( 'network_admin_plugin_action_links_' . NGINX_HELPER_BASENAME, $nginx_helper_admin, 'nginx_helper_settings_link' ); + } else { + $this->loader->add_action( 'admin_menu', $nginx_helper_admin, 'nginx_helper_admin_menu' ); + $this->loader->add_filter( 'plugin_action_links_' . NGINX_HELPER_BASENAME, $nginx_helper_admin, 'nginx_helper_settings_link' ); + } + + $this->loader->add_action( 'admin_bar_menu', $nginx_helper_admin, 'nginx_helper_toolbar_purge_link', 100 ); + $this->loader->add_action( 'wp_ajax_rt_get_feeds', $nginx_helper_admin, 'nginx_helper_get_feeds' ); + + $this->loader->add_action( 'shutdown', $nginx_helper_admin, 'add_timestamps', 99999 ); + $this->loader->add_action( 'add_init', $nginx_helper_admin, 'update_map' ); + + // Add actions to purge. + $this->loader->add_action( 'wp_insert_comment', $nginx_purger, 'purgePostOnComment', 200, 2 ); + $this->loader->add_action( 'transition_comment_status', $nginx_purger, 'purgePostOnCommentChange', 200, 3 ); + $this->loader->add_action( 'transition_post_status', $nginx_helper_admin, 'set_future_post_option_on_future_status', 20, 3 ); + $this->loader->add_action( 'delete_post', $nginx_helper_admin, 'unset_future_post_option_on_delete', 20, 1 ); + $this->loader->add_action( 'nm_check_log_file_size_daily', $nginx_purger, 'checkAndTruncateLogFile', 100, 1 ); + $this->loader->add_action( 'edit_attachment', $nginx_purger, 'purgeImageOnEdit', 100, 1 ); + $this->loader->add_action( 'wpmu_new_blog', $nginx_helper_admin, 'update_new_blog_options', 10, 1 ); + $this->loader->add_action( 'transition_post_status', $nginx_purger, 'purge_on_post_moved_to_trash', 20, 3 ); + $this->loader->add_action( 'edit_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 ); + $this->loader->add_action( 'delete_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 ); + $this->loader->add_action( 'check_ajax_referer', $nginx_purger, 'purge_on_check_ajax_referer', 20, 2 ); + $this->loader->add_action( 'admin_init', $nginx_helper_admin, 'purge_all' ); + + // expose action to allow other plugins to purge the cache + $this->loader->add_action( 'rt_nginx_helper_purge_all', $nginx_purger, 'purgeAll' ); + } /** * Register all of the hooks related to the public-facing functionality @@ -278,42 +277,42 @@ public function get_loader() { public function get_version() { return $this->version; } - - /** - * Check wp version. + + /** + * Check wp version. * - * @since 2.0.0 - * @global type $wp_version - * @return boolean - */ - public function required_wp_version() { - global $wp_version; - - $wp_ok = version_compare( $wp_version, $this->minimum_wp, '>=' ); - - if ( ( false == $wp_ok ) ) { - add_action( 'admin_notices', array( &$this, 'display_notices' ) ); - add_action( 'network_admin_notices', array( &$this, 'display_notices' ) ); - return false; - } - - return true; - } - - /** - * Dispay plugin notices. - */ - public function display_notices() { -?> -
-

- - minimum_WP . ' or higher', 'nginx-helper' ); - ?> - -

-
-minimum_wp, '>=' ); + + if ( ( false == $wp_ok ) ) { + add_action( 'admin_notices', array( &$this, 'display_notices' ) ); + add_action( 'network_admin_notices', array( &$this, 'display_notices' ) ); + return false; + } + + return true; + } + + /** + * Dispay plugin notices. + */ + public function display_notices() { + ?> +
+

+ + minimum_WP . ' or higher', 'nginx-helper' ); + ?> + +

+
+ run(); + // Load WP-CLI command. + if ( defined( 'WP_CLI' ) && WP_CLI ) { + + require_once plugin_dir_path( __FILE__ ) . 'wp-cli.php'; + \WP_CLI::add_command( 'nginx-helper', 'Nginx_Helper_WP_CLI_Command' ); + + } + } run_nginx_helper(); diff --git a/readme.txt b/readme.txt index 3a35980b..45544ea3 100644 --- a/readme.txt +++ b/readme.txt @@ -1,14 +1,14 @@ === Nginx Helper === -Contributors: rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123 +Contributors: rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint +Donate Link: http://rtcamp.com/donate/ Tags: nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks -Requires at least: 3.0 -Tested up to: 4.2.2 -Stable tag: 1.9.4 License: GPLv2 or later (of-course) License URI: http://www.gnu.org/licenses/gpl-2.0.html -Donate Link: http://rtcamp.com/donate/ +Requires at least: 3.0 +Tested up to: 4.9.5 +Stable tag: 1.9.11 -Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does few more things. +Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things. == Description == @@ -21,11 +21,11 @@ Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/publ You will need to follow one or more tutorials below to get desired functionality: -* [Nginx Map + WordPress-Multisite + Static Files Handling](http://rtcamp.com/tutorials/nginx-maps-wordpress-multisite-static-files-handling/) -* [Nginx + WordPress + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-nginx-fastcgi-cache-purge-conditional/) -* [Nginx + WordPress-Multisite (Subdirectories) + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-multisite-subdirectories-nginx-fastcgi-cache-purge/) -* [Nginx + WordPress-Multisite (Subdomains/domain-mapping) + fastcgi_purge_cache](http://rtcamp.com/tutorials/wordpress-multisite-subdomains-domain-mapping-nginx-fastcgi-cache-purge/) -* [Other WordPress-Nginx Tutorials](http://rtcamp.com/wordpress-nginx/tutorials/) +* [Nginx Map + WordPress-Multisite + Static Files Handling](https://easyengine.io/wordpress-nginx/tutorials/multisite/static-files-handling/) +* [Nginx + WordPress + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/) +* [Nginx + WordPress-Multisite (Subdirectories) + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/multisite/subdirectories/fastcgi-cache-with-purging/) +* [Nginx + WordPress-Multisite (Subdomains/domain-mapping) + fastcgi_purge_cache](https://easyengine.io/wordpress-nginx/tutorials/multisite/subdomains/fastcgi-cache-with-purging/) +* [Other WordPress-Nginx Tutorials](https://easyengine.io/wordpress-nginx/tutorials/) == Installation == @@ -45,13 +45,13 @@ For proper configuration, check out our **tutorial list** in the [Description ta == Frequently Asked Questions == -**Important** - Please refer to [http://rtcamp.com/nginx-helper/faq](http://rtcamp.com/nginx-helper/faq) for up-to-date FAQs. +**Important** - Please refer to [https://easyengine.io/nginx-helper/faq](https://easyengine.io/nginx-helper/faq) for up-to-date FAQs. = FAQ - Installation/Comptability = **Q. Will this work out of the box?** -No. You need to make some changes at the Nginx end. Please check our [tutorial list](http://rtcamp.com/wordpress-nginx/tutorials). +No. You need to make some changes at the Nginx end. Please check our [tutorial list](https://easyengine.io/wordpress-nginx/tutorials/). = FAQ - Nginx Fastcgi Cache Purge = @@ -114,6 +114,35 @@ Please post your problem in [our free support forum](http://community.rtcamp.com == Changelog == += 1.9.11 = +* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick +* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor) + += 1.9.10 = +* Fixed issue where Nginx cache folders deleted on purge. [#123](https://github.com/rtCamp/nginx-helper/pull/123) - by [johan-chassaing](https://github.com/johan-chassaing) +* Fixed Redis purge all feature for installation where WordPress lives in a separate folder. [#130](https://github.com/rtCamp/nginx-helper/pull/130) - by [pySilver](https://github.com/pySilver) + += 1.9.9 = +* Fix wp_redirect issue. [#131](https://github.com/rtCamp/nginx-helper/pull/131) - by [matt-h](https://github.com/matt-h) + += 1.9.8 = +* Fixed homepage cache cleared when WPML plugin used [#116](https://github.com/rtCamp/nginx-helper/pull/116) - by [Niwreg](https://profiles.wordpress.org/niwreg/) +* Fixed Purge Cache clears the whole Redis cache [#113](https://github.com/rtCamp/nginx-helper/issues/113) - by HansVanEijsden +* One log file for all site in WPMU. +* Single site Redis cache purge when click on Purge Cache button in WPMU [#122](https://github.com/rtCamp/nginx-helper/pull/122) - by Lars Støttrup Nielsen +* Fixed notices and warnings. + += 1.9.7 = +* Remove timestamp if cron or wp-cli [#114](https://github.com/rtCamp/nginx-helper/pull/114) - by [samedwards](https://profiles.wordpress.org/samedwards/) +* Fixed notices and warnings. + += 1.9.6 = +* Fixed cache purging on post publish. +* Error fixed when redis server not installed. + += 1.9.5 = +Added custom purge URL option. + = 1.9.4 = * Added redis server connection timeout. * Added RedisException handling. @@ -315,6 +344,6 @@ Fix url escaping [#82](https://github.com/rtCamp/nginx-helper/pull/82) - by == Upgrade Notice == -= 1.9.4 = -* Added redis server connection timeout. -* Added RedisException handling. += 1.9.11 = +* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick +* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor) diff --git a/wercker.yml b/wercker.yml new file mode 100644 index 00000000..ff534cc2 --- /dev/null +++ b/wercker.yml @@ -0,0 +1,22 @@ +box: ubuntu + +build: + steps: + + - script: + name: placehoder + code: echo "nothing to build" + +deploy: + steps: + # - install-packages: + # packages: subversion git wget + + - rtcamp/wordpress-svn: + pluginslug: nginx-helper + mainfile: nginx-helper.php + svnuser: $SVNUSER + svnpass: $SVNPASS #wordpress.org password + gituser: $GITUSER + gitpass: $GITPASS #github.com password + gitemail: git@example.com #github.com password diff --git a/wp-cli.php b/wp-cli.php new file mode 100644 index 00000000..fe5e4b65 --- /dev/null +++ b/wp-cli.php @@ -0,0 +1,38 @@ +purgeAll(); + + $message = __( 'Purged Everything!' ); + WP_CLI::success( $message ); + + } + + } + +}