WP-PostViews - Version 1.75

Version Description

Download this release

Release Info

Developer GamerZ
Plugin Icon WP-PostViews
Version 1.75
Comparing to
See all releases

Code changes from version 1.74 to 1.75

Files changed (3) hide show
  1. readme.txt +84 -172
  2. uninstall.php +8 -7
  3. wp-postviews.php +801 -786
readme.txt CHANGED
@@ -1,59 +1,70 @@
1
- === WP-PostViews ===
2
  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: 4.7
7
- Stable tag: 1.74
8
 
9
  Enables you to display how many times a post/page had been viewed.
10
 
11
- == Description ==
12
 
13
- = Build Status =
 
 
 
 
 
 
 
 
14
  [![Build Status](https://travis-ci.org/lesterchan/wp-postviews.svg?branch=master)](https://travis-ci.org/lesterchan/wp-postviews)
15
 
16
- = Development =
17
  [https://github.com/lesterchan/wp-postviews/](https://github.com/lesterchan/wp-postviews/ "https://github.com/lesterchan/wp-postviews/")
18
 
19
- = Translations =
20
  [http://dev.wp-plugins.org/browser/wp-postviews/i18n/](http://dev.wp-plugins.org/browser/wp-postviews/i18n/ "http://dev.wp-plugins.org/browser/wp-postviews/i18n/")
21
 
22
- = Credits =
23
  * Plugin icon by [Iconmoon](http://www.icomoon.io) from [Flaticon](http://www.flaticon.com)
24
 
25
- = Donations =
26
  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.
27
 
28
- == Changelog ==
29
- = Version 1.74 =
 
 
 
30
  * NEW: Bump WordPress 4.7
31
  * NEW: Template variable %POST_CATEGORY_ID%. It returns Post's Category ID. If you are using Yoast SEO Plugin, it will return the priority Category ID. Props @FunFrog-BY
32
 
33
- = Version 1.73 =
34
  * FIXED: In preview mode, don't count views
35
 
36
- = Version 1.72 =
37
  * NEW: Add %POST_THUMBNAIL% to template variables
38
 
39
- = Version 1.71 =
40
  * FIXED: Notices in Widget Constructor for WordPress 4.3
41
 
42
- = Version 1.70 =
43
  * FIXED: Integration with WP-Stats
44
 
45
- = Version 1.69 =
46
  * NEW: Shortcode `[views]` or [views id="POST_ID"]` to embed view count into post
47
  * NEW: Added template variable `%VIEW_COUNT_ROUNDED%` to support rounded view count like 10.1k or 11.2M
48
 
49
- = Version 1.68 =
50
  * NEW: Added action hook 'postviews_increment_views' and 'postviews_increment_views_ajax'
51
  * NEW: Allow custom post type to be chosen under the widget
52
 
53
- = Version 1.67 =
54
  * NEW: Allow user to not use AJAX to update the views even though WP_CACHE is true
55
 
56
- = Version 1.66 =
57
  * NEW: Supports MultiSite Network Activation
58
  * NEW: Add %POST_DATE% and %POST_TIME% to template variables
59
  * NEW: Add China isearch engines bots
@@ -62,233 +73,134 @@ I spent most of my free time creating, updating, maintaining and supporting thes
62
  * FIXED: Notices and better way to get views from meta. Props daankortenbach.
63
  * FIXED: No longer needing add_post_meta() if update_post_meta() fails.
64
 
65
- = Version 1.65 (02-06-2013) =
66
  * FIXED: Views not showing in WP-Admin if "Display Options" is not set to "Display to everyone"
67
 
68
- = Version 1.64 (31-05-2013) =
69
- * NEW: Using Enqueue Script For WP_CACHE Enabled Sites. Props Crowd Favourite
70
- * FIXED: Viewcounter always display eventhough "display to registered users only" is set
71
-
72
- = Version 1.63 (07-05-2013) =
73
- * NEW: Added nonce To PostViews Options Admin Page
74
-
75
- = Version 1.62 (29-11-2012) =
76
- * NEW: Add "Views" Column To Manage Pages In WP-Admin
77
- * NEW: Add Sortable "Views" Column To Manage Posts/Pages In WP-Admin
78
-
79
- = Version 1.61 (21-05-2012) =
80
- * FIXED: Move AJAX Request to wp-admin/admin-ajax.php
81
-
82
- = Version 1.60 (18-02-2011) =
83
- * NEW: Added Views Count To Edit Posts Screen
84
- * FIXED: Removed Global $post
85
-
86
- = Version 1.50 (15-06-2009) =
87
- * NEW: Works For WordPress 2.8 Only
88
- * NEW: Uses jQuery Framework
89
- * NEW: Added In Most Viewed Pages To WP-Stats
90
- * NEW: Use _n() Instead Of __ngettext() And _n_noop() Instead Of __ngettext_noop()
91
- * FIXED: Uses $_SERVER['PHP_SELF'] With plugin_basename(__FILE__) Instead Of Just $_SERVER['REQUEST_URI']
92
- * NEW: Uses New Widget Class From WordPress
93
- * NEW: Merge Widget Code To wp-postviews.php And Remove wp-postviews-widget.php
94
- * NEW: Added get_most_viewed_tag() And get_least_viewed_tag()
95
- * FIXED: Ensure That Post Is Not A Revision
96
- * FIXED: Multiple Loops Filtered Not Cleared
97
-
98
- = Version 1.40 (12-12-2008) =
99
- * NEW: Works For WordPress 2.7 Only
100
- * NEW: Options To Display Views On Certain Places by David Potter
101
- * NEW: Right To Left Language Support by Kambiz R. Khojasteh
102
- * NEW: Output Of the_views() Applied To "the_views" Filter by Kambiz R. Khojasteh
103
- * NEW: Called postviews_textdomain() In views_init() by Kambiz R. Khojasteh
104
- * NEW: Uses plugins_url() And site_url()
105
- * NEW: Added get_least_viewed() And get_least_viewed_category() By JBrinx
106
- * FIXED: "views" Custom Field Gets Created Now When Post Is Published
107
-
108
- = Version 1.31 (16-07-2008) =
109
- * NEW: Works For WordPress 2.6
110
- * NEW: Renamed GET Variables sortby To v_sortby And orderby To v_orderby
111
- * NEW: Better Translation Using __ngetext() by Anna Ozeritskaya
112
- * FIXED: Able To Use v_sortby And v_orderby in query_posts()
113
-
114
- = Version 1.30 (01-06-2008) =
115
- * NEW: Uses /wp-postviews/ Folder Instead Of /postviews/
116
- * NEW: Uses wp-postviews.php Instead Of postviews.php
117
- * NEW: Uses wp-postviews-widget.php Instead Of postviews-widget.php
118
- * NEW: Uses number_format_i18n() Instead Of number_format()
119
- * NEW: Option To Exclude Bots Views In 'WP-Admin -> Settings -> Post Views'
120
- * NEW: Added Most Viewed Template
121
- * NEW: Change The Way WP-PostViews Count Views
122
- * NEW: Should Work With WP-Cache Or WP-SuperCache
123
-
124
- = Version 1.20 (01-10-2007) =
125
- * NEW: Works For WordPress 2.3 Only
126
- * NEW: Most Viewed Widget Added
127
- * NEW: Ability To Uninstall WP-PostViews
128
- * NEW: Uses WP-Stats Filter To Add Stats Into WP-Stats Page
129
-
130
- = Version 1.11 (01-06-2007) =
131
- * FIXED: Wrong URL For Page Under Most Viewed Posts Listing
132
-
133
- = Version 1.10 (01-02-2007) =
134
- * NEW: Works For WordPress 2.1 Only
135
- * NEW: Localization WP-PostViews
136
- * NEW: Added Function To Get Most Viewed Post By Category ID
137
- * FIXED: Views Not Counting In Some Cases
138
-
139
- = Version 1.02 (01-10-2006) =
140
- * NEW: Change In get_most_viewed() To Accommodate Latest Version Of WP-Stats
141
-
142
- = Version 1.01 (01-07-2006) =
143
- * NEW: Added Get Total Views Function
144
- * FIXED: Modified Get Most Viewed Post Function
145
-
146
- = Version 1.00 (01-03-2006) =
147
- * NEW: Initial Release
148
-
149
- == Installation ==
150
-
151
- 1. Open `wp-content/plugins` Folder
152
- 2. Put: `Folder: wp-postviews`
153
- 3. Activate `WP-PostViews` Plugin
154
- 4. Go to `WP-Admin -> Settings -> PostViews` to configure the plugin.
155
-
156
- = Usage =
157
- 1. Open `wp-content/themes/<YOUR THEME NAME>/index.php`
158
- 2. You may place it in archive.php, single.php, post.php or page.php also.
159
- 3. Find: `<?php while (have_posts()) : the_post(); ?>`
160
- 4. Add Anywhere Below It (The Place You Want The Views To Show): `<?php if(function_exists('the_views')) { the_views(); } ?>`
161
- 5. Or you can use the shortcode `[views]` or `[views id="1"]` (where 1 is the post ID) in a post
162
-
163
- == Upgrading ==
164
-
165
- 1. Deactivate `wp-postviews` Plugin
166
- 2. Open `wp-content/plugins` Folder
167
- 3. Put/Overwrite: `Folder: wp-postviews`
168
- 4. Activate `WP-PostViews` Plugin
169
-
170
- == Upgrade Notice ==
171
 
172
  N/A
173
 
174
- == Screenshots ==
175
 
176
  1. PostViews
177
  2. Admin - PostViews Options
178
 
179
- == Frequently Asked Questions ==
180
 
181
- = How To View Stats With Widgets? =
182
  * Go to `WP-Admin -> Appearance -> Widgets`
183
  * The widget name is Views.
184
 
185
- = How To View Stats (Outside WP Loop) =
186
 
187
- = To Display Least Viewed Posts =
188
- * Use:
189
- <code>
190
  <?php if (function_exists('get_least_viewed')): ?>
191
  <ul>
192
  <?php get_least_viewed(); ?>
193
  </ul>
194
  <?php endif; ?>
195
- </code>
 
196
  * 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')`.
197
  * The second value you pass in is the maximum number of post you want to get.
198
  * Default: get_least_viewed('both', 10);
199
 
200
- = To Display Most Viewed Posts =
201
- * Use:
202
- <code>
203
  <?php if (function_exists('get_most_viewed')): ?>
204
  <ul>
205
  <?php get_most_viewed(); ?>
206
  </ul>
207
  <?php endif; ?>
208
- </code>
 
209
  * 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')`.
210
  * The second value you pass in is the maximum number of post you want to get.
211
  * Default: get_most_viewed('both', 10);
212
 
213
- = To Display Least Viewed Posts By Tag =
214
- * Use:
215
- <code>
216
  <?php if (function_exists('get_least_viewed_tag')): ?>
217
  <ul>
218
  <?php get_least_viewed_tag(); ?>
219
  </ul>
220
  <?php endif; ?>
221
- </code>
 
222
  * The first value you pass in is the tag id.
223
  * 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')`.
224
  * The third value you pass in is the maximum number of post you want to get.
225
  * Default: get_least_viewed_tag(1, 'both', 10);
226
 
227
- = To Display Most Viewed Posts By Tag =
228
- * Use:
229
- <code>
230
  <?php if (function_exists('get_most_viewed_tag')): ?>
231
  <ul>
232
  <?php get_most_viewed_tag(); ?>
233
  </ul>
234
  <?php endif; ?>
235
- </code>
 
236
  * The first value you pass in is the tag id.
237
  * 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')`.
238
  * The third value you pass in is the maximum number of post you want to get.
239
  * Default: get_most_viewed_tag(1, 'both', 10);
240
 
241
- = To Display Least Viewed Posts For A Category =
242
- * Use:
243
- <code>
244
  <?php if (function_exists('get_least_viewed_category')): ?>
245
  <ul>
246
  <?php get_least_viewed_category(); ?>
247
  </ul>
248
  <?php endif; ?>
249
- </code>
 
250
  * The first value you pass in is the category id.
251
  * 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')`.
252
  * The third value you pass in is the maximum number of post you want to get.
253
  * Default: get_least_viewed_category(1, 'both', 10);
254
 
255
- = To Display Most Viewed Posts For A Category =
256
- * Use:
257
- <code>
258
  <?php if (function_exists('get_most_viewed_category')): ?>
259
  <ul>
260
  <?php get_most_viewed_category(); ?>
261
  </ul>
262
  <?php endif; ?>
263
- </code>
 
264
  * The first value you pass in is the category id.
265
  * 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')`.
266
  * The third value you pass in is the maximum number of post you want to get.
267
  * Default: get_most_viewed_category(1, 'both', 10);
268
 
269
- = To Sort Most/Least Viewed Posts =
270
  * You can use: `<?php query_posts( array( 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); ?>`
271
  * Or pass in the variables to the URL: `http://yoursite.com/?v_sortby=views&v_orderby=desc`
272
  * You can replace DESC with ASC if you want the least viewed posts.
273
 
274
- = To Display Updating View Count With LiteSpeed Cache =
275
- 1. Use: `<div id="postviews_lscwp"></div>` to replace `<?php if(function_exists('the_views')) { the_views(); } ?>`.
276
- * NOTE: The id can be changed, but the div id and the ajax function must match.
277
- 2. Replace the ajax query in `wp-content/plugins/wp-postviews/postviews-cache.js` with
278
-
279
- `
280
- jQuery.ajax({
281
- type:"GET",
282
- url:viewsCacheL10n.admin_ajax_url,
283
- data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",
284
- cache:!1,
285
- success:function(data) {
286
- if(data) {
287
- jQuery('#postviews_lscwp').html(data+' views');
288
- }
289
- }
290
- });
291
- `
292
-
293
- 3. Purge the cache to use the updated pages.
294
-
1
+ # WP-PostViews
2
  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: 4.7
7
+ Stable tag: 1.75
8
 
9
  Enables you to display how many times a post/page had been viewed.
10
 
11
+ ## Description
12
 
13
+ ### Usage
14
+ 1. Open `wp-content/themes/<YOUR THEME NAME>/index.php`
15
+ 2. You may place it in archive.php, single.php, post.php or page.php also.
16
+ 3. Find: `<?php while (have_posts()) : the_post(); ?>`
17
+ 4. Add Anywhere Below It (The Place You Want The Views To Show): `<?php if(function_exists('the_views')) { the_views(); } ?>`
18
+ 5. Or you can use the shortcode `[views]` or `[views id="1"]` (where 1 is the post ID) in a post
19
+ 6. Go to `WP-Admin -> Settings -> PostViews` to configure the plugin.
20
+
21
+ ### Build Status
22
  [![Build Status](https://travis-ci.org/lesterchan/wp-postviews.svg?branch=master)](https://travis-ci.org/lesterchan/wp-postviews)
23
 
24
+ ### Development
25
  [https://github.com/lesterchan/wp-postviews/](https://github.com/lesterchan/wp-postviews/ "https://github.com/lesterchan/wp-postviews/")
26
 
27
+ ### Translations
28
  [http://dev.wp-plugins.org/browser/wp-postviews/i18n/](http://dev.wp-plugins.org/browser/wp-postviews/i18n/ "http://dev.wp-plugins.org/browser/wp-postviews/i18n/")
29
 
30
+ ### Credits
31
  * Plugin icon by [Iconmoon](http://www.icomoon.io) from [Flaticon](http://www.flaticon.com)
32
 
33
+ ### Donations
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
+
40
+ ### Version 1.74
41
  * NEW: Bump WordPress 4.7
42
  * NEW: Template variable %POST_CATEGORY_ID%. It returns Post's Category ID. If you are using Yoast SEO Plugin, it will return the priority Category ID. Props @FunFrog-BY
43
 
44
+ ### Version 1.73
45
  * FIXED: In preview mode, don't count views
46
 
47
+ ### Version 1.72
48
  * NEW: Add %POST_THUMBNAIL% to template variables
49
 
50
+ ### Version 1.71
51
  * FIXED: Notices in Widget Constructor for WordPress 4.3
52
 
53
+ ### Version 1.70
54
  * FIXED: Integration with WP-Stats
55
 
56
+ ### Version 1.69
57
  * NEW: Shortcode `[views]` or [views id="POST_ID"]` to embed view count into post
58
  * NEW: Added template variable `%VIEW_COUNT_ROUNDED%` to support rounded view count like 10.1k or 11.2M
59
 
60
+ ### Version 1.68
61
  * NEW: Added action hook 'postviews_increment_views' and 'postviews_increment_views_ajax'
62
  * NEW: Allow custom post type to be chosen under the widget
63
 
64
+ ### Version 1.67
65
  * NEW: Allow user to not use AJAX to update the views even though WP_CACHE is true
66
 
67
+ ### Version 1.66
68
  * NEW: Supports MultiSite Network Activation
69
  * NEW: Add %POST_DATE% and %POST_TIME% to template variables
70
  * NEW: Add China isearch engines bots
73
  * FIXED: Notices and better way to get views from meta. Props daankortenbach.
74
  * FIXED: No longer needing add_post_meta() if update_post_meta() fails.
75
 
76
+ ### Version 1.65 (02-06-2013)
77
  * FIXED: Views not showing in WP-Admin if "Display Options" is not set to "Display to everyone"
78
 
79
+ ## Upgrade Notice
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  N/A
82
 
83
+ ## Screenshots
84
 
85
  1. PostViews
86
  2. Admin - PostViews Options
87
 
88
+ ## Frequently Asked Questions
89
 
90
+ ### How To View Stats With Widgets?
91
  * Go to `WP-Admin -> Appearance -> Widgets`
92
  * The widget name is Views.
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.
106
  * Default: get_least_viewed('both', 10);
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.
120
  * Default: get_most_viewed('both', 10);
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')`.
134
  * The third value you pass in is the maximum number of post you want to get.
135
  * Default: get_least_viewed_tag(1, 'both', 10);
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')`.
149
  * The third value you pass in is the maximum number of post you want to get.
150
  * Default: get_most_viewed_tag(1, 'both', 10);
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')`.
164
  * The third value you pass in is the maximum number of post you want to get.
165
  * Default: get_least_viewed_category(1, 'both', 10);
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')`.
179
  * The third value you pass in is the maximum number of post you want to get.
180
  * Default: get_most_viewed_category(1, 'both', 10);
181
 
182
+ ### To Sort Most/Least Viewed Posts
183
  * You can use: `<?php query_posts( array( 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); ?>`
184
  * Or pass in the variables to the URL: `http://yoursite.com/?v_sortby=views&v_orderby=desc`
185
  * You can replace DESC with ASC if you want the least viewed posts.
186
 
187
+ ### To Display Updating View Count With LiteSpeed Cache
188
+ Use: `<div id="postviews_lscwp"></div>` to replace `<?php if(function_exists('the_views')) { the_views(); } ?>`.
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,
196
+ data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews",
197
+ cache:!1,
198
+ success:function(data) {
199
+ if(data) {
200
+ jQuery('#postviews_lscwp').html(data+' views');
201
+ }
202
+ }
203
+ });
204
+ ```
205
+
206
+ Purge the cache to use the updated pages.
 
uninstall.php CHANGED
@@ -2,20 +2,21 @@
2
  /*
3
  * Uninstall plugin
4
  */
 
5
  if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
6
  exit ();
7
 
8
  if ( is_multisite() ) {
9
- $ms_sites = wp_get_sites();
10
 
11
- if( 0 < sizeof( $ms_sites ) ) {
12
  foreach ( $ms_sites as $ms_site ) {
13
- switch_to_blog( $ms_site['blog_id'] );
 
14
  uninstall();
 
15
  }
16
  }
17
-
18
- restore_current_blog();
19
  } else {
20
  uninstall();
21
  }
@@ -25,11 +26,11 @@ function uninstall() {
25
 
26
  $option_names = array( 'views_options', 'widget_views_most_viewed', 'widget_views' );
27
 
28
- if( sizeof( $option_names ) > 0 ) {
29
  foreach( $option_names as $option_name ) {
30
  delete_option( $option_name );
31
  }
32
  }
33
 
34
  $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'views'" );
35
- }
2
  /*
3
  * Uninstall plugin
4
  */
5
+
6
  if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
7
  exit ();
8
 
9
  if ( is_multisite() ) {
10
+ $ms_sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites();
11
 
12
+ if( 0 < count( $ms_sites ) ) {
13
  foreach ( $ms_sites as $ms_site ) {
14
+ $blog_id = class_exists( 'WP_Site' ) ? $ms_site->blog_id : $ms_site['blog_id'];
15
+ switch_to_blog( $blog_id );
16
  uninstall();
17
+ restore_current_blog();
18
  }
19
  }
 
 
20
  } else {
21
  uninstall();
22
  }
26
 
27
  $option_names = array( 'views_options', 'widget_views_most_viewed', 'widget_views' );
28
 
29
+ if( count( $option_names ) > 0 ) {
30
  foreach( $option_names as $option_name ) {
31
  delete_option( $option_name );
32
  }
33
  }
34
 
35
  $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'views'" );
36
+ }
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.74
7
  Author: Lester 'GaMerZ' Chan
8
  Author URI: https://lesterchan.net
9
  Text Domain: wp-postviews
@@ -11,633 +11,653 @@ Text Domain: wp-postviews
11
 
12
 
13
  /*
14
- Copyright 2016 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
- global $wpdb;
237
- $views_options = get_option('views_options');
238
- $where = '';
239
- $temp = '';
240
- $output = '';
241
- if(!empty($mode) && $mode != 'both') {
242
- if(is_array($mode)) {
243
- $mode = implode("','",$mode);
244
- $where = "post_type IN ('".$mode."')";
245
- } else {
246
- $where = "post_type = '$mode'";
247
- }
248
- } else {
249
- $where = '1=1';
250
- }
251
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
252
- if($most_viewed) {
253
- foreach ($most_viewed as $post) {
254
- $post_views = intval($post->views);
255
- $post_title = get_the_title($post);
256
- if($chars > 0) {
257
- $post_title = snippet_text($post_title, $chars);
258
- }
259
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
260
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
261
- $temp = stripslashes($views_options['most_viewed_template']);
262
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
263
- $temp = str_replace('%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp);
264
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
265
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
266
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
267
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
268
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
269
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
270
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
271
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
272
- $output .= $temp;
273
- }
274
- } else {
275
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
276
- }
277
- if($display) {
278
- echo $output;
279
- } else {
280
- return $output;
281
- }
282
- }
 
 
 
 
 
 
 
 
283
  }
284
 
285
 
286
  ### Function: Display Most Viewed Page/Post
287
- if(!function_exists('get_most_viewed')) {
288
- function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
289
- global $wpdb;
290
- $views_options = get_option('views_options');
291
- $where = '';
292
- $temp = '';
293
- $output = '';
294
- if(!empty($mode) && $mode != 'both') {
295
- if(is_array($mode)) {
296
- $mode = implode("','",$mode);
297
- $where = "post_type IN ('".$mode."')";
298
- } else {
299
- $where = "post_type = '$mode'";
300
- }
301
- } else {
302
- $where = '1=1';
303
- }
304
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
305
- if($most_viewed) {
306
- foreach ($most_viewed as $post) {
307
- $post_views = intval($post->views);
308
- $post_title = get_the_title($post);
309
- if($chars > 0) {
310
- $post_title = snippet_text($post_title, $chars);
311
- }
312
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
313
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
314
- $temp = stripslashes($views_options['most_viewed_template']);
315
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
316
- $temp = str_replace('%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp);
317
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
318
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
319
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
320
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
321
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
322
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
323
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
324
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
325
- $output .= $temp;
326
- }
327
- } else {
328
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
329
- }
330
- if($display) {
331
- echo $output;
332
- } else {
333
- return $output;
334
- }
335
- }
 
 
 
 
 
 
 
 
336
  }
337
 
338
 
339
  ### Function: Display Least Viewed Page/Post By Category ID
340
- if(!function_exists('get_least_viewed_category')) {
341
- function get_least_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
342
- global $wpdb;
343
- $views_options = get_option('views_options');
344
- $where = '';
345
- $temp = '';
346
- $output = '';
347
- if(is_array($category_id)) {
348
- $category_sql = 'tt.term_id IN ('.join(',', $category_id).')';
349
- } else {
350
- $category_sql = "tt.term_id = $category_id";
351
- }
352
- if(!empty($mode) && $mode != 'both') {
353
- if(is_array($mode)) {
354
- $mode = implode("','",$mode);
355
- $where = "post_type IN ('".$mode."')";
356
- } else {
357
- $where = "post_type = '$mode'";
358
- }
359
- } else {
360
- $where = '1=1';
361
- }
362
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category' AND $category_sql) WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
363
- if($most_viewed) {
364
- foreach ($most_viewed as $post) {
365
- $post_views = intval($post->views);
366
- $post_title = get_the_title($post);
367
- if($chars > 0) {
368
- $post_title = snippet_text($post_title, $chars);
369
- }
370
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
371
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
372
- $temp = stripslashes($views_options['most_viewed_template']);
373
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
374
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
375
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
376
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
377
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
378
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
379
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
380
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
381
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
382
- $output .= $temp;
383
- }
384
- } else {
385
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
386
- }
387
- if($display) {
388
- echo $output;
389
- } else {
390
- return $output;
391
- }
392
- }
 
 
 
 
 
393
  }
394
 
395
 
396
  ### Function: Display Most Viewed Page/Post By Category ID
397
- if(!function_exists('get_most_viewed_category')) {
398
- function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
399
- global $wpdb;
400
- $views_options = get_option('views_options');
401
- $where = '';
402
- $temp = '';
403
- $output = '';
404
- if(is_array($category_id)) {
405
- $category_sql = 'tt.term_id IN ('.join(',', $category_id).')';
406
- } else {
407
- $category_sql = "tt.term_id = $category_id";
408
- }
409
- if(!empty($mode) && $mode != 'both') {
410
- if(is_array($mode)) {
411
- $mode = implode("','",$mode);
412
- $where = "post_type IN ('".$mode."')";
413
- } else {
414
- $where = "post_type = '$mode'";
415
- }
416
- } else {
417
- $where = '1=1';
418
- }
419
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'category' AND $category_sql) WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
420
- if($most_viewed) {
421
- foreach ($most_viewed as $post) {
422
- $post_views = intval($post->views);
423
- $post_title = get_the_title($post);
424
- if($chars > 0) {
425
- $post_title = snippet_text($post_title, $chars);
426
- }
427
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
428
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
429
- $temp = stripslashes($views_options['most_viewed_template']);
430
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
431
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
432
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
433
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
434
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
435
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
436
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
437
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
438
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
439
- $output .= $temp;
440
- }
441
- } else {
442
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
443
- }
444
- if($display) {
445
- echo $output;
446
- } else {
447
- return $output;
448
- }
449
- }
 
 
 
 
 
450
  }
451
 
452
-
453
- ### Function: Display Most Viewed Page/Post By Tag ID
454
- if(!function_exists('get_most_viewed_tag')) {
455
- function get_most_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
456
- global $wpdb;
457
- $views_options = get_option('views_options');
458
- $where = '';
459
- $temp = '';
460
- $output = '';
461
- if(is_array($tag_id)) {
462
- $tag_sql = 'tt.term_id IN ('.join(',', $tag_id).')';
463
- } else {
464
- $tag_sql = "tt.term_id = $tag_id";
465
- }
466
- if(!empty($mode) && $mode != 'both') {
467
- if(is_array($mode)) {
468
- $mode = implode("','",$mode);
469
- $where = "post_type IN ('".$mode."')";
470
- } else {
471
- $where = "post_type = '$mode'";
472
- }
473
- } else {
474
- $where = '1=1';
475
- }
476
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt2.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag' AND $tag_sql) INNER JOIN $wpdb->term_relationships tr2 ON (p.ID = tr2.object_id) LEFT JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views DESC LIMIT $limit");
477
- if($most_viewed) {
478
- foreach ($most_viewed as $post) {
479
- $post_views = intval($post->views);
480
- $post_title = get_the_title($post);
481
- if($chars > 0) {
482
- $post_title = snippet_text($post_title, $chars);
483
- }
484
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
485
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
486
- $temp = stripslashes($views_options['most_viewed_template']);
487
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
488
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
489
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
490
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
491
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
492
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
493
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
494
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
495
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
496
- $output .= $temp;
497
- }
498
- } else {
499
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
500
- }
501
- if($display) {
502
- echo $output;
503
- } else {
504
- return $output;
505
- }
506
- }
 
 
 
 
507
  }
508
 
509
 
510
- ### Function: Display Least Viewed Page/Post By Tag ID
511
- if(!function_exists('get_least_viewed_tag')) {
512
- function get_least_viewed_tag($tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
513
- global $wpdb;
514
- $views_options = get_option('views_options');
515
- $where = '';
516
- $temp = '';
517
- $output = '';
518
- if(is_array($tag_id)) {
519
- $tag_sql = 'tt.term_id IN ('.join(',', $tag_id).')';
520
- } else {
521
- $tag_sql = "tt.term_id = $tag_id";
522
- }
523
- if(!empty($mode) && $mode != 'both') {
524
- if(is_array($mode)) {
525
- $mode = implode("','",$mode);
526
- $where = "post_type IN ('".$mode."')";
527
- } else {
528
- $where = "post_type = '$mode'";
529
- }
530
- } else {
531
- $where = '1=1';
532
- }
533
- $most_viewed = $wpdb->get_results("SELECT DISTINCT p.*, (pm1.meta_value+0) AS views, IF((pm2.meta_value IS NULL) OR (pm2.meta_value = ''), tt2.term_id, pm2.meta_value) AS cat_id FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pm1 ON pm1.post_id = p.ID LEFT JOIN $wpdb->postmeta pm2 ON (pm2.post_id = p.ID AND pm2.meta_key='_yoast_wpseo_primary_category') INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag' AND $tag_sql) INNER JOIN $wpdb->term_relationships tr2 ON (p.ID = tr2.object_id) LEFT JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category') WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND pm1.meta_key = 'views' AND post_password = '' GROUP BY p.ID ORDER BY views ASC LIMIT $limit");
534
- if($most_viewed) {
535
- foreach ($most_viewed as $post) {
536
- $post_views = intval($post->views);
537
- $post_title = get_the_title($post);
538
- if($chars > 0) {
539
- $post_title = snippet_text($post_title, $chars);
540
- }
541
- $thumbnail = get_the_post_thumbnail($post->ID,'thumbnail',true);
542
- $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
543
- $temp = stripslashes($views_options['most_viewed_template']);
544
- $temp = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $temp);
545
- $temp = str_replace('%POST_TITLE%', $post_title, $temp);
546
- $temp = str_replace('%POST_EXCERPT%', $post_excerpt, $temp);
547
- $temp = str_replace('%POST_CONTENT%', $post->post_content, $temp);
548
- $temp = str_replace('%POST_URL%', get_permalink($post), $temp);
549
- $temp = str_replace('%POST_DATE%', get_the_time(get_option('date_format'), $post), $temp);
550
- $temp = str_replace('%POST_TIME%', get_the_time(get_option('time_format'), $post), $temp);
551
- $temp = str_replace('%POST_THUMBNAIL%', $thumbnail, $temp);
552
- $temp = str_replace('%POST_CATEGORY_ID%', $post->cat_id, $temp);
553
- $output .= $temp;
554
- }
555
- } else {
556
- $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
557
- }
558
- if($display) {
559
- echo $output;
560
- } else {
561
- return $output;
562
- }
563
- }
 
 
 
 
 
564
  }
565
 
566
 
567
  ### Function: Display Total Views
568
  if(!function_exists('get_totalviews')) {
569
- function get_totalviews($display = true) {
570
- global $wpdb;
571
- $total_views = intval($wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'"));
572
- if($display) {
573
- echo number_format_i18n($total_views);
574
- } else {
575
- return $total_views;
576
- }
577
- }
578
  }
579
 
580
 
581
  ### Function: Snippet Text
582
  if(!function_exists('snippet_text')) {
583
- function snippet_text($text, $length = 0) {
584
- if (defined('MB_OVERLOAD_STRING')) {
585
- $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
586
- if (mb_strlen($text) > $length) {
587
- return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
588
- } else {
589
- return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
590
- }
591
- } else {
592
- $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
593
- if (strlen($text) > $length) {
594
- return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
595
- } else {
596
- return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
597
- }
598
- }
599
- }
600
- }
601
-
602
-
603
- ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
604
- function views_post_excerpt($post_excerpt, $post_content, $post_password, $chars = 200) {
605
- if(!empty($post_password)) {
606
- if(!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post_password) {
607
- return __('There is no excerpt because this is a protected post.', 'wp-postviews');
608
- }
609
- }
610
- if(empty($post_excerpt)) {
611
- return snippet_text(strip_tags($post_content), $chars);
612
- } else {
613
- return $post_excerpt;
614
- }
615
  }
616
 
617
 
618
  ### Function: Modify Default WordPress Listing To Make It Sorted By Post Views
619
  function views_fields($content) {
620
- global $wpdb;
621
- $content .= ", ($wpdb->postmeta.meta_value+0) AS views";
622
- return $content;
623
  }
624
  function views_join($content) {
625
- global $wpdb;
626
- $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
627
- return $content;
628
  }
629
  function views_where($content) {
630
- global $wpdb;
631
- $content .= " AND $wpdb->postmeta.meta_key = 'views'";
632
- return $content;
633
  }
634
  function views_orderby($content) {
635
- $orderby = trim(addslashes(get_query_var('v_orderby')));
636
- if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
637
- $orderby = 'desc';
638
- }
639
- $content = " views $orderby";
640
- return $content;
641
  }
642
 
643
 
@@ -645,109 +665,109 @@ function views_orderby($content) {
645
  add_action('publish_post', 'add_views_fields');
646
  add_action('publish_page', 'add_views_fields');
647
  function add_views_fields($post_ID) {
648
- global $wpdb;
649
- if(!wp_is_post_revision($post_ID)) {
650
- add_post_meta($post_ID, 'views', 0, true);
651
- }
652
  }
653
 
654
 
655
  ### Function: Views Public Variables
656
  add_filter('query_vars', 'views_variables');
657
  function views_variables($public_query_vars) {
658
- $public_query_vars[] = 'v_sortby';
659
- $public_query_vars[] = 'v_orderby';
660
- return $public_query_vars;
661
  }
662
 
663
 
664
  ### Function: Sort Views Posts
665
  add_action('pre_get_posts', 'views_sorting');
666
  function views_sorting($local_wp_query) {
667
- if($local_wp_query->get('v_sortby') == 'views') {
668
- add_filter('posts_fields', 'views_fields');
669
- add_filter('posts_join', 'views_join');
670
- add_filter('posts_where', 'views_where');
671
- add_filter('posts_orderby', 'views_orderby');
672
- } else {
673
- remove_filter('posts_fields', 'views_fields');
674
- remove_filter('posts_join', 'views_join');
675
- remove_filter('posts_where', 'views_where');
676
- remove_filter('posts_orderby', 'views_orderby');
677
- }
678
  }
679
 
680
 
681
  ### Function: Plug Into WP-Stats
682
  add_action( 'plugins_loaded', 'postviews_wp_stats' );
683
  function postviews_wp_stats() {
684
- add_filter( 'wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats' );
685
- add_filter( 'wp_stats_page_admin_most', 'postviews_page_admin_most_stats' );
686
- add_filter( 'wp_stats_page_plugins', 'postviews_page_general_stats' );
687
- add_filter( 'wp_stats_page_most', 'postviews_page_most_stats' );
688
  }
689
 
690
 
691
  ### Function: Add WP-PostViews General Stats To WP-Stats Page Options
692
  function postviews_page_admin_general_stats($content) {
693
- $stats_display = get_option('stats_display');
694
- if($stats_display['views'] == 1) {
695
- $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";
696
- } else {
697
- $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";
698
- }
699
- return $content;
700
  }
701
 
702
 
703
  ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options
704
  function postviews_page_admin_most_stats($content) {
705
- $stats_display = get_option('stats_display');
706
- $stats_mostlimit = intval(get_option('stats_mostlimit'));
707
- if($stats_display['viewed_most_post'] == 1) {
708
- $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";
709
- } else {
710
- $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";
711
- }
712
- if($stats_display['viewed_most_page'] == 1) {
713
- $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";
714
- } else {
715
- $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";
716
- }
717
- return $content;
718
  }
719
 
720
 
721
  ### Function: Add WP-PostViews General Stats To WP-Stats Page
722
  function postviews_page_general_stats($content) {
723
- $stats_display = get_option('stats_display');
724
- if($stats_display['views'] == 1) {
725
- $content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n";
726
- $content .= '<ul>'."\n";
727
- $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";
728
- $content .= '</ul>'."\n";
729
- }
730
- return $content;
731
  }
732
 
733
 
734
  ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page
735
  function postviews_page_most_stats($content) {
736
- $stats_display = get_option('stats_display');
737
- $stats_mostlimit = intval(get_option('stats_mostlimit'));
738
- if($stats_display['viewed_most_post'] == 1) {
739
- $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";
740
- $content .= '<ul>'."\n";
741
- $content .= get_most_viewed('post', $stats_mostlimit, 0, false);
742
- $content .= '</ul>'."\n";
743
- }
744
- if($stats_display['viewed_most_page'] == 1) {
745
- $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";
746
- $content .= '<ul>'."\n";
747
- $content .= get_most_viewed('page', $stats_mostlimit, 0, false);
748
- $content .= '</ul>'."\n";
749
- }
750
- return $content;
751
  }
752
 
753
 
@@ -755,26 +775,26 @@ function postviews_page_most_stats($content) {
755
  add_action( 'wp_ajax_postviews', 'increment_views' );
756
  add_action( 'wp_ajax_nopriv_postviews', 'increment_views' );
757
  function increment_views() {
758
- if( empty( $_GET['postviews_id'] ) )
759
- return;
760
 
761
- if( !defined( 'WP_CACHE' ) || !WP_CACHE )
762
- return;
763
 
764
- $views_options = get_option( 'views_options' );
765
 
766
- if( isset( $views_options['use_ajax'] ) && intval( $views_options['use_ajax'] ) === 0 )
767
- return;
768
 
769
- $post_id = intval( $_GET['postviews_id'] );
770
- if( $post_id > 0 ) {
771
- $post_views = get_post_custom( $post_id );
772
- $post_views = intval( $post_views['views'][0] );
773
- update_post_meta( $post_id, 'views', ( $post_views + 1 ) );
774
- do_action( 'postviews_increment_views_ajax', ( $post_views + 1 ) );
775
- echo ( $post_views + 1 );
776
- exit();
777
- }
778
  }
779
 
780
  ### Function Show Post Views Column in WP-Admin
@@ -783,16 +803,16 @@ add_filter('manage_posts_columns', 'add_postviews_column');
783
  add_action('manage_pages_custom_column', 'add_postviews_column_content');
784
  add_filter('manage_pages_columns', 'add_postviews_column');
785
  function add_postviews_column($defaults) {
786
- $defaults['views'] = __( 'Views', 'wp-postviews' );
787
- return $defaults;
788
  }
789
 
790
 
791
  ### Functions Fill In The Views Count
792
  function add_postviews_column_content($column_name) {
793
- if($column_name == 'views') {
794
- if(function_exists('the_views')) { the_views(true, '', '', true); }
795
- }
796
  }
797
 
798
 
@@ -801,191 +821,186 @@ add_filter('manage_edit-post_sortable_columns', 'sort_postviews_column');
801
  add_filter('manage_edit-page_sortable_columns', 'sort_postviews_column');
802
  function sort_postviews_column($defaults)
803
  {
804
- $defaults['views'] = 'views';
805
- return $defaults;
806
  }
807
  add_action('pre_get_posts', 'sort_postviews');
808
  function sort_postviews($query) {
809
- if(!is_admin())
810
- return;
811
- $orderby = $query->get('orderby');
812
- if('views' == $orderby) {
813
- $query->set('meta_key', 'views');
814
- $query->set('orderby', 'meta_value_num');
815
- }
816
  }
817
 
818
  ### Function: Round Numbers To K (Thousand), M (Million) or B (Billion)
819
  function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) {
820
- if( $number < $min_value ) {
821
- return number_format_i18n( $number );
822
- }
823
- $alphabets = array( 1000000000 => 'B', 1000000 => 'M', 1000 => 'K' );
824
- foreach( $alphabets as $key => $value )
825
- if( $number >= $key ) {
826
- return round( $number / $key, $decimal ) . '' . $value;
827
- }
828
  }
829
 
830
 
831
  ### Class: WP-PostViews Widget
832
  class WP_Widget_PostViews extends WP_Widget {
833
- // Constructor
834
- function __construct() {
835
- $widget_ops = array('description' => __('WP-PostViews views statistics', 'wp-postviews'));
836
- parent::__construct('views', __('Views', 'wp-postviews'), $widget_ops);
837
- }
838
-
839
- // Display Widget
840
- function widget($args, $instance) {
841
- $title = apply_filters('widget_title', esc_attr($instance['title']));
842
- $type = esc_attr($instance['type']);
843
- $mode = esc_attr($instance['mode']);
844
- $limit = intval($instance['limit']);
845
- $chars = intval($instance['chars']);
846
- $cat_ids = explode(',', esc_attr($instance['cat_ids']));
847
- echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
848
- echo '<ul>'."\n";
849
- switch($type) {
850
- case 'least_viewed':
851
- get_least_viewed($mode, $limit, $chars);
852
- break;
853
- case 'most_viewed':
854
- get_most_viewed($mode, $limit, $chars);
855
- break;
856
- case 'most_viewed_category':
857
- get_most_viewed_category($cat_ids, $mode, $limit, $chars);
858
- break;
859
- case 'least_viewed_category':
860
- get_least_viewed_category($cat_ids, $mode, $limit, $chars);
861
- break;
862
- }
863
- echo '</ul>'."\n";
864
- echo $args['after_widget'];
865
- }
866
-
867
- // When Widget Control Form Is Posted
868
- function update($new_instance, $old_instance) {
869
- if (!isset($new_instance['submit'])) {
870
- return false;
871
- }
872
- $instance = $old_instance;
873
- $instance['title'] = strip_tags($new_instance['title']);
874
- $instance['type'] = strip_tags($new_instance['type']);
875
- $instance['mode'] = strip_tags($new_instance['mode']);
876
- $instance['limit'] = intval($new_instance['limit']);
877
- $instance['chars'] = intval($new_instance['chars']);
878
- $instance['cat_ids'] = strip_tags($new_instance['cat_ids']);
879
- return $instance;
880
- }
881
-
882
- // DIsplay Widget Control Form
883
- function form($instance) {
884
- $instance = wp_parse_args((array) $instance, array('title' => __('Views', 'wp-postviews'), 'type' => 'most_viewed', 'mode' => '', 'limit' => 10, 'chars' => 200, 'cat_ids' => '0'));
885
- $title = esc_attr($instance['title']);
886
- $type = esc_attr($instance['type']);
887
- $mode = trim(esc_attr($instance['mode']));
888
- $limit = intval($instance['limit']);
889
- $chars = intval($instance['chars']);
890
- $cat_ids = esc_attr($instance['cat_ids']);
891
- $post_types = get_post_types(array(
892
- 'public' => true
893
- ));
894
  ?>
895
- <p>
896
- <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>
897
- </p>
898
- <p>
899
- <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postviews'); ?>
900
- <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
901
- <option value="least_viewed"<?php selected('least_viewed', $type); ?>><?php _e('Least Viewed', 'wp-postviews'); ?></option>
902
- <option value="least_viewed_category"<?php selected('least_viewed_category', $type); ?>><?php _e('Least Viewed By Category', 'wp-postviews'); ?></option>
903
- <optgroup>&nbsp;</optgroup>
904
- <option value="most_viewed"<?php selected('most_viewed', $type); ?>><?php _e('Most Viewed', 'wp-postviews'); ?></option>
905
- <option value="most_viewed_category"<?php selected('most_viewed_category', $type); ?>><?php _e('Most Viewed By Category', 'wp-postviews'); ?></option>
906
- </select>
907
- </label>
908
- </p>
909
- <p>
910
- <label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Views From:', 'wp-postviews'); ?>
911
- <select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat">
912
- <option value=""<?php selected('', $mode); ?>><?php _e('All', 'wp-postviews'); ?></option>
913
- <?php if($post_types > 0): ?>
914
- <?php foreach($post_types as $post_type): ?>
915
- <option value="<?php echo $post_type; ?>"<?php selected($post_type, $mode); ?>><?php printf(__('%s Only', 'wp-postviews'), ucfirst($post_type)); ?></option>
916
- <?php endforeach; ?>
917
- <?php endif; ?>
918
- </select>
919
- </label>
920
- </p>
921
- <p>
922
- <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>
923
- </p>
924
- <p>
925
- <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 />
926
- <small><?php _e('<strong>0</strong> to disable.', 'wp-postviews'); ?></small>
927
- </p>
928
- <p>
929
- <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 />
930
- <small><?php _e('Separate mutiple categories with commas.', 'wp-postviews'); ?></small>
931
- </p>
932
- <p style="color: red;">
933
- <small><?php _e('* If you are not using any category statistics, you can ignore it.', 'wp-postviews'); ?></small>
934
- <p>
935
- <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
936
  <?php
937
- }
938
  }
939
 
940
 
941
  ### Function: Init WP-PostViews Widget
942
  add_action( 'widgets_init', 'widget_views_init' );
943
  function widget_views_init() {
944
- register_widget( 'WP_Widget_PostViews' );
945
  }
946
 
947
 
948
  ### Function: Post Views Options
949
  register_activation_hook( __FILE__, 'views_activation' );
950
  function views_activation( $network_wide ) {
951
- // Add Options
952
- $option_name = 'views_options';
953
- $option = array(
954
- 'count' => 1
955
- , 'exclude_bots' => 0
956
- , 'display_home' => 0
957
- , 'display_single' => 0
958
- , 'display_page' => 0
959
- , 'display_archive' => 0
960
- , 'display_search' => 0
961
- , 'display_other' => 0
962
- , 'use_ajax' => 1
963
- , 'template' => __('%VIEW_COUNT% views', 'wp-postviews')
964
- , 'most_viewed_template' => '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>'
965
- );
966
-
967
- if ( is_multisite() && $network_wide )
968
- {
969
- $ms_sites = wp_get_sites();
970
-
971
- if( 0 < sizeof( $ms_sites ) )
972
- {
973
- foreach ( $ms_sites as $ms_site )
974
- {
975
- switch_to_blog( $ms_site['blog_id'] );
976
- add_option( $option_name, $option );
977
- }
978
- }
979
-
980
- restore_current_blog();
981
- }
982
- else
983
- {
984
- add_option( $option_name, $option );
985
- }
986
  }
987
 
988
  ### Function: Parse View Options
989
  function views_options_parse($key) {
990
- return !empty($_POST[$key]) ? $_POST[$key] : null;
991
  }
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
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
 
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
 
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
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
 
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
  }