Skip to content

Commit

Permalink
Using Enqueue Script For WP_CACHE Enabled Sites. Props Crowd Favourite
Browse files Browse the repository at this point in the history
  • Loading branch information
lesterchan committed May 10, 2013
1 parent c9ef08d commit 19e8a5a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions postviews-cache.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 35 additions & 16 deletions wp-postviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://DPotter.net/Technical/" title="David's Technical Musings">David Potter</a> 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
Expand Down Expand Up @@ -84,27 +84,46 @@ function process_postviews() {
}
}
}
if($should_count) {
if(defined('WP_CACHE') && WP_CACHE) {
echo "\n".'<!-- Start Of Script Generated By WP-PostViews -->'."\n";
wp_print_scripts('jquery');
echo '<script type="text/javascript">'."\n";
echo '/* <![CDATA[ */'."\n";
echo "jQuery.ajax({type:'GET',url:'".admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http'))."',data:'postviews_id=".$id."&action=postviews',cache:false});";
echo '/* ]]> */'."\n";
echo '</script>'."\n";
echo '<!-- End Of Script Generated By WP-PostViews -->'."\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);
}
}
}
}
}


### 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) {
Expand Down Expand Up @@ -821,7 +840,7 @@ function views_init() {
$views_options['template'] = __('%VIEW_COUNT% views', 'wp-postviews');
$views_options['most_viewed_template'] = '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>';
add_option('views_options', $views_options, 'Post Views Options');
// Veersion 1.50 Upgrade
// Version 1.50 Upgrade
delete_option('widget_views_most_viewed');
}
?>

0 comments on commit 19e8a5a

Please sign in to comment.