post-views-counter
Last commit date
css
9 years ago
images
9 years ago
includes
9 years ago
js
9 years ago
languages
9 years ago
index.php
9 years ago
post-views-counter.php
9 years ago
readme.txt
9 years ago
post-views-counter.php
425 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Post Views Counter |
| 4 | Description: Post Views Counter allows you to 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.6 |
| 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-2017, 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.6 |
| 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 | 'page_types_display' => array( 'singular' ), |
| 71 | 'restrict_display' => array( |
| 72 | 'groups' => array(), |
| 73 | 'roles' => array() |
| 74 | ), |
| 75 | 'position' => 'after', |
| 76 | 'display_style' => array( |
| 77 | 'icon' => true, |
| 78 | 'text' => true |
| 79 | ), |
| 80 | 'link_to_post' => true, |
| 81 | 'icon_class' => 'dashicons-chart-bar' |
| 82 | ), |
| 83 | 'version' => '1.2.6' |
| 84 | ); |
| 85 | |
| 86 | /** |
| 87 | * Disable object clone. |
| 88 | */ |
| 89 | private function __clone() { |
| 90 | |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Disable unserializing of the class. |
| 95 | */ |
| 96 | private function __wakeup() { |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Main plugin instance, |
| 102 | * Insures that only one instance of the plugin exists in memory at one time. |
| 103 | * |
| 104 | * @return object |
| 105 | */ |
| 106 | public static function instance() { |
| 107 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Post_Views_Counter ) ) { |
| 108 | |
| 109 | self::$instance = new Post_Views_Counter; |
| 110 | self::$instance->define_constants(); |
| 111 | |
| 112 | add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) ); |
| 113 | |
| 114 | self::$instance->includes(); |
| 115 | |
| 116 | self::$instance->update = new Post_Views_Counter_Update(); |
| 117 | self::$instance->settings = new Post_Views_Counter_Settings(); |
| 118 | self::$instance->query = new Post_Views_Counter_Query(); |
| 119 | self::$instance->cron = new Post_Views_Counter_Cron(); |
| 120 | self::$instance->counter = new Post_Views_Counter_Counter(); |
| 121 | self::$instance->columns = new Post_Views_Counter_Columns(); |
| 122 | self::$instance->crawler_detect = new Post_Views_Counter_Crawler_Detect(); |
| 123 | self::$instance->frontend = new Post_Views_Counter_Frontend(); |
| 124 | self::$instance->dashboard = new Post_Views_Counter_Dashboard(); |
| 125 | self::$instance->widgets = new Post_Views_Counter_Widgets(); |
| 126 | } |
| 127 | return self::$instance; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Setup plugin constants. |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | private function define_constants() { |
| 136 | define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) ); |
| 137 | define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) ); |
| 138 | define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( plugin_basename( __FILE__ ) ) . '/' ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Include required files. |
| 143 | * |
| 144 | * @return void |
| 145 | */ |
| 146 | private function includes() { |
| 147 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/update.php' ); |
| 148 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/settings.php' ); |
| 149 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/columns.php' ); |
| 150 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/query.php' ); |
| 151 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/cron.php' ); |
| 152 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/counter.php' ); |
| 153 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/crawler-detect.php' ); |
| 154 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/frontend.php' ); |
| 155 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/dashboard.php' ); |
| 156 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Class constructor. |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | public function __construct() { |
| 165 | register_activation_hook( __FILE__, array( $this, 'activation' ) ); |
| 166 | register_deactivation_hook( __FILE__, array( $this, 'deactivation' ) ); |
| 167 | |
| 168 | // settings |
| 169 | $this->options = array( |
| 170 | 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ), |
| 171 | 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) ) |
| 172 | ); |
| 173 | |
| 174 | // actions |
| 175 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 176 | add_action( 'wp_loaded', array( $this, 'load_pluggable_functions' ), 10 ); |
| 177 | |
| 178 | // filters |
| 179 | add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); |
| 180 | add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 2 ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Plugin activation function. |
| 185 | */ |
| 186 | public function activation() { |
| 187 | global $wpdb, $charset_collate; |
| 188 | |
| 189 | // required for dbdelta |
| 190 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 191 | |
| 192 | // create post views table |
| 193 | dbDelta( ' |
| 194 | CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views ( |
| 195 | id bigint unsigned NOT NULL, |
| 196 | type tinyint(1) unsigned NOT NULL, |
| 197 | period varchar(8) NOT NULL, |
| 198 | count bigint unsigned NOT NULL, |
| 199 | PRIMARY KEY (type, period, id), |
| 200 | UNIQUE INDEX id_type_period_count (id, type, period, count) USING BTREE, |
| 201 | INDEX type_period_count (type, period, count) USING BTREE |
| 202 | ) ' . $charset_collate . ';' |
| 203 | ); |
| 204 | |
| 205 | // add default options |
| 206 | add_option( 'post_views_counter_settings_general', $this->defaults['general'], '', 'no' ); |
| 207 | add_option( 'post_views_counter_settings_display', $this->defaults['display'], '', 'no' ); |
| 208 | add_option( 'post_views_counter_version', $this->defaults['version'], '', 'no' ); |
| 209 | |
| 210 | // schedule cache flush |
| 211 | $this->schedule_cache_flush(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Plugin deactivation function. |
| 216 | */ |
| 217 | public function deactivation() { |
| 218 | // delete default options |
| 219 | if ( $this->options['general']['deactivation_delete'] ) { |
| 220 | delete_option( 'post_views_counter_settings_general' ); |
| 221 | delete_option( 'post_views_counter_settings_display' ); |
| 222 | |
| 223 | global $wpdb; |
| 224 | |
| 225 | // delete table from database |
| 226 | $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post_views' ); |
| 227 | } |
| 228 | |
| 229 | // remove schedule |
| 230 | wp_clear_scheduled_hook( 'pvc_reset_counts' ); |
| 231 | remove_action( 'pvc_reset_counts', array( Post_Views_Counter()->cron, 'reset_counts' ) ); |
| 232 | |
| 233 | $this->remove_cache_flush(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Schedule cache flushing if it's not already scheduled. |
| 238 | * |
| 239 | * @param bool $forced |
| 240 | */ |
| 241 | public function schedule_cache_flush( $forced = true ) { |
| 242 | if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) { |
| 243 | wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Remove scheduled cache flush and the corresponding action. |
| 249 | */ |
| 250 | public function remove_cache_flush() { |
| 251 | wp_clear_scheduled_hook( 'pvc_flush_cached_counts' ); |
| 252 | remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->cron, 'flush_cached_counts' ) ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Load text domain. |
| 257 | */ |
| 258 | public function load_textdomain() { |
| 259 | load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Load pluggable template functions. |
| 264 | */ |
| 265 | public function load_pluggable_functions() { |
| 266 | include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Enqueue admin scripts and styles. |
| 271 | * |
| 272 | * @global string $post_type |
| 273 | * @param string $page |
| 274 | */ |
| 275 | public function admin_enqueue_scripts( $page ) { |
| 276 | wp_register_style( |
| 277 | 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css' |
| 278 | ); |
| 279 | |
| 280 | wp_register_script( |
| 281 | 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array( 'jquery' ), $this->defaults['version'] |
| 282 | ); |
| 283 | |
| 284 | wp_register_script( |
| 285 | 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array( 'jquery' ), $this->defaults['version'] |
| 286 | ); |
| 287 | |
| 288 | wp_register_script( |
| 289 | 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array( 'jquery', 'inline-edit-post' ), $this->defaults['version'] |
| 290 | ); |
| 291 | |
| 292 | // load on PVC settings page |
| 293 | if ( $page === 'settings_page_post-views-counter' ) { |
| 294 | |
| 295 | wp_enqueue_script( 'pvc-admin-settings' ); |
| 296 | |
| 297 | wp_localize_script( |
| 298 | 'pvc-admin-settings', 'pvcArgsSettings', array( |
| 299 | 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' ), |
| 300 | 'resetViews' => __( 'Are you sure you want to delete all existing data?', 'post-views-counter' ) |
| 301 | ) |
| 302 | ); |
| 303 | |
| 304 | wp_enqueue_style( 'pvc-admin' ); |
| 305 | |
| 306 | // load on single post page |
| 307 | } elseif ( $page === 'post.php' || $page === 'post-new.php' ) { |
| 308 | |
| 309 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 310 | |
| 311 | global $post_type; |
| 312 | |
| 313 | if ( ! in_array( $post_type, (array) $post_types ) ) |
| 314 | return; |
| 315 | |
| 316 | wp_enqueue_style( 'pvc-admin' ); |
| 317 | wp_enqueue_script( 'pvc-admin-post' ); |
| 318 | } elseif ( $page === 'edit.php' ) { |
| 319 | $post_types = Post_Views_Counter()->options['general']['post_types_count']; |
| 320 | |
| 321 | global $post_type; |
| 322 | |
| 323 | if ( ! in_array( $post_type, (array) $post_types ) ) |
| 324 | return; |
| 325 | |
| 326 | wp_enqueue_style( 'pvc-admin' ); |
| 327 | wp_enqueue_script( 'pvc-admin-quick-edit' ); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Add links to plugin support forum. |
| 333 | * |
| 334 | * @param array $links |
| 335 | * @param string $file |
| 336 | * @return array |
| 337 | */ |
| 338 | public function plugin_row_meta( $links, $file ) { |
| 339 | |
| 340 | if ( ! current_user_can( 'install_plugins' ) ) |
| 341 | return $links; |
| 342 | |
| 343 | $plugin = plugin_basename( __FILE__ ); |
| 344 | |
| 345 | if ( $file == $plugin ) { |
| 346 | return array_merge( |
| 347 | $links, array( sprintf( '<a href="http://www.dfactory.eu/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) ) ) |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | return $links; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Add link to settings page. |
| 356 | * |
| 357 | * @staticvar string $plugin |
| 358 | * @param array $links |
| 359 | * @param string $file |
| 360 | * @return array |
| 361 | */ |
| 362 | public function plugin_action_links( $links, $file ) { |
| 363 | if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) |
| 364 | return $links; |
| 365 | |
| 366 | static $plugin; |
| 367 | |
| 368 | $plugin = plugin_basename( __FILE__ ); |
| 369 | |
| 370 | if ( $file == $plugin ) { |
| 371 | $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php' ) . '?page=post-views-counter', __( 'Settings', 'post-views-counter' ) ); |
| 372 | |
| 373 | array_unshift( $links, $settings_link ); |
| 374 | } |
| 375 | |
| 376 | return $links; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Add admin notices. |
| 381 | */ |
| 382 | public function add_notice( $html = '', $status = '', $paragraph = false ) { |
| 383 | $this->notices[] = array( |
| 384 | 'html' => $html, |
| 385 | 'status' => $status, |
| 386 | 'paragraph' => $paragraph |
| 387 | ); |
| 388 | |
| 389 | add_action( 'admin_notices', array( $this, 'display_notice' ) ); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Print admin notices. |
| 394 | */ |
| 395 | public function display_notice() { |
| 396 | foreach ( $this->notices as $notice ) { |
| 397 | echo ' |
| 398 | <div class="post-views-counter ' . $notice['status'] . '"> |
| 399 | ' . ( $notice['paragraph'] ? '<p>' : '' ) . ' |
| 400 | ' . $notice['html'] . ' |
| 401 | ' . ( $notice['paragraph'] ? '</p>' : '' ) . ' |
| 402 | </div>'; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | endif; // end if class_exists check |
| 408 | |
| 409 | /** |
| 410 | * Initialise Post Views Counter. |
| 411 | * |
| 412 | * @return object |
| 413 | */ |
| 414 | function Post_Views_Counter() { |
| 415 | static $instance; |
| 416 | |
| 417 | // first call to instance() initializes the plugin |
| 418 | if ( $instance === null || ! ( $instance instanceof Post_Views_Counter ) ) |
| 419 | $instance = Post_Views_Counter::instance(); |
| 420 | |
| 421 | return $instance; |
| 422 | } |
| 423 | |
| 424 | Post_Views_Counter(); |
| 425 |