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

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