PluginProbe
WP-PostViews / 1.76.1
WP-PostViews v1.76.1
2.0.1 2.0.0 1.01 1.02 1.10 1.11 1.20 1.30 1.31 1.40 1.50 1.66 1.67 1.68 1.69 1.70 1.71 1.72 1.73 1.74 1.75 1.76 1.76.1 1.77 1.78 All 29 releases
wp-postviews / wp-postviews.php

wp-postviews.php in WP-PostViews 1.76.1, at wp-postviews.php

1,022 lines 37.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-PostViews
4 Plugin URI: https://lesterchan.net/portfolio/programming/php/
5 Description: Enables you to display how many times a post/page had been viewed.
6 Version: 1.76.1
7 Author: Lester 'GaMerZ' Chan
8 Author URI: https://lesterchan.net
9 Text Domain: wp-postviews
10 */
11
12
13 /*
14 Copyright 2017 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 ) && ! is_preview() ) {
56 if ( is_single() || is_page() ) {
57 $id = (int) $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 }
62 $should_count = false;
63 switch( (int) $views_options['count'] ) {
64 case 0:
65 $should_count = true;
66 break;
67 case 1:
68 if( empty( $_COOKIE[ USER_COOKIE ] ) && (int) $user_ID === 0 ) {
69 $should_count = true;
70 }
71 break;
72 case 2:
73 if( (int) $user_ID > 0 ) {
74 $should_count = true;
75 }
76 break;
77 }
78 if ( isset( $views_options['exclude_bots'] ) && (int) $views_options['exclude_bots'] === 1 ) {
79 $bots = array(
80 'Google Bot' => 'google'
81 , 'MSN' => 'msnbot'
82 , 'Alex' => 'ia_archiver'
83 , 'Lycos' => 'lycos'
84 , 'Ask Jeeves' => 'jeeves'
85 , 'Altavista' => 'scooter'
86 , 'AllTheWeb' => 'fast-webcrawler'
87 , 'Inktomi' => 'slurp@inktomi'
88 , 'Turnitin.com' => 'turnitinbot'
89 , 'Technorati' => 'technorati'
90 , 'Yahoo' => 'yahoo'
91 , 'Findexa' => 'findexa'
92 , 'NextLinks' => 'findlinks'
93 , 'Gais' => 'gaisbo'
94 , 'WiseNut' => 'zyborg'
95 , 'WhoisSource' => 'surveybot'
96 , 'Bloglines' => 'bloglines'
97 , 'BlogSearch' => 'blogsearch'
98 , 'PubSub' => 'pubsub'
99 , 'Syndic8' => 'syndic8'
100 , 'RadioUserland' => 'userland'
101 , 'Gigabot' => 'gigabot'
102 , 'Become.com' => 'become.com'
103 , 'Baidu' => 'baiduspider'
104 , 'so.com' => '360spider'
105 , 'Sogou' => 'spider'
106 , 'soso.com' => 'sosospider'
107 , 'Yandex' => 'yandex'
108 );
109 $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
110 foreach ( $bots as $name => $lookfor ) {
111 if ( ! empty( $useragent ) && ( false !== stripos( $useragent, $lookfor ) ) ) {
112 $should_count = false;
113 break;
114 }
115 }
116 }
117 $should_count = apply_filters( 'postviews_should_count', $should_count, $id );
118 if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) {
119 update_post_meta( $id, 'views', $post_views + 1 );
120 do_action( 'postviews_increment_views', $post_views + 1 );
121 }
122 }
123 }
124 }
125
126
127 ### Function: Calculate Post Views With WP_CACHE Enabled
128 add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue');
129 function wp_postview_cache_count_enqueue() {
130 global $user_ID, $post;
131
132 if ( !defined( 'WP_CACHE' ) || !WP_CACHE ) {
133 return;
134 }
135
136 $views_options = get_option( 'views_options' );
137
138 if ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) {
139 return;
140 }
141
142 if ( !wp_is_post_revision( $post ) && ( is_single() || is_page() ) ) {
143 $should_count = false;
144 switch( (int) $views_options['count'] ) {
145 case 0:
146 $should_count = true;
147 break;
148 case 1:
149 if ( empty( $_COOKIE[USER_COOKIE] ) && (int) $user_ID === 0) {
150 $should_count = true;
151 }
152 break;
153 case 2:
154 if ( (int) $user_ID > 0 ) {
155 $should_count = true;
156 }
157 break;
158 }
159
160 $should_count = apply_filters( 'postviews_should_count', $should_count, (int) $post->ID );
161 if ( $should_count ) {
162 wp_enqueue_script( 'wp-postviews-cache', plugins_url( 'postviews-cache.js', __FILE__ ), array( 'jquery' ), '1.68', true );
163 wp_localize_script( 'wp-postviews-cache', 'viewsCacheL10n', array( 'admin_ajax_url' => admin_url( 'admin-ajax.php' ), 'post_id' => (int) $post->ID ) );
164 }
165 }
166 }
167
168
169 ### Function: Determine If Post Views Should Be Displayed (By: David Potter)
170 function should_views_be_displayed($views_options = null) {
171 if ($views_options == null) {
172 $views_options = get_option('views_options');
173 }
174 $display_option = 0;
175 if (is_home()) {
176 if (array_key_exists('display_home', $views_options)) {
177 $display_option = $views_options['display_home'];
178 }
179 } elseif (is_single()) {
180 if (array_key_exists('display_single', $views_options)) {
181 $display_option = $views_options['display_single'];
182 }
183 } elseif (is_page()) {
184 if (array_key_exists('display_page', $views_options)) {
185 $display_option = $views_options['display_page'];
186 }
187 } elseif (is_archive()) {
188 if (array_key_exists('display_archive', $views_options)) {
189 $display_option = $views_options['display_archive'];
190 }
191 } elseif (is_search()) {
192 if (array_key_exists('display_search', $views_options)) {
193 $display_option = $views_options['display_search'];
194 }
195 } else {
196 if (array_key_exists('display_other', $views_options)) {
197 $display_option = $views_options['display_other'];
198 }
199 }
200 return (($display_option == 0) || (($display_option == 1) && is_user_logged_in()));
201 }
202
203
204 ### Function: Display The Post Views
205 function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
206 $post_views = (int) get_post_meta( get_the_ID(), 'views', true );
207 $views_options = get_option('views_options');
208 if ($always || should_views_be_displayed($views_options)) {
209 $output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ).$postfix;
210 if($display) {
211 echo apply_filters('the_views', $output);
212 } else {
213 return apply_filters('the_views', $output);
214 }
215 }
216 elseif (!$display) {
217 return '';
218 }
219 }
220
221 ### Function: Short Code For Inserting Views Into Posts
222 add_shortcode( 'views', 'views_shortcode' );
223 function views_shortcode( $atts ) {
224 $attributes = shortcode_atts( array( 'id' => 0 ), $atts );
225 $id = (int) $attributes['id'];
226 if( $id === 0) {
227 $id = get_the_ID();
228 }
229 $views_options = get_option( 'views_options' );
230 $post_views = (int) get_post_meta( $id, 'views', true );
231 $output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) );
232
233 return apply_filters( 'the_views', $output );
234 }
235
236
237 ### Function: Display Least Viewed Page/Post
238 if ( ! function_exists( 'get_least_viewed' ) ) {
239 function get_least_viewed( $mode = '', $limit = 10, $chars = 0, $display = true ) {
240 $views_options = get_option( 'views_options' );
241 $output = '';
242
243 $least_viewed = new WP_Query( array(
244 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
245 'posts_per_page' => $limit,
246 'orderby' => 'meta_value_num',
247 'order' => 'asc',
248 'meta_key' => 'views',
249 ) );
250 if ( $least_viewed->have_posts() ) {
251 while ( $least_viewed->have_posts() ) {
252 $least_viewed->the_post();
253
254 // Post Views.
255 $post_views = get_post_meta( get_the_ID(), 'views', true );
256
257 // Post Title.
258 $post_title = get_the_title();
259 if ( $chars > 0 ) {
260 $post_title = snippet_text( $post_title, $chars );
261 }
262
263 // Post First Category.
264 $categories = get_the_category();
265 $post_category_id = 0;
266 if ( ! empty( $categories ) ) {
267 $post_category_id = $categories[0]->term_id;
268 }
269
270 $temp = stripslashes( $views_options['most_viewed_template'] );
271 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
272 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
273 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
274 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
275 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
276 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
277 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
278 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
279 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
280 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
281 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
282 $output .= $temp;
283 }
284
285 wp_reset_postdata();
286 } else {
287 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
288 }
289
290 if( $display ) {
291 echo $output;
292 } else {
293 return $output;
294 }
295 }
296 }
297
298
299 ### Function: Display Most Viewed Page/Post
300 if ( ! function_exists( 'get_most_viewed' ) ) {
301 function get_most_viewed( $mode = '', $limit = 10, $chars = 0, $display = true ) {
302 $views_options = get_option( 'views_options' );
303 $output = '';
304
305 $most_viewed = new WP_Query( array(
306 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
307 'posts_per_page' => $limit,
308 'orderby' => 'meta_value_num',
309 'order' => 'desc',
310 'meta_key' => 'views',
311 ) );
312 if ( $most_viewed->have_posts() ) {
313 while ( $most_viewed->have_posts() ) {
314 $most_viewed->the_post();
315
316 // Post Views.
317 $post_views = get_post_meta( get_the_ID(), 'views', true );
318
319 // Post Title.
320 $post_title = get_the_title();
321 if ( $chars > 0 ) {
322 $post_title = snippet_text( $post_title, $chars );
323 }
324
325 // Post First Category.
326 $categories = get_the_category();
327 $post_category_id = 0;
328 if ( ! empty( $categories ) ) {
329 $post_category_id = $categories[0]->term_id;
330 }
331
332 $temp = stripslashes( $views_options['most_viewed_template'] );
333 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
334 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
335 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
336 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
337 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
338 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
339 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
340 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
341 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
342 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
343 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
344 $output .= $temp;
345 }
346
347 wp_reset_postdata();
348 } else {
349 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
350 }
351
352 if( $display ) {
353 echo $output;
354 } else {
355 return $output;
356 }
357 }
358 }
359
360
361 ### Function: Display Least Viewed Page/Post By Category ID
362 if ( ! function_exists( 'get_least_viewed_category' ) ) {
363 function get_least_viewed_category( $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) {
364 $views_options = get_option( 'views_options' );
365 $output = '';
366
367 $least_viewed = new WP_Query( array(
368 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
369 'posts_per_page' => $limit,
370 'category__in' => (array) $category_id,
371 'orderby' => 'meta_value_num',
372 'order' => 'asc',
373 'meta_key' => 'views',
374 ) );
375 if ( $least_viewed->have_posts() ) {
376 while ( $least_viewed->have_posts() ) {
377 $least_viewed->the_post();
378
379 // Post Views.
380 $post_views = get_post_meta( get_the_ID(), 'views', true );
381
382 // Post Title.
383 $post_title = get_the_title();
384 if ( $chars > 0 ) {
385 $post_title = snippet_text( $post_title, $chars );
386 }
387
388 // Post First Category.
389 $categories = get_the_category();
390 $post_category_id = 0;
391 if ( ! empty( $categories ) ) {
392 $post_category_id = $categories[0]->term_id;
393 }
394
395 $temp = stripslashes( $views_options['most_viewed_template'] );
396 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
397 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
398 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
399 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
400 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
401 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
402 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
403 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
404 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
405 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
406 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
407 $output .= $temp;
408 }
409
410 wp_reset_postdata();
411 } else {
412 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
413 }
414
415 if($display) {
416 echo $output;
417 } else {
418 return $output;
419 }
420 }
421 }
422
423
424 ### Function: Display Most Viewed Page/Post By Category ID
425 if ( ! function_exists( 'get_most_viewed_category' ) ) {
426 function get_most_viewed_category( $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) {
427 $views_options = get_option( 'views_options' );
428 $output = '';
429
430 $most_viewed = new WP_Query( array(
431 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
432 'posts_per_page' => $limit,
433 'category__in' => (array) $category_id,
434 'orderby' => 'meta_value_num',
435 'order' => 'desc',
436 'meta_key' => 'views',
437 ) );
438 if ( $most_viewed->have_posts() ) {
439 while ( $most_viewed->have_posts() ) {
440 $most_viewed->the_post();
441
442 // Post Views.
443 $post_views = get_post_meta( get_the_ID(), 'views', true );
444
445 // Post Title.
446 $post_title = get_the_title();
447 if ( $chars > 0 ) {
448 $post_title = snippet_text( $post_title, $chars );
449 }
450
451 // Post First Category.
452 $categories = get_the_category();
453 $post_category_id = 0;
454 if ( ! empty( $categories ) ) {
455 $post_category_id = $categories[0]->term_id;
456 }
457
458 $temp = stripslashes( $views_options['most_viewed_template'] );
459 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
460 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
461 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
462 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
463 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
464 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
465 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
466 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
467 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
468 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
469 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
470 $output .= $temp;
471 }
472
473 wp_reset_postdata();
474 } else {
475 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
476 }
477
478 if ( $display ) {
479 echo $output;
480 } else {
481 return $output;
482 }
483 }
484 }
485
486 ### Function: Display Least Viewed Page/Post By Tag ID
487 if ( ! function_exists( 'get_least_viewed_tag' ) ) {
488 function get_least_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) {
489 $views_options = get_option( 'views_options' );
490 $output = '';
491
492 $least_viewed = new WP_Query( array(
493 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
494 'posts_per_page' => $limit,
495 'tag__in' => (array) $tag_id,
496 'orderby' => 'meta_value_num',
497 'order' => 'asc',
498 'meta_key' => 'views',
499 ) );
500 if ( $least_viewed->have_posts() ) {
501 while ( $least_viewed->have_posts() ) {
502 $least_viewed->the_post();
503
504 // Post Views.
505 $post_views = get_post_meta( get_the_ID(), 'views', true );
506
507 // Post Title.
508 $post_title = get_the_title();
509 if ( $chars > 0 ) {
510 $post_title = snippet_text( $post_title, $chars );
511 }
512
513 // Post First Category.
514 $categories = get_the_category();
515 $post_category_id = 0;
516 if ( ! empty( $categories ) ) {
517 $post_category_id = $categories[0]->term_id;
518 }
519
520 $temp = stripslashes( $views_options['most_viewed_template'] );
521 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
522 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
523 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
524 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
525 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
526 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
527 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
528 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
529 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
530 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
531 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
532 $output .= $temp;
533 }
534
535 wp_reset_postdata();
536 } else {
537 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
538 }
539
540 if ( $display ) {
541 echo $output;
542 } else {
543 return $output;
544 }
545 }
546 }
547
548
549 ### Function: Display Most Viewed Page/Post By Tag ID
550 if ( ! function_exists( 'get_most_viewed_tag' ) ) {
551 function get_most_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) {
552 $views_options = get_option( 'views_options' );
553 $output = '';
554
555 $most_viewed = new WP_Query( array(
556 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode,
557 'posts_per_page' => $limit,
558 'tag__in' => (array) $tag_id,
559 'orderby' => 'meta_value_num',
560 'order' => 'desc',
561 'meta_key' => 'views',
562 ) );
563 if ( $most_viewed->have_posts() ) {
564 while ( $most_viewed->have_posts() ) {
565 $most_viewed->the_post();
566
567 // Post Views.
568 $post_views = get_post_meta( get_the_ID(), 'views', true );
569
570 // Post Title.
571 $post_title = get_the_title();
572 if ( $chars > 0 ) {
573 $post_title = snippet_text( $post_title, $chars );
574 }
575
576 // Post First Category.
577 $categories = get_the_category();
578 $post_category_id = 0;
579 if ( ! empty( $categories ) ) {
580 $post_category_id = $categories[0]->term_id;
581 }
582
583 $temp = stripslashes( $views_options['most_viewed_template'] );
584 $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp );
585 $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp );
586 $temp = str_replace( '%POST_TITLE%', $post_title, $temp );
587 $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp );
588 $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp );
589 $temp = str_replace( '%POST_URL%', get_permalink(), $temp );
590 $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp );
591 $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp );
592 $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp);
593 $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp );
594 $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp );
595 $output .= $temp;
596 }
597
598 wp_reset_postdata();
599 } else {
600 $output = '<li>' . __( 'N/A', 'wp-postviews' ) . '</li>' . "\n";
601 }
602
603 if($display) {
604 echo $output;
605 } else {
606 return $output;
607 }
608 }
609 }
610
611
612 ### Function: Display Total Views
613 if(!function_exists('get_totalviews')) {
614 function get_totalviews($display = true) {
615 global $wpdb;
616 $total_views = (int) $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'" );
617 if($display) {
618 echo number_format_i18n($total_views);
619 } else {
620 return $total_views;
621 }
622 }
623 }
624
625
626 ### Function: Snippet Text
627 if(!function_exists('snippet_text')) {
628 function snippet_text($text, $length = 0) {
629 if (defined('MB_OVERLOAD_STRING')) {
630 $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
631 if (mb_strlen($text) > $length) {
632 return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
633 } else {
634 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
635 }
636 } else {
637 $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
638 if (strlen($text) > $length) {
639 return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
640 } else {
641 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
642 }
643 }
644 }
645 }
646
647
648 ### Function: Modify Default WordPress Listing To Make It Sorted By Post Views
649 function views_fields($content) {
650 global $wpdb;
651 $content .= ", ($wpdb->postmeta.meta_value+0) AS views";
652 return $content;
653 }
654 function views_join($content) {
655 global $wpdb;
656 $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
657 return $content;
658 }
659 function views_where($content) {
660 global $wpdb;
661 $content .= " AND $wpdb->postmeta.meta_key = 'views'";
662 return $content;
663 }
664 function views_orderby($content) {
665 $orderby = trim(addslashes(get_query_var('v_orderby')));
666 if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
667 $orderby = 'desc';
668 }
669 $content = " views $orderby";
670 return $content;
671 }
672
673
674 ### Function: Add Views Custom Fields
675 add_action('publish_post', 'add_views_fields');
676 add_action('publish_page', 'add_views_fields');
677 function add_views_fields($post_ID) {
678 global $wpdb;
679 if(!wp_is_post_revision($post_ID)) {
680 add_post_meta($post_ID, 'views', 0, true);
681 }
682 }
683
684
685 ### Function: Views Public Variables
686 add_filter('query_vars', 'views_variables');
687 function views_variables($public_query_vars) {
688 $public_query_vars[] = 'v_sortby';
689 $public_query_vars[] = 'v_orderby';
690 return $public_query_vars;
691 }
692
693
694 ### Function: Sort Views Posts
695 add_action('pre_get_posts', 'views_sorting');
696 function views_sorting($local_wp_query) {
697 if($local_wp_query->get('v_sortby') == 'views') {
698 add_filter('posts_fields', 'views_fields');
699 add_filter('posts_join', 'views_join');
700 add_filter('posts_where', 'views_where');
701 add_filter('posts_orderby', 'views_orderby');
702 } else {
703 remove_filter('posts_fields', 'views_fields');
704 remove_filter('posts_join', 'views_join');
705 remove_filter('posts_where', 'views_where');
706 remove_filter('posts_orderby', 'views_orderby');
707 }
708 }
709
710
711 ### Function: Plug Into WP-Stats
712 add_action( 'plugins_loaded', 'postviews_wp_stats' );
713 function postviews_wp_stats() {
714 add_filter( 'wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats' );
715 add_filter( 'wp_stats_page_admin_most', 'postviews_page_admin_most_stats' );
716 add_filter( 'wp_stats_page_plugins', 'postviews_page_general_stats' );
717 add_filter( 'wp_stats_page_most', 'postviews_page_most_stats' );
718 }
719
720
721 ### Function: Add WP-PostViews General Stats To WP-Stats Page Options
722 function postviews_page_admin_general_stats($content) {
723 $stats_display = get_option('stats_display');
724 if ( (int) $stats_display['views'] === 1 ) {
725 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" checked="checked" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
726 } else {
727 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
728 }
729 return $content;
730 }
731
732
733 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options
734 function postviews_page_admin_most_stats($content) {
735 $stats_display = get_option('stats_display');
736 $stats_mostlimit = (int) get_option('stats_mostlimit');
737 if ( (int) $stats_display['viewed_most_post'] === 1 ) {
738 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" checked="checked" />&nbsp;&nbsp;<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";
739 } else {
740 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" />&nbsp;&nbsp;<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";
741 }
742 if ( (int) $stats_display['viewed_most_page'] === 1 ) {
743 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" checked="checked" />&nbsp;&nbsp;<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";
744 } else {
745 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" />&nbsp;&nbsp;<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";
746 }
747 return $content;
748 }
749
750
751 ### Function: Add WP-PostViews General Stats To WP-Stats Page
752 function postviews_page_general_stats($content) {
753 $stats_display = get_option('stats_display');
754 if ( (int) $stats_display['views'] === 1 ) {
755 $content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n";
756 $content .= '<ul>'."\n";
757 $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";
758 $content .= '</ul>'."\n";
759 }
760 return $content;
761 }
762
763
764 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page
765 function postviews_page_most_stats($content) {
766 $stats_display = get_option('stats_display');
767 $stats_mostlimit = (int) get_option('stats_mostlimit');
768 if ( (int) $stats_display['viewed_most_post'] === 1 ) {
769 $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";
770 $content .= '<ul>'."\n";
771 $content .= get_most_viewed('post', $stats_mostlimit, 0, false);
772 $content .= '</ul>'."\n";
773 }
774 if($stats_display['viewed_most_page'] == 1) {
775 $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";
776 $content .= '<ul>'."\n";
777 $content .= get_most_viewed('page', $stats_mostlimit, 0, false);
778 $content .= '</ul>'."\n";
779 }
780 return $content;
781 }
782
783
784 ### Function: Increment Post Views
785 add_action( 'wp_ajax_postviews', 'increment_views' );
786 add_action( 'wp_ajax_nopriv_postviews', 'increment_views' );
787 function increment_views() {
788 if ( empty( $_GET['postviews_id'] ) ) {
789 return;
790 }
791
792 if ( !defined( 'WP_CACHE' ) || ! WP_CACHE ) {
793 return;
794 }
795
796 $views_options = get_option( 'views_options' );
797
798 if ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) {
799 return;
800 }
801
802 $post_id = (int) sanitize_key( $_GET['postviews_id'] );
803 if( $post_id > 0 ) {
804 $post_views = get_post_custom( $post_id );
805 $post_views = (int) $post_views['views'][0];
806 update_post_meta( $post_id, 'views', ( $post_views + 1 ) );
807 do_action( 'postviews_increment_views_ajax', ( $post_views + 1 ) );
808 echo ( $post_views + 1 );
809 exit();
810 }
811 }
812
813 ### Function Show Post Views Column in WP-Admin
814 add_action('manage_posts_custom_column', 'add_postviews_column_content');
815 add_filter('manage_posts_columns', 'add_postviews_column');
816 add_action('manage_pages_custom_column', 'add_postviews_column_content');
817 add_filter('manage_pages_columns', 'add_postviews_column');
818 function add_postviews_column($defaults) {
819 $defaults['views'] = __( 'Views', 'wp-postviews' );
820 return $defaults;
821 }
822
823
824 ### Functions Fill In The Views Count
825 function add_postviews_column_content($column_name) {
826 if ($column_name === 'views' ) {
827 if ( function_exists('the_views' ) ) {
828 the_views( true, '', '', true );
829 }
830 }
831 }
832
833
834 ### Function Sort Columns
835 add_filter( 'manage_edit-post_sortable_columns', 'sort_postviews_column');
836 add_filter( 'manage_edit-page_sortable_columns', 'sort_postviews_column' );
837 function sort_postviews_column( $defaults ) {
838 $defaults['views'] = 'views';
839 return $defaults;
840 }
841 add_action('pre_get_posts', 'sort_postviews');
842 function sort_postviews($query) {
843 if ( ! is_admin() ) {
844 return;
845 }
846 $orderby = $query->get('orderby');
847 if ( 'views' === $orderby ) {
848 $query->set( 'meta_key', 'views' );
849 $query->set( 'orderby', 'meta_value_num' );
850 }
851 }
852
853 ### Function: Round Numbers To K (Thousand), M (Million) or B (Billion)
854 function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
855 if( $number < $min_value ) {
856 return number_format_i18n( $number );
857 }
858 $alphabets = array( 1000000000 => 'B', 1000000 => 'M', 1000 => 'K' );
859 foreach( $alphabets as $key => $value )
860 if( $number >= $key ) {
861 return round( $number / $key, $decimal ) . '' . $value;
862 }
863 }
864
865
866 ### Class: WP-PostViews Widget
867 class WP_Widget_PostViews extends WP_Widget {
868 // Constructor
869 public function __construct() {
870 $widget_ops = array('description' => __('WP-PostViews views statistics', 'wp-postviews'));
871 parent::__construct('views', __('Views', 'wp-postviews'), $widget_ops);
872 }
873
874 // Display Widget
875 public function widget($args, $instance) {
876 $title = apply_filters('widget_title', esc_attr($instance['title']));
877 $type = esc_attr($instance['type']);
878 $mode = esc_attr($instance['mode']);
879 $limit = (int) $instance['limit'];
880 $chars = (int) $instance['chars'];
881 $cat_ids = explode(',', esc_attr($instance['cat_ids']));
882 echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
883 echo '<ul>'."\n";
884 switch($type) {
885 case 'least_viewed':
886 get_least_viewed($mode, $limit, $chars);
887 break;
888 case 'most_viewed':
889 get_most_viewed($mode, $limit, $chars);
890 break;
891 case 'most_viewed_category':
892 get_most_viewed_category($cat_ids, $mode, $limit, $chars);
893 break;
894 case 'least_viewed_category':
895 get_least_viewed_category($cat_ids, $mode, $limit, $chars);
896 break;
897 }
898 echo '</ul>'."\n";
899 echo $args['after_widget'];
900 }
901
902 // When Widget Control Form Is Posted
903 public function update($new_instance, $old_instance) {
904 if (!isset($new_instance['submit'])) {
905 return false;
906 }
907 $instance = $old_instance;
908 $instance['title'] = strip_tags($new_instance['title']);
909 $instance['type'] = strip_tags($new_instance['type']);
910 $instance['mode'] = strip_tags($new_instance['mode']);
911 $instance['limit'] = (int) $new_instance['limit'];
912 $instance['chars'] = (int) $new_instance['chars'];
913 $instance['cat_ids'] = strip_tags($new_instance['cat_ids']);
914 return $instance;
915 }
916
917 // DIsplay Widget Control Form
918 public function form($instance) {
919 $instance = wp_parse_args((array) $instance, array('title' => __('Views', 'wp-postviews'), 'type' => 'most_viewed', 'mode' => '', 'limit' => 10, 'chars' => 200, 'cat_ids' => '0'));
920 $title = esc_attr($instance['title']);
921 $type = esc_attr($instance['type']);
922 $mode = trim(esc_attr($instance['mode']));
923 $limit = (int) $instance['limit'];
924 $chars = (int) $instance['chars'];
925 $cat_ids = esc_attr($instance['cat_ids']);
926 $post_types = get_post_types(array(
927 'public' => true
928 ));
929 ?>
930 <p>
931 <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>
932 </p>
933 <p>
934 <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postviews'); ?>
935 <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
936 <option value="least_viewed"<?php selected('least_viewed', $type); ?>><?php _e('Least Viewed', 'wp-postviews'); ?></option>
937 <option value="least_viewed_category"<?php selected('least_viewed_category', $type); ?>><?php _e('Least Viewed By Category', 'wp-postviews'); ?></option>
938 <optgroup>&nbsp;</optgroup>
939 <option value="most_viewed"<?php selected('most_viewed', $type); ?>><?php _e('Most Viewed', 'wp-postviews'); ?></option>
940 <option value="most_viewed_category"<?php selected('most_viewed_category', $type); ?>><?php _e('Most Viewed By Category', 'wp-postviews'); ?></option>
941 </select>
942 </label>
943 </p>
944 <p>
945 <label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Views From:', 'wp-postviews'); ?>
946 <select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat">
947 <option value=""<?php selected('', $mode); ?>><?php _e('All', 'wp-postviews'); ?></option>
948 <?php if($post_types > 0): ?>
949 <?php foreach($post_types as $post_type): ?>
950 <option value="<?php echo $post_type; ?>"<?php selected($post_type, $mode); ?>><?php printf(__('%s Only', 'wp-postviews'), ucfirst($post_type)); ?></option>
951 <?php endforeach; ?>
952 <?php endif; ?>
953 </select>
954 </label>
955 </p>
956 <p>
957 <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>
958 </p>
959 <p>
960 <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 />
961 <small><?php _e('<strong>0</strong> to disable.', 'wp-postviews'); ?></small>
962 </p>
963 <p>
964 <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 />
965 <small><?php _e('Separate mutiple categories with commas.', 'wp-postviews'); ?></small>
966 </p>
967 <p style="color: red;">
968 <small><?php _e('* If you are not using any category statistics, you can ignore it.', 'wp-postviews'); ?></small>
969 <p>
970 <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
971 <?php
972 }
973 }
974
975
976 ### Function: Init WP-PostViews Widget
977 add_action( 'widgets_init', 'widget_views_init' );
978 function widget_views_init() {
979 register_widget( 'WP_Widget_PostViews' );
980 }
981
982
983 ### Function: Post Views Options
984 register_activation_hook( __FILE__, 'views_activation' );
985 function views_activation( $network_wide ) {
986 // Add Options
987 $option_name = 'views_options';
988 $option = array(
989 'count' => 1,
990 'exclude_bots' => 0,
991 'display_home' => 0,
992 'display_single' => 0,
993 'display_page' => 0,
994 'display_archive' => 0,
995 'display_search' => 0,
996 'display_other' => 0,
997 'use_ajax' => 1,
998 'template' => __( '%VIEW_COUNT% views', 'wp-postviews' ),
999 'most_viewed_template' => '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>'
1000 );
1001
1002 if ( is_multisite() && $network_wide ) {
1003 $ms_sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites();
1004
1005 if( 0 < count( $ms_sites ) ) {
1006 foreach ( $ms_sites as $ms_site ) {
1007 $blog_id = class_exists( 'WP_Site' ) ? $ms_site->blog_id : $ms_site['blog_id'];
1008 switch_to_blog( $blog_id );
1009 add_option( $option_name, $option );
1010 restore_current_blog();
1011 }
1012 }
1013 } else {
1014 add_option( $option_name, $option );
1015 }
1016 }
1017
1018 ### Function: Parse View Options
1019 function views_options_parse( $key ) {
1020 return ! empty( $_POST[ $key ] ) ? $_POST[ $key ] : null;
1021 }
1022