Hello all,
This post is about a patch to make wp-postratings work with wp-cache.
Please note that it was written as a hack and was not written to be elegant nor tested thoroughly.
Specifically, please note that it was written for wp-postratings 1.0.4 and wp-cache 2.0.22.
Hopefully, Lester will go over the code soon and integrate the concept into a future version of wp-postratings.
I recommend that you backup your data and test this patch first on a local or secondary blog before you try it live to make sure it works for you.
The Setup:
wp-postratings 1.0.4
wp-cache 2.0.22
Linux server with Apache.
The Idea:
The basic idea behind this hack is that ratings are similar to blog remarks, therefore they should remain static and when a visitor submits a new rating it should be enough to delete the relevant page from the cache.
A Working Example:
http://www.digitalpeers.com/webcam-animations.phpThe Code:
1) In file postratings.php, function the_ratings_header(), before the line:
echo "\t".'var ratings_image = \''.get_settings('postratings_image').'\';'."\n";add the following code:
$res = include_once(ABSPATH.'/wp-content/plugins/wp-cache/wp-cache-phase1.php');
if($res) {
$key = md5($_SERVER['SERVER_NAME'].preg_replace('/#.*$/', '', $_SERVER['REQUEST_URI']).wp_cache_get_cookies_values());
echo "\t".'var ratings_page_hash = \''.$key.'\';'."\n";
}
else {
echo "\t".'var ratings_page_hash = \'\';'."\n";
}2) In file postratings.php, function process_ratings(), AFTER the line:
$post_id = intval($_GET['pid']);add the following code:
$page_hash = $_GET['page_hash'];3) In file postratings.php, function process_ratings(), AFTER the line:
echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);add the following code:
$res = include_once(ABSPATH.'/wp-content/plugins/wp-cache/wp-cache.php');
if($res && wp_cache_is_enabled()) {
$res = include_once(ABSPATH.'/wp-content/plugins/wp-cache/wp-cache-phase2.php');
if($res) {
$prefix = 'wp-cache-'.$page_hash;
wp_cache_phase2_clean_cache($prefix);
$cache_cookie = 'wordpress_postrating_cache_'.COOKIEHASH;
if(!isset($_COOKIE[$cache_cookie])) {
$token = md5(uniqid(rand(), true));
setcookie($cache_cookie, $token, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
}
}
}
4) In file postratings-js.js, function rate_process(), AFTER the line:
ratings.setVar("rate", post_rating);add the following code:
ratings.setVar("page_hash", ratings_page_hash);Thanks:
wp-postratings is a wonderful plug-in and I would like to thank Lester for writing it.
Comments and ideas are welcome.
Enjoy,
Nir Aides