Version Description
Download this release
Release Info
Developer | GamerZ |
Plugin | WP-PostViews |
Version | 1.75 |
Comparing to | |
See all releases |
Code changes from version 1.74 to 1.75
- readme.txt +84 -172
- uninstall.php +8 -7
- wp-postviews.php +801 -786
readme.txt
CHANGED
@@ -1,59 +1,70 @@
|
|
1 |
-
|
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.
|
8 |
|
9 |
Enables you to display how many times a post/page had been viewed.
|
10 |
|
11 |
-
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
[![Build Status](https://travis-ci.org/lesterchan/wp-postviews.svg?branch=master)](https://travis-ci.org/lesterchan/wp-postviews)
|
15 |
|
16 |
-
|
17 |
[https://github.com/lesterchan/wp-postviews/](https://github.com/lesterchan/wp-postviews/ "https://github.com/lesterchan/wp-postviews/")
|
18 |
|
19 |
-
|
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 |
-
|
23 |
* Plugin icon by [Iconmoon](http://www.icomoon.io) from [Flaticon](http://www.flaticon.com)
|
24 |
|
25 |
-
|
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 |
-
|
29 |
-
|
|
|
|
|
|
|
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 |
-
|
34 |
* FIXED: In preview mode, don't count views
|
35 |
|
36 |
-
|
37 |
* NEW: Add %POST_THUMBNAIL% to template variables
|
38 |
|
39 |
-
|
40 |
* FIXED: Notices in Widget Constructor for WordPress 4.3
|
41 |
|
42 |
-
|
43 |
* FIXED: Integration with WP-Stats
|
44 |
|
45 |
-
|
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 |
-
|
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 |
-
|
54 |
* NEW: Allow user to not use AJAX to update the views even though WP_CACHE is true
|
55 |
|
56 |
-
|
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 |
-
|
66 |
* FIXED: Views not showing in WP-Admin if "Display Options" is not set to "Display to everyone"
|
67 |
|
68 |
-
|
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 |
-
|
175 |
|
176 |
1. PostViews
|
177 |
2. Admin - PostViews Options
|
178 |
|
179 |
-
|
180 |
|
181 |
-
|
182 |
* Go to `WP-Admin -> Appearance -> Widgets`
|
183 |
* The widget name is Views.
|
184 |
|
185 |
-
|
186 |
|
187 |
-
|
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 |
-
|
|
|
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 |
-
|
201 |
-
|
202 |
-
|
203 |
<?php if (function_exists('get_most_viewed')): ?>
|
204 |
<ul>
|
205 |
<?php get_most_viewed(); ?>
|
206 |
</ul>
|
207 |
<?php endif; ?>
|
208 |
-
|
|
|
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 |
-
|
214 |
-
|
215 |
-
|
216 |
<?php if (function_exists('get_least_viewed_tag')): ?>
|
217 |
<ul>
|
218 |
<?php get_least_viewed_tag(); ?>
|
219 |
</ul>
|
220 |
<?php endif; ?>
|
221 |
-
|
|
|
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 |
-
|
228 |
-
|
229 |
-
|
230 |
<?php if (function_exists('get_most_viewed_tag')): ?>
|
231 |
<ul>
|
232 |
<?php get_most_viewed_tag(); ?>
|
233 |
</ul>
|
234 |
<?php endif; ?>
|
235 |
-
|
|
|
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 |
-
|
242 |
-
|
243 |
-
|
244 |
<?php if (function_exists('get_least_viewed_category')): ?>
|
245 |
<ul>
|
246 |
<?php get_least_viewed_category(); ?>
|
247 |
</ul>
|
248 |
<?php endif; ?>
|
249 |
-
|
|
|
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 |
-
|
256 |
-
|
257 |
-
|
258 |
<?php if (function_exists('get_most_viewed_category')): ?>
|
259 |
<ul>
|
260 |
<?php get_most_viewed_category(); ?>
|
261 |
</ul>
|
262 |
<?php endif; ?>
|
263 |
-
|
|
|
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 |
-
|
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 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
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 <
|
12 |
foreach ( $ms_sites as $ms_site ) {
|
13 |
-
|
|
|
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(
|
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.
|
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 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
*/
|
30 |
|
31 |
|
32 |
### Create Text Domain For Translations
|
33 |
add_action( 'plugins_loaded', 'postviews_textdomain' );
|
34 |
function postviews_textdomain() {
|
35 |
-
|
36 |
}
|
37 |
|
38 |
|
39 |
### Function: Post Views Option Menu
|
40 |
add_action('admin_menu', 'postviews_menu');
|
41 |
function postviews_menu() {
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
}
|
46 |
|
47 |
|
48 |
### Function: Calculate Post Views
|
49 |
add_action( 'wp_head', 'process_postviews' );
|
50 |
function process_postviews() {
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
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 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
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 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
}
|
198 |
|
199 |
|
200 |
### Function: Display The Post Views
|
201 |
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
}
|
216 |
|
217 |
### Function: Short Code For Inserting Views Into Posts
|
218 |
add_shortcode( 'views', 'views_shortcode' );
|
219 |
function views_shortcode( $atts ) {
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
|
229 |
-
|
230 |
}
|
231 |
|
232 |
|
233 |
### Function: Display Least Viewed Page/Post
|
234 |
-
if(!function_exists('get_least_viewed')) {
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
}
|
284 |
|
285 |
|
286 |
### Function: Display Most Viewed Page/Post
|
287 |
-
if(!function_exists('get_most_viewed')) {
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
}
|
337 |
|
338 |
|
339 |
### Function: Display Least Viewed Page/Post By Category ID
|
340 |
-
if(!function_exists('get_least_viewed_category')) {
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
|
|
393 |
}
|
394 |
|
395 |
|
396 |
### Function: Display Most Viewed Page/Post By Category ID
|
397 |
-
if(!function_exists('get_most_viewed_category')) {
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
|
|
|
|
|
|
|
|
|
|
450 |
}
|
451 |
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
|
|
|
|
|
|
|
|
507 |
}
|
508 |
|
509 |
|
510 |
-
### Function: Display
|
511 |
-
if(!function_exists('
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
564 |
}
|
565 |
|
566 |
|
567 |
### Function: Display Total Views
|
568 |
if(!function_exists('get_totalviews')) {
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
}
|
579 |
|
580 |
|
581 |
### Function: Snippet Text
|
582 |
if(!function_exists('snippet_text')) {
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
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 |
-
|
621 |
-
|
622 |
-
|
623 |
}
|
624 |
function views_join($content) {
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
}
|
629 |
function views_where($content) {
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
}
|
634 |
function views_orderby($content) {
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
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 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
}
|
653 |
|
654 |
|
655 |
### Function: Views Public Variables
|
656 |
add_filter('query_vars', 'views_variables');
|
657 |
function views_variables($public_query_vars) {
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
}
|
662 |
|
663 |
|
664 |
### Function: Sort Views Posts
|
665 |
add_action('pre_get_posts', 'views_sorting');
|
666 |
function views_sorting($local_wp_query) {
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
}
|
679 |
|
680 |
|
681 |
### Function: Plug Into WP-Stats
|
682 |
add_action( 'plugins_loaded', 'postviews_wp_stats' );
|
683 |
function postviews_wp_stats() {
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
}
|
689 |
|
690 |
|
691 |
### Function: Add WP-PostViews General Stats To WP-Stats Page Options
|
692 |
function postviews_page_admin_general_stats($content) {
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
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 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
}
|
719 |
|
720 |
|
721 |
### Function: Add WP-PostViews General Stats To WP-Stats Page
|
722 |
function postviews_page_general_stats($content) {
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
}
|
732 |
|
733 |
|
734 |
### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page
|
735 |
function postviews_page_most_stats($content) {
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
|
748 |
-
|
749 |
-
|
750 |
-
|
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 |
-
|
759 |
-
|
760 |
|
761 |
-
|
762 |
-
|
763 |
|
764 |
-
|
765 |
|
766 |
-
|
767 |
-
|
768 |
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
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 |
-
|
787 |
-
|
788 |
}
|
789 |
|
790 |
|
791 |
### Functions Fill In The Views Count
|
792 |
function add_postviews_column_content($column_name) {
|
793 |
-
|
794 |
-
|
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 |
-
|
805 |
-
|
806 |
}
|
807 |
add_action('pre_get_posts', 'sort_postviews');
|
808 |
function sort_postviews($query) {
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
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 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
}
|
829 |
|
830 |
|
831 |
### Class: WP-PostViews Widget
|
832 |
class WP_Widget_PostViews extends WP_Widget {
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
?>
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
|
914 |
-
|
915 |
-
|
916 |
-
|
917 |
-
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
|
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 |
-
|
945 |
}
|
946 |
|
947 |
|
948 |
### Function: Post Views Options
|
949 |
register_activation_hook( __FILE__, 'views_activation' );
|
950 |
function views_activation( $network_wide ) {
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
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 |
-
|
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" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
|
716 |
+
} else {
|
717 |
+
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" /> <label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
|
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" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
729 |
+
} else {
|
730 |
+
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_post" value="viewed_most_post" /> <label for="wpstats_viewed_most_post">'.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
731 |
+
}
|
732 |
+
if($stats_display['viewed_most_page'] == 1) {
|
733 |
+
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" checked="checked" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
734 |
+
} else {
|
735 |
+
$content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most_page" value="viewed_most_page" /> <label for="wpstats_viewed_most_page">'.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
|
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> </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 |
}
|