PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.2
Post Views Counter v1.1.2
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
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
360 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.1.2
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' ) )
26 exit;
27
28 define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) );
29 define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) );
30 define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( plugin_basename( __FILE__ ) ) . '/' );
31
32 include_once( POST_VIEWS_COUNTER_PATH . 'includes/update.php' );
33 include_once( POST_VIEWS_COUNTER_PATH . 'includes/settings.php' );
34 include_once( POST_VIEWS_COUNTER_PATH . 'includes/query.php' );
35 include_once( POST_VIEWS_COUNTER_PATH . 'includes/cron.php' );
36 include_once( POST_VIEWS_COUNTER_PATH . 'includes/counter.php' );
37 include_once( POST_VIEWS_COUNTER_PATH . 'includes/columns.php' );
38 include_once( POST_VIEWS_COUNTER_PATH . 'includes/frontend.php' );
39 include_once( POST_VIEWS_COUNTER_PATH . 'includes/widgets.php' );
40
41 /**
42 * Post Views Counter class
43 *
44 * @class Post_Views_Counter
45 * @version 1.1.2
46 */
47 class Post_Views_Counter {
48
49 private static $_instance;
50 private $instances;
51 private $options;
52 private $defaults = array(
53 'general' => array(
54 'post_types_count' => array( 'post' ),
55 'counter_mode' => 'php',
56 'post_views_column' => true,
57 'time_between_counts' => array(
58 'number' => 24,
59 'type' => 'hours'
60 ),
61 'reset_counts' => array(
62 'number' => 30,
63 'type' => 'days'
64 ),
65 'flush_interval' => array(
66 'number' => 0,
67 'type' => 'minutes'
68 ),
69 'exclude' => array(
70 'groups' => array(),
71 'roles' => array()
72 ),
73 'exclude_ips' => array(),
74 'restrict_edit_views' => false,
75 'deactivation_delete' => false,
76 'cron_run' => true,
77 'cron_update' => true
78 ),
79 'display' => array(
80 'label' => 'Post Views:',
81 'post_types_display' => array( 'post' ),
82 'restrict_display' => array(
83 'groups' => array(),
84 'roles' => array()
85 ),
86 'position' => 'after',
87 'display_style' => array(
88 'icon' => true,
89 'text' => true
90 ),
91 'link_to_post' => true,
92 'icon_class' => 'dashicons-chart-bar'
93 ),
94 'version' => '1.1.2'
95 );
96
97 public static function instance() {
98 if ( self::$_instance === null )
99 self::$_instance = new self();
100
101 return self::$_instance;
102 }
103
104 private function __clone() {
105
106 }
107
108 private function __wakeup() {
109
110 }
111
112 private function __construct() {
113 register_activation_hook( __FILE__, array( &$this, 'activation' ) );
114 register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ) );
115
116 // settings
117 $this->options = array(
118 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ),
119 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) )
120 );
121
122 // actions
123 add_action( 'plugins_loaded', array( &$this, 'load_textdomain' ) );
124 add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts_styles' ) );
125 add_action( 'wp_loaded', array( &$this, 'load_pluggable_functions' ), 10 );
126
127 // filters
128 add_filter( 'plugin_row_meta', array( &$this, 'plugin_extend_links' ), 10, 2 );
129 add_filter( 'plugin_action_links', array( &$this, 'plugin_settings_link' ), 10, 2 );
130 }
131
132 /**
133 * Execution of plugin activation function
134 */
135 public function activation() {
136 global $wpdb, $charset_collate;
137
138 // required for dbdelta
139 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
140
141 // create post views table
142 dbDelta( '
143 CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views (
144 id bigint unsigned NOT NULL,
145 type tinyint(1) unsigned NOT NULL,
146 period varchar(8) NOT NULL,
147 count bigint unsigned NOT NULL,
148 PRIMARY KEY (type, period, id),
149 UNIQUE INDEX id_period (id, period) USING BTREE,
150 INDEX type_period_count (type, period, count) USING BTREE
151 ) ' . $charset_collate . ';'
152 );
153
154 // add default options
155 add_option( 'post_views_counter_settings_general', $this->defaults['general'], '', 'no' );
156 add_option( 'post_views_counter_settings_display', $this->defaults['display'], '', 'no' );
157 add_option( 'post_views_counter_version', $this->defaults['version'], '', 'no' );
158
159 // schedule cache flush
160 $this->schedule_cache_flush();
161 }
162
163 /**
164 * Execution of plugin deactivation function
165 */
166 public function deactivation() {
167 // delete default options
168 if ( $this->options['general']['deactivation_delete'] ) {
169 delete_option( 'post_views_counter_settings_general' );
170 delete_option( 'post_views_counter_settings_display' );
171 }
172
173 // remove schedule
174 wp_clear_scheduled_hook( 'pvc_reset_counts' );
175 remove_action( 'pvc_reset_counts', array( Post_Views_Counter()->get_instance( 'cron' ), 'reset_counts' ) );
176
177 $this->remove_cache_flush();
178 }
179
180 /**
181 * Schedule cache flushing if it's not already scheduled
182 */
183 public function schedule_cache_flush( $forced = true ) {
184 if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) ) {
185 wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' );
186 }
187 }
188
189 /**
190 * Remove scheduled cache flush and the corresponding action
191 */
192 public function remove_cache_flush() {
193 wp_clear_scheduled_hook( 'pvc_flush_cached_counts' );
194 remove_action( 'pvc_flush_cached_counts', array( Post_Views_Counter()->get_instance( 'cron' ), 'flush_cached_counts' ) );
195 }
196
197 /**
198 * Load text domain
199 */
200 public function load_textdomain() {
201 load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' );
202 }
203
204 /**
205 * Load pluggable template functions
206 */
207 public function load_pluggable_functions() {
208 include_once(POST_VIEWS_COUNTER_PATH . 'includes/functions.php');
209 }
210
211 /**
212 * Set instance of class
213 */
214 public function add_instance( $name, $instance ) {
215 $this->instances[$name] = $instance;
216 }
217
218 /**
219 * Get instance of a class
220 */
221 public function get_instance( $name ) {
222 if ( in_array( $name, array( 'counter', 'settings', 'cron' ), true ) )
223 return $this->instances[$name];
224 }
225
226 /**
227 * Get allowed attribute
228 */
229 public function get_attribute( $attribute ) {
230 if ( in_array( $attribute, array( 'options', 'defaults' ), true ) ) {
231 switch ( func_num_args() ) {
232 case 1:
233 return $this->{$attribute};
234
235 case 2:
236 return $this->{$attribute}[func_get_arg( 1 )];
237
238 case 3:
239 return $this->{$attribute}[func_get_arg( 1 )][func_get_arg( 2 )];
240
241 case 4:
242 return $this->{$attribute}[func_get_arg( 1 )][func_get_arg( 2 )][func_get_arg( 3 )];
243 }
244 } else
245 return false;
246 }
247
248 /**
249 * Enqueue admin scripts and styles
250 */
251 public function admin_scripts_styles( $page ) {
252 wp_register_style(
253 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.css'
254 );
255
256 wp_register_script(
257 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', array( 'jquery' ), $this->defaults['version']
258 );
259
260 wp_register_script(
261 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', array( 'jquery' ), $this->defaults['version']
262 );
263
264 wp_register_script(
265 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', array( 'jquery', 'inline-edit-post' ), $this->defaults['version']
266 );
267
268 // load on PVC settings page
269 if ( $page === 'settings_page_post-views-counter' ) {
270
271 wp_enqueue_script( 'pvc-admin-settings' );
272
273 wp_localize_script(
274 'pvc-admin-settings', 'pvcArgsSettings', array(
275 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' )
276 )
277 );
278
279 wp_enqueue_style( 'pvc-admin' );
280
281 // load on single post page
282 } elseif ( $page === 'post.php' || $page === 'post-new.php' ) {
283
284 $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
285
286 global $post_type;
287
288 if ( ! in_array( $post_type, (array) $post_types ) )
289 return;
290
291 wp_enqueue_style( 'pvc-admin' );
292 wp_enqueue_script( 'pvc-admin-post' );
293 } elseif ( $page === 'edit.php' ) {
294 $post_types = Post_Views_Counter()->get_attribute( 'options', 'general', 'post_types_count' );
295
296 global $post_type;
297
298 if ( ! in_array( $post_type, (array) $post_types ) )
299 return;
300
301 wp_enqueue_style( 'pvc-admin' );
302 wp_enqueue_script( 'pvc-admin-quick-edit' );
303 }
304 }
305
306 /**
307 * Add links to plugin support forum.
308 */
309 public function plugin_extend_links( $links, $file ) {
310
311 if ( ! current_user_can( 'install_plugins' ) )
312 return $links;
313
314 $plugin = plugin_basename( __FILE__ );
315
316 if ( $file == $plugin ) {
317 return array_merge(
318 $links, array( sprintf( '<a href="http://www.dfactory.eu/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) ) )
319 );
320 }
321
322 return $links;
323 }
324
325 /**
326 * Add link to settings page
327 */
328 public function plugin_settings_link( $links, $file ) {
329 if ( ! is_admin() || ! current_user_can( 'manage_options' ) )
330 return $links;
331
332 static $plugin;
333
334 $plugin = plugin_basename( __FILE__ );
335
336 if ( $file == $plugin ) {
337 $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php' ) . '?page=post-views-counter', __( 'Settings', 'post-views-counter' ) );
338
339 array_unshift( $links, $settings_link );
340 }
341
342 return $links;
343 }
344
345 }
346
347 /**
348 * Initialise Post Views Counter
349 */
350 function Post_Views_Counter() {
351 static $instance;
352
353 // first call to instance() initializes the plugin
354 if ( $instance === null || ! ($instance instanceof Post_Views_Counter) )
355 $instance = Post_Views_Counter::instance();
356
357 return $instance;
358 }
359
360 Post_Views_Counter();