Skip to content

Instantly share code, notes, and snippets.

@kadai
Created October 29, 2018 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadai/5afeb2356e628d788577d1f70e2eb5dc to your computer and use it in GitHub Desktop.
Save kadai/5afeb2356e628d788577d1f70e2eb5dc to your computer and use it in GitHub Desktop.
Some solutions to some common problems found in the plugin Swift Performance.
<?php
/*
* We verify the HTTP code that WordPress will send and then we decide if the current page
* should be allowed to be cached.
*/
add_filter('swift_performance_is_cacheable', 'dtorrer_swift_iscacheable', 20, 1);
function dtorrer_swift_iscacheable( $cacheable = FALSE ){
if( FALSE === $cacheable ){ return $cacheable; }
/*
* We obtain the status code that will be sent to the user.
*/
$current = http_response_code();
/*
* If the current HTTP code is one of the list bellow, it should not
* be cached. For that, we should return FALSE.
*/
$codes = [
301, 302, 307, 308,
400, 401, 403, 405, 408, 418, 421,
500, 502, 503, 504, 508
];
if( in_array($current, $codes) ){
return FALSE;
}
return $cacheable;
}
?>
<?php
/*
* We verify the HTTP code that WordPress will send and then we decide if the current page
* should be allowed to be cached.
*/
add_filter('swift_performance_is_cacheable', 'dtorrer_swift_iscacheable', 20, 1);
function dtorrer_swift_iscacheable( $cacheable = FALSE ){
if( FALSE === $cacheable ){ return $cacheable; }
/*
* If the HTTP code is not exactly 200, we should not cache it.
*/
if( 200 !== http_response_code() ){
return FALSE;
}
return $cacheable;
}
?>
<?php
/*
* We look at the requested document and then we decide if it should be allowed
* to be cached by Swift Performance.
*/
add_filter('swift_performance_is_cacheable', 'dtorrer_swift_iscacheable', 20, 1);
function dtorrer_swift_iscacheable( $cacheable = FALSE ){
if( FALSE === $cacheable ){ return $cacheable; }
/*
* If any of the next points is accessed, we should return FALSE to prevent
* it from being cached.
*/
if( isset($_SERVER['REQUEST_URI']) ){
$list = array(
'/robots.txt',
'/xmlrpc.php',
'/post-sitemap.xml',
'/sitemap_index.xml',
'/wp-json/',
'/page/',
'/category/',
'/tag/',
'/wp-admin/',
'/project_tag-sitemap.xml',
'/page-sitemap.xml',
'/project-sitemap.xml',
'/project_category-sitemap.xml',
);
$regex = "(" . implode('|', $list) . ")";
if( 1 === preg_match($regex, $_SERVER["REQUEST_URI"]) ){
return FALSE;
}
}
return $cacheable;
}
?>
<?php
/*
* We ensure that the cron job is properly hooked uppon activation. Or when run and it is not
* scheduled already.
*/
register_activation_hook(plugin_basename( __FILE__ ), 'dtorrer_activate_cronswift');
add_action('plugins_loaded', 'dtorrer_activate_cronswift');
function dtorrer_activate_cronswift(){
if( !wp_next_scheduled('dtorrer_validate_swiftcache') ){
wp_schedule_event(time(), 'hourly', 'dtorrer_validate_swiftcache');
}
}
/*
* We make sure to turn off the cron job when plugin is deactivated.
*/
register_deactivation_hook(plugin_basename( __FILE__ ), 'dtorrer_turnoff_cronswift');
function dtorrer_turnoff_cronswift(){
wp_clear_scheduled_hook('dtorrer_validate_swiftcache');
}
/*
* We perform the verification of the cache size of Swift Performance. If it is bigger than expected,
* then we delete it.
*/
add_action('dtorrer_validate_swiftcache', 'dtorrer_doSwiftCacheVerification');
function dtorrer_doSwiftCacheVerification(){
/* If the required classes does not exist, we exit */
if( !class_exists('Swift_Performance_Cache') || !class_exists('Swift_Performance_Lite') ){
return FALSE;
}
/* Each page should be around 0.8 MB of size, can be adjusted */
$pagesize = 0.8;
/* We calculate the needed values */
$max = (count(Swift_Performance_Lite::get_prebuild_urls(TRUE)) * $pagesize) * (1024 * 1024);
$directory = trailingslashit(Swift_Performance_Lite::get_option('cache-path')) . SWIFT_PERFORMANCE_CACHE_BASE_DIR;
$size = Swift_Performance_Lite::cache_dir_size($directory);
/* If the size of the cache is bigger than expected, we purge it */
if( $size >= $max ){
global $wpdb;
$wpdb->query('DROP TABLE IF EXISTS ' . SWIFT_PERFORMANCE_TABLE_PREFIX . 'warmup');
delete_option(SWIFT_PERFORMANCE_TABLE_PREFIX . 'db_version');
delete_transient('swift_performance_initial_prebuild_links');
Swift_Performance_Lite::db_install();
Swift_Performance_Cache::clear_all_cache();
Swift_Performance_Lite::get_prebuild_urls();
}
}
?>
<?php
/*
* We ensure that the cron job is properly hooked uppon activation. Or when run and it is not
* scheduled already.
*/
register_activation_hook(plugin_basename( __FILE__ ), 'dtorrer_activate_cronswift');
add_action('plugins_loaded', 'dtorrer_activate_cronswift');
function dtorrer_activate_cronswift(){
if( !wp_next_scheduled('dtorrer_validate_swiftcache') ){
wp_schedule_event(time(), 'daily', 'dtorrer_validate_swiftcache');
}
}
/*
* We make sure to turn off the cron job when plugin is deactivated.
*/
register_deactivation_hook(plugin_basename( __FILE__ ), 'dtorrer_turnoff_cronswift');
function dtorrer_turnoff_cronswift(){
wp_clear_scheduled_hook('dtorrer_validate_swiftcache');
}
/*
* We perform the verification of the cache size of Swift Performance. If it is bigger than expected,
* then we delete it.
*/
add_action('dtorrer_validate_swiftcache', 'dtorrer_doSwiftCacheVerification');
function dtorrer_doSwiftCacheVerification(){
/* If the required classes does not exist, we exit */
if( !class_exists('Swift_Performance_Cache') || !class_exists('Swift_Performance_Lite') ){
return FALSE;
}
/* We purge the cache */
global $wpdb;
$wpdb->query('DROP TABLE IF EXISTS ' . SWIFT_PERFORMANCE_TABLE_PREFIX . 'warmup');
delete_option(SWIFT_PERFORMANCE_TABLE_PREFIX . 'db_version');
delete_transient('swift_performance_initial_prebuild_links');
Swift_Performance_Lite::db_install();
Swift_Performance_Cache::clear_all_cache();
Swift_Performance_Lite::get_prebuild_urls();
}
?>
<?php
/*
* We get a list of "Enabled Hosts" from the cache directory that Swift Performance uses. The directories at the root
* folder are named after hosts of what Swift has created cache for.
*/
add_filter('swift_performance_enabled_hosts', 'dtorrer_swift_cachedirectories', 20, 1);
function dtorrer_swift_cachedirectories( $list = array() ){
/*
* If we are on a multisite install, we should not mess with this.
*/
if( is_multisite() ){ return $list; }
$dirs = array();
/*
* We check if Swift Performance is running and then we get the list of directories from the cache.
*/
if( class_exists('Swift_Performance_Lite') ){
$base = trailingslashit(Swift_Performance_Lite::get_option('cache-path')) . SWIFT_PERFORMANCE_CACHE_BASE_DIR;
$dirs = glob($base . '*/');
/*
* Each directory at the root of the cache of Swift Performance is named after a host, so we can return it
* as a list of enabled hosts.
*/
if( is_array($dirs) ){
foreach( $dirs as $key => $current ){
$dirs[$key] = untrailingslashit(str_replace($base, '', $current));
}
}
}
return array_unique(array_merge($list, $dirs), SORT_STRING);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment