WordPress.org

Support

Support » Plugins and Hacks » WP-PostViews » [Plugin: WP-PostViews] Does Postviews work with Super Cache

[Plugin: WP-PostViews] Does Postviews work with Super Cache

Viewing 4 replies - 1 through 4 (of 4 total)
  • Yes it’s working, but not displaying real time counts on posts before the cleaning cache. So, it’s counting in background with super cache. I hope, author adds a feature in future versions to resolve this issue.

    Here is some change to support WP Super Cache.
    WP-PostViews 1.62

    function process_postviews() {
    	//...
    		//...
    			if($should_count) {
    				global $cache_enabled; //WP Super Cache enabled or not
    				if(defined('WP_CACHE') && WP_CACHE && $cache_enabled) {
    					echo "\n".'<!-- Start Of Script Generated By WP-PostViews -->'."\n";
    					//...
    				} else {
    					//...
    				}
    			}
    		//...
    	//...
    }
    
    function add_postviews_column($defaults) {
        $defaults['views'] = __('Views', 'wp-postviews');
        return $defaults;
    }
    
    //AJAX get post views
    add_action( 'wp_ajax_postviews_get', 'postviews_get' );
    add_action( 'wp_ajax_nopriv_postviews_get', 'postviews_get' );
    function postviews_get() {
    	if ( !empty( $_GET['postviews_id'] ) ) {
    		$post_id = intval( $_GET['postviews_id'] );
    		if ( $post_id > 0 ) {
    			$views_options = get_option( 'views_options' );
    			$post_views = get_post_custom( $post_id );
    			$output = str_replace( '%VIEW_COUNT%', number_format_i18n( intval( $post_views['views'][0] ) ), $views_options['template'] );
    			$output = apply_filters( 'the_views', $output );
    			echo $output;
    		}
    	}
    	exit();
    }
    
    //the_views support WP Super Cache via AJAX
    function the_views( $display = true, $prefix = '', $postfix = '', $always = false ) {
    	$output = '';
    	$views_options = get_option( 'views_options' );
    	if ( $always || should_views_be_displayed( $views_options ) ) {
    		global $cache_enabled;
    		if ( defined( 'WP_CACHE' ) && WP_CACHE && $cache_enabled ) {
    			global $post;
    			if ( is_int( $post ) ) {
    				$post = get_post( $post );
    			}
    			$id = intval( $post->ID );
    
    			wp_print_scripts( 'jquery' );
    			$output = "\n" . '<!-- Start Of Script WP-PostViews Get Action -->' . "\n";
    			$output .= '<font id="views_' . $id . '">&nbsp;</font>' . "\n";
    			$output .= '<script type="text/javascript">' . "\n";
    			$output .= '/* <![CDATA[ */' . "\n";
    			$output .= "jQuery.ajax({type:'GET',url:'" . admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ) . "',data:'postviews_id=" . $id . "&action=postviews_get',cache:false,complete:function(jqXHR,textStatus){var obj=jQuery('#views_" . $id . "');obj.before('" . $prefix . "'+jqXHR.responseText+'" . $postfix . "');obj.remove();}});\n";
    			$output .= '/* ]]> */' . "\n";
    			$output .= '</script>' . "\n";
    			$output .= '<!-- End Of Script WP-PostViews Get Action -->' . "\n";
    		} else {
    			$output = $prefix . str_replace( '%VIEW_COUNT%', number_format_i18n( intval( post_custom( 'views' ) ) ), $views_options['template'] ) . $postfix;
    			$output = apply_filters( 'the_views', $output );
    		}
    	}
    	if ( $display ) {
    		echo $output;
    		return '';
    	} else {
    		return $output;
    	}
    }

    D J

    @wwwpornolatinonet

    The code above works, but it is recommendable to be used only in single post.

    I tried it on a grid template with 50 posts and the website loaded in 30 seconds.

    Can you come up with a code that if a post is visited then reload the cache for that post.

    if a post is visited then reload the cache for that post?

    I am sorry, You may get contact with the plugin author(if you can, or someone else known how WP Super Cache works), to solve this problem.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: WP-PostViews] Does Postviews work with Super Cache’ is closed to new replies.
Skip to toolbar