├── entry-views.php ├── inc ├── functions.php ├── shortcodes.php ├── template.php └── widget-entry-views.php ├── languages └── entry-views.pot ├── readme.md ├── readme.txt ├── screenshot-1.png └── screenshot-2.png /entry-views.php: -------------------------------------------------------------------------------- 1 | 35 | * @copyright Copyright (c) 2010 - 2014, Justin Tadlock 36 | * @link http://themehybrid.com/plugins/entry-views 37 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 38 | */ 39 | 40 | /** 41 | * Plugin loader class. 42 | * 43 | * @since 1.0.0 44 | */ 45 | final class Entry_Views_Plugin { 46 | 47 | /** 48 | * Holds the instances of this class. 49 | * 50 | * @since 1.0.0 51 | * @access private 52 | * @var object 53 | */ 54 | private static $instance; 55 | 56 | /** 57 | * The post ID to update the entry views for. 58 | * 59 | * @since 1.0.0 60 | * @access public 61 | * @var int 62 | */ 63 | public $post_id = 0; 64 | 65 | /** 66 | * Sets up needed actions/filters for the plugin to initialize. 67 | * 68 | * @since 1.0.0 69 | * @access public 70 | * @return void 71 | */ 72 | public function __construct() { 73 | 74 | add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 ); 75 | add_action( 'plugins_loaded', array( $this, 'includes' ), 3 ); 76 | add_action( 'init', array( $this, 'post_type_support' ), 10 ); 77 | add_action( 'widgets_init', array( $this, 'register_widgets' ), 10 ); 78 | add_action( 'template_redirect', array( $this, 'load' ), 99 ); 79 | add_action( 'wp_ajax_entry_views', array( $this, 'update_ajax' ), 10 ); 80 | add_action( 'wp_ajax_nopriv_entry_views', array( $this, 'update_ajax' ), 10 ); 81 | } 82 | 83 | /** 84 | * Loads the translation files. 85 | * 86 | * @since 1.0.0 87 | * @access public 88 | * @return void 89 | */ 90 | function i18n() { 91 | load_plugin_textdomain( 'entry-views', false, 'entry-views/languages' ); 92 | } 93 | 94 | /** 95 | * Loads the initial files needed by the plugin. 96 | * 97 | * @since 1.0.0 98 | * @access public 99 | * @return void 100 | */ 101 | function includes() { 102 | $path = trailingslashit( plugin_dir_path( __FILE__ ) ); 103 | 104 | require_once( "{$path}inc/functions.php" ); 105 | require_once( "{$path}inc/template.php" ); 106 | require_once( "{$path}inc/shortcodes.php" ); 107 | require_once( "{$path}inc/widget-entry-views.php" ); 108 | } 109 | 110 | /** 111 | * Adds support for 'entry-views' to the 'post', 'page', and 'attachment' post types (default WordPress 112 | * post types). For all other post types, the theme should explicitly register support for this feature. 113 | * 114 | * @since 1.0.0 115 | * @access public 116 | * @return void 117 | */ 118 | function post_type_support() { 119 | 120 | /* Core post types. */ 121 | add_post_type_support( 'post', array( 'entry-views' ) ); 122 | add_post_type_support( 'page', array( 'entry-views' ) ); 123 | add_post_type_support( 'attachment', array( 'entry-views' ) ); 124 | 125 | /* Plugin post types. */ 126 | add_post_type_support( 'literature', array( 'entry-views' ) ); 127 | add_post_type_support( 'portfolio_item', array( 'entry-views' ) ); 128 | add_post_type_support( 'recipe', array( 'entry-views' ) ); 129 | add_post_type_support( 'restaurant_item', array( 'entry-views' ) ); 130 | } 131 | 132 | /** 133 | * Registers the plugin's widgets. 134 | * 135 | * @since 1.0.0 136 | * @access public 137 | * @return void 138 | */ 139 | public function register_widgets() { 140 | 141 | register_widget( 'EV_Widget_Entry_Views' ); 142 | } 143 | 144 | /** 145 | * Checks if we're on a singular post view and if the current post type supports the 'entry-views' 146 | * extension. If so, set the $post_id variable and load the needed JavaScript. 147 | * 148 | * @since 1.0.0 149 | * @access public 150 | * @return void 151 | */ 152 | function load() { 153 | 154 | /* Check if we're on a singular post view. */ 155 | if ( is_singular() && !is_preview() ) { 156 | 157 | /* Get the post object. */ 158 | $post = get_queried_object(); 159 | 160 | /* Check if the post type supports the 'entry-views' feature. */ 161 | if ( post_type_supports( $post->post_type, 'entry-views' ) ) { 162 | 163 | /* Set the post ID for later use because we wouldn't want a custom query to change this. */ 164 | $this->post_id = get_queried_object_id(); 165 | 166 | /* Enqueue the jQuery library. */ 167 | wp_enqueue_script( 'jquery' ); 168 | 169 | /* Load the entry views JavaScript in the footer. */ 170 | add_action( 'wp_footer', array( $this, 'load_scripts' ) ); 171 | } 172 | } 173 | } 174 | 175 | /** 176 | * Callback function hooked to 'wp_ajax_entry_views' and 'wp_ajax_nopriv_entry_views'. It checks the 177 | * AJAX nonce and passes the given $post_id to the entry views update function. 178 | * 179 | * @since 1.0.0 180 | * @access public 181 | * @return void 182 | */ 183 | function update_ajax() { 184 | 185 | /* Check the AJAX nonce to make sure this is a valid request. */ 186 | check_ajax_referer( 'entry_views_ajax' ); 187 | 188 | /* If the post ID is set, set it to the $post_id variable and make sure it's an integer. */ 189 | if ( isset( $_POST['post_id'] ) ) 190 | $post_id = absint( $_POST['post_id'] ); 191 | 192 | /* If $post_id isn't empty, pass it to the ev_set_post_view_count() function to update the view count. */ 193 | if ( !empty( $post_id ) ) 194 | ev_set_post_view_count( $post_id ); 195 | } 196 | 197 | /** 198 | * Displays a small script that sends an AJAX request for the page. It passes the $post_id to the AJAX 199 | * callback function for updating the meta. 200 | * 201 | * @since 1.0.0 202 | * @access public 203 | * @return void 204 | */ 205 | function load_scripts() { 206 | 207 | /* Create a nonce for the AJAX request. */ 208 | $nonce = wp_create_nonce( 'entry_views_ajax' ); 209 | 210 | /* Display the JavaScript needed. */ 211 | echo '' . "\n"; 212 | } 213 | 214 | /** 215 | * Returns the instance. 216 | * 217 | * @since 1.0.0 218 | * @access public 219 | * @return object 220 | */ 221 | public static function get_instance() { 222 | 223 | if ( !self::$instance ) 224 | self::$instance = new self; 225 | 226 | return self::$instance; 227 | } 228 | } 229 | 230 | Entry_Views_Plugin::get_instance(); 231 | -------------------------------------------------------------------------------- /inc/functions.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright Copyright (c) 2010 - 2014, Justin Tadlock 9 | * @link http://themehybrid.com/plugins/entry-views 10 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 11 | */ 12 | 13 | /** 14 | * Returns the meta key used throughout the plugin for getting/setting the post view count, which is 15 | * saved as post metadata. Developers can also filter this via the `ev_meta_key` hook if they need 16 | * to alter it to match a meta key that was previously used by another plugin. 17 | * 18 | * @since 1.0.0 19 | * @access public 20 | * @return string 21 | */ 22 | function ev_get_meta_key() { 23 | return apply_filters( 'ev_meta_key', 'Views' ); 24 | } 25 | 26 | /** 27 | * Sets the post view count of specific post by adding +1 to the total count. This function should only 28 | * be used if you want to add an addtional +1 to the count. 29 | * 30 | * @since 1.0.0 31 | * @access public 32 | * @param int $post_id 33 | * @return void 34 | */ 35 | function ev_set_post_view_count( $post_id ) { 36 | 37 | /* Get the number of views the post currently has. */ 38 | $old_views = get_post_meta( $post_id, ev_get_meta_key(), true ); 39 | 40 | /* Add +1 to the number of current views. */ 41 | $new_views = absint( $old_views ) + 1; 42 | 43 | /* Update the view count with the new view count. */ 44 | update_post_meta( $post_id, ev_get_meta_key(), $new_views, $old_views ); 45 | } 46 | 47 | /** 48 | * Gets the view count of a specific post. 49 | * 50 | * @since 1.0.0 51 | * @access public 52 | * @param int $post_id 53 | * @return int 54 | */ 55 | function ev_get_post_view_count( $post_id ) { 56 | 57 | /* Get the number of views the post has. */ 58 | $views = get_post_meta( $post_id, ev_get_meta_key(), true ); 59 | 60 | /* Return the view count and make sure it's an integer. */ 61 | return !empty( $views ) ? number_format_i18n( absint( $views ) ) : 0; 62 | } 63 | -------------------------------------------------------------------------------- /inc/shortcodes.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright Copyright (c) 2010 - 2014, Justin Tadlock 9 | * @link http://themehybrid.com/plugins/entry-views 10 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 11 | */ 12 | 13 | /* Register shortcodes. */ 14 | add_action( 'init', 'ev_register_shortcodes' ); 15 | 16 | /** 17 | * Registers shortcodes for the plugin. 18 | * 19 | * @since 1.0.0 20 | * @access public 21 | * @return void 22 | */ 23 | function ev_register_shortcodes() { 24 | 25 | add_shortcode( 'entry-views', 'ev_entry_views_shortcode' ); 26 | } 27 | 28 | /** 29 | * Gets the number of views a specific post has. 30 | * 31 | * @since 1.0.0 32 | * @access public 33 | * @param array $attr Attributes for use in the shortcode. 34 | * @return string 35 | */ 36 | function ev_entry_views_shortcode( $attr = '' ) { 37 | 38 | $defaults = array( 39 | 'before' => '', 40 | 'after' => '', 41 | 'text' => '', 42 | 'post_id' => get_the_ID() 43 | ); 44 | 45 | $attr = shortcode_atts( $defaults, $attr, 'entry-views' ); 46 | 47 | return ev_get_post_views( $attr ); 48 | } 49 | -------------------------------------------------------------------------------- /inc/template.php: -------------------------------------------------------------------------------- 1 | 8 | * @copyright Copyright (c) 2010 - 2014, Justin Tadlock 9 | * @link http://themehybrid.com/plugins/entry-views 10 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 11 | */ 12 | 13 | /** 14 | * Outputs a specific post's view count. This is a wrapper function for ev_get_post_views(). It simply 15 | * prints the output of that function to the screen. 16 | * 17 | * @since 1.0.0 18 | * @access public 19 | * @param array $args 20 | * @return void 21 | */ 22 | function ev_post_views( $args = array() ) { 23 | echo ev_get_post_views( $args ); 24 | } 25 | 26 | /** 27 | * Template tag for getting a specific post's view count. It will default to the current post in The 28 | * Loop. To use the 'text' argument, either pass a nooped plural using _n_noop() or a single text string. 29 | * 30 | * @since 1.0.0 31 | * @access public 32 | * @param array $args 33 | * @return string 34 | */ 35 | function ev_get_post_views( $args = array() ) { 36 | 37 | $defaults = array( 38 | 'post_id' => get_the_ID(), 39 | 'before' => '', 40 | 'after' => '', 41 | /* Translators: %s is the number of views a post has. */ 42 | 'text' => _n_noop( '%s View', '%s Views', 'entry-views' ), 43 | 'wrap' => '%s' 44 | ); 45 | 46 | $args = wp_parse_args( $args, $defaults ); 47 | 48 | $views = ev_get_post_view_count( $args['post_id'] ); 49 | 50 | $text = is_array( $args['text'] ) ? translate_nooped_plural( $args['text'], $views ) : $args['text']; 51 | 52 | $html = sprintf( 53 | $args['wrap'], 54 | 'class="entry-views" itemprop="interactionCount" itemscope="itemscope" itemtype="http://schema.org/UserPageVisits"', 55 | sprintf( $text, $views ) 56 | ); 57 | 58 | return $args['before'] . $html . $args['after']; 59 | } 60 | -------------------------------------------------------------------------------- /inc/widget-entry-views.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2010 - 2014, Justin Tadlock 7 | * @link http://themehybrid.com/plugins/entry-views 8 | * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 9 | */ 10 | 11 | /** 12 | * Entry Views widget. 13 | * 14 | * @since 1.0.0 15 | */ 16 | class EV_Widget_Entry_Views extends WP_Widget { 17 | 18 | /** 19 | * Default arguments for the widget settings. 20 | * 21 | * @since 1.0.0 22 | * @access public 23 | * @var array 24 | */ 25 | public $defaults = array(); 26 | 27 | /** 28 | * Set up the widget's unique name, ID, class, description, and other options. 29 | * 30 | * @since 1.0.0 31 | * @access public 32 | * @return void 33 | */ 34 | function __construct() { 35 | 36 | /* Set up the widget options. */ 37 | $widget_options = array( 38 | 'classname' => 'widget-entry-views', 39 | 'description' => esc_html__( 'Display posts based on their view count.', 'entry-views' ) 40 | ); 41 | 42 | /* Set up the widget control options. */ 43 | $control_options = array( 44 | 'width' => 200, 45 | 'height' => 350 46 | ); 47 | 48 | /* Create the widget. */ 49 | $this->WP_Widget( 50 | 'ev-entry-views', 51 | __( 'Entry Views', 'entry-views' ), 52 | $widget_options, 53 | $control_options 54 | ); 55 | 56 | /* Set up defaults. */ 57 | $this->defaults = array( 58 | 'title' => esc_attr__( 'Views', 'entry-views' ), 59 | 'posts_per_page' => 10, 60 | 'post_type' => 'post', 61 | 'order' => 'DESC', 62 | 'show_view_count' => true 63 | ); 64 | } 65 | 66 | /** 67 | * Outputs the widget based on the arguments input through the widget controls. 68 | * 69 | * @since 1.0.0 70 | * @access public 71 | * @param array $sidebar 72 | * @param array $instance 73 | * @return void 74 | */ 75 | function widget( $sidebar, $instance ) { 76 | 77 | /* Set the $args for wp_get_archives() to the $instance array. */ 78 | $args = wp_parse_args( $instance, $this->defaults ); 79 | 80 | /* Output the sidebar's $before_widget wrapper. */ 81 | echo $sidebar['before_widget']; 82 | 83 | /* If a title was input by the user, display it. */ 84 | if ( !empty( $args['title'] ) ) 85 | echo $sidebar['before_title'] . apply_filters( 'widget_title', $args['title'], $instance, $this->id_base ) . $sidebar['after_title']; 86 | 87 | /* Query the most/least viewed posts. */ 88 | $loop = new WP_Query( 89 | array( 90 | 'post_type' => $args['post_type'], 91 | 'posts_per_page' => $args['posts_per_page'], 92 | 'order' => $args['order'], 93 | 'orderby' => 'meta_value_num', 94 | 'ignore_sticky_posts' => true, 95 | 'meta_key' => ev_get_meta_key() 96 | ) 97 | ); 98 | 99 | if ( $loop->have_posts() ) : ?> 100 | 101 | 115 | 116 | defaults ); 167 | 168 | $post_types = array(); 169 | $_post_types = get_post_types( array( 'public' => true ), 'objects' ); 170 | 171 | foreach ( $_post_types as $_post_type ) { 172 | if ( post_type_supports( $_post_type->name, 'entry-views' ) ) 173 | $post_types[] = $_post_type; 174 | } 175 | 176 | /* Create an array of order options. */ 177 | $order = array( 178 | 'ASC' => esc_attr__( 'Ascending', 'entry-views' ), 179 | 'DESC' => esc_attr__( 'Descending', 'entry-views' ) 180 | ); 181 | 182 | ?> 183 |

184 | 185 | 186 |

187 |

188 | 189 | 194 |

195 |

196 | 197 | 198 |

199 |

200 | 201 | 206 |

207 |

208 | 210 |

211 | \n" 13 | "Language-Team: LANGUAGE \n" 14 | 15 | #. Translators: %s is the number of views a post has. 16 | 17 | #: inc/template.php:42 18 | msgid "%s View" 19 | msgid_plural "%s Views" 20 | msgstr[0] "" 21 | msgstr[1] "" 22 | 23 | #: inc/widget-entry-views.php:39 24 | msgid "Display posts based on their view count." 25 | msgstr "" 26 | 27 | #: inc/widget-entry-views.php:51 28 | msgid "Entry Views" 29 | msgstr "" 30 | 31 | #: inc/widget-entry-views.php:58 32 | msgid "Views" 33 | msgstr "" 34 | 35 | #: inc/widget-entry-views.php:178 36 | msgid "Ascending" 37 | msgstr "" 38 | 39 | #: inc/widget-entry-views.php:179 40 | msgid "Descending" 41 | msgstr "" 42 | 43 | #: inc/widget-entry-views.php:184 44 | msgid "Title:" 45 | msgstr "" 46 | 47 | #: inc/widget-entry-views.php:188 48 | msgid "Post Type:" 49 | msgstr "" 50 | 51 | #: inc/widget-entry-views.php:196 52 | msgid "Limit:" 53 | msgstr "" 54 | 55 | #: inc/widget-entry-views.php:200 56 | msgid "Order:" 57 | msgstr "" 58 | 59 | #: inc/widget-entry-views.php:209 60 | msgid "Show view count?" 61 | msgstr "" 62 | #. Plugin Name of the plugin/theme 63 | msgid "Entry Views" 64 | msgstr "" 65 | 66 | #. Plugin URI of the plugin/theme 67 | msgid "http://themehybrid.com/plugins/entry-views" 68 | msgstr "" 69 | 70 | #. Description of the plugin/theme 71 | msgid "A WordPress plugin for tracking the number of post views." 72 | msgstr "" 73 | 74 | #. Author of the plugin/theme 75 | msgid "Justin Tadlock" 76 | msgstr "" 77 | 78 | #. Author URI of the plugin/theme 79 | msgid "http://justintadlock.com" 80 | msgstr "" 81 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Entry Views # 2 | 3 | A WordPress plugin for tracking post/page view statistics. 4 | 5 | Entry Views keeps track of the number of views a post/page (or any post type) has. It comes handy with a widget for showing the most viewed posts, a shortcode for displaying a post's view count, and handy template tags for use within theme template files. 6 | 7 | ## Changelog ## 8 | 9 | ### Version 1.0.0 ### 10 | 11 | * Everything is new! -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Entry Views === 2 | 3 | Contributors: greenshady 4 | Donate link: http://themehybrid.com/donate 5 | Tags: ajax, counter, post, posts, statistics, stats, tracking 6 | Requires at least: 3.8 7 | Tested up to: 3.9.1 8 | Stable tag: 1.0.0 9 | License: GPLv2 or later 10 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 11 | 12 | A WordPress plugin for tracking post/page view statistics. 13 | 14 | == Description == 15 | 16 | Entry Views keeps track of the number of views a post/page (or any post type) has. It comes handy with a widget for showing the most viewed posts, a shortcode for displaying a post's view count, and handy template tags for use within theme template files. 17 | 18 | ### Professional Support ### 19 | 20 | If you need professional plugin support from me, the plugin author, you can access the support forums at [Theme Hybrid](http://themehybrid.com/support), which is a professional WordPress help/support site where I handle support for all my plugins and themes for a community of 40,000+ users (and growing). 21 | 22 | ### Plugin Development ### 23 | 24 | If you're a plugin author or just a code hobbyist, you can follow the development of this plugin on it's [GitHub repository](https://github.com/justintadlock/entry-views). 25 | 26 | ### Donations ### 27 | 28 | Yes, I do accept donations. If you want to buy me a beer or whatever, you can do so from my [donations page](http://themehybrid.com/donate). I appreciate all donations, no matter the size. Further development of this plugin is not contingent on donations, but they are always a nice incentive. 29 | 30 | == Installation == 31 | 32 | 1. Uzip the `entry-views.zip` folder. 33 | 2. Upload the `entry-views` folder to your `/wp-content/plugins` directory. 34 | 3. In your WordPress dashboard, head over to the *Plugins* section. 35 | 4. Activate *Entry Views*. 36 | 37 | == Frequently Asked Questions == 38 | 39 | ### Why was this plugin created? ### 40 | 41 | The plugin was originally created as a script for use with some of my themes. However, it's not a good idea to package something like this with a theme because it'd stop working when switching between themes. Therefore, the script was ported over into a plugin for everyone to use, regardless of the theme they're using. 42 | 43 | ### How do I use the plugin? ### 44 | 45 | As soon as you install and activate the plugin, it'll start tracking post views immediately. From that point, you can use the "Entry Views" widget in one of your theme's sidebars to display the most viewed posts. 46 | 47 | ### Can I show the view count for each post? ### 48 | 49 | Yes, you can certainly do this. It will require editing your theme's templates and inserting a simple line of code within The Loop. You'll need to enter the following: 50 | 51 | 52 | 53 | ### What post types does this plugin support? ### 54 | 55 | By default, it supports the following post types: 56 | 57 | * Post 58 | * Page 59 | * Media/Attachment 60 | * Portfolio Item - [Custom Content Portfolio Plugin](http://wordpress.org/plugins/custom-content-portfolio) 61 | * Restaurant Item - [Restaurant Plugin](http://wordpress.org/plugins/restaurant) 62 | * Recipe - Upcoming recipe plugin 63 | * Literature - Upcoming literature/writing plugin 64 | 65 | ### Will you add support for other post types? ### 66 | 67 | Yes, I definitely will. If you give me the name of the post type or of the plugin that you use, I will be more than happy to add support for it. 68 | 69 | ### Can I add support for a custom post type? ### 70 | 71 | Yes, you can absolutely do this. If you're registering the post type yourself, simply add support for `entry-views` in your `supports` array. 72 | 73 | If you're adding support for a post type that you're not registering, add the following code to your plugin file or theme's `functions.php`: 74 | 75 | add_action( 'init', 'my_register_post_type_support' ); 76 | 77 | function my_register_post_type_support() { 78 | add_post_type_support( 'your_post_type_name', 'entry-views' ); 79 | } 80 | 81 | ### What features do you plan to add in the future? ### 82 | 83 | One feature I'd like to add is to the admin area. It'd be neat to show each post's views on the edit posts screen and even allow you to sort the posts by number of views. 84 | 85 | Other features are really up to you all. If you have a feature request, please don't hesitate to ask. 86 | 87 | ### Can you help me? ### 88 | 89 | Unfortunately, I cannot provide free support for this plugin to everyone. I honestly wish I could. My day job requires too much of my time for that, which is how I pay the bills and eat. However, you can sign up for my [support forums](http://themehybrid.com/support) for full support of this plugin, all my other plugins, and all my themes for one price. 90 | 91 | == Screenshots == 92 | 93 | == Changelog == 94 | 95 | ### Version 1.0.0 ### 96 | 97 | * Everything is new! -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justintadlock/entry-views/af47bdd488359d92be8f154e2089e6bcca9719a8/screenshot-1.png -------------------------------------------------------------------------------- /screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/justintadlock/entry-views/af47bdd488359d92be8f154e2089e6bcca9719a8/screenshot-2.png --------------------------------------------------------------------------------