From 7697c5f5868b01fd40942052c73b80f3b7ef71ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Wed, 31 Aug 2016 19:24:57 +0200 Subject: [PATCH 01/11] Update README.md --- README.md | 336 +----------------------------------------------------- 1 file changed, 1 insertion(+), 335 deletions(-) diff --git a/README.md b/README.md index 16e967b2..9d9fb635 100644 --- a/README.md +++ b/README.md @@ -1,335 +1 @@ -# Nginx Helper # -**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 -**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.5.2 -**Stable tag:** 1.9.7 -**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-cache whenever a post is edited/published. Also does a few more things. - -## Description ## - -1. Removes `index.php` from permalinks when using WordPress with nginx. -1. Adds support for purging redis-cache when used as full-page cache created using [nginx-srcache-module](https://github.com/openresty/srcache-nginx-module#caching-with-redis) -1. Adds support for nginx fastcgi_cache_purge & proxy_cache_purge directive from [module](https://github.com/FRiCKLE/ngx_cache_purge "ngx_cache_purge module"). Provides settings so you can customize purging rules. -1. Adds support for nginx `map{..}` on a WordPress-multisite network installation. Using it, Nginx can serve PHP file uploads even if PHP/MySQL crashes. Please check the tutorial list below for related Nginx configurations. - -### Tutorials ### - -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/) - - -## Installation ## - -Automatic Installation - -1. Log in to your WordPress admin panel, navigate to the Plugins menu and click Add New. -1. In the search field type “Nginx Helper” and click Search Plugins. From the search results, pick Nginx Helper and click Install Now. Wordpress will ask you to confirm to complete the installation. - -Manual Installation - -1. Extract the zip file. -1. Upload them to `/wp-content/plugins/` directory on your WordPress installation. -1. Then activate the Plugin from Plugins page. - -For proper configuration, check out our **tutorial list** in the [Description tab](http://wordpress.org/extend/plugins/nginx-helper). - -## Frequently Asked Questions ## - -**Important** - Please refer to [http://rtcamp.com/nginx-helper/faq](http://rtcamp.com/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). - -### FAQ - Nginx Fastcgi Cache Purge ### - -**Q. There's a 'purge all' button? Does it purge the whole site?** - -Yes, it does. It physically empties the cache directory. It is set by default to `/var/run/nginx-cache/`. - -If your cache directory is different, you can override this in your wp-config.php by adding -`define('RT_WP_NGINX_HELPER_CACHE_PATH','/var/run/nginx-cache/');` - -Replace the path with your own. - -**Q. Does it work for custom posts and taxonomies?** - -Yes. It handles all post-types the same way. - -**Q. How do I know my Nginx config is correct for fastcgi purging?** - -Manually purging any page from the cache, by following instructions in the previous answer. - -Version 1.3.4 onwards, Nginx Helper adds a comment at the end of the HTML source ('view source' in your favourite browser): -`<!--Cached using Nginx-Helper on 2012-10-08 07:01:45. It took 42 queries executed in 0.280 seconds.-->`. This shows the time when the page was last cached. This date/time will be reset whenever this page is purged and refreshed in the cache. Just check this comment before and after a manual purge. - -As long as you don't purge the page (or make changes that purge it from the cache), the timestamp will remain as is, even if you keep refreshing the page. This means the page was served from the cache and it's working! - -The rest shows you the database queries and time saved on loading this page. (This would have been the additional resource load, if you weren't using fast-cgi-cache.) - - -**Q. I need to flush a cached page immediately! How do I do that?** - -Nginx helper plugin handles usual scenarios, when a page in the cache will need purging. For example, when a post is edited or a comment is approved on a post. - -To purge a page immediately, follow these instructions: - -* Let's say we have a page at the following domain: http://yoursite.com/about. -* Between the domain name and the rest of the URL, insert '/purge/'. -* So, in the above example, the purge URL will be http://yoursite.com/purge/about. -* Just open this in a browser and the page will be purged instantly. -* Needless to say, this won't work, if you have a page or taxonomy called 'purge'. - - -### FAQ - Nginx Map ### - -**Q. My multisite already uses `WPMU_ACCEL_REDIRECT`. Do I still need Nginx Map?** - -Definitely. `WPMU_ACCEL_REDIRECT` reduces the load on PHP, but it still ask WordPress i.e. PHP/MySQL to do some work for static files e.g. images in your post. Nginx map lets nginx handle files on its own bypassing wordpress which gives you much better performance without using a CDN. - -**Q. I am using X plugin. Will it work on Nginx?** - -Most likely yes. A wordpress plugin, if not using explicitly any Apache-only mod, should work on Nginx. Some plugin may need some extra work. - - -### Still need help! ### - -Please post your problem in [our free support forum](http://community.rtcamp.com/c/wordpress-nginx). - -## Screenshots ## -###1. Nginx plugin settings### -![Nginx plugin settings](https://ps.w.org/nginx-helper/assets/screenshot-1.png) - -###2. Remaining settings### -![Remaining settings](https://ps.w.org/nginx-helper/assets/screenshot-2.png) - - -## Changelog ## - -### 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. -* Added composer.json support -* Fixed cache purging link in admin bar. -* Updated the initial settings to include the 'purge_method' [#99](https://github.com/rtCamp/nginx-helper/pull/99) - by -[gagan0123](https://github.com/gagan0123) - -### 1.9.2 ### -Fix purging for Redis cache and FastCGI cache - -### 1.9.1 ### -Fix purging for custom post types - -### 1.9 ### -Added Redis cache purge support. - -### 1.8.13 ### -Fixed PHP notice for an undefined index when "Enable Logging" is not set. - -### 1.8.12 ### -Updated readme and changelog - -### 1.8.11 ### -Fix url escaping [#82](https://github.com/rtCamp/nginx-helper/pull/82) - by -[javisperez](https://github.com/javisperez) - -### 1.8.10 ### -* Security bug fix - -### 1.8.9 ### -* Default setting fix and wp-cli example correction - by [bcole808](https://profiles.wordpress.org/bcole808/) - -### 1.8.8 ### -* Added option to purge cache without nginx purge module - by [bcole808](https://profiles.wordpress.org/bcole808/) - -### 1.8.7 ### -* Added action `rt_nginx_helper_purge_all` to purge cache from other plugins - by [gungeekatx](https://profiles.wordpress.org/gungeekatx/) - -### 1.8.6 ### -* Removed wercker.yml from plugin zip/svn. -* Updated readme - -### 1.8.5 ### -* Added WP_CLI support - by [Udit Desai](https://profiles.wordpress.org/desaiuditd/) - -### 1.8.4 ### -* Fix undefined index issue and correct "purge_archive_on_del" key - -### 1.8.3 ### -* Tested with WordPress 4.0 -* Fix issue #69 - -### 1.8.1 ### -* Tested with wordpress 3.9.1 -* Fix confilct with Mailchimp's Social plugin - -### 1.8 ### -* New admin UI -* Fix missing wp_sanitize_redirect function call - -### 1.7.6 ### -* Update Backend UI -* Added Language Support - -### 1.7.5 ### -* Fixed option name mismatch issue to purge homepage on delete. - -### 1.7.4 ### -* Disable purge and stamp by default. - -### 1.7.3 ### -* Suppressed `unlink` related error-messages which can be safely ignored. -* Fixed a bug in purge-all option. - -### 1.7.2 ### -* [pjv](http://profiles.wordpress.org/pjv/) fixed bug in logging file. - -### 1.7.1 ### -* Fixes bug in true purge and admin screen. - -### 1.7 ### -* True full cache purge added. -* Map file location changed to uploads' directory to fix http://rtcamp.com/support/topic/plugin-update-removes-map-file/ -* Log file location also changed to uploads' directory. - -### 1.6.13 ### -* [pjv](http://profiles.wordpress.org/pjv/) changed the way home URL is accessed. Instead of site option, the plugin now uses home_URL() function. - -### 1.6.12 ### -* [telofy](http://wordpress.org/support/profile/telofy) added purging of atom and RDF feeds. - -### 1.6.11 ### -* Removed comments from Admin screens since, it was interfering with media uploads in 3.5 up. - -### 1.6.10 ### -* Cleaned up code. -* Added credits for code. -* Improved attachment purging. - -### 1.6.9 ### -* Added Faux to Purge all buttons, to avoid misleading users. - -### 1.6.8 ### -* [daankortenbach](http://profiles.wordpress.org/daankortenbach) added Purge Cache link to wp-admin bar - -### 1.6.7 ### -* [jk3us](http://profiles.wordpress.org/jk3us) added better content-type detection for cache verification comments - -### 1.6.6 ### -* [darren-slatten](http://profiles.wordpress.org/darren-slatten/) added Manual 'Purge all URLs' functionality - -### 1.6.5 ### -* Fixed typo that interfered with archive purge settings. Thanks to [Daan Kortenbach](http://profiles.wordpress.org/daankortenbach/) for pointing this out. - -### 1.6.4 ### -* Improved code for map generation to better conventions since the nesting confused some servers. -* Added map update process to admin_init for frequent refreshes. - -### 1.6.3 ### -* Fixed duplicate entries. - -### 1.6.2 ### -* Another bug fix in the revised code for improved multisite and multidomain mapping. - -### 1.6.1 ### -* Fixed bug in the revised code for improved multisite and multidomain mapping. - -### 1.6 ### -* Revised code for improved multisite and multidomain mapping. - -### 1.5 ### -* Timestamp now only gets added to content-type text/html -* Added option to toggle timestamp creation - -### 1.4 ### -* Fixed bug related to nomenclature of comment status that caused purge to fail. - -### 1.3.9 ### -* Removed extraneous headers. - -### 1.3.8 ### - -* Fixed bug in single post/page/post-type purging code. Thanks to Greg for pointing this out here: http://rtcamp.com/support/topic/updating-post-nginx-helper-purge-cache-post/. - -### 1.3.7 ### - -* Changed the action hook, back to 'shutdown' from 'wp_footer' to add verification comments. -* Added a check to prevent adding comments to ajax requests, - -### 1.3.6 ### - -* Changed the action hook, from 'shutdown' to 'wp_footer' to add verification comments. This was interfering with other plugins. - -### 1.3.5 ### - -* Improved Readme. -* Improved cache verification comments. - -### 1.3.4 ### - -* Fixed duplicate entries generated for maps (Harmless, but doesn't look good!) -* Added timestamp html comments for cache verification, as described here: http://rtcamp.com/wordpress-nginx/tutorials/checklist/ - -### 1.3.3 ### - -* Fixed map generation for multi domain installs using domain mapping plugin, where blog ids were not displayed. - -### 1.3.2 ### - -* Fixed map generation for multi domain installs with domain mapping plugin. - -### 1.3.1 ### - -* Minor fixes for directory structure and file names. - -### 1.3 ### - -* Improved Readme. - -### 1.2 ### - -* Fixed map generation error. -* Fixed purging logic. -* Fixed UI where purge settings were lost on disabling and re-enabling purge. -* Minor Ui rearrangement. - -### 1.1 ### - -* Improved readme.txt. Added Screenshots. - -### 1.0 ### - -* First release - -## Upgrade Notice ## - -### 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. +##Nothing yet From 50c39abd5bf7c2c0e31cf0aa2cfce26a95f9ece4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 09:32:19 +0200 Subject: [PATCH 02/11] Added missing return statements. Removed redundant closing tag. --- includes/redis-delete.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/includes/redis-delete.php b/includes/redis-delete.php index 1be1e9ec..777302f6 100644 --- a/includes/redis-delete.php +++ b/includes/redis-delete.php @@ -74,6 +74,8 @@ function delete_multi_keys( $key ) return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } + + return false; } /* @@ -90,6 +92,8 @@ function flush_entire_db() return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } + + return false; } /* @@ -111,6 +115,8 @@ function delete_single_key( $key ) return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } + + return false; } /* @@ -141,6 +147,6 @@ function delete_keys_by_wildcard( $pattern ) return false; } } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } -} -?> \ No newline at end of file + return false; +} \ No newline at end of file From 0768ad7d69cba7186e6f8a346ffbfc84a8b55e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 09:50:18 +0200 Subject: [PATCH 03/11] Fixed admin referrer check. --- nginx-helper.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nginx-helper.php b/nginx-helper.php index a05905ce..7df74e25 100644 --- a/nginx-helper.php +++ b/nginx-helper.php @@ -304,7 +304,9 @@ function purge_all() return; } - check_admin_referer( 'nginx_helper-purge_all' ); + if ( check_admin_referer( 'nginx_helper-purge_all' ) !== 1 ) { + return; + } switch ( $action ) { case 'purge': @@ -312,6 +314,7 @@ function purge_all() break; } wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) ); + } function true_purge_all() From 74c6acbcb90d0dc92a12d8c85249955a5d57f3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 10:51:33 +0200 Subject: [PATCH 04/11] Formatting cleanup. Removed unused global variables. --- admin/install.php | 50 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/admin/install.php b/admin/install.php index 0882f2c0..257cdeb4 100644 --- a/admin/install.php +++ b/admin/install.php @@ -5,21 +5,21 @@ * Parts of code based off http://wordpress.org/extend/plugins/nginx-manager/ by http://profiles.wordpress.org/hpatoio/ and http://profiles.wordpress.org/rukbat/ */ namespace rtCamp\WP\Nginx { - if ( preg_match( '#' . basename( __FILE__ ) . '#', $_SERVER[ 'PHP_SELF' ] ) ) { + + if ( preg_match( '#' . basename( __FILE__ ) . '#', $_SERVER['PHP_SELF'] ) ) { die( 'You are not allowed to call this page directly.' ); } function rt_wp_nginx_helper_install() { - global $wp_roles, $rt_wp_nginx_helper; - - if ( ! current_user_can( 'activate_plugins' ) ) { - return; - } + if ( ! current_user_can( 'activate_plugins' ) ) { + return; + } $role = get_role( 'administrator' ); if ( empty( $role ) ) { update_site_option( "rt_wp_nginx_helper_init_check", __( 'Sorry, you need to be an administrator to use Nginx Helper', 'nginx-helper' ) ); + return; } @@ -69,31 +69,31 @@ function rt_wp_nginx_helper_remove_capability( $capability ) { } function rt_wp_nginx_helper_get_options() { - $rt_wp_nginx_helper_get_options = array( ); - $rt_wp_nginx_helper_get_options[ 'log_level' ] = 'INFO'; - $rt_wp_nginx_helper_get_options[ 'log_filesize' ] = 5; + $rt_wp_nginx_helper_get_options = array(); + $rt_wp_nginx_helper_get_options['log_level'] = 'INFO'; + $rt_wp_nginx_helper_get_options['log_filesize'] = 5; - $rt_wp_nginx_helper_get_options[ 'enable_purge' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_map' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_log' ] = 0; - $rt_wp_nginx_helper_get_options[ 'enable_stamp' ] = 0; + $rt_wp_nginx_helper_get_options['enable_purge'] = 0; + $rt_wp_nginx_helper_get_options['enable_map'] = 0; + $rt_wp_nginx_helper_get_options['enable_log'] = 0; + $rt_wp_nginx_helper_get_options['enable_stamp'] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_new' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_edit' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_homepage_on_del' ] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_new'] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_edit'] = 1; + $rt_wp_nginx_helper_get_options['purge_homepage_on_del'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_new' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_edit' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_del' ] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_new'] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_edit'] = 1; + $rt_wp_nginx_helper_get_options['purge_archive_on_del'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_new_comment' ] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_archive_on_deleted_comment' ] = 0; + $rt_wp_nginx_helper_get_options['purge_archive_on_new_comment'] = 0; + $rt_wp_nginx_helper_get_options['purge_archive_on_deleted_comment'] = 0; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_mod' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_new_comment' ] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_page_on_deleted_comment' ] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_mod'] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_new_comment'] = 1; + $rt_wp_nginx_helper_get_options['purge_page_on_deleted_comment'] = 1; - $rt_wp_nginx_helper_get_options[ 'purge_method' ] = 'get_request'; + $rt_wp_nginx_helper_get_options['purge_method'] = 'get_request'; return $rt_wp_nginx_helper_get_options; } From b88bb780db1afeacc8a3f022768620d72180cc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 11:06:33 +0200 Subject: [PATCH 05/11] Removed unused variables. --- admin/lib/nginx-general.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/admin/lib/nginx-general.php b/admin/lib/nginx-general.php index 48440781..94b2e295 100644 --- a/admin/lib/nginx-general.php +++ b/admin/lib/nginx-general.php @@ -6,8 +6,6 @@ function nginx_general_options_page() { global $rt_wp_nginx_helper, $rt_wp_nginx_purger; - $update = 0; - $error_time = false; $error_log_filesize = false; $rt_wp_nginx_helper->options['enable_purge'] = (isset( $_POST['enable_purge'] ) and ( $_POST['enable_purge'] == 1) ) ? 1 : 0; $rt_wp_nginx_helper->options['cache_method'] = (isset( $_POST['cache_method'] ) ) ? $_POST['cache_method'] : 'enable_fastcgi'; @@ -61,7 +59,6 @@ function nginx_general_options_page() $rt_wp_nginx_helper->options['redis_prefix'] = ( isset( $_POST['redis_prefix'] ) ) ? $_POST['redis_prefix'] : 'nginx-cache:'; } update_site_option( 'rt_wp_nginx_helper_options', $rt_wp_nginx_helper->options ); - $update = 1; } $rt_wp_nginx_helper->options = get_site_option( 'rt_wp_nginx_helper_options' ); @@ -79,7 +76,6 @@ function nginx_general_options_page() /** * Check for single multiple with subdomain OR multiple with subdirectory site */ - $nginx_setting_link = '#'; if ( is_multisite() ) { if ( SUBDOMAIN_INSTALL == false ) { $nginx_setting_link = 'https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdirectories/fastcgi-cache-with-purging/'; From d9a9bcd420133fad627af7e1eb720a3cf6978dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 15:02:09 +0200 Subject: [PATCH 06/11] Added support for purging a single site in a multisite setup (Redis Only!). Added support for purging wildcard urls (Redis Only!). Various code clean up. --- Purgeable.php | 41 +++++++++++++++++++ admin/admin.php | 8 +++- includes/redis-delete.php | 83 ++++++++++++++------------------------- nginx-helper.php | 69 ++++++++++++++------------------ redis-purger.php | 56 +++++++++++++++++++------- 5 files changed, 148 insertions(+), 109 deletions(-) create mode 100644 Purgeable.php diff --git a/Purgeable.php b/Purgeable.php new file mode 100644 index 00000000..683bfba0 --- /dev/null +++ b/Purgeable.php @@ -0,0 +1,41 @@ +'; @@ -114,11 +114,15 @@ function nginx_admin_page_init() { add_action( 'admin_bar_menu', array( &$this, 'nginx_toolbar_purge_item' ), 100 ); } + /** + * Add the Purge Cache link to the top admin bar + * @param \WP_Admin_Bar $admin_bar + */ function nginx_toolbar_purge_item( $admin_bar ) { if ( !current_user_can( 'manage_options' ) ) { return; } - $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'all' ) ); + $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'site' ) ); $nonced_url = wp_nonce_url( $purge_url, 'nginx_helper-purge_all' ); $admin_bar->add_menu( array( 'id' => 'nginx-helper-purge-all', 'title' => __( 'Purge Cache', 'nginx-helper' ), 'href' => $nonced_url, 'meta' => array( 'title' => __( 'Purge Cache', 'nginx-helper' ), ), ) ); } diff --git a/includes/redis-delete.php b/includes/redis-delete.php index 777302f6..2061cc2b 100644 --- a/includes/redis-delete.php +++ b/includes/redis-delete.php @@ -3,7 +3,7 @@ //TODO:: phpRedis based implementation https://github.com/phpredis/phpredis#eval //include predis (php implementation for redis) -global $myredis, $rt_wp_nginx_helper, $redis_api, $lua, $rt_wp_nginx_purger; +global $myredis, $rt_wp_nginx_helper, $redis_api, $rt_wp_nginx_purger; $host = $rt_wp_nginx_helper->options['redis_hostname']; $port = $rt_wp_nginx_helper->options['redis_port']; @@ -41,25 +41,13 @@ } } -//Lua Script -$lua = <<keys( $key ); @@ -78,29 +66,12 @@ function delete_multi_keys( $key ) return false; } -/* - * Delete all the keys from currently selected database +/** + * Single Key Delete + * @param string $key can be nginx-cache:httpsGETexample.com/ + * + * @return bool|int|mixed */ - -function flush_entire_db() -{ - global $myredis, $rt_wp_nginx_purger; - try { - if ( !empty( $myredis ) ) { - return $myredis->flushdb(); - } else { - return false; - } - } catch ( Exception $e ) { $rt_wp_nginx_purger->log( $e->getMessage(), 'ERROR' ); } - - return false; -} - -/* - Single Key Delete Example - e.g. $key can be nginx-cache:httpsGETexample.com/ - */ - function delete_single_key( $key ) { global $myredis, $redis_api, $rt_wp_nginx_purger; @@ -119,29 +90,33 @@ function delete_single_key( $key ) return false; } -/* - Delete Keys by wildcar - e.g. $key can be nginx-cache:httpsGETexample.com* +/** + * Delete Keys by wildcard + * @param string $pattern can be nginx-cache:httpsGETexample.com* + * + * @return mixed What is returned depends on what the LUA script itself returns. */ - function delete_keys_by_wildcard( $pattern ) { - global $myredis, $lua, $redis_api, $rt_wp_nginx_purger; - /* - Lua Script block to delete multiple keys using wildcard - Script will return count i.e. number of keys deleted - if return value is 0, that means no matches were found - */ + global $myredis, $redis_api, $rt_wp_nginx_purger; + + //Lua Script + $lua = <<eval( $lua, 1, $pattern ); } else if( $redis_api == 'php-redis') { - return $myredis->eval( $lua, array( $pattern ), 1 ); + return $myredis->eval( $lua, [ $pattern ], 1 ); } } else { return false; diff --git a/nginx-helper.php b/nginx-helper.php index 7df74e25..9062fb44 100644 --- a/nginx-helper.php +++ b/nginx-helper.php @@ -3,12 +3,12 @@ Plugin Name: Nginx Helper Plugin URI: https://rtcamp.com/nginx-helper/ Description: Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things. - Version: 1.9.7 + Version: 1.10.0 Author: rtCamp Author URI: https://rtcamp.com Text Domain: nginx-helper Requires at least: 3.0 - Tested up to: 4.5.2 + Tested up to: 4.6 */ namespace rtCamp\WP\Nginx { @@ -17,18 +17,17 @@ class Helper { - var $minium_WP = '3.0'; - var $options = null; - var $plugin_name = 'nginx-helper'; + private $minium_WP = '3.0'; + private $plugin_name = 'nginx-helper'; + public $options = null; const WP_CLI_COMMAND = 'nginx-helper'; function __construct() { - - if ( !$this->required_wp_version() ) - if ( !$this->required_php_version() ) - return; + if ( ! $this->required_wp_version() && ! version_compare(phpversion(), '5.4.0', '>=') ) { + return; + } // Load Plugin Text Domain add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); @@ -46,25 +45,12 @@ function start_helper() { global $rt_wp_nginx_purger; + + add_action( 'admin_init', array( &$this, 'purge' ) ); add_action( 'shutdown', array( &$this, 'add_timestamps' ), 99999 ); add_action( 'add_init', array( &$this, 'update_map' ) ); - - //add_action( 'save_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // add_action( 'publish_post', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // add_action( 'publish_page', array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); add_action( 'wp_insert_comment', array( &$rt_wp_nginx_purger, 'purgePostOnComment' ), 200, 2 ); add_action( 'transition_comment_status', array( &$rt_wp_nginx_purger, 'purgePostOnCommentChange' ), 200, 3 ); - - // $args = array( '_builtin' => false ); - // $_rt_custom_post_types = get_post_types( $args ); - // if ( isset( $post_types ) && !empty( $post_types ) ) { - // if ( $this->options['rt_wp_custom_post_types'] == true ) { - // foreach ( $_rt_custom_post_types as $post_type ) { - // add_action( 'publish_' . trim( $post_type ), array( &$rt_wp_nginx_purger, 'purgePost' ), 200, 1 ); - // } - // } - // } - add_action( 'transition_post_status', array( &$this, 'set_future_post_option_on_future_status' ), 20, 3 ); add_action( 'delete_post', array( &$this, 'unset_future_post_option_on_delete' ), 20, 1 ); add_action( 'nm_check_log_file_size_daily', array( &$rt_wp_nginx_purger, 'checkAndTruncateLogFile' ), 100, 1 ); @@ -74,10 +60,11 @@ function start_helper() add_action( 'edit_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 ); add_action( 'delete_term', array( &$rt_wp_nginx_purger, 'purge_on_term_taxonomy_edited' ), 20, 3 ); add_action( 'check_ajax_referer', array( &$rt_wp_nginx_purger, 'purge_on_check_ajax_referer' ), 20, 2 ); - add_action( 'admin_init', array( &$this, 'purge_all' ) ); - // expose action to allow other plugins to purge the cache - add_action( 'rt_nginx_helper_purge_all', array( &$this, 'true_purge_all' ) ); + // expose actions to allow other plugins to purge the cache + add_action( 'rt_nginx_helper_purge_all', [ $rt_wp_nginx_purger, 'true_purge_all' ] ); + // TODO: Only works in Redis atm. + add_action( 'rt_nginx_helper_purge_wildcard', [ $rt_wp_nginx_purger, 'purgeWildcard'] ); // Load WP-CLI command if ( defined( 'WP_CLI' ) && WP_CLI ) { @@ -288,7 +275,7 @@ function show_notice() echo '

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

'; } - function purge_all() + function purge() { if ( !isset( $_REQUEST['nginx_helper_action'] ) ) return; @@ -297,6 +284,7 @@ function purge_all() wp_die( 'Sorry, you do not have the necessary privileges to edit these options.' ); $action = $_REQUEST['nginx_helper_action']; + $what_to_purge = $_REQUEST['nginx_helper_urls']; if ( $action == 'done' ) { add_action( 'admin_notices', array( &$this, 'show_notice' ) ); @@ -308,21 +296,24 @@ function purge_all() return; } - switch ( $action ) { - case 'purge': - $this->true_purge_all(); - break; + if ( $action === 'purge' ) { + global $rt_wp_nginx_purger; + + switch ( $what_to_purge ) { + case 'all': + $rt_wp_nginx_purger->true_purge_all(); + break; + case 'site': + $rt_wp_nginx_purger->purgeCurrentSite(); + break; + default: + // unknown action + } } wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) ); } - function true_purge_all() - { - global $rt_wp_nginx_purger; - $rt_wp_nginx_purger->true_purge_all(); - } - /** * Load the translation file for current language. */ @@ -355,7 +346,7 @@ function load_plugin_textdomain() $rt_wp_nginx_helper = new \rtCamp\WP\Nginx\Helper; if ( !empty( $rt_wp_nginx_helper->options['cache_method'] ) && $rt_wp_nginx_helper->options['cache_method'] == "enable_redis" ) { - $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Redispurger; + $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Redis_Purger; } else { $rt_wp_nginx_purger = new \rtCamp\WP\Nginx\Purger; } diff --git a/redis-purger.php b/redis-purger.php index 3fbe948a..ca3bdde3 100644 --- a/redis-purger.php +++ b/redis-purger.php @@ -2,7 +2,7 @@ namespace rtCamp\WP\Nginx { - class Redispurger { + class Redis_Purger implements Purgeable { public function __construct() { @@ -38,7 +38,6 @@ function purgePostOnCommentChange( $newstatus, $oldstatus, $comment ) return; } - $_post_id = $comment->comment_post_ID; $_comment_id = $comment->comment_ID; @@ -109,6 +108,7 @@ function purgePost( $_ID ) $this->log( "Function purgePost BEGIN ===" ); if ( $rt_wp_nginx_helper->options['purge_homepage_on_edit'] == 1 ) { + $homepage_url = get_site_url(); $this->log( "Purging homepage '$homepage_url'" ); $this->_purge_homepage(); } @@ -544,7 +544,7 @@ private function _purge_all_daily_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_daily_archives = $wpdb->get_results( $_query_daily_archives ) ) { @@ -570,7 +570,7 @@ private function _purge_all_monthly_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_monthly_archives = $wpdb->get_results( $_query_monthly_archives ) ) { @@ -596,7 +596,7 @@ private function _purge_all_yearly_archives() WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date) ORDER BY post_date DESC" - ); + , null); if ( $_yearly_archives = $wpdb->get_results( $_query_yearly_archives ) ) { @@ -667,20 +667,32 @@ function purge_on_check_ajax_referer( $action, $result ) function true_purge_all() { global $rt_wp_nginx_helper; - $prefix = substr($rt_wp_nginx_helper->options['redis_prefix'],0, -1); - $this->log( "* * * * *" ); - $this->log( "* Purged Everything!" ); - $this->log( "* * * * *" ); - //delete_multi_keys("*"); + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + $this->log( "* * * * * * * * * * * *" ); + $this->log( "* Purged Everything! *" ); + $this->log( "* * * * * * * * * * * *" ); delete_keys_by_wildcard($prefix."*"); } - + + /** + * @inheritdoc + */ + function purgeWildcard($pattern) + { + global $rt_wp_nginx_helper; + $this->log("Purging the pattern: $pattern"); + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + delete_keys_by_wildcard($prefix . $pattern); + } + + /** + * Handles custom urls entered on the settings page under "Custom Purge Urls". + */ function purge_urls() { global $rt_wp_nginx_helper; $parse = parse_url( site_url() ); - $host = $rt_wp_nginx_helper->options['redis_hostname']; $prefix = $rt_wp_nginx_helper->options['redis_prefix']; $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; @@ -712,8 +724,24 @@ function purge_urls() } } } - } + } + } + + /** + * @inheritdoc + */ + function purgeCurrentSite() + { + global $rt_wp_nginx_helper; + + $prefix = $rt_wp_nginx_helper->options['redis_prefix']; + $parse = wp_parse_url( get_site_url() ); + // 'path' is needed on multisite-subfolder setups. + delete_keys_by_wildcard($prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*'); + + $this->log( "* * * * * * * * * * * *" ); + $this->log( "* Purged Current Site *" ); + $this->log( "* * * * * * * * * * * *" ); } - } } From 7a938be5a929fa060468d7ec1007d12eccf3535f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 15:18:51 +0200 Subject: [PATCH 07/11] Added missing "require" --- nginx-helper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/nginx-helper.php b/nginx-helper.php index 9062fb44..a1a60b06 100644 --- a/nginx-helper.php +++ b/nginx-helper.php @@ -338,6 +338,7 @@ function load_plugin_textdomain() $rtwpAdminPanel = new \rtCamp\WP\Nginx\Admin(); } + require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'Purgeable.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'purger.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'redis-purger.php'); require_once (rtCamp\WP\Nginx\RT_WP_NGINX_HELPER_PATH . 'compatibility.php'); From 05fd5ad4213673611f912c69caf62aad576c7a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 15:35:10 +0200 Subject: [PATCH 08/11] Made a check for multisite. --- admin/admin.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/admin/admin.php b/admin/admin.php index 5b725ae1..776ba8ae 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -122,7 +122,11 @@ function nginx_toolbar_purge_item( $admin_bar ) { if ( !current_user_can( 'manage_options' ) ) { return; } - $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'site' ) ); + if (is_multisite()) { + $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'site' ) ); + } else { + $purge_url = add_query_arg( array( 'nginx_helper_action' => 'purge', 'nginx_helper_urls' => 'all' ) ); + } $nonced_url = wp_nonce_url( $purge_url, 'nginx_helper-purge_all' ); $admin_bar->add_menu( array( 'id' => 'nginx-helper-purge-all', 'title' => __( 'Purge Cache', 'nginx-helper' ), 'href' => $nonced_url, 'meta' => array( 'title' => __( 'Purge Cache', 'nginx-helper' ), ), ) ); } From 0f0f7de5a5052c7d20cad6ac34c2108ce308b183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 15:36:08 +0200 Subject: [PATCH 09/11] Minor cleanup. Implements the new Purgeable interface, but doesn't yet support single site purging nor wildcard purging. --- purger.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/purger.php b/purger.php index 52042f13..45620f66 100644 --- a/purger.php +++ b/purger.php @@ -2,7 +2,7 @@ namespace rtCamp\WP\Nginx { - class Purger { + class Purger implements Purgeable { function purgePostOnComment( $comment_id, $comment ) { $oldstatus = ''; @@ -840,5 +840,22 @@ function purge_urls() { } } - } + + /** + * @inheritdoc + */ + function purgeCurrentSite() { + // TODO: Implement purgeSingleSite() method. + // For now, just purge all + $this->true_purge_all(); + } + + /** + * @inheritdoc + */ + function purgeWildcard( $pattern ) { + // TODO: Implement purgeWildcard() method. + // For now, just purge all + $this->true_purge_all(); + }} } From 791ad2c7b0fda4fae816a2f692c3f532d1a45eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 15:46:31 +0200 Subject: [PATCH 10/11] Update README.md --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d9fb635..635d34a4 100644 --- a/README.md +++ b/README.md @@ -1 +1,12 @@ -##Nothing yet +# Multisite Fixes + +## Changes from main repo +- Fixes purging of single sites in a multisite setup (Important: Redis Only!! FastCGI Cache support is not implemented yet.) + +### Details +Now when you click the purge cache link in the toolbar on a site, will only purge the cache of that site. As mentioned above, this is only supported in Redis atm, as Redis supports purging with wildcards. +The big red button in Nginx Helper settings still purges the entire cache. + +### Tests +Tested on a "subfolder" multisite setup with Redis Cache. +Should also work in a "subdomain" multisite, but is untested. From 9525805b1d4f8bf0abb58ce3b1c7cdaa9755f9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20St=C3=B8ttrup=20Nielsen?= Date: Thu, 1 Sep 2016 18:14:10 +0200 Subject: [PATCH 11/11] Kill log debug.log notice. Stops spam in debug.log at the notice level. --- admin/lib/nginx-general.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/admin/lib/nginx-general.php b/admin/lib/nginx-general.php index 94b2e295..ad72e2d0 100644 --- a/admin/lib/nginx-general.php +++ b/admin/lib/nginx-general.php @@ -421,10 +421,10 @@ function nginx_general_options_page()