All Good Things Must Come To An End

As you all know, I have been doing WordPress plugins and supporting it for the past 6 years. These 6 years of my life, I have been through my polytechnic education, my national service as well as my university education.

I just graduated from university in December 2009 and have been looking for full-time jobs. I am offered a full-time job and will be starting work on 1st February 2010.

I regret to say that I am NOT ABLE to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

However, I WILL still update my plugins whenever I can and you still can report bugs to me via email and I will try to fix it.

Author Topic: Patch: WP-PostRatings and WP-Cache  (Read 53203 times)

0 Members and 1 Guest are viewing this topic.

Offline nir

  • Newbie
  • *
  • Posts: 3
    • View Profile
Patch: WP-PostRatings and WP-Cache
« on: 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: [Select]
echo "\t".'var ratings_image = \''.get_settings('postratings_image').'\';'."\n";
add the following code:
Code: [Select]
$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: [Select]
$post_id = intval($_GET['pid']);
add the following code:
Code: [Select]
$page_hash = $_GET['page_hash'];

3) In file postratings.php, function process_ratings(), AFTER the line:
Code: [Select]
echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
add the following code:
Code: [Select]
$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: [Select]
ratings.setVar("rate", post_rating);
add the following code:
Code: [Select]
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 »

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #1 on: 30 January 2007, 14:28 »
So this is to get wp-cache working with wp-postratings?
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #2 on: 30 January 2007, 14:56 »
yea

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline nir

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #3 on: 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 »

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #4 on: 30 January 2007, 23:45 »
Cool...
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline nir

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #5 on: 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: [Select]
echo "\n".'<!-- Start Of Script Generated By WP-PostRatings 1.10 -->'."\n";
add the following code:

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

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

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

Code: [Select]
echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
add the following code:

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

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

Nir

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #6 on: 18 February 2007, 11:35 »
Thanks nir!!!!!

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #7 on: 18 February 2007, 15:33 »
OK, I'll add it to the tips and tricks page?
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #8 on: 18 February 2007, 23:17 »
actually u can leave it here lol, because this is already stickied

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #9 on: 19 February 2007, 15:01 »
Oh it is? I didn't notice. :)
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline dsone

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache.
« Reply #10 on: 19 February 2007, 16:14 »
oh thats cool, thx NIR!

Offline hennk

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #11 on: 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?

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Patch: WP-PostRatings and WP-Cache
« Reply #12 on: 12 March 2007, 01:14 »
hmmm since u said something about e accelerator. can u disable it?

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline hennk

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #13 on: 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  :(

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #14 on: 12 March 2007, 22:17 »
Hmm
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline GaMerZ

  • lesterchan.net
  • Administrator
  • *****
  • Posts: 11,692
    • View Profile
  • WordPress Version: WordPress 3.0.x
  • Theme Name: lesterchan.net v3.4
Re: Patch: WP-PostRatings and WP-Cache
« Reply #15 on: 13 March 2007, 01:14 »
hmm is there any way u can duplicate the site on a new folder and try it?

++ lesterchan.net - Lester Chan's Website

I regret to say that I am not able to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #16 on: 13 March 2007, 01:32 »
Virtual Visitors?
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline hennk

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #17 on: 01 April 2007, 16:22 »
let me try

Offline Ttech

  • Official Support
  • Global Moderator
  • *****
  • Posts: 4,296
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #18 on: 01 April 2007, 17:13 »
ok
Learning and Helping one day at a time.




DevNode.org Operator - The IRC network for Programers

Offline sokrin

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Patch: WP-PostRatings and WP-Cache
« Reply #19 on: 13 June 2007, 00:17 »
does this still apply to version 1.11?