├── .editorconfig ├── postviews-cache.js ├── uninstall.php ├── README.md ├── wp-postviews.pot ├── postviews-options.php └── wp-postviews.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | # WordPress Coding Standards 5 | # https://make.wordpress.org/core/handbook/coding-standards/ 6 | 7 | root = true 8 | 9 | [*] 10 | charset = utf-8 11 | end_of_line = lf 12 | insert_final_newline = true 13 | trim_trailing_whitespace = true 14 | indent_style = tab 15 | 16 | [{.jshintrc,*.json,*.yml}] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [{*.txt,wp-config-sample.php}] 21 | end_of_line = crlf 22 | -------------------------------------------------------------------------------- /postviews-cache.js: -------------------------------------------------------------------------------- 1 | fetch( viewsCacheL10n.admin_ajax_url, { 2 | method: "POST", 3 | credentials: 'same-origin', 4 | headers: { 5 | 'Content-Type': 'application/x-www-form-urlencoded', 6 | 'Cache-Control': 'no-cache', 7 | }, 8 | body: new URLSearchParams( 9 | { 10 | action: 'postviews', 11 | nonce: viewsCacheL10n.nonce, 12 | postviews_id: viewsCacheL10n.post_id, 13 | cache: !1, 14 | } 15 | ), 16 | }) 17 | .then(function(response) { 18 | return response.json(); 19 | }) 20 | .then(function(data) { 21 | }) 22 | .catch(function(error) { 23 | console.log('WP-PostViews'); 24 | console.log(error); 25 | }); 26 | -------------------------------------------------------------------------------- /uninstall.php: -------------------------------------------------------------------------------- 1 | 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 | } 23 | 24 | function uninstall() { 25 | global $wpdb; 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 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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: 6.8 7 | Stable tag: 1.78 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//index.php` 15 | 2. You may place it in archive.php, single.php, post.php or page.php also. 16 | 3. Find: `` 17 | 4. Add Anywhere Below It (The Place You Want The Views To Show): `` 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 | ### Development 22 | [https://github.com/lesterchan/wp-postviews/](https://github.com/lesterchan/wp-postviews/ "https://github.com/lesterchan/wp-postviews/") 23 | 24 | ### Translations 25 | [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/") 26 | 27 | ### Credits 28 | * Plugin icon by [Iconmoon](http://www.icomoon.io) from [Flaticon](http://www.flaticon.com) 29 | 30 | ### Donations 31 | 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. 32 | 33 | ## Changelog 34 | ### Version 1.78 35 | * NEW: Add %POST_THUMBNAIL_URL% to template variables 36 | 37 | ### Version 1.77 38 | * NEW: Use Vanilla JS. Props @JiveDig 39 | * NEW: Bump to WordPress 6.2 40 | * NEW: Support views under fields for Rest API. Props @vitro-mod 41 | 42 | ## Version 1.76.1 43 | * NEW: Add Post Author in views template 44 | * NEW: Bump for WordPress 5.3 45 | 46 | ### Version 1.76 47 | * NEW: Added postviews_should_count filter 48 | * FIXED: Change to (int) from intval() and use sanitize_key() with it. 49 | 50 | ### Version 1.75 51 | * NEW: Use WP_Query() for most/least viewed posts 52 | 53 | ### Version 1.74 54 | * NEW: Bump WordPress 4.7 55 | * 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 56 | 57 | ### Version 1.73 58 | * FIXED: In preview mode, don't count views 59 | 60 | ### Version 1.72 61 | * NEW: Add %POST_THUMBNAIL% to template variables 62 | 63 | ### Version 1.71 64 | * FIXED: Notices in Widget Constructor for WordPress 4.3 65 | 66 | ### Version 1.70 67 | * FIXED: Integration with WP-Stats 68 | 69 | ### Version 1.69 70 | * NEW: Shortcode `[views]` or [views id="POST_ID"]` to embed view count into post 71 | * NEW: Added template variable `%VIEW_COUNT_ROUNDED%` to support rounded view count like 10.1k or 11.2M 72 | 73 | ### Version 1.68 74 | * NEW: Added action hook 'postviews_increment_views' and 'postviews_increment_views_ajax' 75 | * NEW: Allow custom post type to be chosen under the widget 76 | 77 | ### Version 1.67 78 | * NEW: Allow user to not use AJAX to update the views even though WP_CACHE is true 79 | 80 | ### Version 1.66 81 | * NEW: Supports MultiSite Network Activation 82 | * NEW: Add %POST_DATE% and %POST_TIME% to template variables 83 | * NEW: Add China isearch engines bots 84 | * NEW: Ability to pass in an array of post types for get_most/least_*() functions. Props Leo Plaw. 85 | * FIXED: Moved uninstall to uninstall.php and hence fix missing nonce. Props Julio Potier. 86 | * FIXED: Notices and better way to get views from meta. Props daankortenbach. 87 | * FIXED: No longer needing add_post_meta() if update_post_meta() fails. 88 | 89 | ### Version 1.65 (02-06-2013) 90 | * FIXED: Views not showing in WP-Admin if "Display Options" is not set to "Display to everyone" 91 | 92 | ## Upgrade Notice 93 | 94 | N/A 95 | 96 | ## Screenshots 97 | 98 | 1. PostViews 99 | 2. Admin - PostViews Options 100 | 101 | ## Frequently Asked Questions 102 | 103 | ### How To View Stats With Widgets? 104 | * Go to `WP-Admin -> Appearance -> Widgets` 105 | * The widget name is Views. 106 | 107 | ### To Display Least Viewed Posts 108 | 109 | ```php 110 | 111 | 114 | 115 | ``` 116 | 117 | * 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')`. 118 | * The second value you pass in is the maximum number of post you want to get. 119 | * Default: get_least_viewed('both', 10); 120 | 121 | ### To Display Most Viewed Posts 122 | 123 | ```php 124 | 125 | 128 | 129 | ``` 130 | 131 | * 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')`. 132 | * The second value you pass in is the maximum number of post you want to get. 133 | * Default: get_most_viewed('both', 10); 134 | 135 | ### To Display Least Viewed Posts By Tag 136 | 137 | ```php 138 | 139 | 142 | 143 | ``` 144 | 145 | * The first value you pass in is the tag id. 146 | * 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')`. 147 | * The third value you pass in is the maximum number of post you want to get. 148 | * Default: get_least_viewed_tag(1, 'both', 10); 149 | 150 | ### To Display Most Viewed Posts By Tag 151 | 152 | ```php 153 | 154 | 157 | 158 | ``` 159 | 160 | * The first value you pass in is the tag id. 161 | * 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')`. 162 | * The third value you pass in is the maximum number of post you want to get. 163 | * Default: get_most_viewed_tag(1, 'both', 10); 164 | 165 | ### To Display Least Viewed Posts For A Category 166 | 167 | ```php 168 | 169 | 172 | 173 | ``` 174 | 175 | * The first value you pass in is the category id. 176 | * 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')`. 177 | * The third value you pass in is the maximum number of post you want to get. 178 | * Default: get_least_viewed_category(1, 'both', 10); 179 | 180 | ### To Display Most Viewed Posts For A Category 181 | 182 | ```php 183 | 184 | 187 | 188 | ``` 189 | 190 | * The first value you pass in is the category id. 191 | * 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')`. 192 | * The third value you pass in is the maximum number of post you want to get. 193 | * Default: get_most_viewed_category(1, 'both', 10); 194 | 195 | ### To Sort Most/Least Viewed Posts 196 | * You can use: ` 'views', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); ?>` 197 | * Or pass in the variables to the URL: `http://yoursite.com/?v_sortby=views&v_orderby=desc` 198 | * You can replace DESC with ASC if you want the least viewed posts. 199 | 200 | ### To Display Updating View Count With LiteSpeed Cache 201 | Use: `
` to replace ``. 202 | NOTE: The id can be changed, but the div id and the ajax function must match. 203 | Replace the ajax query in `wp-content/plugins/wp-postviews/postviews-cache.js` with 204 | 205 | ```javascript 206 | jQuery.ajax({ 207 | type:"GET", 208 | url:viewsCacheL10n.admin_ajax_url, 209 | data:"postviews_id="+viewsCacheL10n.post_id+"&action=postviews", 210 | cache:!1, 211 | success:function(data) { 212 | if(data) { 213 | jQuery('#postviews_lscwp').html(data+' views'); 214 | } 215 | } 216 | }); 217 | ``` 218 | 219 | Purge the cache to use the updated pages. 220 | 221 | ### To Get Views With REST API 222 | You can obtain the number of post views by adding `views` to your `_fields` parameter: 223 | `/wp/v2/posts?_fields=views,title` 224 | -------------------------------------------------------------------------------- /wp-postviews.pot: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2012 WP-PostViews 2 | # This file is distributed under the same license as the WP-PostViews package. 3 | msgid "" 4 | msgstr "" 5 | "Project-Id-Version: WP-PostViews 1.61\n" 6 | "Report-Msgid-Bugs-To: http://wordpress.org/tag/wp-postviews\n" 7 | "POT-Creation-Date: 2012-05-22 00:48:11+00:00\n" 8 | "MIME-Version: 1.0\n" 9 | "Content-Type: text/plain; charset=UTF-8\n" 10 | "Content-Transfer-Encoding: 8bit\n" 11 | "PO-Revision-Date: 2012-MO-DA HO:MI+ZONE\n" 12 | "Last-Translator: FULL NAME \n" 13 | "Language-Team: LANGUAGE \n" 14 | 15 | #: postviews-options.php:46 postviews-options.php:139 16 | msgid "Post Views Options" 17 | msgstr "" 18 | 19 | #: postviews-options.php:51 20 | msgid "Updated" 21 | msgstr "" 22 | 23 | #: postviews-options.php:56 24 | msgid "No Post Views Option Updated" 25 | msgstr "" 26 | 27 | #: postviews-options.php:63 postviews-options.php:303 28 | msgid "UNINSTALL WP-PostViews" 29 | msgstr "" 30 | 31 | #: postviews-options.php:71 32 | msgid "Setting Key '%s' has been deleted." 33 | msgstr "" 34 | 35 | #: postviews-options.php:75 36 | msgid "Error deleting Setting Key '%s'." 37 | msgstr "" 38 | 39 | #: postviews-options.php:85 40 | msgid "Post Meta Key '%s' has been deleted." 41 | msgstr "" 42 | 43 | #: postviews-options.php:89 44 | msgid "Error deleting Post Meta Key '%s'." 45 | msgstr "" 46 | 47 | #: postviews-options.php:111 postviews-options.php:261 48 | msgid "Uninstall WP-PostViews" 49 | msgstr "" 50 | 51 | #: postviews-options.php:112 52 | msgid "" 53 | "Click Here To Finish The Uninstallation And WP-PostViews " 54 | "Will Be Deactivated Automatically." 55 | msgstr "" 56 | 57 | #: postviews-options.php:125 wp-postviews.php:797 58 | msgid "%VIEW_COUNT% views" 59 | msgstr "" 60 | 61 | #: postviews-options.php:128 wp-postviews.php:798 62 | msgid "views" 63 | msgstr "" 64 | 65 | #: postviews-options.php:142 66 | msgid "Count Views From:" 67 | msgstr "" 68 | 69 | #: postviews-options.php:145 70 | msgid "Everyone" 71 | msgstr "" 72 | 73 | #: postviews-options.php:146 74 | msgid "Guests Only" 75 | msgstr "" 76 | 77 | #: postviews-options.php:147 78 | msgid "Registered Users Only" 79 | msgstr "" 80 | 81 | #: postviews-options.php:152 82 | msgid "Exclude Bot Views:" 83 | msgstr "" 84 | 85 | #: postviews-options.php:155 86 | msgid "No" 87 | msgstr "" 88 | 89 | #: postviews-options.php:156 postviews-options.php:302 90 | msgid "Yes" 91 | msgstr "" 92 | 93 | #: postviews-options.php:162 94 | msgid "Views Template:" 95 | msgstr "" 96 | 97 | #: postviews-options.php:163 postviews-options.php:174 98 | msgid "Allowed Variables:" 99 | msgstr "" 100 | 101 | #: postviews-options.php:165 postviews-options.php:180 102 | msgid "Restore Default Template" 103 | msgstr "" 104 | 105 | #: postviews-options.php:173 106 | msgid "Most Viewed Template:" 107 | msgstr "" 108 | 109 | #: postviews-options.php:187 110 | msgid "Display Options" 111 | msgstr "" 112 | 113 | #: postviews-options.php:188 114 | msgid "" 115 | "These options specify where the view counts should be displayed and to whom. " 116 | "\tBy default view counts will be displayed to all visitors. Note that the " 117 | "theme files must contain a call to the_views() in order for any " 118 | "view count to be displayed." 119 | msgstr "" 120 | 121 | #: postviews-options.php:191 122 | msgid "Home Page:" 123 | msgstr "" 124 | 125 | #: postviews-options.php:194 postviews-options.php:204 126 | #: postviews-options.php:214 postviews-options.php:224 127 | #: postviews-options.php:234 postviews-options.php:244 128 | msgid "Display to everyone" 129 | msgstr "" 130 | 131 | #: postviews-options.php:195 postviews-options.php:205 132 | #: postviews-options.php:215 postviews-options.php:225 133 | #: postviews-options.php:235 postviews-options.php:245 134 | msgid "Display to registered users only" 135 | msgstr "" 136 | 137 | #: postviews-options.php:196 138 | msgid "Don't display on home page" 139 | msgstr "" 140 | 141 | #: postviews-options.php:201 142 | msgid "Single Posts:" 143 | msgstr "" 144 | 145 | #: postviews-options.php:206 146 | msgid "Don't display on single posts" 147 | msgstr "" 148 | 149 | #: postviews-options.php:211 150 | msgid "Pages:" 151 | msgstr "" 152 | 153 | #: postviews-options.php:216 154 | msgid "Don't display on pages" 155 | msgstr "" 156 | 157 | #: postviews-options.php:221 158 | msgid "Archive Pages:" 159 | msgstr "" 160 | 161 | #: postviews-options.php:226 162 | msgid "Don't display on archive pages" 163 | msgstr "" 164 | 165 | #: postviews-options.php:231 166 | msgid "Search Pages:" 167 | msgstr "" 168 | 169 | #: postviews-options.php:236 170 | msgid "Don't display on search pages" 171 | msgstr "" 172 | 173 | #: postviews-options.php:241 174 | msgid "Other Pages:" 175 | msgstr "" 176 | 177 | #: postviews-options.php:246 178 | msgid "Don't display on other pages" 179 | msgstr "" 180 | 181 | #: postviews-options.php:252 182 | msgid "Save Changes" 183 | msgstr "" 184 | 185 | #: postviews-options.php:263 186 | msgid "" 187 | "Deactivating WP-PostViews plugin does not remove any data that may have been " 188 | "created, such as the views data. To completely remove this plugin, you can " 189 | "uninstall it here." 190 | msgstr "" 191 | 192 | #: postviews-options.php:266 193 | msgid "WARNING:" 194 | msgstr "" 195 | 196 | #: postviews-options.php:267 197 | msgid "" 198 | "Once uninstalled, this cannot be undone. You should use a Database Backup " 199 | "plugin of WordPress to back up all the data first." 200 | msgstr "" 201 | 202 | #: postviews-options.php:270 203 | msgid "The following WordPress Options/PostMetas will be DELETED:" 204 | msgstr "" 205 | 206 | #: postviews-options.php:275 207 | msgid "WordPress Options" 208 | msgstr "" 209 | 210 | #: postviews-options.php:276 211 | msgid "WordPress PostMetas" 212 | msgstr "" 213 | 214 | #: postviews-options.php:303 215 | msgid "" 216 | "You Are About To Uninstall WP-PostViews From WordPress.\\nThis Action Is Not " 217 | "Reversible.\\n\\n Choose [Cancel] To Stop, [OK] To Uninstall." 218 | msgstr "" 219 | 220 | #: wp-postviews.php:42 221 | msgid "PostViews" 222 | msgstr "" 223 | 224 | #: wp-postviews.php:191 wp-postviews.php:233 wp-postviews.php:280 225 | #: wp-postviews.php:327 wp-postviews.php:374 wp-postviews.php:421 226 | msgid "N/A" 227 | msgstr "" 228 | 229 | #: wp-postviews.php:472 230 | msgid "There is no excerpt because this is a protected post." 231 | msgstr "" 232 | 233 | #. #-#-#-#-# plugin.pot (WP-PostViews 1.61) #-#-#-#-# 234 | #. Plugin Name of the plugin/theme 235 | #: wp-postviews.php:574 wp-postviews.php:576 wp-postviews.php:604 236 | msgid "WP-PostViews" 237 | msgstr "" 238 | 239 | #: wp-postviews.php:587 wp-postviews.php:589 wp-postviews.php:618 240 | msgid "%s Most Viewed Post" 241 | msgid_plural "%s Most Viewed Posts" 242 | msgstr[0] "" 243 | msgstr[1] "" 244 | 245 | #: wp-postviews.php:592 wp-postviews.php:594 wp-postviews.php:624 246 | msgid "%s Most Viewed Page" 247 | msgid_plural "%s Most Viewed Pages" 248 | msgstr[0] "" 249 | msgstr[1] "" 250 | 251 | #: wp-postviews.php:606 252 | msgid "%s view was generated." 253 | msgid_plural "%s views were generated." 254 | msgstr[0] "" 255 | msgstr[1] "" 256 | 257 | #: wp-postviews.php:674 258 | msgid "WP-PostViews views statistics" 259 | msgstr "" 260 | 261 | #: wp-postviews.php:675 wp-postviews.php:725 262 | msgid "Views" 263 | msgstr "" 264 | 265 | #: wp-postviews.php:734 266 | msgid "Title:" 267 | msgstr "" 268 | 269 | #: wp-postviews.php:737 270 | msgid "Statistics Type:" 271 | msgstr "" 272 | 273 | #: wp-postviews.php:739 274 | msgid "Least Viewed" 275 | msgstr "" 276 | 277 | #: wp-postviews.php:740 278 | msgid "Least Viewed By Category" 279 | msgstr "" 280 | 281 | #: wp-postviews.php:742 282 | msgid "Most Viewed" 283 | msgstr "" 284 | 285 | #: wp-postviews.php:743 286 | msgid "Most Viewed By Category" 287 | msgstr "" 288 | 289 | #: wp-postviews.php:748 290 | msgid "Include Views From:" 291 | msgstr "" 292 | 293 | #: wp-postviews.php:750 294 | msgid "Posts & Pages" 295 | msgstr "" 296 | 297 | #: wp-postviews.php:751 298 | msgid "Posts Only" 299 | msgstr "" 300 | 301 | #: wp-postviews.php:752 302 | msgid "Pages Only" 303 | msgstr "" 304 | 305 | #: wp-postviews.php:757 306 | msgid "No. Of Records To Show:" 307 | msgstr "" 308 | 309 | #: wp-postviews.php:760 310 | msgid "Maximum Post Title Length (Characters):" 311 | msgstr "" 312 | 313 | #: wp-postviews.php:761 314 | msgid "0 to disable." 315 | msgstr "" 316 | 317 | #: wp-postviews.php:764 318 | msgid "Category IDs:" 319 | msgstr "" 320 | 321 | #: wp-postviews.php:765 322 | msgid "Separate mutiple categories with commas." 323 | msgstr "" 324 | 325 | #: wp-postviews.php:768 326 | msgid "* If you are not using any category statistics, you can ignore it." 327 | msgstr "" 328 | 329 | #. Plugin URI of the plugin/theme 330 | msgid "http://lesterchan.net/portfolio/programming/php/" 331 | msgstr "" 332 | 333 | #. Description of the plugin/theme 334 | msgid "" 335 | "Enables you to display how many times a post/page had been viewed. Modified " 336 | "by David Potter to include options for when and where to display " 338 | "view counts." 339 | msgstr "" 340 | 341 | #. Author of the plugin/theme 342 | msgid "Lester 'GaMerZ' Chan" 343 | msgstr "" 344 | 345 | #. Author URI of the plugin/theme 346 | msgid "http://lesterchan.net" 347 | msgstr "" 348 | -------------------------------------------------------------------------------- /postviews-options.php: -------------------------------------------------------------------------------- 1 | (int) sanitize_key( views_options_parse('views_count') ) 14 | , 'exclude_bots' => (int) sanitize_key( views_options_parse('views_exclude_bots') ) 15 | , 'display_home' => (int) sanitize_key( views_options_parse('views_display_home') ) 16 | , 'display_single' => (int) sanitize_key( views_options_parse('views_display_single') ) 17 | , 'display_page' => (int) sanitize_key( views_options_parse('views_display_page') ) 18 | , 'display_archive' => (int) sanitize_key( views_options_parse('views_display_archive') ) 19 | , 'display_search' => (int) sanitize_key( views_options_parse('views_display_search') ) 20 | , 'display_other' => (int) sanitize_key( views_options_parse('views_display_other') ) 21 | , 'use_ajax' => (int) sanitize_key( views_options_parse('views_use_ajax') ) 22 | , 'template' => wp_kses_post( trim( views_options_parse('views_template_template') ) ) 23 | , 'most_viewed_template' => wp_kses_post( trim( views_options_parse('views_template_most_viewed') ) ) 24 | ); 25 | $update_views_queries = array(); 26 | $update_views_text = array(); 27 | $update_views_queries[] = update_option( 'views_options', $views_options ); 28 | $update_views_text[] = __( 'Post Views Options', 'wp-postviews' ); 29 | $i = 0; 30 | 31 | foreach( $update_views_queries as $update_views_query ) { 32 | if( $update_views_query ) { 33 | $text .= '

' . $update_views_text[$i] . ' ' . __( 'Updated', 'wp-postviews' ) . '

'; 34 | } 35 | $i++; 36 | } 37 | if( empty( $text ) ) { 38 | $text = '

' . __( 'No Post Views Option Updated', 'wp-postviews' ) . '

'; 39 | } 40 | } 41 | 42 | $views_options = get_option( 'views_options' ); 43 | 44 | // Default 45 | if( !isset ( $views_options['use_ajax'] ) ) { 46 | $views_options['use_ajax'] = 1; 47 | } 48 | ?> 49 | 65 |

' . $text . '

'; } ?> 66 |
67 | 68 |
69 |

70 | 71 | 72 | 73 | 80 | 81 | 82 | 83 | 89 | 90 | 91 | 92 | 93 | 102 | 103 | 104 | 105 | 106 | 107 | 114 | 117 | 118 | 119 | 136 | 139 | 140 |
74 | 79 |
84 | 88 |
94 | 98 |

99 | 100 |

101 |
108 |

109 |
110 | - %VIEW_COUNT%
111 | - %VIEW_COUNT_ROUNDED%

112 | 113 |
115 | 116 |
120 |

121 |
122 | - %VIEW_COUNT%
123 | - %VIEW_COUNT_ROUNDED%
124 | - %POST_TITLE%
125 | - %POST_DATE%
126 | - %POST_TIME%
127 | - %POST_EXCERPT%
128 | - %POST_CONTENT%
129 | - %POST_URL%
130 | - %POST_THUMBNAIL%
131 | - %POST_THUMBNAIL_URL%
132 | - %POST_CATEGORY_ID%
133 | - %POST_AUTHOR%

134 | 135 |
137 | 138 |
141 |

142 |

the_views() in order for any view count to be displayed.', 'wp-postviews' ); ?>

143 | 144 | 145 | 146 | 153 | 154 | 155 | 156 | 163 | 164 | 165 | 166 | 173 | 174 | 175 | 176 | 183 | 184 | 185 | 186 | 193 | 194 | 195 | 196 | 203 | 204 |
147 | 152 |
157 | 162 |
167 | 172 |
177 | 182 |
187 | 192 |
197 | 202 |
205 |

206 | 207 |

208 |
209 |
210 | -------------------------------------------------------------------------------- /wp-postviews.php: -------------------------------------------------------------------------------- 1 | ID; 60 | $views_options = get_option( 'views_options' ); 61 | if ( !$post_views = get_post_meta( $post->ID, 'views', true ) ) { 62 | $post_views = 0; 63 | } 64 | $should_count = false; 65 | switch( (int) $views_options['count'] ) { 66 | case 0: 67 | $should_count = true; 68 | break; 69 | case 1: 70 | if( empty( $_COOKIE[ USER_COOKIE ] ) && (int) $user_ID === 0 ) { 71 | $should_count = true; 72 | } 73 | break; 74 | case 2: 75 | if( (int) $user_ID > 0 ) { 76 | $should_count = true; 77 | } 78 | break; 79 | } 80 | if ( isset( $views_options['exclude_bots'] ) && (int) $views_options['exclude_bots'] === 1 ) { 81 | $bots = array( 82 | 'Google Bot' => 'google' 83 | , 'MSN' => 'msnbot' 84 | , 'Alex' => 'ia_archiver' 85 | , 'Lycos' => 'lycos' 86 | , 'Ask Jeeves' => 'jeeves' 87 | , 'Altavista' => 'scooter' 88 | , 'AllTheWeb' => 'fast-webcrawler' 89 | , 'Inktomi' => 'slurp@inktomi' 90 | , 'Turnitin.com' => 'turnitinbot' 91 | , 'Technorati' => 'technorati' 92 | , 'Yahoo' => 'yahoo' 93 | , 'Findexa' => 'findexa' 94 | , 'NextLinks' => 'findlinks' 95 | , 'Gais' => 'gaisbo' 96 | , 'WiseNut' => 'zyborg' 97 | , 'WhoisSource' => 'surveybot' 98 | , 'Bloglines' => 'bloglines' 99 | , 'BlogSearch' => 'blogsearch' 100 | , 'PubSub' => 'pubsub' 101 | , 'Syndic8' => 'syndic8' 102 | , 'RadioUserland' => 'userland' 103 | , 'Gigabot' => 'gigabot' 104 | , 'Become.com' => 'become.com' 105 | , 'Baidu' => 'baiduspider' 106 | , 'so.com' => '360spider' 107 | , 'Sogou' => 'spider' 108 | , 'soso.com' => 'sosospider' 109 | , 'Yandex' => 'yandex' 110 | , 'Ahrefs' => 'AhrefsBot' 111 | , 'Bing' => 'bingbot' 112 | , 'Apple' => 'applebot' 113 | , 'GitCrawler' => 'GitCrawlerBot' 114 | , 'Bytedance' => 'Bytespider' 115 | , 'webmeup' => 'BLEXBot' 116 | ); 117 | $useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; 118 | foreach ( $bots as $name => $lookfor ) { 119 | if ( ! empty( $useragent ) && ( false !== stripos( $useragent, $lookfor ) ) ) { 120 | $should_count = false; 121 | break; 122 | } 123 | } 124 | } 125 | $should_count = apply_filters( 'postviews_should_count', $should_count, $id ); 126 | if( $should_count && ( ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) || ( !defined( 'WP_CACHE' ) || !WP_CACHE ) ) ) { 127 | update_post_meta( $id, 'views', $post_views + 1 ); 128 | do_action( 'postviews_increment_views', $post_views + 1 ); 129 | } 130 | } 131 | } 132 | } 133 | 134 | ### Function: Calculate Post Views With WP_CACHE Enabled 135 | add_action('wp_enqueue_scripts', 'wp_postview_cache_count_enqueue'); 136 | function wp_postview_cache_count_enqueue() { 137 | global $user_ID, $post; 138 | 139 | if ( !defined( 'WP_CACHE' ) || !WP_CACHE ) { 140 | return; 141 | } 142 | 143 | $views_options = get_option( 'views_options' ); 144 | 145 | if ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) { 146 | return; 147 | } 148 | 149 | if ( !wp_is_post_revision( $post ) && ( is_single() || is_page() ) ) { 150 | $should_count = false; 151 | switch( (int) $views_options['count'] ) { 152 | case 0: 153 | $should_count = true; 154 | break; 155 | case 1: 156 | if ( empty( $_COOKIE[USER_COOKIE] ) && (int) $user_ID === 0) { 157 | $should_count = true; 158 | } 159 | break; 160 | case 2: 161 | if ( (int) $user_ID > 0 ) { 162 | $should_count = true; 163 | } 164 | break; 165 | } 166 | 167 | $should_count = apply_filters( 'postviews_should_count', $should_count, (int) $post->ID ); 168 | if ( $should_count ) { 169 | wp_enqueue_script( 'wp-postviews-cache', plugins_url( 'postviews-cache.js', __FILE__ ), array(), WP_POSTVIEWS_VERSION, true ); 170 | wp_localize_script( 'wp-postviews-cache', 'viewsCacheL10n', array( 'admin_ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wp_postviews_nonce' ), 'post_id' => (int) $post->ID ) ); 171 | } 172 | } 173 | } 174 | 175 | 176 | ### Function: Determine If Post Views Should Be Displayed (By: David Potter) 177 | function should_views_be_displayed($views_options = null) { 178 | if ($views_options == null) { 179 | $views_options = get_option('views_options'); 180 | } 181 | $display_option = 0; 182 | if (is_home()) { 183 | if (array_key_exists('display_home', $views_options)) { 184 | $display_option = $views_options['display_home']; 185 | } 186 | } elseif (is_single()) { 187 | if (array_key_exists('display_single', $views_options)) { 188 | $display_option = $views_options['display_single']; 189 | } 190 | } elseif (is_page()) { 191 | if (array_key_exists('display_page', $views_options)) { 192 | $display_option = $views_options['display_page']; 193 | } 194 | } elseif (is_archive()) { 195 | if (array_key_exists('display_archive', $views_options)) { 196 | $display_option = $views_options['display_archive']; 197 | } 198 | } elseif (is_search()) { 199 | if (array_key_exists('display_search', $views_options)) { 200 | $display_option = $views_options['display_search']; 201 | } 202 | } else { 203 | if (array_key_exists('display_other', $views_options)) { 204 | $display_option = $views_options['display_other']; 205 | } 206 | } 207 | return (($display_option == 0) || (($display_option == 1) && is_user_logged_in())); 208 | } 209 | 210 | 211 | ### Function: Display The Post Views 212 | function the_views($display = true, $prefix = '', $postfix = '', $always = false) { 213 | $post_views = (int) get_post_meta( get_the_ID(), 'views', true ); 214 | $views_options = get_option('views_options'); 215 | if ($always || should_views_be_displayed($views_options)) { 216 | $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; 217 | if($display) { 218 | echo apply_filters('the_views', $output); 219 | } else { 220 | return apply_filters('the_views', $output); 221 | } 222 | } 223 | elseif (!$display) { 224 | return ''; 225 | } 226 | } 227 | 228 | ### Function: Short Code For Inserting Views Into Posts 229 | add_shortcode( 'views', 'views_shortcode' ); 230 | function views_shortcode( $atts ) { 231 | $attributes = shortcode_atts( array( 'id' => 0 ), $atts ); 232 | $id = (int) $attributes['id']; 233 | if( $id === 0) { 234 | $id = get_the_ID(); 235 | } 236 | $views_options = get_option( 'views_options' ); 237 | $post_views = (int) get_post_meta( $id, 'views', true ); 238 | $output = str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ); 239 | 240 | return apply_filters( 'the_views', $output ); 241 | } 242 | 243 | 244 | ### Function: Display Least Viewed Page/Post 245 | if ( ! function_exists( 'get_least_viewed' ) ) { 246 | function get_least_viewed( $mode = '', $limit = 10, $chars = 0, $display = true ) { 247 | $views_options = get_option( 'views_options' ); 248 | $output = ''; 249 | 250 | $least_viewed = new WP_Query( array( 251 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 252 | 'posts_per_page' => $limit, 253 | 'orderby' => 'meta_value_num', 254 | 'order' => 'asc', 255 | 'meta_key' => 'views', 256 | ) ); 257 | if ( $least_viewed->have_posts() ) { 258 | while ( $least_viewed->have_posts() ) { 259 | $least_viewed->the_post(); 260 | 261 | // Post Views. 262 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 263 | 264 | // Post Title. 265 | $post_title = get_the_title(); 266 | if ( $chars > 0 ) { 267 | $post_title = snippet_text( $post_title, $chars ); 268 | } 269 | 270 | // Post First Category. 271 | $categories = get_the_category(); 272 | $post_category_id = 0; 273 | if ( ! empty( $categories ) ) { 274 | $post_category_id = $categories[0]->term_id; 275 | } 276 | 277 | $temp = stripslashes( $views_options['most_viewed_template'] ); 278 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 279 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 280 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 281 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 282 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 283 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 284 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 285 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 286 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 287 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 288 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 289 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 290 | $output .= $temp; 291 | } 292 | 293 | wp_reset_postdata(); 294 | } else { 295 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 296 | } 297 | 298 | if( $display ) { 299 | echo $output; 300 | } else { 301 | return $output; 302 | } 303 | } 304 | } 305 | 306 | 307 | ### Function: Display Most Viewed Page/Post 308 | if ( ! function_exists( 'get_most_viewed' ) ) { 309 | function get_most_viewed( $mode = '', $limit = 10, $chars = 0, $display = true ) { 310 | $views_options = get_option( 'views_options' ); 311 | $output = ''; 312 | 313 | $most_viewed = new WP_Query( array( 314 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 315 | 'posts_per_page' => $limit, 316 | 'orderby' => 'meta_value_num', 317 | 'order' => 'desc', 318 | 'meta_key' => 'views', 319 | ) ); 320 | if ( $most_viewed->have_posts() ) { 321 | while ( $most_viewed->have_posts() ) { 322 | $most_viewed->the_post(); 323 | 324 | // Post Views. 325 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 326 | 327 | // Post Title. 328 | $post_title = get_the_title(); 329 | if ( $chars > 0 ) { 330 | $post_title = snippet_text( $post_title, $chars ); 331 | } 332 | 333 | // Post First Category. 334 | $categories = get_the_category(); 335 | $post_category_id = 0; 336 | if ( ! empty( $categories ) ) { 337 | $post_category_id = $categories[0]->term_id; 338 | } 339 | 340 | $temp = stripslashes( $views_options['most_viewed_template'] ); 341 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 342 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 343 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 344 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 345 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 346 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 347 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 348 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 349 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 350 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 351 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 352 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 353 | $output .= $temp; 354 | } 355 | 356 | wp_reset_postdata(); 357 | } else { 358 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 359 | } 360 | 361 | if( $display ) { 362 | echo $output; 363 | } else { 364 | return $output; 365 | } 366 | } 367 | } 368 | 369 | 370 | ### Function: Display Least Viewed Page/Post By Category ID 371 | if ( ! function_exists( 'get_least_viewed_category' ) ) { 372 | function get_least_viewed_category( $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) { 373 | $views_options = get_option( 'views_options' ); 374 | $output = ''; 375 | 376 | $least_viewed = new WP_Query( array( 377 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 378 | 'posts_per_page' => $limit, 379 | 'category__in' => (array) $category_id, 380 | 'orderby' => 'meta_value_num', 381 | 'order' => 'asc', 382 | 'meta_key' => 'views', 383 | ) ); 384 | if ( $least_viewed->have_posts() ) { 385 | while ( $least_viewed->have_posts() ) { 386 | $least_viewed->the_post(); 387 | 388 | // Post Views. 389 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 390 | 391 | // Post Title. 392 | $post_title = get_the_title(); 393 | if ( $chars > 0 ) { 394 | $post_title = snippet_text( $post_title, $chars ); 395 | } 396 | 397 | // Post First Category. 398 | $categories = get_the_category(); 399 | $post_category_id = 0; 400 | if ( ! empty( $categories ) ) { 401 | $post_category_id = $categories[0]->term_id; 402 | } 403 | 404 | $temp = stripslashes( $views_options['most_viewed_template'] ); 405 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 406 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 407 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 408 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 409 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 410 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 411 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 412 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 413 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 414 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 415 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 416 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 417 | $output .= $temp; 418 | } 419 | 420 | wp_reset_postdata(); 421 | } else { 422 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 423 | } 424 | 425 | if($display) { 426 | echo $output; 427 | } else { 428 | return $output; 429 | } 430 | } 431 | } 432 | 433 | 434 | ### Function: Display Most Viewed Page/Post By Category ID 435 | if ( ! function_exists( 'get_most_viewed_category' ) ) { 436 | function get_most_viewed_category( $category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) { 437 | $views_options = get_option( 'views_options' ); 438 | $output = ''; 439 | 440 | $most_viewed = new WP_Query( array( 441 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 442 | 'posts_per_page' => $limit, 443 | 'category__in' => (array) $category_id, 444 | 'orderby' => 'meta_value_num', 445 | 'order' => 'desc', 446 | 'meta_key' => 'views', 447 | ) ); 448 | if ( $most_viewed->have_posts() ) { 449 | while ( $most_viewed->have_posts() ) { 450 | $most_viewed->the_post(); 451 | 452 | // Post Views. 453 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 454 | 455 | // Post Title. 456 | $post_title = get_the_title(); 457 | if ( $chars > 0 ) { 458 | $post_title = snippet_text( $post_title, $chars ); 459 | } 460 | 461 | // Post First Category. 462 | $categories = get_the_category(); 463 | $post_category_id = 0; 464 | if ( ! empty( $categories ) ) { 465 | $post_category_id = $categories[0]->term_id; 466 | } 467 | 468 | $temp = stripslashes( $views_options['most_viewed_template'] ); 469 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 470 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 471 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 472 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 473 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 474 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 475 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 476 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 477 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 478 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 479 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 480 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 481 | $output .= $temp; 482 | } 483 | 484 | wp_reset_postdata(); 485 | } else { 486 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 487 | } 488 | 489 | if ( $display ) { 490 | echo $output; 491 | } else { 492 | return $output; 493 | } 494 | } 495 | } 496 | 497 | ### Function: Display Least Viewed Page/Post By Tag ID 498 | if ( ! function_exists( 'get_least_viewed_tag' ) ) { 499 | function get_least_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) { 500 | $views_options = get_option( 'views_options' ); 501 | $output = ''; 502 | 503 | $least_viewed = new WP_Query( array( 504 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 505 | 'posts_per_page' => $limit, 506 | 'tag__in' => (array) $tag_id, 507 | 'orderby' => 'meta_value_num', 508 | 'order' => 'asc', 509 | 'meta_key' => 'views', 510 | ) ); 511 | if ( $least_viewed->have_posts() ) { 512 | while ( $least_viewed->have_posts() ) { 513 | $least_viewed->the_post(); 514 | 515 | // Post Views. 516 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 517 | 518 | // Post Title. 519 | $post_title = get_the_title(); 520 | if ( $chars > 0 ) { 521 | $post_title = snippet_text( $post_title, $chars ); 522 | } 523 | 524 | // Post First Category. 525 | $categories = get_the_category(); 526 | $post_category_id = 0; 527 | if ( ! empty( $categories ) ) { 528 | $post_category_id = $categories[0]->term_id; 529 | } 530 | 531 | $temp = stripslashes( $views_options['most_viewed_template'] ); 532 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 533 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 534 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 535 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 536 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 537 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 538 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 539 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 540 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 541 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 542 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 543 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 544 | $output .= $temp; 545 | } 546 | 547 | wp_reset_postdata(); 548 | } else { 549 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 550 | } 551 | 552 | if ( $display ) { 553 | echo $output; 554 | } else { 555 | return $output; 556 | } 557 | } 558 | } 559 | 560 | 561 | ### Function: Display Most Viewed Page/Post By Tag ID 562 | if ( ! function_exists( 'get_most_viewed_tag' ) ) { 563 | function get_most_viewed_tag( $tag_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true ) { 564 | $views_options = get_option( 'views_options' ); 565 | $output = ''; 566 | 567 | $most_viewed = new WP_Query( array( 568 | 'post_type' => ( empty( $mode ) || $mode === 'both' ) ? 'any' : $mode, 569 | 'posts_per_page' => $limit, 570 | 'tag__in' => (array) $tag_id, 571 | 'orderby' => 'meta_value_num', 572 | 'order' => 'desc', 573 | 'meta_key' => 'views', 574 | ) ); 575 | if ( $most_viewed->have_posts() ) { 576 | while ( $most_viewed->have_posts() ) { 577 | $most_viewed->the_post(); 578 | 579 | // Post Views. 580 | $post_views = get_post_meta( get_the_ID(), 'views', true ); 581 | 582 | // Post Title. 583 | $post_title = get_the_title(); 584 | if ( $chars > 0 ) { 585 | $post_title = snippet_text( $post_title, $chars ); 586 | } 587 | 588 | // Post First Category. 589 | $categories = get_the_category(); 590 | $post_category_id = 0; 591 | if ( ! empty( $categories ) ) { 592 | $post_category_id = $categories[0]->term_id; 593 | } 594 | 595 | $temp = stripslashes( $views_options['most_viewed_template'] ); 596 | $temp = str_replace( '%VIEW_COUNT%', number_format_i18n( $post_views ), $temp ); 597 | $temp = str_replace( '%VIEW_COUNT_ROUNDED%', postviews_round_number( $post_views ), $temp ); 598 | $temp = str_replace( '%POST_TITLE%', $post_title, $temp ); 599 | $temp = str_replace( '%POST_EXCERPT%', get_the_excerpt(), $temp ); 600 | $temp = str_replace( '%POST_CONTENT%', get_the_content(), $temp ); 601 | $temp = str_replace( '%POST_URL%', get_permalink(), $temp ); 602 | $temp = str_replace( '%POST_DATE%', get_the_time( get_option( 'date_format' ) ), $temp ); 603 | $temp = str_replace( '%POST_TIME%', get_the_time( get_option( 'time_format' ) ), $temp ); 604 | $temp = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( null,'thumbnail',true ), $temp); 605 | $temp = str_replace( '%POST_THUMBNAIL_URL%', get_the_post_thumbnail_url( null,'thumbnail',true ), $temp); 606 | $temp = str_replace( '%POST_CATEGORY_ID%', $post_category_id, $temp ); 607 | $temp = str_replace( '%POST_AUTHOR%', get_the_author(), $temp ); 608 | $output .= $temp; 609 | } 610 | 611 | wp_reset_postdata(); 612 | } else { 613 | $output = '
  • ' . __( 'N/A', 'wp-postviews' ) . '
  • ' . "\n"; 614 | } 615 | 616 | if($display) { 617 | echo $output; 618 | } else { 619 | return $output; 620 | } 621 | } 622 | } 623 | 624 | 625 | ### Function: Display Total Views 626 | if(!function_exists('get_totalviews')) { 627 | function get_totalviews($display = true) { 628 | global $wpdb; 629 | $total_views = (int) $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'" ); 630 | if($display) { 631 | echo number_format_i18n($total_views); 632 | } else { 633 | return $total_views; 634 | } 635 | } 636 | } 637 | 638 | 639 | ### Function: Snippet Text 640 | if(!function_exists('snippet_text')) { 641 | function snippet_text($text, $length = 0) { 642 | if (defined('MB_OVERLOAD_STRING')) { 643 | $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); 644 | if (mb_strlen($text) > $length) { 645 | return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; 646 | } else { 647 | return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); 648 | } 649 | } else { 650 | $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); 651 | if (strlen($text) > $length) { 652 | return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; 653 | } else { 654 | return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); 655 | } 656 | } 657 | } 658 | } 659 | 660 | 661 | ### Function: Modify Default WordPress Listing To Make It Sorted By Post Views 662 | function views_fields($content) { 663 | global $wpdb; 664 | $content .= ", ($wpdb->postmeta.meta_value+0) AS views"; 665 | return $content; 666 | } 667 | function views_join($content) { 668 | global $wpdb; 669 | $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID"; 670 | return $content; 671 | } 672 | function views_where($content) { 673 | global $wpdb; 674 | $content .= " AND $wpdb->postmeta.meta_key = 'views'"; 675 | return $content; 676 | } 677 | function views_orderby($content) { 678 | $orderby = trim(addslashes(get_query_var('v_orderby'))); 679 | if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) { 680 | $orderby = 'desc'; 681 | } 682 | $content = " views $orderby"; 683 | return $content; 684 | } 685 | 686 | 687 | ### Function: Add Views Custom Fields 688 | add_action('publish_post', 'add_views_fields'); 689 | add_action('publish_page', 'add_views_fields'); 690 | function add_views_fields($post_ID) { 691 | global $wpdb; 692 | if(!wp_is_post_revision($post_ID)) { 693 | add_post_meta($post_ID, 'views', 0, true); 694 | } 695 | } 696 | 697 | 698 | ### Function: Views Public Variables 699 | add_filter('query_vars', 'views_variables'); 700 | function views_variables($public_query_vars) { 701 | $public_query_vars[] = 'v_sortby'; 702 | $public_query_vars[] = 'v_orderby'; 703 | return $public_query_vars; 704 | } 705 | 706 | 707 | ### Function: Sort Views Posts 708 | add_action('pre_get_posts', 'views_sorting'); 709 | function views_sorting($local_wp_query) { 710 | if($local_wp_query->get('v_sortby') == 'views') { 711 | add_filter('posts_fields', 'views_fields'); 712 | add_filter('posts_join', 'views_join'); 713 | add_filter('posts_where', 'views_where'); 714 | add_filter('posts_orderby', 'views_orderby'); 715 | } else { 716 | remove_filter('posts_fields', 'views_fields'); 717 | remove_filter('posts_join', 'views_join'); 718 | remove_filter('posts_where', 'views_where'); 719 | remove_filter('posts_orderby', 'views_orderby'); 720 | } 721 | } 722 | 723 | 724 | ### Function: Plug Into WP-Stats 725 | add_action( 'plugins_loaded', 'postviews_wp_stats' ); 726 | function postviews_wp_stats() { 727 | add_filter( 'wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats' ); 728 | add_filter( 'wp_stats_page_admin_most', 'postviews_page_admin_most_stats' ); 729 | add_filter( 'wp_stats_page_plugins', 'postviews_page_general_stats' ); 730 | add_filter( 'wp_stats_page_most', 'postviews_page_most_stats' ); 731 | } 732 | 733 | 734 | ### Function: Add WP-PostViews General Stats To WP-Stats Page Options 735 | function postviews_page_admin_general_stats($content) { 736 | $stats_display = get_option('stats_display'); 737 | if ( (int) $stats_display['views'] === 1 ) { 738 | $content .= '  
    '."\n"; 739 | } else { 740 | $content .= '  
    '."\n"; 741 | } 742 | return $content; 743 | } 744 | 745 | 746 | ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options 747 | function postviews_page_admin_most_stats($content) { 748 | $stats_display = get_option('stats_display'); 749 | $stats_mostlimit = (int) get_option('stats_mostlimit'); 750 | if ( (int) $stats_display['viewed_most_post'] === 1 ) { 751 | $content .= '  
    '."\n"; 752 | } else { 753 | $content .= '  
    '."\n"; 754 | } 755 | if ( (int) $stats_display['viewed_most_page'] === 1 ) { 756 | $content .= '  
    '."\n"; 757 | } else { 758 | $content .= '  
    '."\n"; 759 | } 760 | return $content; 761 | } 762 | 763 | 764 | ### Function: Add WP-PostViews General Stats To WP-Stats Page 765 | function postviews_page_general_stats($content) { 766 | $stats_display = get_option('stats_display'); 767 | if ( (int) $stats_display['views'] === 1 ) { 768 | $content .= '

    '.__('WP-PostViews', 'wp-postviews').'

    '."\n"; 769 | $content .= '
      '."\n"; 770 | $content .= '
    • '.sprintf(_n('%s view was generated.', '%s views were generated.', get_totalviews(false), 'wp-postviews'), number_format_i18n(get_totalviews(false))).'
    • '."\n"; 771 | $content .= '
    '."\n"; 772 | } 773 | return $content; 774 | } 775 | 776 | 777 | ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page 778 | function postviews_page_most_stats($content) { 779 | $stats_display = get_option('stats_display'); 780 | $stats_mostlimit = (int) get_option('stats_mostlimit'); 781 | if ( (int) $stats_display['viewed_most_post'] === 1 ) { 782 | $content .= '

    '.sprintf(_n('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'

    '."\n"; 783 | $content .= '
      '."\n"; 784 | $content .= get_most_viewed('post', $stats_mostlimit, 0, false); 785 | $content .= '
    '."\n"; 786 | } 787 | if($stats_display['viewed_most_page'] == 1) { 788 | $content .= '

    '.sprintf(_n('%s Most Viewed Page', '%s Most Viewed Pages', $stats_mostlimit, 'wp-postviews'), number_format_i18n($stats_mostlimit)).'

    '."\n"; 789 | $content .= '
      '."\n"; 790 | $content .= get_most_viewed('page', $stats_mostlimit, 0, false); 791 | $content .= '
    '."\n"; 792 | } 793 | return $content; 794 | } 795 | 796 | 797 | ### Function: Increment Post Views 798 | add_action( 'wp_ajax_postviews', 'increment_views' ); 799 | add_action( 'wp_ajax_nopriv_postviews', 'increment_views' ); 800 | function increment_views() { 801 | $security = check_ajax_referer( 'wp_postviews_nonce', 'nonce' ); 802 | 803 | if ( false === $security ) { 804 | wp_send_json_error(); 805 | wp_die(); 806 | } 807 | 808 | if ( ! isset( $_POST['postviews_id'] ) || empty( $_POST['postviews_id'] ) ) { 809 | return; 810 | } 811 | 812 | if ( !defined( 'WP_CACHE' ) || ! WP_CACHE ) { 813 | return; 814 | } 815 | 816 | $views_options = get_option( 'views_options' ); 817 | 818 | if ( isset( $views_options['use_ajax'] ) && (int) $views_options['use_ajax'] === 0 ) { 819 | return; 820 | } 821 | 822 | $post_id = (int) sanitize_key( $_POST['postviews_id'] ); 823 | if( $post_id > 0 ) { 824 | $post_views = (int) get_post_meta( $post_id, 'views', true ); 825 | $post_views = $post_views + 1; 826 | update_post_meta( $post_id, 'views', $post_views ); 827 | do_action( 'postviews_increment_views_ajax', $post_views ); 828 | wp_send_json_success( [ 'views' => $post_views ] ); 829 | exit(); 830 | } 831 | } 832 | 833 | ### Function Show Post Views Column in WP-Admin 834 | add_action('manage_posts_custom_column', 'add_postviews_column_content'); 835 | add_filter('manage_posts_columns', 'add_postviews_column'); 836 | add_action('manage_pages_custom_column', 'add_postviews_column_content'); 837 | add_filter('manage_pages_columns', 'add_postviews_column'); 838 | function add_postviews_column($defaults) { 839 | $defaults['views'] = __( 'Views', 'wp-postviews' ); 840 | return $defaults; 841 | } 842 | 843 | 844 | ### Functions Fill In The Views Count 845 | function add_postviews_column_content($column_name) { 846 | if ($column_name === 'views' ) { 847 | if ( function_exists('the_views' ) ) { 848 | the_views( true, '', '', true ); 849 | } 850 | } 851 | } 852 | 853 | 854 | ### Function Sort Columns 855 | add_filter( 'manage_edit-post_sortable_columns', 'sort_postviews_column'); 856 | add_filter( 'manage_edit-page_sortable_columns', 'sort_postviews_column' ); 857 | function sort_postviews_column( $defaults ) { 858 | $defaults['views'] = 'views'; 859 | return $defaults; 860 | } 861 | add_action('pre_get_posts', 'sort_postviews'); 862 | function sort_postviews($query) { 863 | if ( ! is_admin() ) { 864 | return; 865 | } 866 | $orderby = $query->get('orderby'); 867 | if ( 'views' === $orderby ) { 868 | $query->set( 'meta_key', 'views' ); 869 | $query->set( 'orderby', 'meta_value_num' ); 870 | } 871 | } 872 | 873 | ### Function: Round Numbers To K (Thousand), M (Million) or B (Billion) 874 | function postviews_round_number( $number, $min_value = 1000, $decimal = 1 ) { 875 | if( $number < $min_value ) { 876 | return number_format_i18n( $number ); 877 | } 878 | $alphabets = array( 1000000000 => 'B', 1000000 => 'M', 1000 => 'K' ); 879 | foreach( $alphabets as $key => $value ) 880 | if( $number >= $key ) { 881 | return round( $number / $key, $decimal ) . '' . $value; 882 | } 883 | } 884 | 885 | 886 | ### Class: WP-PostViews Widget 887 | class WP_Widget_PostViews extends WP_Widget { 888 | // Constructor 889 | public function __construct() { 890 | $widget_ops = array('description' => __('WP-PostViews views statistics', 'wp-postviews')); 891 | parent::__construct('views', __('Views', 'wp-postviews'), $widget_ops); 892 | } 893 | 894 | // Display Widget 895 | public function widget($args, $instance) { 896 | $title = apply_filters('widget_title', esc_attr($instance['title'])); 897 | $type = esc_attr($instance['type']); 898 | $mode = esc_attr($instance['mode']); 899 | $limit = (int) $instance['limit']; 900 | $chars = (int) $instance['chars']; 901 | $cat_ids = explode(',', esc_attr($instance['cat_ids'])); 902 | echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; 903 | echo '
      '."\n"; 904 | switch($type) { 905 | case 'least_viewed': 906 | get_least_viewed($mode, $limit, $chars); 907 | break; 908 | case 'most_viewed': 909 | get_most_viewed($mode, $limit, $chars); 910 | break; 911 | case 'most_viewed_category': 912 | get_most_viewed_category($cat_ids, $mode, $limit, $chars); 913 | break; 914 | case 'least_viewed_category': 915 | get_least_viewed_category($cat_ids, $mode, $limit, $chars); 916 | break; 917 | } 918 | echo '
    '."\n"; 919 | echo $args['after_widget']; 920 | } 921 | 922 | // When Widget Control Form Is Posted 923 | public function update($new_instance, $old_instance) { 924 | if (!isset($new_instance['submit'])) { 925 | return false; 926 | } 927 | $instance = $old_instance; 928 | $instance['title'] = strip_tags($new_instance['title']); 929 | $instance['type'] = strip_tags($new_instance['type']); 930 | $instance['mode'] = strip_tags($new_instance['mode']); 931 | $instance['limit'] = (int) $new_instance['limit']; 932 | $instance['chars'] = (int) $new_instance['chars']; 933 | $instance['cat_ids'] = strip_tags($new_instance['cat_ids']); 934 | return $instance; 935 | } 936 | 937 | // DIsplay Widget Control Form 938 | public function form($instance) { 939 | $instance = wp_parse_args((array) $instance, array('title' => __('Views', 'wp-postviews'), 'type' => 'most_viewed', 'mode' => '', 'limit' => 10, 'chars' => 200, 'cat_ids' => '0')); 940 | $title = esc_attr($instance['title']); 941 | $type = esc_attr($instance['type']); 942 | $mode = trim(esc_attr($instance['mode'])); 943 | $limit = (int) $instance['limit']; 944 | $chars = (int) $instance['chars']; 945 | $cat_ids = esc_attr($instance['cat_ids']); 946 | $post_types = get_post_types(array( 947 | 'public' => true 948 | )); 949 | ?> 950 |

    951 | 952 |

    953 |

    954 | 963 |

    964 |

    965 | 975 |

    976 |

    977 | 978 |

    979 |

    980 |
    981 | 0 to disable.', 'wp-postviews'); ?> 982 |

    983 |

    984 |
    985 | 986 |

    987 |

    988 | 989 |

    990 | 991 | 1, 1010 | 'exclude_bots' => 0, 1011 | 'display_home' => 0, 1012 | 'display_single' => 0, 1013 | 'display_page' => 0, 1014 | 'display_archive' => 0, 1015 | 'display_search' => 0, 1016 | 'display_other' => 0, 1017 | 'use_ajax' => 1, 1018 | 'template' => __( '%VIEW_COUNT% views', 'wp-postviews' ), 1019 | 'most_viewed_template' => '

  • %POST_TITLE% - %VIEW_COUNT% '.__('views', 'wp-postviews').'
  • ' 1020 | ); 1021 | 1022 | if ( is_multisite() && $network_wide ) { 1023 | $ms_sites = function_exists( 'get_sites' ) ? get_sites() : wp_get_sites(); 1024 | 1025 | if( 0 < count( $ms_sites ) ) { 1026 | foreach ( $ms_sites as $ms_site ) { 1027 | $blog_id = class_exists( 'WP_Site' ) ? $ms_site->blog_id : $ms_site['blog_id']; 1028 | switch_to_blog( $blog_id ); 1029 | add_option( $option_name, $option ); 1030 | restore_current_blog(); 1031 | } 1032 | } 1033 | } else { 1034 | add_option( $option_name, $option ); 1035 | } 1036 | } 1037 | 1038 | ### Function: Parse View Options 1039 | function views_options_parse( $key ) { 1040 | return ! empty( $_POST[ $key ] ) ? $_POST[ $key ] : null; 1041 | } 1042 | 1043 | 1044 | ### Function: Register views meta field to use it in REST API 1045 | add_action('rest_api_init', 'register_rest_views_field'); 1046 | function register_rest_views_field(){ 1047 | register_rest_field('post', 'views', array( 1048 | 'get_callback' => function ($post) { 1049 | if (!$post_views = get_post_meta($post['id'], 'views', true)) { 1050 | $post_views = 0; 1051 | } 1052 | 1053 | return (int) $post_views; 1054 | } 1055 | )); 1056 | } 1057 | --------------------------------------------------------------------------------