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