diff --git a/README.md b/README.md index 17b15dd..78a1eb3 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # WP-PostViews -Contributors: GamerZ -Donate link: http://lesterchan.net/site/donation/ -Tags: views, hits, counter, postviews -Requires at least: 2.8 -Tested up to: 3.5 -Stable tag: trunk +Contributors: GamerZ +Donate link: http://lesterchan.net/site/donation/ +Tags: views, hits, counter, postviews +Requires at least: 2.8 +Tested up to: 3.5 +Stable tag: trunk Enables you to display how many times a post/page had been viewed. @@ -35,6 +35,9 @@ Enables you to display how many times a post/page had been viewed. * I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appericiate it. If not feel free to use it without any obligations. ## Changelog +### Version 1.64 (07-05-2013) +* NEW: Using Enqueue Script For WP_CACHE Enabled Sites. Props Crowd Favourite + ### Version 1.63 (07-05-2013) * NEW: Added nonce To PostViews Options Admin Page diff --git a/postviews-cache.js b/postviews-cache.js new file mode 100644 index 0000000..4df8836 --- /dev/null +++ b/postviews-cache.js @@ -0,0 +1 @@ +jQuery.ajax({type:"GET",url:viewsCacheL10n.admin_ajax_url,data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",cache:!1}); \ No newline at end of file diff --git a/wp-postviews.php b/wp-postviews.php index 806dc6c..7ed8b04 100644 --- a/wp-postviews.php +++ b/wp-postviews.php @@ -3,7 +3,7 @@ Plugin Name: WP-PostViews Plugin URI: http://lesterchan.net/portfolio/programming/php/ Description: Enables you to display how many times a post/page had been viewed. Modified by David Potter to include options for when and where to display view counts. -Version: 1.63 +Version: 1.64 Author: Lester 'GaMerZ' Chan Author URI: http://lesterchan.net Text Domain: wp-postviews @@ -84,20 +84,9 @@ function process_postviews() { } } } - if($should_count) { - if(defined('WP_CACHE') && WP_CACHE) { - echo "\n".''."\n"; - wp_print_scripts('jquery'); - echo ''."\n"; - echo ''."\n"; - } else { - if(!update_post_meta($id, 'views', ($post_views+1))) { - add_post_meta($id, 'views', 1, true); - } + if($should_count && (!defined('WP_CACHE') || !WP_CACHE)) { + if(!update_post_meta($id, 'views', ($post_views+1))) { + add_post_meta($id, 'views', 1, true); } } } @@ -105,6 +94,36 @@ function process_postviews() { } +### Function: Calculate Post Views With WP_CACHE Enabled +add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue'); +function wp_postview_cache_count_enqueue() { + global $user_ID, $post; + if (!wp_is_post_revision($post) && (is_single() || is_page())) { + $views_options = get_option('views_options'); + switch(intval($views_options['count'])) { + case 0: + $should_count = true; + break; + case 1: + if (empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) { + $should_count = true; + } + break; + case 2: + if (intval($user_ID) > 0) { + $should_count = true; + } + break; + } + if ($should_count && defined('WP_CACHE') && WP_CACHE) { + // Enqueue and localize script here + wp_enqueue_script('wp-postviews-cache', plugins_url('postviews-cache.js', __FILE__), array('jquery'), '1.64', true); + wp_localize_script('wp-postviews-cache', 'viewsCacheL10n', array('admin_ajax_url' => admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http')), 'post_id' => intval($post->ID))); + } + } +} + + ### Function: Determine If Post Views Should Be Displayed (By: David Potter) function should_views_be_displayed($views_options = null) { if ($views_options == null) { @@ -821,7 +840,7 @@ function views_init() { $views_options['template'] = __('%VIEW_COUNT% views', 'wp-postviews'); $views_options['most_viewed_template'] = '
  • %POST_TITLE% - %VIEW_COUNT% '.__('views', 'wp-postviews').'
  • '; add_option('views_options', $views_options, 'Post Views Options'); - // Veersion 1.50 Upgrade + // Version 1.50 Upgrade delete_option('widget_views_most_viewed'); } ?> \ No newline at end of file