├── 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 |
184 | 185 | 186 |
187 |188 | 189 | 194 |
195 |196 | 197 | 198 |
199 |200 | 201 | 206 |
207 |208 | 210 |
211 | \n" 13 | "Language-Team: LANGUAGE