post-views-counter
Last commit date
assets
12 years ago
css
12 years ago
images
12 years ago
includes
12 years ago
js
12 years ago
languages
12 years ago
index.php
12 years ago
post-views-counter.php
12 years ago
readme.txt
12 years ago
post-views-counter.php
317 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.0.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, 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 | |
| 25 | if(!defined('ABSPATH')) exit; |
| 26 | |
| 27 | define('POST_VIEWS_COUNTER_URL', plugins_url('', __FILE__)); |
| 28 | define('POST_VIEWS_COUNTER_PATH', plugin_dir_path(__FILE__)); |
| 29 | define('POST_VIEWS_COUNTER_REL_PATH', dirname(plugin_basename(__FILE__)).'/'); |
| 30 | |
| 31 | include_once(POST_VIEWS_COUNTER_PATH.'includes/update.php'); |
| 32 | include_once(POST_VIEWS_COUNTER_PATH.'includes/settings.php'); |
| 33 | include_once(POST_VIEWS_COUNTER_PATH.'includes/query.php'); |
| 34 | include_once(POST_VIEWS_COUNTER_PATH.'includes/cron.php'); |
| 35 | include_once(POST_VIEWS_COUNTER_PATH.'includes/counter.php'); |
| 36 | include_once(POST_VIEWS_COUNTER_PATH.'includes/columns.php'); |
| 37 | include_once(POST_VIEWS_COUNTER_PATH.'includes/frontend.php'); |
| 38 | include_once(POST_VIEWS_COUNTER_PATH.'includes/widgets.php'); |
| 39 | |
| 40 | |
| 41 | class Post_Views_Counter |
| 42 | { |
| 43 | private static $_instance; |
| 44 | private $instances; |
| 45 | private $options; |
| 46 | private $defaults = array( |
| 47 | 'general' => array( |
| 48 | 'post_types_count' => array('post'), |
| 49 | 'counter_mode' => 'php', |
| 50 | 'post_views_column' => true, |
| 51 | 'time_between_counts' => array( |
| 52 | 'number' => 24, |
| 53 | 'type' => 'hours' |
| 54 | ), |
| 55 | 'reset_counts' => array( |
| 56 | 'number' => 30, |
| 57 | 'type' => 'days' |
| 58 | ), |
| 59 | 'exclude' => array( |
| 60 | 'groups' => array(), |
| 61 | 'roles' => array() |
| 62 | ), |
| 63 | 'exclude_ips' => array(), |
| 64 | 'deactivation_delete' => false, |
| 65 | 'cron_run' => true, |
| 66 | 'cron_update' => true |
| 67 | ), |
| 68 | 'display' => array( |
| 69 | 'label' => 'Post Views:', |
| 70 | 'post_types_display' => array('post'), |
| 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-visibility' |
| 82 | ), |
| 83 | 'version' => '1.0.0' |
| 84 | ); |
| 85 | |
| 86 | |
| 87 | public static function instance() |
| 88 | { |
| 89 | if(self::$_instance === null) |
| 90 | self::$_instance = new self(); |
| 91 | |
| 92 | return self::$_instance; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | private function __clone() {} |
| 97 | private function __wakeup() {} |
| 98 | |
| 99 | |
| 100 | private function __construct() |
| 101 | { |
| 102 | register_activation_hook(__FILE__, array(&$this, 'activation')); |
| 103 | register_deactivation_hook(__FILE__, array(&$this, 'deactivation')); |
| 104 | |
| 105 | // settings |
| 106 | $this->options = array( |
| 107 | 'general' => array_merge($this->defaults['general'], get_option('post_views_counter_settings_general', $this->defaults['general'])), |
| 108 | 'display' => array_merge($this->defaults['display'], get_option('post_views_counter_settings_display', $this->defaults['display'])) |
| 109 | ); |
| 110 | |
| 111 | // actions |
| 112 | add_action('plugins_loaded', array(&$this, 'load_textdomain')); |
| 113 | add_action('admin_enqueue_scripts', array(&$this, 'admin_scripts_styles')); |
| 114 | add_action('wp', array(&$this, 'load_pluggable_functions'), 10); |
| 115 | |
| 116 | // filters |
| 117 | add_filter('plugin_action_links', array(&$this, 'plugin_settings_link'), 10, 2); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * Execution of plugin activation function |
| 123 | */ |
| 124 | public function activation() |
| 125 | { |
| 126 | global $wpdb, $charset_collate; |
| 127 | |
| 128 | // required for dbdelta |
| 129 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
| 130 | |
| 131 | // creates post views table |
| 132 | dbDelta(' |
| 133 | CREATE TABLE IF NOT EXISTS '.$wpdb->prefix.'post_views ( |
| 134 | id bigint unsigned NOT NULL, |
| 135 | type tinyint(1) unsigned NOT NULL, |
| 136 | period varchar(8) NOT NULL, |
| 137 | count bigint unsigned NOT NULL, |
| 138 | PRIMARY KEY (type, period, id), |
| 139 | UNIQUE INDEX id_period (id, period) USING BTREE, |
| 140 | INDEX type_period_count (type, period, count) USING BTREE |
| 141 | ) '.$charset_collate.';' |
| 142 | ); |
| 143 | |
| 144 | // adds default options |
| 145 | add_option('post_views_counter_settings_general', $this->defaults['general'], '', 'no'); |
| 146 | add_option('post_views_counter_settings_display', $this->defaults['display'], '', 'no'); |
| 147 | add_option('post_views_counter_version', $this->defaults['version'], '', 'no'); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * Execution of plugin deactivation function |
| 153 | */ |
| 154 | public function deactivation() |
| 155 | { |
| 156 | // deletes default options |
| 157 | if($this->options['general']['deactivation_delete']) |
| 158 | { |
| 159 | delete_option('post_views_counter_settings_general'); |
| 160 | delete_option('post_views_counter_settings_display'); |
| 161 | } |
| 162 | |
| 163 | // removes schedule |
| 164 | wp_clear_scheduled_hook('pvc_reset_counts'); |
| 165 | remove_action('pvc_reset_counts', array(Post_Views_Counter()->get_instance('cron'), 'reset_counts')); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Loads text domain |
| 171 | */ |
| 172 | public function load_textdomain() |
| 173 | { |
| 174 | load_plugin_textdomain('post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH.'languages/'); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Load pluggable template functions |
| 180 | */ |
| 181 | public function load_pluggable_functions() |
| 182 | { |
| 183 | include_once(POST_VIEWS_COUNTER_PATH.'includes/functions.php'); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Sets instance of class |
| 189 | */ |
| 190 | public function add_instance($name, $instance) |
| 191 | { |
| 192 | $this->instances[$name] = $instance; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * Gets instance of class |
| 198 | */ |
| 199 | public function get_instance($name) |
| 200 | { |
| 201 | if(in_array($name, array('counter', 'settings'), true)) |
| 202 | return $this->instances[$name]; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Gets allowed attribute |
| 208 | */ |
| 209 | public function get_attribute($attribute) |
| 210 | { |
| 211 | if(in_array($attribute, array('options', 'defaults'), true)) |
| 212 | { |
| 213 | switch(func_num_args()) |
| 214 | { |
| 215 | case 1: |
| 216 | return $this->{$attribute}; |
| 217 | |
| 218 | case 2: |
| 219 | return $this->{$attribute}[func_get_arg(1)]; |
| 220 | |
| 221 | case 3: |
| 222 | return $this->{$attribute}[func_get_arg(1)][func_get_arg(2)]; |
| 223 | |
| 224 | case 4: |
| 225 | return $this->{$attribute}[func_get_arg(1)][func_get_arg(2)][func_get_arg(3)]; |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Equeues admin scripts and styles |
| 235 | */ |
| 236 | public function admin_scripts_styles($page) |
| 237 | { |
| 238 | // loads only for settings page |
| 239 | if($page === 'settings_page_post-views-counter') |
| 240 | { |
| 241 | wp_register_script( |
| 242 | 'post-views-counter-admin-chosen', |
| 243 | POST_VIEWS_COUNTER_URL.'/assets/chosen/chosen.jquery.min.js', |
| 244 | array('jquery') |
| 245 | ); |
| 246 | |
| 247 | wp_register_script( |
| 248 | 'post-views-counter-admin', |
| 249 | POST_VIEWS_COUNTER_URL.'/js/admin.js', |
| 250 | array('jquery', 'post-views-counter-admin-chosen') |
| 251 | ); |
| 252 | |
| 253 | wp_enqueue_script('post-views-counter-admin-chosen'); |
| 254 | wp_enqueue_script('post-views-counter-admin'); |
| 255 | |
| 256 | wp_localize_script( |
| 257 | 'post-views-counter-admin', |
| 258 | 'pvcArgsSettings', |
| 259 | array( |
| 260 | 'resetToDefaults' => __('Are you sure you want to reset these settings to defaults?', 'post-views-counter') |
| 261 | ) |
| 262 | ); |
| 263 | |
| 264 | wp_register_style( |
| 265 | 'post-views-counter-admin', |
| 266 | POST_VIEWS_COUNTER_URL.'/css/admin.css' |
| 267 | ); |
| 268 | |
| 269 | wp_register_style( |
| 270 | 'post-views-counter-chosen', |
| 271 | POST_VIEWS_COUNTER_URL.'/assets/chosen/chosen.min.css' |
| 272 | ); |
| 273 | |
| 274 | wp_enqueue_style('post-views-counter-chosen'); |
| 275 | wp_enqueue_style('post-views-counter-admin'); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * Adds link to settings page |
| 282 | */ |
| 283 | public function plugin_settings_link($links, $file) |
| 284 | { |
| 285 | if(!is_admin() || !current_user_can('manage_options')) |
| 286 | return $links; |
| 287 | |
| 288 | static $plugin; |
| 289 | |
| 290 | $plugin = plugin_basename(__FILE__); |
| 291 | |
| 292 | if($file == $plugin) |
| 293 | { |
| 294 | $settings_link = sprintf('<a href="%s">%s</a>', admin_url('options-general.php').'?page=post-views-counter', __('Settings', 'post-views-counter')); |
| 295 | |
| 296 | array_unshift($links, $settings_link); |
| 297 | } |
| 298 | |
| 299 | return $links; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | function Post_Views_Counter() |
| 305 | { |
| 306 | static $instance; |
| 307 | |
| 308 | // first call to instance() initializes the plugin |
| 309 | if($instance === null || !($instance instanceof Post_Views_Counter)) |
| 310 | $instance = Post_Views_Counter::instance(); |
| 311 | |
| 312 | return $instance; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | Post_Views_Counter(); |
| 317 | ?> |