Version Description
Download this release
Release Info
Developer | GamerZ |
Plugin | WP-PostViews |
Version | 1.76 |
Comparing to | |
See all releases |
Code changes from version 1.75 to 1.76
- postviews-options.php +13 -13
- readme.txt +20 -16
- wp-postviews.php +68 -59
postviews-options.php
CHANGED
@@ -2,25 +2,25 @@
|
|
2 |
### Variables Variables Variables
|
3 |
$base_name = plugin_basename( 'wp-postviews/postviews-options.php' );
|
4 |
$base_page = 'admin.php?page='.$base_name;
|
5 |
-
$id = (isset($_GET['id'] ) ?
|
6 |
-
$mode = (isset($_GET['mode'] ) ? trim($_GET['mode'] ) : '' );
|
7 |
$text = '';
|
8 |
|
9 |
### Form Processing
|
10 |
if(!empty($_POST['Submit'] )) {
|
11 |
check_admin_referer( 'wp-postviews_options' );
|
12 |
$views_options = array(
|
13 |
-
'count' =>
|
14 |
-
, 'exclude_bots' =>
|
15 |
-
, 'display_home' =>
|
16 |
-
, 'display_single' =>
|
17 |
-
, 'display_page' =>
|
18 |
-
, 'display_archive' =>
|
19 |
-
, 'display_search' =>
|
20 |
-
, 'display_other' =>
|
21 |
-
, 'use_ajax' =>
|
22 |
-
, 'template' => trim( views_options_parse('views_template_template') )
|
23 |
-
, 'most_viewed_template' => trim( views_options_parse('views_template_most_viewed') )
|
24 |
);
|
25 |
$update_views_queries = array();
|
26 |
$update_views_text = array();
|
2 |
### Variables Variables Variables
|
3 |
$base_name = plugin_basename( 'wp-postviews/postviews-options.php' );
|
4 |
$base_page = 'admin.php?page='.$base_name;
|
5 |
+
$id = ( isset($_GET['id'] ) ? (int) sanitize_key( $_GET['id'] ) : 0 );
|
6 |
+
$mode = ( isset($_GET['mode'] ) ? sanitize_key( trim( $_GET['mode'] ) ) : '' );
|
7 |
$text = '';
|
8 |
|
9 |
### Form Processing
|
10 |
if(!empty($_POST['Submit'] )) {
|
11 |
check_admin_referer( 'wp-postviews_options' );
|
12 |
$views_options = array(
|
13 |
+
'count' => (int) sanitize_key( views_options_parse('views_count') )
|
14 |
+
, 'exclude_bots' => (int) sanitize_key( views_options_parse('views_exclude_bots') )
|
15 |
+
, 'display_home' => (int) sanitize_key( views_options_parse('views_display_home') )
|
16 |
+
, 'display_single' => (int) sanitize_key( views_options_parse('views_display_single') )
|
17 |
+
, 'display_page' => (int) sanitize_key( views_options_parse('views_display_page') )
|
18 |
+
, 'display_archive' => (int) sanitize_key( views_options_parse('views_display_archive') )
|
19 |
+
, 'display_search' => (int) sanitize_key( views_options_parse('views_display_search') )
|
20 |
+
, 'display_other' => (int) sanitize_key( views_options_parse('views_display_other') )
|
21 |
+
, 'use_ajax' => (int) sanitize_key( views_options_parse('views_use_ajax') )
|
22 |
+
, 'template' => wp_kses_post( trim( views_options_parse('views_template_template') ) )
|
23 |
+
, 'most_viewed_template' => wp_kses_post( trim( views_options_parse('views_template_most_viewed') ) )
|
24 |
);
|
25 |
$update_views_queries = array();
|
26 |
$update_views_text = array();
|
readme.txt
CHANGED
@@ -3,8 +3,8 @@ Contributors: GamerZ
|
|
3 |
Donate link: https://lesterchan.net/site/donation/
|
4 |
Tags: views, hits, counter, postviews
|
5 |
Requires at least: 4.0
|
6 |
-
Tested up to:
|
7 |
-
Stable tag: 1.
|
8 |
|
9 |
Enables you to display how many times a post/page had been viewed.
|
10 |
|
@@ -34,6 +34,10 @@ Enables you to display how many times a post/page had been viewed.
|
|
34 |
I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
|
35 |
|
36 |
## Changelog
|
|
|
|
|
|
|
|
|
37 |
### Version 1.75
|
38 |
* NEW: Use WP_Query() for most/least viewed posts
|
39 |
|
@@ -93,13 +97,13 @@ N/A
|
|
93 |
|
94 |
### To Display Least Viewed Posts
|
95 |
|
96 |
-
|
97 |
<?php if (function_exists('get_least_viewed')): ?>
|
98 |
<ul>
|
99 |
<?php get_least_viewed(); ?>
|
100 |
</ul>
|
101 |
<?php endif; ?>
|
102 |
-
|
103 |
|
104 |
* The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
105 |
* The second value you pass in is the maximum number of post you want to get.
|
@@ -107,13 +111,13 @@ N/A
|
|
107 |
|
108 |
### To Display Most Viewed Posts
|
109 |
|
110 |
-
|
111 |
<?php if (function_exists('get_most_viewed')): ?>
|
112 |
<ul>
|
113 |
<?php get_most_viewed(); ?>
|
114 |
</ul>
|
115 |
<?php endif; ?>
|
116 |
-
|
117 |
|
118 |
* The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
119 |
* The second value you pass in is the maximum number of post you want to get.
|
@@ -121,13 +125,13 @@ N/A
|
|
121 |
|
122 |
### To Display Least Viewed Posts By Tag
|
123 |
|
124 |
-
|
125 |
<?php if (function_exists('get_least_viewed_tag')): ?>
|
126 |
<ul>
|
127 |
<?php get_least_viewed_tag(); ?>
|
128 |
</ul>
|
129 |
<?php endif; ?>
|
130 |
-
|
131 |
|
132 |
* The first value you pass in is the tag id.
|
133 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
@@ -136,13 +140,13 @@ N/A
|
|
136 |
|
137 |
### To Display Most Viewed Posts By Tag
|
138 |
|
139 |
-
|
140 |
<?php if (function_exists('get_most_viewed_tag')): ?>
|
141 |
<ul>
|
142 |
<?php get_most_viewed_tag(); ?>
|
143 |
</ul>
|
144 |
<?php endif; ?>
|
145 |
-
|
146 |
|
147 |
* The first value you pass in is the tag id.
|
148 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
@@ -151,13 +155,13 @@ N/A
|
|
151 |
|
152 |
### To Display Least Viewed Posts For A Category
|
153 |
|
154 |
-
|
155 |
<?php if (function_exists('get_least_viewed_category')): ?>
|
156 |
<ul>
|
157 |
<?php get_least_viewed_category(); ?>
|
158 |
</ul>
|
159 |
<?php endif; ?>
|
160 |
-
|
161 |
|
162 |
* The first value you pass in is the category id.
|
163 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
@@ -166,13 +170,13 @@ N/A
|
|
166 |
|
167 |
### To Display Most Viewed Posts For A Category
|
168 |
|
169 |
-
|
170 |
<?php if (function_exists('get_most_viewed_category')): ?>
|
171 |
<ul>
|
172 |
<?php get_most_viewed_category(); ?>
|
173 |
</ul>
|
174 |
<?php endif; ?>
|
175 |
-
|
176 |
|
177 |
* The first value you pass in is the category id.
|
178 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
@@ -189,7 +193,7 @@ Use: `<div id="postviews_lscwp"></div>` to replace `<?php if(function_exists('th
|
|
189 |
NOTE: The id can be changed, but the div id and the ajax function must match.
|
190 |
Replace the ajax query in `wp-content/plugins/wp-postviews/postviews-cache.js` with
|
191 |
|
192 |
-
|
193 |
jQuery.ajax({
|
194 |
type:"GET",
|
195 |
url:viewsCacheL10n.admin_ajax_url,
|
@@ -201,6 +205,6 @@ jQuery.ajax({
|
|
201 |
}
|
202 |
}
|
203 |
});
|
204 |
-
|
205 |
|
206 |
Purge the cache to use the updated pages.
|
3 |
Donate link: https://lesterchan.net/site/donation/
|
4 |
Tags: views, hits, counter, postviews
|
5 |
Requires at least: 4.0
|
6 |
+
Tested up to: 5.0
|
7 |
+
Stable tag: 1.76
|
8 |
|
9 |
Enables you to display how many times a post/page had been viewed.
|
10 |
|
34 |
I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
|
35 |
|
36 |
## Changelog
|
37 |
+
### Version 1.76
|
38 |
+
* NEW: Added postviews_should_count filter
|
39 |
+
* FIXED: Change to (int) from intval() and use sanitize_key() with it.
|
40 |
+
|
41 |
### Version 1.75
|
42 |
* NEW: Use WP_Query() for most/least viewed posts
|
43 |
|
97 |
|
98 |
### To Display Least Viewed Posts
|
99 |
|
100 |
+
~~~
|
101 |
<?php if (function_exists('get_least_viewed')): ?>
|
102 |
<ul>
|
103 |
<?php get_least_viewed(); ?>
|
104 |
</ul>
|
105 |
<?php endif; ?>
|
106 |
+
~~~
|
107 |
|
108 |
* The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
109 |
* The second value you pass in is the maximum number of post you want to get.
|
111 |
|
112 |
### To Display Most Viewed Posts
|
113 |
|
114 |
+
~~~
|
115 |
<?php if (function_exists('get_most_viewed')): ?>
|
116 |
<ul>
|
117 |
<?php get_most_viewed(); ?>
|
118 |
</ul>
|
119 |
<?php endif; ?>
|
120 |
+
~~~
|
121 |
|
122 |
* The first value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
123 |
* The second value you pass in is the maximum number of post you want to get.
|
125 |
|
126 |
### To Display Least Viewed Posts By Tag
|
127 |
|
128 |
+
~~~
|
129 |
<?php if (function_exists('get_least_viewed_tag')): ?>
|
130 |
<ul>
|
131 |
<?php get_least_viewed_tag(); ?>
|
132 |
</ul>
|
133 |
<?php endif; ?>
|
134 |
+
~~~
|
135 |
|
136 |
* The first value you pass in is the tag id.
|
137 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
140 |
|
141 |
### To Display Most Viewed Posts By Tag
|
142 |
|
143 |
+
~~~
|
144 |
<?php if (function_exists('get_most_viewed_tag')): ?>
|
145 |
<ul>
|
146 |
<?php get_most_viewed_tag(); ?>
|
147 |
</ul>
|
148 |
<?php endif; ?>
|
149 |
+
~~~
|
150 |
|
151 |
* The first value you pass in is the tag id.
|
152 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
155 |
|
156 |
### To Display Least Viewed Posts For A Category
|
157 |
|
158 |
+
~~~
|
159 |
<?php if (function_exists('get_least_viewed_category')): ?>
|
160 |
<ul>
|
161 |
<?php get_least_viewed_category(); ?>
|
162 |
</ul>
|
163 |
<?php endif; ?>
|
164 |
+
~~~
|
165 |
|
166 |
* The first value you pass in is the category id.
|
167 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
170 |
|
171 |
### To Display Most Viewed Posts For A Category
|
172 |
|
173 |
+
~~~
|
174 |
<?php if (function_exists('get_most_viewed_category')): ?>
|
175 |
<ul>
|
176 |
<?php get_most_viewed_category(); ?>
|
177 |
</ul>
|
178 |
<?php endif; ?>
|
179 |
+
~~~
|
180 |
|
181 |
* The first value you pass in is the category id.
|
182 |
* The second value you pass in is the post type that you want. If you want to get every post types, just use 'both'. It also supports PHP array: example `array('post', 'page')`.
|
193 |
NOTE: The id can be changed, but the div id and the ajax function must match.
|
194 |
Replace the ajax query in `wp-content/plugins/wp-postviews/postviews-cache.js` with
|
195 |
|
196 |
+
~~~
|
197 |
jQuery.ajax({
|
198 |
type:"GET",
|
199 |
url:viewsCacheL10n.admin_ajax_url,
|
205 |
}
|
206 |
}
|
207 |
});
|
208 |
+
~~~
|
209 |
|
210 |
Purge the cache to use the updated pages.
|
wp-postviews.php
CHANGED
@@ -3,7 +3,7 @@
|
|
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.
|
7 |
Author: Lester 'GaMerZ' Chan
|
8 |
Author URI: https://lesterchan.net
|
9 |
Text Domain: wp-postviews
|
@@ -49,35 +49,34 @@ function postviews_menu() {
|
|
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 =
|
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(
|
64 |
case 0:
|
65 |
$should_count = true;
|
66 |
break;
|
67 |
case 1:
|
68 |
-
if(empty( $_COOKIE[USER_COOKIE] ) &&
|
69 |
$should_count = true;
|
70 |
}
|
71 |
break;
|
72 |
case 2:
|
73 |
-
if(
|
74 |
$should_count = true;
|
75 |
}
|
76 |
break;
|
77 |
}
|
78 |
-
if(
|
79 |
-
$bots = array
|
80 |
-
(
|
81 |
'Google Bot' => 'google'
|
82 |
, 'MSN' => 'msnbot'
|
83 |
, 'Alex' => 'ia_archiver'
|
@@ -109,15 +108,16 @@ function process_postviews() {
|
|
109 |
);
|
110 |
$useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
|
111 |
foreach ( $bots as $name => $lookfor ) {
|
112 |
-
if ( ! empty( $useragent ) && (
|
113 |
$should_count = false;
|
114 |
break;
|
115 |
}
|
116 |
}
|
117 |
}
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
121 |
}
|
122 |
}
|
123 |
}
|
@@ -129,34 +129,38 @@ 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'] ) &&
|
138 |
return;
|
|
|
139 |
|
140 |
if ( !wp_is_post_revision( $post ) && ( is_single() || is_page() ) ) {
|
141 |
$should_count = false;
|
142 |
-
switch(
|
143 |
case 0:
|
144 |
$should_count = true;
|
145 |
break;
|
146 |
case 1:
|
147 |
-
if ( empty( $_COOKIE[USER_COOKIE] ) &&
|
148 |
$should_count = true;
|
149 |
}
|
150 |
break;
|
151 |
case 2:
|
152 |
-
if (
|
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' =>
|
160 |
}
|
161 |
}
|
162 |
}
|
@@ -199,7 +203,7 @@ function should_views_be_displayed($views_options = null) {
|
|
199 |
|
200 |
### Function: Display The Post Views
|
201 |
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
|
202 |
-
$post_views =
|
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;
|
@@ -218,12 +222,12 @@ function the_views($display = true, $prefix = '', $postfix = '', $always = false
|
|
218 |
add_shortcode( 'views', 'views_shortcode' );
|
219 |
function views_shortcode( $atts ) {
|
220 |
$attributes = shortcode_atts( array( 'id' => 0 ), $atts );
|
221 |
-
$id =
|
222 |
if( $id === 0) {
|
223 |
$id = get_the_ID();
|
224 |
}
|
225 |
$views_options = get_option( 'views_options' );
|
226 |
-
$post_views =
|
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 );
|
@@ -603,7 +607,7 @@ if ( ! function_exists( 'get_most_viewed_tag' ) ) {
|
|
603 |
if(!function_exists('get_totalviews')) {
|
604 |
function get_totalviews($display = true) {
|
605 |
global $wpdb;
|
606 |
-
$total_views =
|
607 |
if($display) {
|
608 |
echo number_format_i18n($total_views);
|
609 |
} else {
|
@@ -711,7 +715,7 @@ function postviews_wp_stats() {
|
|
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']
|
715 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" checked="checked" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
|
716 |
} else {
|
717 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
|
@@ -723,13 +727,13 @@ function postviews_page_admin_general_stats($content) {
|
|
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 =
|
727 |
-
if($stats_display['viewed_most_post']
|
728 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" checked="checked" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
729 |
} else {
|
730 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
731 |
}
|
732 |
-
if($stats_display['viewed_most_page']
|
733 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" checked="checked" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
734 |
} else {
|
735 |
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
@@ -741,7 +745,7 @@ function postviews_page_admin_most_stats($content) {
|
|
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']
|
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";
|
@@ -754,8 +758,8 @@ function postviews_page_general_stats($content) {
|
|
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 =
|
758 |
-
if($stats_display['viewed_most_post']
|
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);
|
@@ -775,21 +779,24 @@ function postviews_page_most_stats($content) {
|
|
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'] ) &&
|
787 |
return;
|
|
|
788 |
|
789 |
-
$post_id =
|
790 |
if( $post_id > 0 ) {
|
791 |
$post_views = get_post_custom( $post_id );
|
792 |
-
$post_views =
|
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 );
|
@@ -810,28 +817,30 @@ function add_postviews_column($defaults) {
|
|
810 |
|
811 |
### Functions Fill In The Views Count
|
812 |
function add_postviews_column_content($column_name) {
|
813 |
-
if($column_name
|
814 |
-
if(function_exists('the_views')) {
|
|
|
|
|
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'
|
833 |
-
$query->set('meta_key', 'views');
|
834 |
-
$query->set('orderby', 'meta_value_num');
|
835 |
}
|
836 |
}
|
837 |
|
@@ -851,18 +860,18 @@ function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
|
|
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 =
|
865 |
-
$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";
|
@@ -885,7 +894,7 @@ function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
|
|
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 |
}
|
@@ -893,20 +902,20 @@ function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
|
|
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'] =
|
897 |
-
$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 =
|
909 |
-
$chars =
|
910 |
$cat_ids = esc_attr($instance['cat_ids']);
|
911 |
$post_types = get_post_types(array(
|
912 |
'public' => true
|
@@ -1001,6 +1010,6 @@ function views_activation( $network_wide ) {
|
|
1001 |
}
|
1002 |
|
1003 |
### Function: Parse View Options
|
1004 |
-
function views_options_parse($key) {
|
1005 |
-
return !empty($_POST[$key]) ? $_POST[$key] : null;
|
1006 |
}
|
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
|
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'
|
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 |
}
|
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 |
}
|
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;
|
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 );
|
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 {
|
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" /> <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" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
|
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" /> <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" /> <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" /> <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" /> <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";
|
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";
|
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);
|
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 );
|
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 |
|
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";
|
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 |
}
|
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
|
1010 |
}
|
1011 |
|
1012 |
### Function: Parse View Options
|
1013 |
+
function views_options_parse( $key ) {
|
1014 |
+
return ! empty( $_POST[ $key ] ) ? $_POST[ $key ] : null;
|
1015 |
}
|