Commit 14af1a2d authored by lucha's avatar lucha
Browse files

[auto] plugin: wp-super-cache 1.6.4

parent aec578c9
# WP Super Cache #
* Contributors: donncha, automattic, kraftbj
* Tags: performance, caching, wp-cache, wp-super-cache, cache
* Tested up to: 4.9.6
* Stable tag: 1.6.2
* Requires at least: 3.0
* Tested up to: 4.9.8
* Stable tag: 1.6.4
* Requires at least: 3.1
* Requires PHP: 5.2.4
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
......@@ -89,6 +89,11 @@ There is one regular WordPress filter too. Use the "do_createsupercache" filter
to customize the checks made before caching. The filter accepts one parameter.
The output of WP-Cache's wp_cache_get_cookies_values() function.
WP Super Cache has it's own plugin system. This code is loaded when WP Super Cache loads and can be used to change how caching is done. This is before most of WordPress loads so some functionality will not be available. Plugins can be located anywhere that PHP can load them. Add your own plugin by calling wpsc_add_plugin( $name ) where $name is the full filename and path to the plugin. You only need to call that function once to add it. Use wpsc_delete_plugin( $name ) to remove it from the list of loaded plugins.
The cookies WP Super Cache uses to identify "known users" can be modified now by adding the names of those cookies to a list in the plugin configuration. Use wpsc_add_cookie( $name ) to add a new cookie, and wpsc_delete_cookie( $name ) to remove it. The cookie names also modify the mod_rewrite rules used by the plugin but I recommend using Simple mode caching to avoid complications with updating the .htaccess file.
The cookie name and value are used to differenciate users so you can have one cookie, but different values for each type of user on your site for example. They'll be served different cache files.
See [plugins/searchengine.php](https://github.com/Automattic/wp-super-cache/blob/4cda5c0f2218e40e118232b5bf22d227fb3206b7/plugins/searchengine.php) as an example I use for my [No Adverts for Friends](https://odd.blog/no-adverts-for-friends/) plugin.
### Troubleshooting ###
......@@ -200,7 +205,7 @@ Cached files are served before almost all of WordPress is loaded. While that's g
This plugin caches entire pages but some plugins think they can run PHP code every time a page loads. To fix this, the plugin needs to use Javascript/AJAX methods or the wpsc_cachedata filter described in the previous answer to update or display dynamic information.
### Why do my WP Super Cache plugins disappear when I upgrade the plugin? ###
WordPress deletes the plugin folder when it updates a plugin. This is the same with WP Super Cache so any modified files in wp-super-cache/plugins/ will be deleted. You can define the variable $wp_cache_plugins_dir in wp-config.php or wp-content/wp-cache-config.php and point it at a directory outside of the wp-super-cache folder. The plugin will look there for it's plugins.
WordPress deletes the plugin folder when it updates a plugin. This is the same with WP Super Cache so any modified files in wp-super-cache/plugins/ will be deleted. You can put your custom plugins in a different directory in a number of ways. You can define the variable $wp_cache_plugins_dir in wp-config.php or wp-content/wp-cache-config.php and point it at a directory outside of the wp-super-cache folder. The plugin will look there for it's plugins. Or if you distribute a plugin that needs to load early you can use the function `wpsc_add_plugin( $filename )` to add a new plugin wherever it may be. Use `wpsc_delete_plugin( $filename )` to remove the plugin file. See [#574](https://github.com/Automattic/wp-super-cache/pull/574/) or [this post](https://odd.blog/2017/10/25/writing-wp-super-cache-plugins/) on writing WP Super Cache plugins.
### What does the Cache Rebuild feature do? ###
When a visitor leaves a comment the cached file for that page is deleted and the next visitor recreates the cached page. A page takes time to load so what happens if it receives 100 visitors during this time? There won't be a cached page so WordPress will serve a fresh page for each user and the plugin will try to create a cached page for each of those 100 visitors causing a huge load on your server. This feature stops this happening. The cached page is not cleared when a comment is left. It is marked for rebuilding instead. The next visitor within the next 10 seconds will regenerate the cached page while the old page is served to the other 99 visitors. The page is eventually loaded by the first visitor and the cached page updated. See [this post](https://odd.blog/2009/01/23/wp-super-cache-089/) for more.
......@@ -261,6 +266,29 @@ Your theme is probably responsive which means it resizes the page to suit whatev
## Changelog ##
### 1.6.4 ###
* Changes between [1.6.3 and 1.6.4](https://github.com/Automattic/wp-super-cache/compare/1.6.3...1.6.4)
* Fixes for WP-CLI (#587) (#592)
* Bumped the minimum WordPress version to 3.1 to use functions introduced then. (#591)
* Fixes to wpsc_post_transition to avoid a fatal error using get_sample_permalink. (#595)
* Fixed the admin bar "Delete Cache" link. (#589)
* Fixed the headings used in the settings page. (#597)
### 1.6.3 ###
* Changes between [1.6.2 and 1.6.3](https://github.com/Automattic/wp-super-cache/compare/1.6.2...1.6.3)
* Added cookie helper functions (#580)
* Added plugin helper functions (#574)
* Added actions to modify cookie and plugin lists. (#582)
* Really disable garbage collection when timeout = 0 (#571)
* Added warnings about DISABLE_WP_CRON (#575)
* Don't clean expired cache files after preload if garbage collection is disabled (#572)
* On preload, if deleting a post don't delete the sub directories if it's the homepage. (#573)
* Fix generation of semaphores when using WP CLI (#576)
* Fix deleting from the admin bar (#578)
* Avoid a strpos() warning. (#579)
* Improve deleting of cache in edit/delete/publish actions (#577)
* Fixes to headers code (#496)
### 1.6.2 ###
* Fixed serving expired supercache files (#562)
* Write directly to the config file to avoid permission problems with wp-content. (#563)
......@@ -663,4 +691,4 @@ Your theme is probably responsive which means it resizes the page to suit whatev
## Upgrade Notice ##
Fix problems writing to the config file for some users.
Bug fixes
<?php
if ( false == isset( $_SERVER['HTTP_HOST'] ) ) {
global $WPSC_HTTP_HOST, $blogcacheid;
if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
$WPSC_HTTP_HOST = htmlentities( $_SERVER['HTTP_HOST'] );
} elseif ( PHP_SAPI === 'cli' && function_exists( 'get_option' ) ) {
$WPSC_HTTP_HOST = (string) parse_url( get_option( 'home' ), PHP_URL_HOST );
} else {
$cache_enabled = false;
$WPSC_HTTP_HOST = '';
} else {
$WPSC_HTTP_HOST = htmlentities( $_SERVER['HTTP_HOST'] );
}
// We want to be able to identify each blog in a WordPress MU install
......@@ -17,7 +21,7 @@ if ( ( defined( 'WP_ALLOW_MULTISITE' ) && constant( 'WP_ALLOW_MULTISITE' ) == tr
$base = '';
}
$request_uri = str_replace( '..', '', preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI'] ) );
if ( strpos( $request_uri, '/', 1 ) ) {
if ( strlen( $request_uri ) > 0 && strpos( $request_uri, '/', 1 ) ) {
if ( $base == '/' ) {
$blogcacheid = substr( $request_uri, 1, strpos( $request_uri, '/', 1 ) - 1 );
} else {
......
......@@ -49,6 +49,28 @@ if ( is_array( $plugins ) ) {
}
}
if ( isset( $wpsc_plugins ) && is_array( $wpsc_plugins ) ) {
foreach( $wpsc_plugins as $plugin_file ) {
if ( file_exists( ABSPATH . $plugin_file ) ) {
include_once( ABSPATH . $plugin_file );
}
}
}
if (
file_exists( WPCACHEHOME . '../wp-super-cache-plugins/' ) &&
is_dir( WPCACHEHOME . '../wp-super-cache-plugins/' )
) {
$plugins = glob( WPCACHEHOME . '../wp-super-cache-plugins/*.php' );
if ( is_array( $plugins ) ) {
foreach ( $plugins as $plugin ) {
if ( is_file( $plugin ) ) {
require_once $plugin;
}
}
}
}
$wp_start_time = microtime();
if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() ) {
......
......@@ -120,7 +120,7 @@ function wp_cache_serve_cache_file() {
} elseif ( isset( $wpsc_save_headers ) && $wpsc_save_headers ) {
wp_cache_debug( 'Saving headers. Cannot serve a supercache file.' );
return false;
} elseif ( ( filemtime( $file ) + $cache_max_time ) < time() ) {
} elseif ( $cache_max_time > 0 && ( filemtime( $file ) + $cache_max_time ) < time() ) {
wp_cache_debug( sprintf( "Cache has expired and is older than %d seconds old.", $cache_max_time ) );
return false;
}
......@@ -196,17 +196,15 @@ function wp_cache_serve_cache_file() {
// don't try to match modified dates if using dynamic code.
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
if ( function_exists( 'apache_request_headers' ) ) {
$request = apache_request_headers();
$remote_mod_time = ( isset ( $request[ 'If-Modified-Since' ] ) ) ? $request[ 'If-Modified-Since' ] : null;
} else {
if ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) )
$remote_mod_time = $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ];
else
$remote_mod_time = null;
$headers = apache_request_headers();
$remote_mod_time = isset ( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;
if ( is_null( $remote_mod_time ) && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
$remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
}
$local_mod_time = gmdate("D, d M Y H:i:s",filemtime( $file )).' GMT';
if ( !is_null($remote_mod_time) && $remote_mod_time == $local_mod_time ) {
if ( ! is_null( $remote_mod_time ) && $remote_mod_time == $local_mod_time ) {
header( $_SERVER[ 'SERVER_PROTOCOL' ] . " 304 Not Modified" );
exit();
}
......@@ -322,6 +320,7 @@ function wp_cache_late_loader() {
}
function wp_cache_get_cookies_values() {
global $wpsc_cookies;
static $string = '';
if ( $string != '' ) {
......@@ -350,6 +349,20 @@ function wp_cache_get_cookies_values() {
// If you use this hook, make sure you update your .htaccess rules with the same conditions
$string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
if (
isset( $wpsc_cookies ) &&
is_array( $wpsc_cookies ) &&
! empty( $wpsc_cookies )
) {
foreach( $wpsc_cookies as $name ) {
if ( isset( $_COOKIE[ $name ] ) ) {
wp_cache_debug( "wp_cache_get_cookies_values - found extra cookie: $name" );
$string .= $name . "=" . $_COOKIE[ $name ] . ",";
}
}
}
if ( $string != '' )
$string = md5( $string );
......@@ -698,9 +711,10 @@ function wpsc_delete_files( $dir, $delete = true ) {
}
}
$orig_dir = $dir;
$dir = wpsc_get_realpath( $dir );
if ( ! $dir ) {
wp_cache_debug( 'wpsc_delete_files: directory does not exist: ' . $dir );
wp_cache_debug( 'wpsc_delete_files: directory does not exist: ' . $orig_dir );
return false;
}
......@@ -1340,15 +1354,21 @@ function wpcache_logged_in_message() {
function wp_cache_user_agent_is_rejected() {
global $cache_rejected_user_agent;
if (!function_exists('apache_request_headers')) return false;
if ( empty( $cache_rejected_user_agent ) || ! is_array( $cache_rejected_user_agent ) ) {
return false;
}
$headers = apache_request_headers();
if (!isset($headers["User-Agent"])) return false;
if ( false == is_array( $cache_rejected_user_agent ) )
if ( empty( $headers['User-Agent'] ) ) {
return false;
foreach ($cache_rejected_user_agent as $expr) {
if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
}
foreach ( $cache_rejected_user_agent as $user_agent ) {
if ( ! empty( $user_agent ) && stristr( $headers['User-Agent'], $user_agent ) ) {
return true;
}
}
return false;
}
......@@ -2325,6 +2345,11 @@ function wp_cache_rebuild_or_delete( $file ) {
function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
if ( $cache_max_time == 0 ) {
wp_cache_debug( "wp_cache_phase2_clean_expired: disabled because GC disabled.", 2 );
return false;
}
clearstatcache();
if( !wp_cache_writers_entry() )
return false;
......@@ -2662,6 +2687,10 @@ function wpsc_delete_post_archives( $post ) {
}
function wpsc_delete_cats_tags( $post ) {
if ( function_exists( '_deprecated_function' ) ) {
_deprecated_function( __FUNCTION__, 'WP Super Cache 1.6.3', 'wpsc_delete_post_archives' );
}
$post = get_post($post);
$categories = get_the_category($post->ID);
if ( $categories ) {
......@@ -2688,32 +2717,46 @@ function wpsc_delete_cats_tags( $post ) {
}
function wpsc_post_transition( $new_status, $old_status, $post ) {
if (
($old_status == 'publish' && $new_status != 'publish' ) // post unpublished
||
($new_status == 'publish') // post published or updated
) {
//wpsc_delete_cats_tags( $post );
wpsc_delete_post_archives( $post );
$post_url = get_permalink( $post->ID );
wpsc_delete_url_cache( $post_url );
$ptype = is_object( $post ) ? get_post_type_object( $post->post_type ) : null;
if ( empty( $ptype ) || ! $ptype->public ) {
return;
}
if ( $old_status === 'publish' && $new_status !== 'publish' ) { // post unpublished
if ( ! function_exists( 'get_sample_permalink' ) ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
list( $permalink, $post_name ) = get_sample_permalink( $post );
$post_url = str_replace( array( "%postname%", "%pagename%" ), $post->post_name, $permalink );
}
elseif ( $old_status !== 'publish' && $new_status === 'publish' ) { // post published
wp_cache_post_edit( $post->ID );
return;
}
if ( ! empty( $post_url ) ) {
wp_cache_debug( 'wpsc_post_transition: deleting cache of post: ' . $post_url );
wpsc_delete_url_cache( $post_url );
wpsc_delete_post_archives( $post );
}
}
/* check if we want to clear out all cached files on post updates, otherwise call standard wp_cache_post_change() */
function wp_cache_post_edit($post_id) {
function wp_cache_post_edit( $post_id ) {
global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir, $wp_cache_object_cache;
static $last_post_edited = -1;
if ( $post_id == $last_post_edited ) {
wp_cache_debug( "wp_cache_post_edit: Already processed post $post_id.", 4 );
$action = current_filter();
wp_cache_debug( "wp_cache_post_edit(${action}): Already processed post $post_id.", 4 );
return $post_id;
}
$post = get_post( $post_id );
if ( is_object( $post ) == false )
if ( ! is_object( $post ) || 'auto-draft' === $post->post_status ) {
return $post_id;
}
// Some users are inexplicibly seeing this error on scheduled posts.
// define this constant to disable the post status check.
......@@ -2733,8 +2776,10 @@ function wp_cache_post_edit($post_id) {
prune_super_cache( get_supercache_dir(), true );
}
} else {
wp_cache_debug( "wp_cache_post_edit: Clearing cache for post $post_id on post edit.", 2 );
$action = current_filter();
wp_cache_debug( "wp_cache_post_edit: Clearing cache for post $post_id on ${action}", 2 );
wp_cache_post_change( $post_id );
wpsc_delete_post_archives( $post_id ); // delete related archive pages.
}
}
......@@ -2776,7 +2821,8 @@ function wp_cache_post_change( $post_id ) {
static $last_processed = -1;
if ( $post_id == $last_processed ) {
wp_cache_debug( "wp_cache_post_change: Already processed post $post_id.", 4 );
$action = current_filter();
wp_cache_debug( "wp_cache_post_change(${action}): Already processed post $post_id.", 4 );
return $post_id;
}
$post = get_post( $post_id );
......@@ -2834,10 +2880,17 @@ function wp_cache_post_change( $post_id ) {
if ( get_option( 'show_on_front' ) == 'page' ) {
wp_cache_debug( 'Post change: deleting page_on_front and page_for_posts pages.', 4 );
wp_cache_debug( 'Post change: page_on_front ' . get_option( 'page_on_front' ), 4 );
$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( get_option( 'page_for_posts' ) ) ) );
wp_cache_debug( 'Post change: Deleting files in: ' . str_replace( '//', '/', $dir . $permalink ) );
wpsc_rebuild_files( $dir . $permalink );
do_action( 'gc_cache', 'prune', $permalink );
/**
* It's possible that page_for_posts is zero.
* Quick fix to reduce issues in debugging.
*/
wp_cache_debug( 'Post change: page_for_posts ' . get_option( 'page_for_posts' ), 4 );
if ( get_option( 'page_for_posts' ) ) {
$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( get_option( 'page_for_posts' ) ) ) );
wp_cache_debug( 'Post change: Deleting files in: ' . str_replace( '//', '/', $dir . $permalink ) );
wpsc_rebuild_files( $dir . $permalink );
do_action( 'gc_cache', 'prune', $permalink );
}
}
} else {
wp_cache_debug( 'wp_cache_post_change: not deleting all pages.', 4 );
......@@ -3029,4 +3082,23 @@ function wp_cache_gc_watcher() {
}
}
?>
if ( ! function_exists( 'apache_request_headers' ) ) {
/**
* A fallback for get request headers.
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
*
* @return array List of request headers
*/
function apache_request_headers() {
$headers = array();
foreach ( array_keys( $_SERVER ) as $skey ) {
if ( 0 === strpos( $skey, 'HTTP_' ) ) {
$header = implode( '-', array_map( 'ucfirst', array_slice( explode( '_', strtolower( $skey ) ) , 1 ) ) );
$headers[ $header ] = $_SERVER[ $skey ];
}
}
return $headers;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment