PluginProbe
WP-PostViews / 1.75
WP-PostViews v1.75
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.75, at wp-postviews.php

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