| 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. |
| 6 |
Version: 1.66 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: http://lesterchan.net |
| 9 |
Text Domain: wp-postviews |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
/* |
| 14 |
Copyright 2014 Lester Chan (email : lesterchan@gmail.com) |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; either version 2 of the License, or |
| 19 |
(at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
### Create Text Domain For Translations |
| 33 |
add_action( 'plugins_loaded', 'postviews_textdomain' ); |
| 34 |
function postviews_textdomain() { |
| 35 |
load_plugin_textdomain( 'wp-postviews', false, dirname( plugin_basename( __FILE__ ) ) ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
### Function: Post Views Option Menu |
| 40 |
add_action('admin_menu', 'postviews_menu'); |
| 41 |
function postviews_menu() { |
| 42 |
if (function_exists('add_options_page')) { |
| 43 |
add_options_page(__('PostViews', 'wp-postviews'), __('PostViews', 'wp-postviews'), 'manage_options', 'wp-postviews/postviews-options.php') ; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
### Function: Calculate Post Views |
| 49 |
add_action('wp_head', 'process_postviews'); |
| 50 |
function process_postviews() { |
| 51 |
global $user_ID, $post; |
| 52 |
if(is_int($post)) { |
| 53 |
$post = get_post($post); |
| 54 |
} |
| 55 |
if(!wp_is_post_revision($post)) { |
| 56 |
if(is_single() || is_page()) { |
| 57 |
$id = intval($post->ID); |
| 58 |
$views_options = get_option('views_options'); |
| 59 |
if ( ! $post_views = get_post_meta( $post->ID, 'views', true ) ) |
| 60 |
$post_views = 0; |
| 61 |
$should_count = false; |
| 62 |
switch(intval($views_options['count'])) { |
| 63 |
case 0: |
| 64 |
$should_count = true; |
| 65 |
break; |
| 66 |
case 1: |
| 67 |
if(empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) { |
| 68 |
$should_count = true; |
| 69 |
} |
| 70 |
break; |
| 71 |
case 2: |
| 72 |
if(intval($user_ID) > 0) { |
| 73 |
$should_count = true; |
| 74 |
} |
| 75 |
break; |
| 76 |
} |
| 77 |
if(intval($views_options['exclude_bots']) == 1) { |
| 78 |
$bots = array |
| 79 |
( |
| 80 |
'Google Bot' => 'googlebot' |
| 81 |
, 'Google Bot' => 'google' |
| 82 |
, 'MSN' => 'msnbot' |
| 83 |
, 'Alex' => 'ia_archiver' |
| 84 |
, 'Lycos' => 'lycos' |
| 85 |
, 'Ask Jeeves' => 'jeeves' |
| 86 |
, 'Altavista' => 'scooter' |
| 87 |
, 'AllTheWeb' => 'fast-webcrawler' |
| 88 |
, 'Inktomi' => 'slurp@inktomi' |
| 89 |
, 'Turnitin.com' => 'turnitinbot' |
| 90 |
, 'Technorati' => 'technorati' |
| 91 |
, 'Yahoo' => 'yahoo' |
| 92 |
, 'Findexa' => 'findexa' |
| 93 |
, 'NextLinks' => 'findlinks' |
| 94 |
, 'Gais' => 'gaisbo' |
| 95 |
, 'WiseNut' => 'zyborg' |
| 96 |
, 'WhoisSource' => 'surveybot' |
| 97 |
, 'Bloglines' => 'bloglines' |
| 98 |
, 'BlogSearch' => 'blogsearch' |
| 99 |
, 'PubSub' => 'pubsub' |
| 100 |
, 'Syndic8' => 'syndic8' |
| 101 |
, 'RadioUserland' => 'userland' |
| 102 |
, 'Gigabot' => 'gigabot' |
| 103 |
, 'Become.com' => 'become.com' |
| 104 |
, 'Baidu' => 'baiduspider' |
| 105 |
, 'so.com' => '360spider' |
| 106 |
, 'Sogou' => 'spider' |
| 107 |
, 'soso.com' => 'sosospider' |
| 108 |
, 'Yandex' => 'yandex' |
| 109 |
); |
| 110 |
$useragent = $_SERVER['HTTP_USER_AGENT']; |
| 111 |
foreach ($bots as $name => $lookfor) { |
| 112 |
if (stristr($useragent, $lookfor) !== false) { |
| 113 |
$should_count = false; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
if($should_count && (!defined('WP_CACHE') || !WP_CACHE)) { |
| 119 |
update_post_meta($id, 'views', ($post_views + 1)); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
### Function: Calculate Post Views With WP_CACHE Enabled |
| 127 |
add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue'); |
| 128 |
function wp_postview_cache_count_enqueue() { |
| 129 |
global $user_ID, $post; |
| 130 |
if (!wp_is_post_revision($post) && (is_single() || is_page())) { |
| 131 |
$views_options = get_option('views_options'); |
| 132 |
$should_count = false; |
| 133 |
switch(intval($views_options['count'])) { |
| 134 |
case 0: |
| 135 |
$should_count = true; |
| 136 |
break; |
| 137 |
case 1: |
| 138 |
if (empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) { |
| 139 |
$should_count = true; |
| 140 |
} |
| 141 |
break; |
| 142 |
case 2: |
| 143 |
if (intval($user_ID) > 0) { |
| 144 |
$should_count = true; |
| 145 |
} |
| 146 |
break; |
| 147 |
} |
| 148 |
if ($should_count && defined('WP_CACHE') && WP_CACHE) { |
| 149 |
// Enqueue and localize script here |
| 150 |
wp_enqueue_script('wp-postviews-cache', plugins_url('postviews-cache.js', __FILE__), array('jquery'), '1.64', true); |
| 151 |
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))); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
### Function: Determine If Post Views Should Be Displayed (By: David Potter) |
| 158 |
function should_views_be_displayed($views_options = null) { |
| 159 |
if ($views_options == null) { |
| 160 |
$views_options = get_option('views_options'); |
| 161 |
} |
| 162 |
$display_option = 0; |
| 163 |
if (is_home()) { |
| 164 |
if (array_key_exists('display_home', $views_options)) { |
| 165 |
$display_option = $views_options['display_home']; |
| 166 |
} |
| 167 |
} elseif (is_single()) { |
| 168 |
if (array_key_exists('display_single', $views_options)) { |
| 169 |
$display_option = $views_options['display_single']; |
| 170 |
} |
| 171 |
} elseif (is_page()) { |
| 172 |
if (array_key_exists('display_page', $views_options)) { |
| 173 |
$display_option = $views_options['display_page']; |
| 174 |
} |
| 175 |
} elseif (is_archive()) { |
| 176 |
if (array_key_exists('display_archive', $views_options)) { |
| 177 |
$display_option = $views_options['display_archive']; |
| 178 |
} |
| 179 |
} elseif (is_search()) { |
| 180 |
if (array_key_exists('display_search', $views_options)) { |
| 181 |
$display_option = $views_options['display_search']; |
| 182 |
} |
| 183 |
} else { |
| 184 |
if (array_key_exists('display_other', $views_options)) { |
| 185 |
$display_option = $views_options['display_other']; |
| 186 |
} |
| 187 |
} |
| 188 |
return (($display_option == 0) || (($display_option == 1) && is_user_logged_in())); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
### Function: Display The Post Views |
| 193 |
function the_views($display = true, $prefix = '', $postfix = '', $always = false) { |
| 194 |
$post_views = intval(post_custom('views')); |
| 195 |
$views_options = get_option('views_options'); |
| 196 |
if ($always || should_views_be_displayed($views_options)) { |
| 197 |
$output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix; |
| 198 |
if($display) { |
| 199 |
echo apply_filters('the_views', $output); |
| 200 |
} else { |
| 201 |
return apply_filters('the_views', $output); |
| 202 |
} |
| 203 |
} |
| 204 |
elseif (!$display) { |
| 205 |
return ''; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
### Function: Display Least Viewed Page/Post |
| 211 |
if(!function_exists('get_least_viewed')) { |
| 212 |
function get_least_viewed($mode = '', $limit = 10, $chars = 0, $display = true) { |
| 213 |
global $wpdb; |
| 214 |
$views_options = get_option('views_options'); |
| 215 |
$where = ''; |
| 216 |
$temp = ''; |
| 217 |
$output = ''; |
| 218 |
if(!empty($mode) && $mode != 'both') { |
| 219 |
if(is_array($mode)) { |
| 220 |
$mode = implode("','",$mode); |
| 221 |
$where = "post_type IN ('".$mode."')"; |
| 222 |
} else { |
| 223 |
$where = "post_type = '$mode'"; |
| 224 |
} |
| 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 ASC LIMIT $limit"); |
| 229 |
if($most_viewed) { |
| 230 |
foreach ($most_viewed as $post) { |
| 231 |
$post_views = intval($post->views); |
| 232 |
$post_title = get_the_title($post); |
| 233 |
if($chars > 0) { |
| 234 |
$post_title = snippet_text($post_title, $chars); |
| 235 |
} |
| 236 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 237 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 238 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 239 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 240 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 241 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 242 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 243 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 244 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 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 Most Viewed Page/Post |
| 260 |
if(!function_exists('get_most_viewed')) { |
| 261 |
function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) { |
| 262 |
global $wpdb; |
| 263 |
$views_options = get_option('views_options'); |
| 264 |
$where = ''; |
| 265 |
$temp = ''; |
| 266 |
$output = ''; |
| 267 |
if(!empty($mode) && $mode != 'both') { |
| 268 |
if(is_array($mode)) { |
| 269 |
$mode = implode("','",$mode); |
| 270 |
$where = "post_type IN ('".$mode."')"; |
| 271 |
} else { |
| 272 |
$where = "post_type = '$mode'"; |
| 273 |
} |
| 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 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"); |
| 278 |
if($most_viewed) { |
| 279 |
foreach ($most_viewed as $post) { |
| 280 |
$post_views = intval($post->views); |
| 281 |
$post_title = get_the_title($post); |
| 282 |
if($chars > 0) { |
| 283 |
$post_title = snippet_text($post_title, $chars); |
| 284 |
} |
| 285 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 286 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 287 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 288 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 289 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 290 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 291 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 292 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 293 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 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 Leased Viewed Page/Post By Category ID |
| 309 |
if(!function_exists('get_least_viewed_category')) { |
| 310 |
function get_least_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 311 |
global $wpdb; |
| 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 |
if(is_array($mode)) { |
| 323 |
$mode = implode("','",$mode); |
| 324 |
$where = "post_type IN ('".$mode."')"; |
| 325 |
} else { |
| 326 |
$where = "post_type = '$mode'"; |
| 327 |
} |
| 328 |
} else { |
| 329 |
$where = '1=1'; |
| 330 |
} |
| 331 |
$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"); |
| 332 |
if($most_viewed) { |
| 333 |
foreach ($most_viewed as $post) { |
| 334 |
$post_views = intval($post->views); |
| 335 |
$post_title = get_the_title($post); |
| 336 |
if($chars > 0) { |
| 337 |
$post_title = snippet_text($post_title, $chars); |
| 338 |
} |
| 339 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 340 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 341 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 342 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 343 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 344 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 345 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 346 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 347 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 348 |
$output .= $temp; |
| 349 |
} |
| 350 |
} else { |
| 351 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 352 |
} |
| 353 |
if($display) { |
| 354 |
echo $output; |
| 355 |
} else { |
| 356 |
return $output; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
### Function: Display Most Viewed Page/Post By Category ID |
| 363 |
if(!function_exists('get_most_viewed_category')) { |
| 364 |
function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 365 |
global $wpdb; |
| 366 |
$views_options = get_option('views_options'); |
| 367 |
$where = ''; |
| 368 |
$temp = ''; |
| 369 |
$output = ''; |
| 370 |
if(is_array($category_id)) { |
| 371 |
$category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')'; |
| 372 |
} else { |
| 373 |
$category_sql = "$wpdb->term_taxonomy.term_id = $category_id"; |
| 374 |
} |
| 375 |
if(!empty($mode) && $mode != 'both') { |
| 376 |
if(is_array($mode)) { |
| 377 |
$mode = implode("','",$mode); |
| 378 |
$where = "post_type IN ('".$mode."')"; |
| 379 |
} else { |
| 380 |
$where = "post_type = '$mode'"; |
| 381 |
} |
| 382 |
} else { |
| 383 |
$where = '1=1'; |
| 384 |
} |
| 385 |
$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"); |
| 386 |
if($most_viewed) { |
| 387 |
foreach ($most_viewed as $post) { |
| 388 |
$post_views = intval($post->views); |
| 389 |
$post_title = get_the_title($post); |
| 390 |
if($chars > 0) { |
| 391 |
$post_title = snippet_text($post_title, $chars); |
| 392 |
} |
| 393 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 394 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 395 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 396 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 397 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 398 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 399 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 400 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 401 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 402 |
$output .= $temp; |
| 403 |
} |
| 404 |
} else { |
| 405 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 406 |
} |
| 407 |
if($display) { |
| 408 |
echo $output; |
| 409 |
} else { |
| 410 |
return $output; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
### Function: Display Most Viewed Page/Post By Tag ID |
| 417 |
if(!function_exists('get_most_viewed_tag')) { |
| 418 |
function get_most_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 419 |
global $wpdb; |
| 420 |
$views_options = get_option('views_options'); |
| 421 |
$where = ''; |
| 422 |
$temp = ''; |
| 423 |
$output = ''; |
| 424 |
if(is_array($tag_id)) { |
| 425 |
$tag_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $tag_id).')'; |
| 426 |
} else { |
| 427 |
$tag_sql = "$wpdb->term_taxonomy.term_id = $tag_id"; |
| 428 |
} |
| 429 |
if(!empty($mode) && $mode != 'both') { |
| 430 |
if(is_array($mode)) { |
| 431 |
$mode = implode("','",$mode); |
| 432 |
$where = "post_type IN ('".$mode."')"; |
| 433 |
} else { |
| 434 |
$where = "post_type = '$mode'"; |
| 435 |
} |
| 436 |
} else { |
| 437 |
$where = '1=1'; |
| 438 |
} |
| 439 |
$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 = 'post_tag' AND $tag_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit"); |
| 440 |
if($most_viewed) { |
| 441 |
foreach ($most_viewed as $post) { |
| 442 |
$post_views = intval($post->views); |
| 443 |
$post_title = get_the_title($post); |
| 444 |
if($chars > 0) { |
| 445 |
$post_title = snippet_text($post_title, $chars); |
| 446 |
} |
| 447 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 448 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 449 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 450 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 451 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 452 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 453 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 454 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 455 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 456 |
$output .= $temp; |
| 457 |
} |
| 458 |
} else { |
| 459 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 460 |
} |
| 461 |
if($display) { |
| 462 |
echo $output; |
| 463 |
} else { |
| 464 |
return $output; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
### Function: Display Least Viewed Page/Post By Tag ID |
| 471 |
if(!function_exists('get_least_viewed_tag')) { |
| 472 |
function get_least_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) { |
| 473 |
global $wpdb; |
| 474 |
$views_options = get_option('views_options'); |
| 475 |
$where = ''; |
| 476 |
$temp = ''; |
| 477 |
$output = ''; |
| 478 |
if(is_array($tag_id)) { |
| 479 |
$tag_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $tag_id).')'; |
| 480 |
} else { |
| 481 |
$tag_sql = "$wpdb->term_taxonomy.term_id = $tag_id"; |
| 482 |
} |
| 483 |
if(!empty($mode) && $mode != 'both') { |
| 484 |
if(is_array($mode)) { |
| 485 |
$mode = implode("','",$mode); |
| 486 |
$where = "post_type IN ('".$mode."')"; |
| 487 |
} else { |
| 488 |
$where = "post_type = '$mode'"; |
| 489 |
} |
| 490 |
} else { |
| 491 |
$where = '1=1'; |
| 492 |
} |
| 493 |
$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 = 'post_tag' AND $tag_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views ASC LIMIT $limit"); |
| 494 |
if($most_viewed) { |
| 495 |
foreach ($most_viewed as $post) { |
| 496 |
$post_views = intval($post->views); |
| 497 |
$post_title = get_the_title($post); |
| 498 |
if($chars > 0) { |
| 499 |
$post_title = snippet_text($post_title, $chars); |
| 500 |
} |
| 501 |
$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars); |
| 502 |
$temp = stripslashes($views_options['most_viewed_template']); |
| 503 |
$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp); |
| 504 |
$temp = str_replace("%POST_TITLE%", $post_title, $temp); |
| 505 |
$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp); |
| 506 |
$temp = str_replace("%POST_CONTENT%", $post->post_content, $temp); |
| 507 |
$temp = str_replace("%POST_URL%", get_permalink($post), $temp); |
| 508 |
$temp = str_replace("%POST_DATE%", get_the_time(get_option('date_format'), $post), $temp); |
| 509 |
$temp = str_replace("%POST_TIME%", get_the_time(get_option('time_format'), $post), $temp); |
| 510 |
$output .= $temp; |
| 511 |
} |
| 512 |
} else { |
| 513 |
$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n"; |
| 514 |
} |
| 515 |
if($display) { |
| 516 |
echo $output; |
| 517 |
} else { |
| 518 |
return $output; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
### Function: Display Total Views |
| 525 |
if(!function_exists('get_totalviews')) { |
| 526 |
function get_totalviews($display = true) { |
| 527 |
global $wpdb; |
| 528 |
$total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'")); |
| 529 |
if($display) { |
| 530 |
echo number_format_i18n($total_views); |
| 531 |
} else { |
| 532 |
return $total_views; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
### Function: Snippet Text |
| 539 |
if(!function_exists('snippet_text')) { |
| 540 |
function snippet_text($text, $length = 0) { |
| 541 |
if (defined('MB_OVERLOAD_STRING')) { |
| 542 |
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); |
| 543 |
if (mb_strlen($text) > $length) { |
| 544 |
return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; |
| 545 |
} else { |
| 546 |
return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); |
| 547 |
} |
| 548 |
} else { |
| 549 |
$text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); |
| 550 |
if (strlen($text) > $length) { |
| 551 |
return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; |
| 552 |
} else { |
| 553 |
return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected |
| 561 |
function views_post_excerpt($post_excerpt, $post_content, $post_password, $chars = 200) { |
| 562 |
if(!empty($post_password)) { |
| 563 |
if(!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post_password) { |
| 564 |
return __('There is no excerpt because this is a protected post.', 'wp-postviews'); |
| 565 |
} |
| 566 |
} |
| 567 |
if(empty($post_excerpt)) { |
| 568 |
return snippet_text(strip_tags($post_content), $chars); |
| 569 |
} else { |
| 570 |
return $post_excerpt; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
### Function: Modify Default WordPress Listing To Make It Sorted By Post Views |
| 576 |
function views_fields($content) { |
| 577 |
global $wpdb; |
| 578 |
$content .= ", ($wpdb->postmeta.meta_value+0) AS views"; |
| 579 |
return $content; |
| 580 |
} |
| 581 |
function views_join($content) { |
| 582 |
global $wpdb; |
| 583 |
$content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID"; |
| 584 |
return $content; |
| 585 |
} |
| 586 |
function views_where($content) { |
| 587 |
global $wpdb; |
| 588 |
$content .= " AND $wpdb->postmeta.meta_key = 'views'"; |
| 589 |
return $content; |
| 590 |
} |
| 591 |
function views_orderby($content) { |
| 592 |
$orderby = trim(addslashes(get_query_var('v_orderby'))); |
| 593 |
if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) { |
| 594 |
$orderby = 'desc'; |
| 595 |
} |
| 596 |
$content = " views $orderby"; |
| 597 |
return $content; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
### Function: Add Views Custom Fields |
| 602 |
add_action('publish_post', 'add_views_fields'); |
| 603 |
add_action('publish_page', 'add_views_fields'); |
| 604 |
function add_views_fields($post_ID) { |
| 605 |
global $wpdb; |
| 606 |
if(!wp_is_post_revision($post_ID)) { |
| 607 |
add_post_meta($post_ID, 'views', 0, true); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
### Function: Delete Views Custom Fields |
| 613 |
add_action('delete_post', 'delete_views_fields'); |
| 614 |
function delete_views_fields($post_ID) { |
| 615 |
global $wpdb; |
| 616 |
if(!wp_is_post_revision($post_ID)) { |
| 617 |
delete_post_meta($post_ID, 'views'); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
### Function: Views Public Variables |
| 623 |
add_filter('query_vars', 'views_variables'); |
| 624 |
function views_variables($public_query_vars) { |
| 625 |
$public_query_vars[] = 'v_sortby'; |
| 626 |
$public_query_vars[] = 'v_orderby'; |
| 627 |
return $public_query_vars; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
### Function: Sort Views Posts |
| 632 |
add_action('pre_get_posts', 'views_sorting'); |
| 633 |
function views_sorting($local_wp_query) { |
| 634 |
if($local_wp_query->get('v_sortby') == 'views') { |
| 635 |
add_filter('posts_fields', 'views_fields'); |
| 636 |
add_filter('posts_join', 'views_join'); |
| 637 |
add_filter('posts_where', 'views_where'); |
| 638 |
add_filter('posts_orderby', 'views_orderby'); |
| 639 |
} else { |
| 640 |
remove_filter('posts_fields', 'views_fields'); |
| 641 |
remove_filter('posts_join', 'views_join'); |
| 642 |
remove_filter('posts_where', 'views_where'); |
| 643 |
remove_filter('posts_orderby', 'views_orderby'); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
### Function: Plug Into WP-Stats |
| 649 |
add_action('wp','postviews_wp_stats'); |
| 650 |
function postviews_wp_stats() { |
| 651 |
if(function_exists('stats_page')) { |
| 652 |
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')) { |
| 653 |
add_filter('wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats'); |
| 654 |
add_filter('wp_stats_page_admin_most', 'postviews_page_admin_most_stats'); |
| 655 |
add_filter('wp_stats_page_plugins', 'postviews_page_general_stats'); |
| 656 |
add_filter('wp_stats_page_most', 'postviews_page_most_stats'); |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
### Function: Add WP-PostViews General Stats To WP-Stats Page Options |
| 663 |
function postviews_page_admin_general_stats($content) { |
| 664 |
$stats_display = get_option('stats_display'); |
| 665 |
if($stats_display['views'] == 1) { |
| 666 |
$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"; |
| 667 |
} else { |
| 668 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n"; |
| 669 |
} |
| 670 |
return $content; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options |
| 675 |
function postviews_page_admin_most_stats($content) { |
| 676 |
$stats_display = get_option('stats_display'); |
| 677 |
$stats_mostlimit = intval(get_option('stats_mostlimit')); |
| 678 |
if($stats_display['viewed_most_post'] == 1) { |
| 679 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" checked="checked" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 680 |
} else { |
| 681 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 682 |
} |
| 683 |
if($stats_display['viewed_most_page'] == 1) { |
| 684 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" checked="checked" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 685 |
} else { |
| 686 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n"; |
| 687 |
} |
| 688 |
return $content; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
### Function: Add WP-PostViews General Stats To WP-Stats Page |
| 693 |
function postviews_page_general_stats($content) { |
| 694 |
$stats_display = get_option('stats_display'); |
| 695 |
if($stats_display['views'] == 1) { |
| 696 |
$content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n"; |
| 697 |
$content .= '<ul>'."\n"; |
| 698 |
$content .= '<li>'.sprintf(_n('<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"; |
| 699 |
$content .= '</ul>'."\n"; |
| 700 |
} |
| 701 |
return $content; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page |
| 706 |
function postviews_page_most_stats($content) { |
| 707 |
$stats_display = get_option('stats_display'); |
| 708 |
$stats_mostlimit = intval(get_option('stats_mostlimit')); |
| 709 |
if($stats_display['viewed_most_post'] == 1) { |
| 710 |
$content .= '<p><strong>'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n"; |
| 711 |
$content .= '<ul>'."\n"; |
| 712 |
$content .= get_most_viewed('post', $stats_mostlimit, 0, false); |
| 713 |
$content .= '</ul>'."\n"; |
| 714 |
} |
| 715 |
if($stats_display['viewed_most_page'] == 1) { |
| 716 |
$content .= '<p><strong>'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n"; |
| 717 |
$content .= '<ul>'."\n"; |
| 718 |
$content .= get_most_viewed('page', $stats_mostlimit, 0, false); |
| 719 |
$content .= '</ul>'."\n"; |
| 720 |
} |
| 721 |
return $content; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
### Function: Increment Post Views |
| 726 |
add_action('wp_ajax_postviews', 'increment_views'); |
| 727 |
add_action('wp_ajax_nopriv_postviews', 'increment_views'); |
| 728 |
function increment_views() { |
| 729 |
global $wpdb; |
| 730 |
if(!empty($_GET['postviews_id'])) |
| 731 |
{ |
| 732 |
$post_id = intval($_GET['postviews_id']); |
| 733 |
if($post_id > 0 && defined('WP_CACHE') && WP_CACHE) { |
| 734 |
$post_views = get_post_custom($post_id); |
| 735 |
$post_views = intval($post_views['views'][0]); |
| 736 |
update_post_meta($post_id, 'views', ($post_views + 1)); |
| 737 |
echo ($post_views + 1); |
| 738 |
} |
| 739 |
} |
| 740 |
exit(); |
| 741 |
} |
| 742 |
|
| 743 |
### Function Show Post Views Column in WP-Admin |
| 744 |
add_action('manage_posts_custom_column', 'add_postviews_column_content'); |
| 745 |
add_filter('manage_posts_columns', 'add_postviews_column'); |
| 746 |
add_action('manage_pages_custom_column', 'add_postviews_column_content'); |
| 747 |
add_filter('manage_pages_columns', 'add_postviews_column'); |
| 748 |
function add_postviews_column($defaults) { |
| 749 |
$defaults['views'] = __( 'Views', 'wp-postviews' ); |
| 750 |
return $defaults; |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
### Functions Fill In The Views Count |
| 755 |
function add_postviews_column_content($column_name) { |
| 756 |
if($column_name == 'views') { |
| 757 |
if(function_exists('the_views')) { the_views(true, '', '', true); } |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
### Function Sort Columns |
| 763 |
add_filter('manage_edit-post_sortable_columns', 'sort_postviews_column'); |
| 764 |
add_filter('manage_edit-page_sortable_columns', 'sort_postviews_column'); |
| 765 |
function sort_postviews_column($defaults) |
| 766 |
{ |
| 767 |
$defaults['views'] = 'views'; |
| 768 |
return $defaults; |
| 769 |
} |
| 770 |
add_action('pre_get_posts', 'sort_postviews'); |
| 771 |
function sort_postviews($query) { |
| 772 |
if(!is_admin()) |
| 773 |
return; |
| 774 |
$orderby = $query->get('orderby'); |
| 775 |
if('views' == $orderby) { |
| 776 |
$query->set('meta_key', 'views'); |
| 777 |
$query->set('orderby', 'meta_value_num'); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
### Class: WP-PostViews Widget |
| 783 |
class WP_Widget_PostViews extends WP_Widget { |
| 784 |
// Constructor |
| 785 |
function WP_Widget_PostViews() { |
| 786 |
$widget_ops = array('description' => __('WP-PostViews views statistics', 'wp-postviews')); |
| 787 |
$this->WP_Widget('views', __('Views', 'wp-postviews'), $widget_ops); |
| 788 |
} |
| 789 |
|
| 790 |
// Display Widget |
| 791 |
function widget($args, $instance) { |
| 792 |
extract($args); |
| 793 |
$title = apply_filters('widget_title', esc_attr($instance['title'])); |
| 794 |
$type = esc_attr($instance['type']); |
| 795 |
$mode = esc_attr($instance['mode']); |
| 796 |
$limit = intval($instance['limit']); |
| 797 |
$chars = intval($instance['chars']); |
| 798 |
$cat_ids = explode(',', esc_attr($instance['cat_ids'])); |
| 799 |
echo $before_widget.$before_title.$title.$after_title; |
| 800 |
echo '<ul>'."\n"; |
| 801 |
switch($type) { |
| 802 |
case 'least_viewed': |
| 803 |
get_least_viewed($mode, $limit, $chars); |
| 804 |
break; |
| 805 |
case 'most_viewed': |
| 806 |
get_most_viewed($mode, $limit, $chars); |
| 807 |
break; |
| 808 |
case 'most_viewed_category': |
| 809 |
get_most_viewed_category($cat_ids, $mode, $limit, $chars); |
| 810 |
break; |
| 811 |
case 'least_viewed_category': |
| 812 |
get_least_viewed_category($cat_ids, $mode, $limit, $chars); |
| 813 |
break; |
| 814 |
} |
| 815 |
echo '</ul>'."\n"; |
| 816 |
echo $after_widget; |
| 817 |
} |
| 818 |
|
| 819 |
// When Widget Control Form Is Posted |
| 820 |
function update($new_instance, $old_instance) { |
| 821 |
if (!isset($new_instance['submit'])) { |
| 822 |
return false; |
| 823 |
} |
| 824 |
$instance = $old_instance; |
| 825 |
$instance['title'] = strip_tags($new_instance['title']); |
| 826 |
$instance['type'] = strip_tags($new_instance['type']); |
| 827 |
$instance['mode'] = strip_tags($new_instance['mode']); |
| 828 |
$instance['limit'] = intval($new_instance['limit']); |
| 829 |
$instance['chars'] = intval($new_instance['chars']); |
| 830 |
$instance['cat_ids'] = strip_tags($new_instance['cat_ids']); |
| 831 |
return $instance; |
| 832 |
} |
| 833 |
|
| 834 |
// DIsplay Widget Control Form |
| 835 |
function form($instance) { |
| 836 |
global $wpdb; |
| 837 |
$instance = wp_parse_args((array) $instance, array('title' => __('Views', 'wp-postviews'), 'type' => 'most_viewed', 'mode' => 'both', 'limit' => 10, 'chars' => 200, 'cat_ids' => '0')); |
| 838 |
$title = esc_attr($instance['title']); |
| 839 |
$type = esc_attr($instance['type']); |
| 840 |
$mode = esc_attr($instance['mode']); |
| 841 |
$limit = intval($instance['limit']); |
| 842 |
$chars = intval($instance['chars']); |
| 843 |
$cat_ids = esc_attr($instance['cat_ids']); |
| 844 |
?> |
| 845 |
<p> |
| 846 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label> |
| 847 |
</p> |
| 848 |
<p> |
| 849 |
<label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postviews'); ?> |
| 850 |
<select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat"> |
| 851 |
<option value="least_viewed"<?php selected('least_viewed', $type); ?>><?php _e('Least Viewed', 'wp-postviews'); ?></option> |
| 852 |
<option value="least_viewed_category"<?php selected('least_viewed_category', $type); ?>><?php _e('Least Viewed By Category', 'wp-postviews'); ?></option> |
| 853 |
<optgroup> </optgroup> |
| 854 |
<option value="most_viewed"<?php selected('most_viewed', $type); ?>><?php _e('Most Viewed', 'wp-postviews'); ?></option> |
| 855 |
<option value="most_viewed_category"<?php selected('most_viewed_category', $type); ?>><?php _e('Most Viewed By Category', 'wp-postviews'); ?></option> |
| 856 |
</select> |
| 857 |
</label> |
| 858 |
</p> |
| 859 |
<p> |
| 860 |
<label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Views From:', 'wp-postviews'); ?> |
| 861 |
<select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat"> |
| 862 |
<option value="both"<?php selected('both', $mode); ?>><?php _e('Posts & Pages', 'wp-postviews'); ?></option> |
| 863 |
<option value="post"<?php selected('post', $mode); ?>><?php _e('Posts Only', 'wp-postviews'); ?></option> |
| 864 |
<option value="page"<?php selected('page', $mode); ?>><?php _e('Pages Only', 'wp-postviews'); ?></option> |
| 865 |
</select> |
| 866 |
</label> |
| 867 |
</p> |
| 868 |
<p> |
| 869 |
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('No. Of Records To Show:', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label> |
| 870 |
</p> |
| 871 |
<p> |
| 872 |
<label for="<?php echo $this->get_field_id('chars'); ?>"><?php _e('Maximum Post Title Length (Characters):', 'wp-postviews'); ?> <input class="widefat" id="<?php echo $this->get_field_id('chars'); ?>" name="<?php echo $this->get_field_name('chars'); ?>" type="text" value="<?php echo $chars; ?>" /></label><br /> |
| 873 |
<small><?php _e('<strong>0</strong> to disable.', 'wp-postviews'); ?></small> |
| 874 |
</p> |
| 875 |
<p> |
| 876 |
<label for="<?php echo $this->get_field_id('cat_ids'); ?>"><?php _e('Category IDs:', 'wp-postviews'); ?> <span style="color: red;">*</span> <input class="widefat" id="<?php echo $this->get_field_id('cat_ids'); ?>" name="<?php echo $this->get_field_name('cat_ids'); ?>" type="text" value="<?php echo $cat_ids; ?>" /></label><br /> |
| 877 |
<small><?php _e('Seperate mutiple categories with commas.', 'wp-postviews'); ?></small> |
| 878 |
</p> |
| 879 |
<p style="color: red;"> |
| 880 |
<small><?php _e('* If you are not using any category statistics, you can ignore it.', 'wp-postviews'); ?></small> |
| 881 |
<p> |
| 882 |
<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" /> |
| 883 |
<?php |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
### Function: Init WP-PostViews Widget |
| 889 |
add_action( 'widgets_init', 'widget_views_init' ); |
| 890 |
function widget_views_init() { |
| 891 |
register_widget( 'WP_Widget_PostViews' ); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
### Function: Post Views Options |
| 896 |
register_activation_hook( __FILE__, 'views_activation' ); |
| 897 |
function views_activation( $network_wide ) { |
| 898 |
// Add Options |
| 899 |
$option_name = 'views_options'; |
| 900 |
$option = array( |
| 901 |
'count' => 1 |
| 902 |
, 'exclude_bots' => 0 |
| 903 |
, 'display_home' => 0 |
| 904 |
, 'display_single' => 0 |
| 905 |
, 'display_page' => 0 |
| 906 |
, 'display_archive' => 0 |
| 907 |
, 'display_search' => 0 |
| 908 |
, 'display_other' => 0 |
| 909 |
, 'template' => __('%VIEW_COUNT% views', 'wp-postviews') |
| 910 |
, 'most_viewed_template' => '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>' |
| 911 |
); |
| 912 |
|
| 913 |
if ( is_multisite() && $network_wide ) |
| 914 |
{ |
| 915 |
$ms_sites = wp_get_sites(); |
| 916 |
|
| 917 |
if( 0 < sizeof( $ms_sites ) ) |
| 918 |
{ |
| 919 |
foreach ( $ms_sites as $ms_site ) |
| 920 |
{ |
| 921 |
switch_to_blog( $ms_site['blog_id'] ); |
| 922 |
add_option( $option_name, $option ); |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
restore_current_blog(); |
| 927 |
} |
| 928 |
else |
| 929 |
{ |
| 930 |
add_option( $option_name, $option ); |
| 931 |
} |
| 932 |
} |
| 933 |
?> |