PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.0.6
Post Views Counter v1.0.6
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / post-views-counter.php
post-views-counter Last commit date
assets 11 years ago css 11 years ago images 11 years ago includes 11 years ago js 11 years ago languages 11 years ago index.php 11 years ago post-views-counter.php 11 years ago readme.txt 11 years ago
post-views-counter.php
333 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.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-2015, 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')) 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 * Post Views Counter class
42 *
43 * @class Post_Views_Counter
44 * @version 1.0.6
45 */
46 class Post_Views_Counter {
47 private static $_instance;
48 private $instances;
49 private $options;
50 private $defaults = array(
51 'general' => array(
52 'post_types_count' => array('post'),
53 'counter_mode' => 'php',
54 'post_views_column' => true,
55 'time_between_counts' => array(
56 'number' => 24,
57 'type' => 'hours'
58 ),
59 'reset_counts' => array(
60 'number' => 30,
61 'type' => 'days'
62 ),
63 'flush_interval' => array(
64 'number' => 0,
65 'type' => 'minutes'
66 ),
67 'exclude' => array(
68 'groups' => array(),
69 'roles' => array()
70 ),
71 'exclude_ips' => array(),
72 'deactivation_delete' => false,
73 'cron_run' => true,
74 'cron_update' => true
75 ),
76 'display' => array(
77 'label' => 'Post Views:',
78 'post_types_display' => array('post'),
79 'restrict_display' => array(
80 'groups' => array(),
81 'roles' => array()
82 ),
83 'position' => 'after',
84 'display_style' => array(
85 'icon' => true,
86 'text' => true
87 ),
88 'link_to_post' => true,
89 'icon_class' => 'dashicons-visibility'
90 ),
91 'version' => '1.0.6'
92 );
93
94
95 public static function instance() {
96 if(self::$_instance === null)
97 self::$_instance = new self();
98
99 return self::$_instance;
100 }
101
102
103 private function __clone() {}
104 private function __wakeup() {}
105
106
107 private function __construct() {
108 register_activation_hook(__FILE__, array(&$this, 'activation'));
109 register_deactivation_hook(__FILE__, array(&$this, 'deactivation'));
110
111 // settings
112 $this->options = array(
113 'general' => array_merge($this->defaults['general'], get_option('post_views_counter_settings_general', $this->defaults['general'])),
114 'display' => array_merge($this->defaults['display'], get_option('post_views_counter_settings_display', $this->defaults['display']))
115 );
116
117 // actions
118 add_action('plugins_loaded', array(&$this, 'load_textdomain'));
119 add_action('admin_enqueue_scripts', array(&$this, 'admin_scripts_styles'));
120 add_action('wp_loaded', array(&$this, 'load_pluggable_functions'), 10);
121
122 // filters
123 add_filter('plugin_action_links', array(&$this, 'plugin_settings_link'), 10, 2);
124 }
125
126
127 /**
128 * Execution of plugin activation function
129 */
130 public function activation() {
131 global $wpdb, $charset_collate;
132
133 // required for dbdelta
134 require_once(ABSPATH.'wp-admin/includes/upgrade.php');
135
136 // create post views table
137 dbDelta('
138 CREATE TABLE IF NOT EXISTS '.$wpdb->prefix.'post_views (
139 id bigint unsigned NOT NULL,
140 type tinyint(1) unsigned NOT NULL,
141 period varchar(8) NOT NULL,
142 count bigint unsigned NOT NULL,
143 PRIMARY KEY (type, period, id),
144 UNIQUE INDEX id_period (id, period) USING BTREE,
145 INDEX type_period_count (type, period, count) USING BTREE
146 ) '.$charset_collate.';'
147 );
148
149 // add default options
150 add_option('post_views_counter_settings_general', $this->defaults['general'], '', 'no');
151 add_option('post_views_counter_settings_display', $this->defaults['display'], '', 'no');
152 add_option('post_views_counter_version', $this->defaults['version'], '', 'no');
153
154 // schedule cache flush
155 $this->schedule_cache_flush();
156 }
157
158
159 /**
160 * Execution of plugin deactivation function
161 */
162 public function deactivation() {
163 // delete default options
164 if ($this->options['general']['deactivation_delete']) {
165 delete_option('post_views_counter_settings_general');
166 delete_option('post_views_counter_settings_display');
167 }
168
169 // remove schedule
170 wp_clear_scheduled_hook('pvc_reset_counts');
171 remove_action('pvc_reset_counts', array(Post_Views_Counter()->get_instance('cron'), 'reset_counts'));
172
173 $this->remove_cache_flush();
174 }
175
176 /**
177 * Schedule cache flushing if it's not already scheduled
178 */
179 public function schedule_cache_flush($forced = true) {
180 if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) {
181 wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' );
182 }
183 }
184
185 /**
186 * Remove scheduled cache flush and the corresponding action
187 */
188 public function remove_cache_flush() {
189 wp_clear_scheduled_hook( 'pvc_flush_cached_counts' );
190 remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->get_instance('cron'), 'flush_cached_counts' ) );
191 }
192
193
194 /**
195 * Load text domain
196 */
197 public function load_textdomain() {
198 load_plugin_textdomain('post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH.'languages/');
199 }
200
201
202 /**
203 * Load pluggable template functions
204 */
205 public function load_pluggable_functions() {
206 include_once(POST_VIEWS_COUNTER_PATH.'includes/functions.php');
207 }
208
209
210 /**
211 * Set instance of class
212 */
213 public function add_instance($name, $instance) {
214 $this->instances[$name] = $instance;
215 }
216
217
218 /**
219 * Get instance of a class
220 */
221 public function get_instance($name) {
222 if(in_array($name, array('counter', 'settings'), true))
223 return $this->instances[$name];
224 }
225
226
227 /**
228 * Get allowed attribute
229 */
230 public function get_attribute($attribute) {
231 if (in_array($attribute, array('options', 'defaults'), true)) {
232 switch(func_num_args()) {
233 case 1:
234 return $this->{$attribute};
235
236 case 2:
237 return $this->{$attribute}[func_get_arg(1)];
238
239 case 3:
240 return $this->{$attribute}[func_get_arg(1)][func_get_arg(2)];
241
242 case 4:
243 return $this->{$attribute}[func_get_arg(1)][func_get_arg(2)][func_get_arg(3)];
244 }
245 }
246 else
247 return false;
248 }
249
250
251 /**
252 * Enqueue admin scripts and styles
253 */
254 public function admin_scripts_styles($page) {
255 // load on PVC settings page only
256 if ($page === 'settings_page_post-views-counter') {
257 wp_register_script(
258 'post-views-counter-admin-chosen',
259 POST_VIEWS_COUNTER_URL.'/assets/chosen/chosen.jquery.min.js',
260 array('jquery'),
261 $this->defaults['version']
262 );
263
264 wp_register_script(
265 'post-views-counter-admin',
266 POST_VIEWS_COUNTER_URL.'/js/admin.js',
267 array('jquery', 'post-views-counter-admin-chosen'),
268 $this->defaults['version']
269 );
270
271 wp_enqueue_script('post-views-counter-admin-chosen');
272 wp_enqueue_script('post-views-counter-admin');
273
274 wp_localize_script(
275 'post-views-counter-admin',
276 'pvcArgsSettings',
277 array(
278 'resetToDefaults' => __('Are you sure you want to reset these settings to defaults?', 'post-views-counter')
279 )
280 );
281
282 wp_register_style(
283 'post-views-counter-admin',
284 POST_VIEWS_COUNTER_URL.'/css/admin.css'
285 );
286
287 wp_register_style(
288 'post-views-counter-chosen',
289 POST_VIEWS_COUNTER_URL.'/assets/chosen/chosen.min.css'
290 );
291
292 wp_enqueue_style('post-views-counter-chosen');
293 wp_enqueue_style('post-views-counter-admin');
294 }
295 }
296
297
298 /**
299 * Add link to settings page
300 */
301 public function plugin_settings_link($links, $file) {
302 if (!is_admin() || !current_user_can('manage_options'))
303 return $links;
304
305 static $plugin;
306
307 $plugin = plugin_basename(__FILE__);
308
309 if ($file == $plugin) {
310 $settings_link = sprintf('<a href="%s">%s</a>', admin_url('options-general.php').'?page=post-views-counter', __('Settings', 'post-views-counter'));
311
312 array_unshift($links, $settings_link);
313 }
314
315 return $links;
316 }
317 }
318
319 /**
320 * Initialise Post Views Counter
321 */
322 function Post_Views_Counter() {
323 static $instance;
324
325 // first call to instance() initializes the plugin
326 if ($instance === null || !($instance instanceof Post_Views_Counter))
327 $instance = Post_Views_Counter::instance();
328
329 return $instance;
330 }
331
332 Post_Views_Counter();
333 ?>