post-views-counter
Last commit date
css
10 years ago
images
10 years ago
includes
10 years ago
js
10 years ago
languages
10 years ago
index.php
10 years ago
post-views-counter.php
10 years ago
readme.txt
10 years ago
post-views-counter.php
369 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Post Views Counter |
| 4 | Description: Forget WP-PostViews. Display how many times a post, page or custom post type had been viewed in a simple, fast and reliable way. |
| 5 | Version: 1.2.0 |
| 6 | Author: dFactory |
| 7 | Author URI: http://www.dfactory.eu/ |
| 8 | Plugin URI: http://www.dfactory.eu/plugins/post-views-counter/ |
| 9 | License: MIT License |
| 10 | License URI: http://opensource.org/licenses/MIT |
| 11 | Text Domain: post-views-counter |
| 12 | Domain Path: /languages |
| 13 | |
| 14 | Post Views Counter |
| 15 | Copyright (C) 2014-2016, Digital Factory - info@digitalfactory.pl |
| 16 | |
| 17 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 18 | |
| 19 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 20 | |
| 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | // exit if accessed directly |
| 25 | if ( ! defined( 'ABSPATH' ) ) |
| 26 | exit; |
| 27 | |
| 28 | if ( ! class_exists( 'Post_Views_Counter' ) ) : |
| 29 | |
| 30 | /** |
| 31 | * Post Views Counter final class. |
| 32 | * |
| 33 | * @class Post_Views_Counter |
| 34 | * @version 1.2.0 |
| 35 | */ |
| 36 | final class Post_Views_Counter { |
| 37 | |
| 38 | private static $instance; |
| 39 | public $options; |
| 40 | public $defaults = array( |
| 41 | 'general' => array( |
| 42 | 'post_types_count' => array( 'post' ), |
| 43 | 'counter_mode' => 'php', |
| 44 | 'post_views_column' => true, |
| 45 | 'time_between_counts' => array( |
| 46 | 'number' => 24, |
| 47 | 'type' => 'hours' |
| 48 | ), |
| 49 | 'reset_counts' => array( |
| 50 | 'number' => 30, |
| 51 | 'type' => 'days' |
| 52 | ), |
| 53 | 'flush_interval' => array( |
| 54 | 'number' => 0, |
| 55 | 'type' => 'minutes' |
| 56 | ), |
| 57 | 'exclude' => array( |
| 58 | 'groups' => array(), |
| 59 | 'roles' => array() |
| 60 | ), |
| 61 | 'exclude_ips' => array(), |
| 62 | 'restrict_edit_views' => false, |
| 63 | 'deactivation_delete' => false, |
| 64 | 'cron_run' => true, |
| 65 | 'cron_update' => true |
| 66 | ), |
| 67 | 'display' => array( |
| 68 | 'label' => 'Post Views:', |
| 69 | 'post_types_display' => array( 'post' ), |
| 70 | 'restrict_display' => array( |
| 71 | 'groups' => array(), |
| 72 | 'roles' => array() |
| 73 | ), |
| 74 | 'position' => 'after', |
| 75 | 'display_style' => array( |
| 76 | 'icon' => true, |
| 77 | 'text' => true |
| 78 | ), |
| 79 | 'link_to_post' => true, |
| 80 | 'icon_class' => 'dashicons-chart-bar' |
| 81 | ), |
| 82 | 'version' => '1.2.0' |
| 83 | ); |
| 84 | |
| 85 | /** |
| 86 | * Disable object clone. |
| 87 | */ |
| 88 | private function __clone() {} |
| 89 | |
| 90 | /** |
| 91 | * Disable unserializing of the class. |
| 92 | */ |
| 93 | private function __wakeup() {} |
| 94 | |
| 95 | /** |
| 96 | * Main Post_Views_Counter instance, |
| 97 | * Insures that only one instance of Post_Views_Counter exists in memory at one time. |
| 98 | * |
| 99 | * @return object |
| 100 | */ |
| 101 | public static function instance() { |
| 102 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Post_Views_Counter ) ) { |
| 103 | self::$instance = new Post_Views_Counter; |
| 104 | self::$instance->define_constants(); |
| 105 | |
| 106 | add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) ); |
| 107 | |
| 108 | self::$instance->includes(); |
| 109 | self::$instance->update = new Post_Views_Counter_Update(); |
| 110 | self::$instance->settings = new Post_Views_Counter_Settings(); |
| 111 | self::$instance->query = new Post_Views_Counter_Query(); |
| 112 | self::$instance->cron = new Post_Views_Counter_Cron(); |
| 113 | self::$instance->counter = new Post_Views_Counter_Counter(); |
| 114 | self::$instance->columns = new Post_Views_Counter_Columns(); |
| 115 | self::$instance->frontend = new Post_Views_Counter_Frontend(); |
| 116 | self::$instance->dashboard = new Post_Views_Counter_Dashboard(); |
| 117 | self::$instance->widgets = new Post_Views_Counter_Widgets(); |
| 118 | } |
| 119 | return self::$instance; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Setup plugin constants. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | private function define_constants() { |
| 128 | define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) ); |
| 129 | define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) ); |
| 130 | define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( plugin_basename( __FILE__ ) ) . '/' ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Include required files |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | private function includes() { |
| 139 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/update.php' ); |
| 140 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/settings.php' ); |
| 141 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/columns.php' ); |
| 142 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/query.php' ); |
| 143 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/cron.php' ); |
| 144 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/counter.php' ); |
| 145 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/frontend.php' ); |
| 146 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/dashboard.php' ); |
| 147 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Class constructor. |
| 152 | * |
| 153 | * @return void |
| 154 | */ |
| 155 | public function __construct() { |
| 156 | register_activation_hook( __FILE__, array( $this, 'activation' ) ); |
| 157 | register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) ); |
| 158 | |
| 159 | // settings |
| 160 | $this->options = array( |
| 161 | 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ), |
| 162 | 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) ) |
| 163 | ); |
| 164 | |
| 165 | // actions |
| 166 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts_styles' ) ); |
| 167 | add_action( 'wp_loaded', array( $this, 'load_pluggable_functions' ), 10 ); |
| 168 | |
| 169 | // filters |
| 170 | add_filter( 'plugin_row_meta', array( $this, 'plugin_extend_links' ), 10, 2 ); |
| 171 | add_filter( 'plugin_action_links', array( $this, 'plugin_settings_link' ), 10, 2 ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Plugin activation function. |
| 176 | */ |
| 177 | public function activation() { |
| 178 | global $wpdb, $charset_collate; |
| 179 | |
| 180 | // required for dbdelta |
| 181 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 182 | |
| 183 | // create post views table |
| 184 | dbDelta( ' |
| 185 | CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views ( |
| 186 | id bigint unsigned NOT NULL, |
| 187 | type tinyint(1) unsigned NOT NULL, |
| 188 | period varchar(8) NOT NULL, |
| 189 | count bigint unsigned NOT NULL, |
| 190 | PRIMARY KEY (type, period, id), |
| 191 | UNIQUE INDEX id_period (id, period) USING BTREE, |
| 192 | INDEX type_period_count (type, period, count) USING BTREE |
| 193 | ) ' . $charset_collate . ';' |
| 194 | ); |
| 195 | |
| 196 | // add default options |
| 197 | add_option( 'post_views_counter_settings_general', $this->defaults['general'], '', 'no' ); |
| 198 | add_option( 'post_views_counter_settings_display', $this->defaults['display'], '', 'no' ); |
| 199 | add_option( 'post_views_counter_version', $this->defaults['version'], '', 'no' ); |
| 200 | |
| 201 | // schedule cache flush |
| 202 | $this->schedule_cache_flush(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Plugin deactivation function. |
| 207 | */ |
| 208 | public function deactivation() { |
| 209 | // delete default options |
| 210 | if ( $this->options['general']['deactivation_delete'] ) { |
| 211 | delete_option( 'post_views_counter_settings_general' ); |
| 212 | delete_option( 'post_views_counter_settings_display' ); |
| 213 | } |
| 214 | |
| 215 | // remove schedule |
| 216 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 217 | remove_action( 'pvc_reset_counts', array( Post_Views_Counter()->cron, 'reset_counts' ) ); |
| 218 | |
| 219 | $this->remove_cache_flush(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Schedule cache flushing if it's not already scheduled. |
| 224 | */ |
| 225 | public function schedule_cache_flush( $forced = true ) { |
| 226 | if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) { |
| 227 | wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Remove scheduled cache flush and the corresponding action. |
| 233 | */ |
| 234 | public function remove_cache_flush() { |
| 235 | wp_clear_scheduled_hook( 'pvc_flush_cached_counts' ); |
| 236 | remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->cron, 'flush_cached_counts' ) ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Load text domain. |
| 241 | */ |
| 242 | public function load_textdomain() { |
| 243 | load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Load pluggable template functions. |
| 248 | */ |
| 249 | public function load_pluggable_functions() { |
| 250 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Enqueue admin scripts and styles. |
| 255 | */ |
| 256 | public function admin_scripts_styles( $page ) { |
| 257 | wp_register_style( |
| 258 | 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css' |
| 259 | ); |
| 260 | |
| 261 | wp_register_script( |
| 262 | 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array( 'jquery' ), $this->defaults['version'] |
| 263 | ); |
| 264 | |
| 265 | wp_register_script( |
| 266 | 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array( 'jquery' ), $this->defaults['version'] |
| 267 | ); |
| 268 | |
| 269 | wp_register_script( |
| 270 | 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array( 'jquery', 'inline-edit-post' ), $this->defaults['version'] |
| 271 | ); |
| 272 | |
| 273 | // load on PVC settings page |
| 274 | if ( $page === 'settings_page_post-views-counter' ) { |
| 275 | |
| 276 | wp_enqueue_script( 'pvc-admin-settings' ); |
| 277 | |
| 278 | wp_localize_script( |
| 279 | 'pvc-admin-settings', 'pvcArgsSettings', array( |
| 280 | 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' ) |
| 281 | ) |
| 282 | ); |
| 283 | |
| 284 | wp_enqueue_style( 'pvc-admin' ); |
| 285 | |
| 286 | // load on single post page |
| 287 | } elseif ( $page === 'post.php' || $page === 'post-new.php' ) { |
| 288 | |
| 289 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 290 | |
| 291 | global $post_type; |
| 292 | |
| 293 | if ( ! in_array( $post_type, (array) $post_types ) ) |
| 294 | return; |
| 295 | |
| 296 | wp_enqueue_style( 'pvc-admin' ); |
| 297 | wp_enqueue_script( 'pvc-admin-post' ); |
| 298 | } elseif ( $page === 'edit.php' ) { |
| 299 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 300 | |
| 301 | global $post_type; |
| 302 | |
| 303 | if ( ! in_array( $post_type, (array) $post_types ) ) |
| 304 | return; |
| 305 | |
| 306 | wp_enqueue_style( 'pvc-admin' ); |
| 307 | wp_enqueue_script( 'pvc-admin-quick-edit' ); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Add links to plugin support forum. |
| 313 | */ |
| 314 | public function plugin_extend_links( $links, $file ) { |
| 315 | |
| 316 | if ( ! current_user_can( 'install_plugins' ) ) |
| 317 | return $links; |
| 318 | |
| 319 | $plugin = plugin_basename( __FILE__ ); |
| 320 | |
| 321 | if ( $file == $plugin ) { |
| 322 | return array_merge( |
| 323 | $links, array( sprintf( '<a href="http://www.dfactory.eu/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) ) ) |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | return $links; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Add link to settings page. |
| 332 | */ |
| 333 | public function plugin_settings_link( $links, $file ) { |
| 334 | if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) |
| 335 | return $links; |
| 336 | |
| 337 | static $plugin; |
| 338 | |
| 339 | $plugin = plugin_basename( __FILE__ ); |
| 340 | |
| 341 | if ( $file == $plugin ) { |
| 342 | $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php' ) . '?page=post-views-counter', __( 'Settings', 'post-views-counter' ) ); |
| 343 | |
| 344 | array_unshift( $links, $settings_link ); |
| 345 | } |
| 346 | |
| 347 | return $links; |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
| 352 | endif; // end if class_exists check |
| 353 | |
| 354 | /** |
| 355 | * Initialise Post Views Counter. |
| 356 | * |
| 357 | * @return object |
| 358 | */ |
| 359 | function Post_Views_Counter() { |
| 360 | static $instance; |
| 361 | |
| 362 | // first call to instance() initializes the plugin |
| 363 | if ( $instance === null || ! ( $instance instanceof Post_Views_Counter ) ) |
| 364 | $instance = Post_Views_Counter::instance(); |
| 365 | |
| 366 | return $instance; |
| 367 | } |
| 368 | |
| 369 | Post_Views_Counter(); |