diff --git a/index-wp-redis.php b/index-wp-redis.php index 6f16b901e496aff654c8101540729a550ab6ad00..bd11f107da95774cedaf43d543dc21142e5e5c7f 100644 --- a/index-wp-redis.php +++ b/index-wp-redis.php @@ -10,16 +10,13 @@ function getMicroTime($t) } -$debug = false; - +$debug = true; +$cache = false; $ip_of_your_website = '127.0.0.1'; $secret_string = 'changeme'; -if(!defined('WP_USE_THEMES')) { - define('WP_USE_THEMES', true); -} // so we don't confuse the cloudflare server if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { @@ -56,8 +53,9 @@ try { // This page is cached, lets display it } else if ($redis->exists($redis_key)) { + $cache = true; $html_of_page = $redis->get($redis_key); - echo $html_of_page; + echo $html_of_page; // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website && strstr($current_url, 'preview=true') == false) { @@ -70,7 +68,7 @@ try { require('./wp-blog-header.php'); $html_of_page = ob_get_contents(); ob_end_clean(); - echo $html_of_page; + echo $html_of_page; $unlimited = get_option('wp-redis-cache-debug',false); $seconds_cache_redis = get_option('wp-redis-cache-seconds',43200); @@ -81,9 +79,9 @@ try { // When a page displays after an "HTTP 404: Not Found" error occurs, do not cache // When the search was used, do not cache - if (!is_404() && !is_search()) { + if ((!is_404()) and (!is_search())) { if ($unlimited) { - $redis->setex($redis_key, $html_of_page); + $redis->set($redis_key, $html_of_page); } else { @@ -106,16 +104,16 @@ try { require('./wp-blog-header.php'); } -if ($_SERVER['REMOTE_ADDR'] != $ip_of_your_website) { - // How long did it take to load the page? (CloudFlare may strip out comments) - $end = microtime(); - $time = (@getMicroTime($end) - @getMicroTime($start)); - echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->"; - if ($debug) { - echo "<!-- wp-redis-cache-seconds = " . $seconds_cache_redis . " -->"; - echo "<!-- wp-redis-cache-secret = " . $secret_string . "-->"; - echo "<!-- wp-redis-cache-ip = " . $ip_of_your_website . "-->"; - echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->"; - echo "<!-- wp-redis-cache-debug = " . $debug . "-->"; - } + +$end = microtime(); +$time = (@getMicroTime($end) - @getMicroTime($start)); +if ($debug) { + echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->"; + echo "<!-- Site was cached = " . $cache . " -->"; + echo "<!-- wp-redis-cache-seconds = " . $seconds_cache_redis . " -->"; + echo "<!-- wp-redis-cache-secret = " . $secret_string . "-->"; + echo "<!-- wp-redis-cache-ip = " . $ip_of_your_website . "-->"; + echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->"; + echo "<!-- wp-redis-cache-debug = " . $debug . "-->"; } + diff --git a/wp-redis-cache/cache.php b/wp-redis-cache/cache.php index e4dd76e4fc1e1db44e1f79997e47ea545fb14141..e47efeb8a50c588459055d6695d186d8b3bc8305 100644 --- a/wp-redis-cache/cache.php +++ b/wp-redis-cache/cache.php @@ -1,25 +1,73 @@ <?php add_action('transition_post_status', 'refresh_wp_redis_cache',10,3); - +add_action('wp_ajax_clear_wp_redis_cache', 'clear_wp_redis_cache'); +add_action( 'admin_footer', 'clear_wp_redis_cache_javascript' ); //clears the cache after you update a post function refresh_wp_redis_cache( $new, $old, $post ) { -if($new == "publish") -{ -$permalink = get_permalink( $post->ID ); + if($new == "publish") + { + $permalink = get_permalink( $post->ID ); + + // aaronstpierre: this needs to be include_once so as not to cauase a redeclaration error + include_once("predis5.2.php"); //we need this to use Redis inside of PHP + $redis = new Predis_Client(); -// aaronstpierre: this needs to be include_once so as not to cauase a redeclaration error -include_once("predis5.2.php"); //we need this to use Redis inside of PHP -$redis = new Predis_Client(); + $redis_key = md5($permalink); + $redis->del($redis_key); + + //refresh the front page + $frontPage = get_home_url() . "/"; + $redis_key = md5($frontPage); + $redis->del($redis_key); + } +} -$redis_key = md5($permalink); -$redis->del($redis_key); +// clears the whole cache +function clear_wp_redis_cache() +{ + include_once("predis5.2.php"); //we need this to use Redis inside of PHP + $args = array( 'post_type' => 'any', 'posts_per_page' => -1); + $wp_query = new WP_Query( $args); // to get all Posts + $redis = new Predis_Client(); + // Loop all posts and clear the cache + $i = 0; + while ( $wp_query->have_posts() ) : $wp_query->the_post(); + $permalink = get_permalink(); -//refresh the front page -$frontPage = get_home_url() . "/"; -$redis_key = md5($frontPage); -$redis->del($redis_key); + $redis_key = md5($permalink); + if (($redis->exists($redis_key)) == true ) { + $redis->del($redis_key); + $i++; + } + + + endwhile; + + echo $i++." of " . $wp_query -> found_posts . " posts was cleared in cache"; + die(); } + +function clear_wp_redis_cache_javascript() { +?> +<script type="text/javascript" > +jQuery(document).ready(function($) { + + jQuery('#WPRedisClearCache').click(function(){ + var data = { + action: 'clear_wp_redis_cache', + }; + + // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php + $.post(ajaxurl, data, function(response) { + alert(response); + }); + }); +}); +</script> +<?php } +?> + diff --git a/wp-redis-cache/options.php b/wp-redis-cache/options.php index 0923754ff6e2f17ebe2e2ebb8fe053e9d01146e2..4a110809a94d3051a389b94488cb0a6a81389e7f 100644 --- a/wp-redis-cache/options.php +++ b/wp-redis-cache/options.php @@ -143,7 +143,7 @@ function edit_redis_options() { <input type="checkbox" name="wp-redis-cache-unlimited" size="45" value="true" <?php checked('true', get_option('wp-redis-cache-unlimited')); ?>/></p> <p><input type="submit" name="Submit" value="Update Options" /></p> - + <p><input type="button" id="WPRedisClearCache" name="WPRedisClearCache" value="Clear Cache"></p> <input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="wp-redis-cache-seconds,wp-redis-cache-unlimited" />