• Since WP-PostRatings plugin is using PHP to display ratings data of the post, it is generally incompatible with any caching plugin.

    But here is working code that solves this by simply cleaning the cache of post (and everything related) whenever the post is rated.

    /**
     * Make WP-PostRatings compatible with the Gator Cache.
     */
    function wp_postratings_clear_gator_cache( $rate_userid, $post_id ) {
    	if ( $post_id && class_exists( 'WpGatorCache' ) ) {
    		//$options = WpGatorCache::getOptions();
    		// NOTE: use standard WP function since the main class method getOptions() is protected
    		$options = get_option( WpGatorCache::PREFIX . '_opts', array( 'refresh' => array('home' => true, 'archive' => true) ) );
    		$path_ref = '';
    
    		// purge cache of the rated post
    		if ( null !== ($path = parse_url( get_permalink( $post_id ), PHP_URL_PATH )) ) {
    			WpGatorCache::purgePath( $path );
    		}
    		// purge cache also for the page on which the post was rated (other than single)
    		if ( isset($_SERVER['HTTP_REFERER']) && '' !== ($path_ref = parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_PATH )) && $path !== $path_ref ) {
    			WpGatorCache::purgePath( $path_ref );
    		}
    		// refresh home page if desired and not already cleared
    		if ( $options['refresh']['home'] && $path_ref !== '/' ) {
    			WpGatorCache::purgePath( '/' );
    		}
    		// refresh taxonomy archive pages if desired and not already cleared
    		if ( $options['refresh']['archive'] && false !== ($terms = WpGatorCache::getArchiveTerms( get_post( $post_id ) )) ) {
    			foreach ( $terms as $term ) {
    				if ( null !== ($path_tax = parse_url( get_term_link( $term, $term->taxonomy ), PHP_URL_PATH )) && $path_tax !== $path_ref ) {
    					WpGatorCache::purgePath( $path_tax );
    				}
    			}
    		}
    		// TODO: purge cache for other pages that might contain the rated post (e.g. post type archive)
    	}
    }
    add_action( 'rate_post', 'wp_postratings_clear_gator_cache', 10, 2 );

    https://www.ads-software.com/plugins/gator-cache/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author GatorDog

    (@gatordog)

    Thanks, looks like the rate_post hook can simply call the same function as the save posts hook in the GatorCache plugin?

    Thread Starter lop_cz

    (@lop_cz)

    Yes, it is similar to the save post hook, but not exactly the same.

    For example clearing the cache for parent post is not necessary. But forcing to clear the cache of page on which the post was rated (home or index pages or archives) is necessary so the user can see his rating was already counted after going back. That’s the reason why I’m using the Referer header.

    It’s not a perfect solution but currently the only available since the author of WP-PostRatings plugin is not willing to fully ajaxify the display method of current ratings data.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP-PostRatings compatibility fix’ is closed to new replies.