Lester Chan's Forums
05 July 2008, 00:13 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Lester Chan’s WordPress Plugins June 2008 Update (Wave 2)
 
   Home   Help Search Login Register  
Pages: [1] 2   Go Down
  Print  
Author Topic: Patch: WP-PostRatings and WP-Cache  (Read 16037 times)
0 Members and 1 Guest are viewing this topic.
nir
Newbie
*
Offline Offline

Posts: 3


View Profile
« Topic Start: 30 January 2007, 10:30 »

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.php


The Code:
1) In file postratings.php, function the_ratings_header(), before the line:
Code:
echo "\t".'var ratings_image = \''.get_settings('postratings_image').'\';'."\n";

add the following code:
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:
Code:
$post_id = intval($_GET['pid']);

add the following code:
Code:
$page_hash = $_GET['page_hash'];


3) In file postratings.php, function process_ratings(), AFTER the line:
Code:
echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);

add the following code:
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:
Code:
ratings.setVar("rate", post_rating);

add the following code:
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

« Last Edit: 22 February 2007, 17:36 by GaMerZ » Logged
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #1: 30 January 2007, 14:28 »

So this is to get wp-cache working with wp-postratings?
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 6,229



View Profile WWW
« Reply #2: 30 January 2007, 14:56 »

yea
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
nir
Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #3: 30 January 2007, 15:52 »

I made a modification to the code in bullet (3) of the original post.

The code now sets a cookie that causes wp-cache to generate a different hash for the page for that visitor.
This in turn has the effect that once a visitor submits a rating, wp-cache starts caching a special version of the page just for them. This is needed to maintain the mechanism that prevents a user from submitting a rating twice for the same post. It is also similar to the behavior of wp-cache in regard to WordPress comments.

Limitation of the patch:
If the cookie is denied and the postratings logging method is not set to "Logged by Cookie" then visitors might sometimes find that they are not allowed to submit a rating even if they did not submit one yet, just because wp-cache stored a cached version of the page of a visitor who already submitted a rating.

Nir
« Last Edit: 30 January 2007, 18:41 by nir » Logged
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #4: 30 January 2007, 23:45 »

Cool...
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
nir
Newbie
*
Offline Offline

Posts: 3


View Profile
« Reply #5: 18 February 2007, 08:36 »

Hello All,

The following is an attempt at patching wp-postratings 1.10 to work with wp-cache 2.1 on WordPress 2.1.
This is a response to a request by dsone: forums.lesterchan.net/index.php?topic=315.0

This patch was not tested since I do not use these software versions, but it is based on the original working patch.
What has changed from the original patch?
a) In bullet (1) I added javascript <script> tags to the patch since the wp-postratings 1.10 header does not emit them anymore.
b) In bullet (4) the change now affects postratings-js.php instead of postratings-js.js.

Let me know if it works for you or if it needs any corrections...


The Code:
1) In file postratings.php, function the_ratings_header(), AFTER the line:

Code:
echo "\n".'<!-- Start Of Script Generated By WP-PostRatings 1.10 -->'."\n";

add the following code:

Code:
echo '<script type="text/javascript">'."\n";
echo '/* <![CDATA[ */'."\n";

$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";
}

echo '/* ]]> */'."\n";
echo '</script>'."\n";


2) In file postratings.php, function process_ratings(), AFTER the line:

Code:
$post_id = intval($_GET['pid']);

add the following code:

Code:
$page_hash = $_GET['page_hash'];


3) In file postratings.php, function process_ratings(), AFTER the line:

Code:
echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);

add the following code:

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.php, function rate_process(), AFTER the line:

Code:
ratings.setVar("rate", post_rating);

add the following code:

Code:
ratings.setVar("page_hash", ratings_page_hash);


Nir
Logged
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 6,229



View Profile WWW
« Reply #6: 18 February 2007, 11:35 »

Thanks nir!!!!!
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #7: 18 February 2007, 15:33 »

OK, I'll add it to the tips and tricks page?
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 6,229



View Profile WWW
« Reply #8: 18 February 2007, 23:17 »

actually u can leave it here lol, because this is already stickied
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #9: 19 February 2007, 15:01 »

Oh it is? I didn't notice. Smiley
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
dsone
Newbie
*
Offline Offline

Posts: 5


View Profile
« Reply #10: 19 February 2007, 16:14 »

oh thats cool, thx NIR!
Logged
hennk
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #11: 11 March 2007, 21:20 »

Hi, i tried your solution but I still get an error:

Warning: Cannot modify header information - headers already sent by (output started at /home/xxxcam/public_html/wp-content/plugins/postratings/postratings.php:698) in /home/xxxcam/public_html/wp-content/plugins/postratings/postratings.php on line 710

Maybe this is because of a combination of and wp-cache and eaccelarator?

Everything works till you click the stars to rate. It does pick up the rating but returns this above error.

Any idea?
Logged
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 6,229



View Profile WWW
« Reply #12: 12 March 2007, 01:14 »

hmmm since u said something about e accelerator. can u disable it?
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
hennk
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #13: 12 March 2007, 21:57 »

Tried that but my site has 110K visitors on wordpress and without eaccelarator and wpcache it gets a load of 40+ and goes blank right away  Sad
Logged
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #14: 12 March 2007, 22:17 »

Hmm
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
GaMerZ
lesterchan.net
Administrator
*****
Offline Offline

Posts: 6,229



View Profile WWW
« Reply #15: 13 March 2007, 01:14 »

hmm is there any way u can duplicate the site on a new folder and try it?
Logged


++ lesterchan.net - Lester Chan's Website

I DO NOT provide support for any modifications of any plugin to meet your needs/requirements, unless it is a paid job and I have the time to do it due to the large number of request.
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #16: 13 March 2007, 01:32 »

Virtual Visitors?
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
hennk
Newbie
*
Offline Offline

Posts: 4


View Profile
« Reply #17: 01 April 2007, 16:22 »

let me try
Logged
Ttech
Official Support
Administrator
*****
Offline Offline

Posts: 4,228



View Profile WWW
« Reply #18: 01 April 2007, 17:13 »

ok
Logged

Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers
sokrin
Newbie
*
Offline Offline

Posts: 13


View Profile
« Reply #19: 13 June 2007, 00:17 »

does this still apply to version 1.11?
Logged
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.045 seconds with 23 queries.