| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-PostViews |
| 4 |
Plugin URI: http://lesterchan.net/portfolio/programming/php/ |
| 5 |
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. |
| 6 |
Version: 1.40 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: http://lesterchan.net |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
Copyright 2008 Lester Chan (email : lesterchan@gmail.com) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License as published by |
| 17 |
the Free Software Foundation; either version 2 of the License, or |
| 18 |
(at your option) any later version. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
### Load WP-Config File If This File Is Called Directly |
| 32 |
if (!function_exists('add_action')) { |
| 33 |
$wp_root = '../../..'; |
| 34 |
if (file_exists($wp_root.'/wp-load.php')) { |
| 35 |
require_once($wp_root.'/wp-load.php'); |
| 36 |
} else { |
| 37 |
require_once($wp_root.'/wp-config.php'); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
### Create Text Domain For Translations |
| 43 |
add_action('init', 'postviews_textdomain'); |
| 44 |
function postviews_textdomain() { |
| 45 |
load_plugin_textdomain('wp-postviews', false, 'wp-postviews'); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
### Function: Post Views Option Menu |
| 50 |
add_action('admin_menu', 'postviews_menu'); |
| 51 |
function postviews_menu() { |
| 52 |
if (function_exists('add_options_page')) { |
| 53 |
add_options_page(__('PostViews', 'wp-postviews'), __('PostViews', 'wp-postviews'), 'manage_options', 'wp-postviews/postviews-options.php') ; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
### Function: Calculate Post Views |
| 59 |
add_action('wp_head', 'process_postviews'); |
| 60 |
function process_postviews() { |
| 61 |
global $user_ID, $post; |
| 62 |
if(is_single() || is_page()) { |
| 63 |
$id = intval($post->ID); |
| 64 |
$views_options = get_option('views_options'); |
| 65 |
$post_views = get_post_custom($id); |
| 66 |
$post_views = intval($post_views['views'][0]); |
| 67 |
$should_count = false; |
| 68 |
switch(intval($views_options['count'])) { |
| 69 |
case 0: |
| 70 |
$should_count = true; |
| 71 |
break; |
| 72 |
case 1: |
| 73 |
if(empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) { |
| 74 |
$should_count = true; |
| 75 |
} |
| 76 |
break; |
| 77 |
case 2: |
| 78 |
if(intval($user_ID) > 0) { |
| 79 |
$should_count = true; |
| 80 |
} |
| 81 |
break; |
| 82 |
} |
| 83 |
if(intval($views_options['exclude_bots']) == 1) { |
| 84 |
$bots = array('Google Bot' => 'googlebot', 'Google Bot' => 'google', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'jeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot', 'Technorati' => 'technorati', 'Yahoo' => 'yahoo', 'Findexa' => 'findexa', 'NextLinks' => 'findlinks', 'Gais' => 'gaisbo', 'WiseNut' => 'zyborg', 'WhoisSource' => 'surveybot', 'Bloglines' => 'bloglines', 'BlogSearch' => 'blogsearch', 'PubSub' => 'pubsub', 'Syndic8' => 'syndic8', 'RadioUserland' => 'userland', 'Gigabot' => 'gigabot', 'Become.com' => 'become.com'); |
| 85 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
| 86 |
foreach ($bots as $name => $lookfor) { |
| 87 |
if (stristr($useragent, $lookfor) !== false) { |
| 88 |
$should_count = false; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
if($should_count) { |
| 94 |
if(defined('WP_CACHE') && WP_CACHE) { |
| 95 |
echo "\n".'<!-- Start Of Script Generated By WP-PostViews 1.40 -->'."\n"; |
| 96 |
wp_print_scripts(array('sack')); |
| 97 |
echo '<script type="text/javascript">'."\n"; |
| 98 |
echo '/* <![CDATA[ */'."\n"; |
| 99 |
echo "\t".'var postviews_ajax_url = \''.plugins_url('wp-postviews/wp-postviews.php')."';\n"; |
| 100 |
echo "\t".'postviews_count = new sack(postviews_ajax_url);'."\n"; |
| 101 |
echo "\t".'postviews_count.setVar("postviews_id", '.$id.');'."\n"; |
| 102 |
echo "\t".'postviews_count.method = \'GET\';'."\n"; |
| 103 |
echo "\t".'postviews_count.runAJAX();'."\n"; |
| 104 |
echo "\t".'postviews_count = null;'."\n"; |
| 105 |
echo '/* ]]> */'."\n"; |
| 106 |
echo '</script>'."\n"; |
| 107 |
echo '<!-- End Of Script Generated By WP-PostViews 1.40 -->'."\n"; |
| 108 |
} else { |
| 109 |
if(!update_post_meta($id, 'views', ($post_views+1))) { |
| 110 |
add_post_meta($id, 'views', 1, true); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
### Function: Determine If Post Views Should Be Displayed (By: David Potter) |
| 119 |
function should_views_be_displayed($views_options = null) { |
| 120 |
if ($views_options == null) { |
| 121 |
$views_options = get_option('views_options'); |
| 122 |
} |
| 123 |
$display_option = 0; |
| 124 |
if (is_home()) { |
| 125 |
if (array_key_exists('display_home', $views_options)) { |
| 126 |
$display_option = $views_options['display_home']; |
| 127 |
} |
| 128 |
} elseif (is_single()) { |
| 129 |
if (array_key_exists('display_single', $views_options)) { |
| 130 |
$display_option = $views_options['display_single']; |
| 131 |
} |
| 132 |
} elseif (is_page()) { |
| 133 |
if (array_key_exists('display_page', $views_options)) { |
| 134 |
$display_option = $views_options['display_page']; |
| 135 |
} |
| 136 |
} elseif (is_archive()) { |
| 137 |
if (array_key_exists('display_archive', $views_options)) { |
| 138 |
$display_option = $views_options['display_archive']; |
| 139 |
} |
| 140 |
} elseif (is_search()) { |
| 141 |
if (array_key_exists('display_search', $views_options)) { |
| 142 |
$display_option = $views_options['display_search']; |
| 143 |
} |
| 144 |
} else { |
| 145 |
if (array_key_exists('display_other', $views_options)) { |
| 146 |
$display_option = $views_options['display_other']; |
| 147 |
} |
| 148 |
} |
| 149 |
return (($display_option == 0) || (($display_option == 1) && is_user_logged_in())); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
### Function: Display The Post Views |
| 154 |
function the_views($display = true, $prefix = '', $postfix = '', $always = false) { |
| 155 |
$post_views = intval(post_custom('views')); |
| 156 |
$views_options = get_option('views_options'); |
| 157 |
if ($always || should_views_be_displayed($views_options)) { |
| 158 |
$output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix; |
| 159 |
if($display) { |
| 160 |
echo apply_filters('the_views', $output); |
| 161 |
} else { |
| 162 |
return apply_filters('the_views', $output); |
| 163 |
} |
| 164 |
} |
| 165 |
elseif (!$display) { |
| 166 |
return ''; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
### Function: Display Least Viewed Page/Post |
| 172 |
if(!function_exists('get_least_viewed')) { |
| 173 |
function get_least_viewed($mode = '', $limit = 10, $chars = 0, $display = true) { |
| 174 |
global $wpdb, $post; |
| 175 |
$views_options = get_option('views_options'); |
| 176 |
$where = ''; |
| 177 |
$temp = ''; |
| 178 |
$output = ''; |
| 179 |
if(!empty($mode) && $mode != 'both') { |
| 180 |
$where = "post_type = '$mode'"; |
| 181 |
} else { |
| 182 |
$where = '1=1'; |
| 183 |
} |
| 184 |
$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views ASC LIMIT $limit"); |
| 185 |
if($most_viewed) { |
| 186 |
foreach ($most_viewed as $post) { |
| 187 |
$post_views = intval($post->views); |
| 188 |
$post_title = get_the_title(); |
| 189 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password); |
| 190 |
$post_content = get_the_content(); |
| 191 |
if($chars > 0) { |
| 192 |
$temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), number_format_i18n($post_views))."</li>\n"; |
| 193 |
} else { |
| 194 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 195 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 196 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 197 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 198 |
$temp = str_replace("%POST_CONTENT%", $post_content, $temp); |
| 199 |
$temp = str_replace("%POST_URL%", get_permalink(), $temp); |
| 200 |
} |
| 201 |
$output .= $temp; |
| 202 |
} |
| 203 |
} else { |
| 204 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 205 |
} |
| 206 |
if($display) { |
| 207 |
echo $output; |
| 208 |
} else { |
| 209 |
return $output; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
### Function: Display Most Viewed Page/Post |
| 216 |
if(!function_exists('get_most_viewed')) { |
| 217 |
function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) { |
| 218 |
global $wpdb, $post; |
| 219 |
$views_options = get_option('views_options'); |
| 220 |
$where = ''; |
| 221 |
$temp = ''; |
| 222 |
$output = ''; |
| 223 |
if(!empty($mode) && $mode != 'both') { |
| 224 |
$where = "post_type = '$mode'"; |
| 225 |
} else { |
| 226 |
$where = '1=1'; |
| 227 |
} |
| 228 |
$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit"); |
| 229 |
if($most_viewed) { |
| 230 |
foreach ($most_viewed as $post) { |
| 231 |
$post_views = intval($post->views); |
| 232 |
$post_title = get_the_title(); |
| 233 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password); |
| 234 |
$post_content = get_the_content(); |
| 235 |
if($chars > 0) { |
| 236 |
$temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), number_format_i18n($post_views))."</li>\n"; |
| 237 |
} else { |
| 238 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 239 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 240 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 241 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 242 |
$temp = str_replace("%POST_CONTENT%", $post_content, $temp); |
| 243 |
$temp = str_replace("%POST_URL%", get_permalink(), $temp); |
| 244 |
} |
| 245 |
$output .= $temp; |
| 246 |
} |
| 247 |
} else { |
| 248 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 249 |
} |
| 250 |
if($display) { |
| 251 |
echo $output; |
| 252 |
} else { |
| 253 |
return $output; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
### Function: Display Leased Viewed Page/Post By Category ID |
| 260 |
if(!function_exists('get_least_viewed_category')) { |
| 261 |
function get_least_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 262 |
global $wpdb, $post; |
| 263 |
$views_options = get_option('views_options'); |
| 264 |
$where = ''; |
| 265 |
$temp = ''; |
| 266 |
$output = ''; |
| 267 |
if(is_array($category_id)) { |
| 268 |
$category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')'; |
| 269 |
} else { |
| 270 |
$category_sql = "$wpdb->term_taxonomy.term_id = $category_id"; |
| 271 |
} |
| 272 |
if(!empty($mode) && $mode != 'both') { |
| 273 |
$where = "post_type = '$mode'"; |
| 274 |
} else { |
| 275 |
$where = '1=1'; |
| 276 |
} |
| 277 |
$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_date < '".current_time('mysql')."' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views ASC LIMIT $limit"); |
| 278 |
if($most_viewed) { |
| 279 |
foreach ($most_viewed as $post) { |
| 280 |
$post_views = intval($post->views); |
| 281 |
$post_title = get_the_title(); |
| 282 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password); |
| 283 |
$post_content = get_the_content(); |
| 284 |
if($chars > 0) { |
| 285 |
$temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), number_format_i18n($post_views))."</li>\n"; |
| 286 |
} else { |
| 287 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 288 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 289 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 290 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 291 |
$temp = str_replace("%POST_CONTENT%", $post_content, $temp); |
| 292 |
$temp = str_replace("%POST_URL%", get_permalink(), $temp); |
| 293 |
} |
| 294 |
$output .= $temp; |
| 295 |
} |
| 296 |
} else { |
| 297 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 298 |
} |
| 299 |
if($display) { |
| 300 |
echo $output; |
| 301 |
} else { |
| 302 |
return $output; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
### Function: Display Most Viewed Page/Post By Category ID |
| 309 |
if(!function_exists('get_most_viewed_category')) { |
| 310 |
function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 311 |
global $wpdb, $post; |
| 312 |
$views_options = get_option('views_options'); |
| 313 |
$where = ''; |
| 314 |
$temp = ''; |
| 315 |
$output = ''; |
| 316 |
if(is_array($category_id)) { |
| 317 |
$category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')'; |
| 318 |
} else { |
| 319 |
$category_sql = "$wpdb->term_taxonomy.term_id = $category_id"; |
| 320 |
} |
| 321 |
if(!empty($mode) && $mode != 'both') { |
| 322 |
$where = "post_type = '$mode'"; |
| 323 |
} else { |
| 324 |
$where = '1=1'; |
| 325 |
} |
| 326 |
$most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_date < '".current_time('mysql')."' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit"); |
| 327 |
if($most_viewed) { |
| 328 |
foreach ($most_viewed as $post) { |
| 329 |
$post_views = intval($post->views); |
| 330 |
$post_title = get_the_title(); |
| 331 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password); |
| 332 |
$post_content = get_the_content(); |
| 333 |
if($chars > 0) { |
| 334 |
$temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), number_format_i18n($post_views))."</li>\n"; |
| 335 |
} else { |
| 336 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 337 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 338 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 339 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 340 |
$temp = str_replace("%POST_CONTENT%", $post_content, $temp); |
| 341 |
$temp = str_replace("%POST_URL%", get_permalink(), $temp); |
| 342 |
} |
| 343 |
$output .= $temp; |
| 344 |
} |
| 345 |
} else { |
| 346 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 347 |
} |
| 348 |
if($display) { |
| 349 |
echo $output; |
| 350 |
} else { |
| 351 |
return $output; |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
### Function: Display Total Views |
| 358 |
if(!function_exists('get_totalviews')) { |
| 359 |
function get_totalviews($display = true) { |
| 360 |
global $wpdb; |
| 361 |
$total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'")); |
| 362 |
if($display) { |
| 363 |
echo $total_views; |
| 364 |
} else { |
| 365 |
return $total_views; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
### Function: Snippet Text |
| 372 |
if(!function_exists('snippet_text')) { |
| 373 |
function snippet_text($text, $length = 0) { |
| 374 |
if (defined('MB_OVERLOAD_STRING')) { |
| 375 |
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); |
| 376 |
if (mb_strlen($text) > $length) { |
| 377 |
return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; |
| 378 |
} else { |
| 379 |
return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); |
| 380 |
} |
| 381 |
} else { |
| 382 |
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); |
| 383 |
if (strlen($text) > $length) { |
| 384 |
return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; |
| 385 |
} else { |
| 386 |
return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected |
| 394 |
function views_post_excerpt($post_excerpt, $post_content, $post_password) { |
| 395 |
if(!empty($post_password)) { |
| 396 |
if(!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post_password) { |
| 397 |
return __('There is no excerpt because this is a protected post.', 'wp-postviews'); |
| 398 |
} |
| 399 |
} |
| 400 |
if(empty($post_excerpt)) { |
| 401 |
return snippet_text(strip_tags($post_content), 200); |
| 402 |
} else { |
| 403 |
return $post_excerpt; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
### Function: Modify Default WordPress Listing To Make It Sorted By Post Views |
| 409 |
function views_fields($content) { |
| 410 |
global $wpdb; |
| 411 |
$content .= ", ($wpdb->postmeta.meta_value+0) AS views"; |
| 412 |
return $content; |
| 413 |
} |
| 414 |
function views_join($content) { |
| 415 |
global $wpdb; |
| 416 |
$content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID"; |
| 417 |
return $content; |
| 418 |
} |
| 419 |
function views_where($content) { |
| 420 |
global $wpdb; |
| 421 |
$content .= " AND $wpdb->postmeta.meta_key = 'views'"; |
| 422 |
return $content; |
| 423 |
} |
| 424 |
function views_orderby($content) { |
| 425 |
$orderby = trim(addslashes(get_query_var('v_orderby'))); |
| 426 |
if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) { |
| 427 |
$orderby = 'desc'; |
| 428 |
} |
| 429 |
$content = " views $orderby"; |
| 430 |
return $content; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
### Function: Add Views Custom Fields |
| 435 |
add_action('publish_post', 'add_views_fields'); |
| 436 |
function add_views_fields($post_ID) { |
| 437 |
global $wpdb; |
| 438 |
add_post_meta($post_ID, 'views', 0, true); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
### Function: Delete Views Custom Fields |
| 443 |
add_action('delete_post', 'delete_views_fields'); |
| 444 |
function delete_views_fields($post_ID) { |
| 445 |
global $wpdb; |
| 446 |
delete_post_meta($post_ID, 'views'); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
### Function: Views Public Variables |
| 451 |
add_filter('query_vars', 'views_variables'); |
| 452 |
function views_variables($public_query_vars) { |
| 453 |
$public_query_vars[] = 'v_sortby'; |
| 454 |
$public_query_vars[] = 'v_orderby'; |
| 455 |
return $public_query_vars; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
### Function: Sort Ratings Posts |
| 460 |
add_action('pre_get_posts', 'views_sorting'); |
| 461 |
function views_sorting() { |
| 462 |
if(get_query_var('v_sortby') == 'views') { |
| 463 |
add_filter('posts_fields', 'views_fields'); |
| 464 |
add_filter('posts_join', 'views_join'); |
| 465 |
add_filter('posts_where', 'views_where'); |
| 466 |
add_filter('posts_orderby', 'views_orderby'); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
### Function: Plug Into WP-Stats |
| 472 |
if(strpos(get_option('stats_url'), $_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], 'stats-options.php') || strpos($_SERVER['REQUEST_URI'], 'wp-stats/wp-stats.php')) { |
| 473 |
add_filter('wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats'); |
| 474 |
add_filter('wp_stats_page_admin_most', 'postviews_page_admin_most_stats'); |
| 475 |
add_filter('wp_stats_page_plugins', 'postviews_page_general_stats'); |
| 476 |
add_filter('wp_stats_page_most', 'postviews_page_most_stats'); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
### Function: Add WP-PostViews General Stats To WP-Stats Page Options |
| 481 |
function postviews_page_admin_general_stats($content) { |
| 482 |
$stats_display = get_option('stats_display'); |
| 483 |
if($stats_display['views'] == 1) { |
| 484 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" checked="checked" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n"; |
| 485 |
} else { |
| 486 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n"; |
| 487 |
} |
| 488 |
return $content; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options |
| 493 |
function postviews_page_admin_most_stats($content) { |
| 494 |
$stats_display = get_option('stats_display'); |
| 495 |
$stats_mostlimit = intval(get_option('stats_mostlimit')); |
| 496 |
if($stats_display['viewed_most'] == 1) { |
| 497 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most" value="viewed_most" checked="checked" /> <label for="wpstats_viewed_most">'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 498 |
} else { |
| 499 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most" value="viewed_most" /> <label for="wpstats_viewed_most">'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 500 |
} |
| 501 |
return $content; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
### Function: Add WP-PostViews General Stats To WP-Stats Page |
| 506 |
function postviews_page_general_stats($content) { |
| 507 |
$stats_display = get_option('stats_display'); |
| 508 |
if($stats_display['views'] == 1) { |
| 509 |
$content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n"; |
| 510 |
$content .= '<ul>'."\n"; |
| 511 |
$content .= '<li>'.sprintf(__ngettext('<strong>%s</strong> view was generated.', '<strong>%s</strong> views were generated.', get_totalviews(false), 'wp-postviews'), number_format_i18n(get_totalviews(false))).'</li>'."\n"; |
| 512 |
$content .= '</ul>'."\n"; |
| 513 |
} |
| 514 |
return $content; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page |
| 519 |
function postviews_page_most_stats($content) { |
| 520 |
$stats_display = get_option('stats_display'); |
| 521 |
$stats_mostlimit = intval(get_option('stats_mostlimit')); |
| 522 |
if($stats_display['viewed_most'] == 1) { |
| 523 |
$content .= '<p><strong>'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n"; |
| 524 |
$content .= '<ul>'."\n"; |
| 525 |
$content .= get_most_viewed('post', $stats_mostlimit, 0, false); |
| 526 |
$content .= '</ul>'."\n"; |
| 527 |
} |
| 528 |
return $content; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
### Function: Increment Post Views |
| 533 |
increment_views(); |
| 534 |
function increment_views() { |
| 535 |
global $wpdb; |
| 536 |
$post_id = intval($_GET['postviews_id']); |
| 537 |
if($post_id > 0) { |
| 538 |
$post_views = get_post_custom($post_id); |
| 539 |
$post_views = intval($post_views['views'][0]); |
| 540 |
if(!update_post_meta($post_id, 'views', ($post_views+1))) { |
| 541 |
add_post_meta($post_id, 'views', 1, true); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
### Function: Post Views Options |
| 548 |
add_action('activate_wp-postviews/wp-postviews.php', 'views_init'); |
| 549 |
function views_init() { |
| 550 |
postviews_textdomain(); |
| 551 |
// Add Options |
| 552 |
$views_options = array(); |
| 553 |
$views_options['count'] = 1; |
| 554 |
$views_options['exclude_bots'] = 0; |
| 555 |
$views_options['display_home'] = 0; |
| 556 |
$views_options['display_single'] = 0; |
| 557 |
$views_options['display_page'] = 0; |
| 558 |
$views_options['display_archive'] = 0; |
| 559 |
$views_options['display_search'] = 0; |
| 560 |
$views_options['display_other'] = 0; |
| 561 |
$views_options['template'] = __('%VIEW_COUNT% views', 'wp-postviews'); |
| 562 |
$views_options['most_viewed_template'] = '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>'; |
| 563 |
add_option('views_options', $views_options, 'Post Views Options'); |
| 564 |
} |
| 565 |
?> |