PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.12
Post Views Counter v1.3.12
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 3 years ago css 3 years ago includes 3 years ago js 3 years ago languages 3 years ago index.php 6 years ago post-views-counter.php 3 years ago readme.txt 3 years ago
post-views-counter.php
733 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.3.12
6 Author: Digital Factory
7 Author URI: http://www.dfactory.co/
8 Plugin URI: http://www.dfactory.co/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-2022, 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.3.12
35 */
36 final class Post_Views_Counter {
37
38 private static $instance;
39 public $options;
40 public $defaults = [
41 'general' => [
42 'post_types_count' => [ 'post' ],
43 'counter_mode' => 'php',
44 'post_views_column' => true,
45 'restrict_edit_views' => false,
46 'time_between_counts' => [
47 'number' => 24,
48 'type' => 'hours'
49 ],
50 'reset_counts' => [
51 'number' => 30,
52 'type' => 'days'
53 ],
54 'flush_interval' => [
55 'number' => 0,
56 'type' => 'minutes'
57 ],
58 'exclude' => [
59 'groups' => [],
60 'roles' => []
61 ],
62 'exclude_ips' => [],
63 'strict_counts' => false,
64 'deactivation_delete' => false,
65 'cron_run' => true,
66 'cron_update' => true,
67 'update_version' => 1,
68 'update_notice' => true,
69 'update_delay_date' => 0
70 ],
71 'display' => [
72 'label' => 'Post Views:',
73 'post_types_display' => [ 'post' ],
74 'page_types_display' => [ 'singular' ],
75 'restrict_display' => [
76 'groups' => [],
77 'roles' => []
78 ],
79 'position' => 'after',
80 'display_style' => [
81 'icon' => true,
82 'text' => true
83 ],
84 'icon_class' => 'dashicons-chart-bar',
85 'toolbar_statistics' => true
86 ],
87 'version' => '1.3.12'
88 ];
89
90 /**
91 * Disable object cloning.
92 *
93 * @return void
94 */
95 public function __clone() {}
96
97 /**
98 * Disable unserializing of the class.
99 *
100 * @return void
101 */
102 public function __wakeup() {}
103
104 /**
105 * Main plugin instance, insures that only one instance of the plugin exists in memory at one time.
106 *
107 * @return object
108 */
109 public static function instance() {
110 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Post_Views_Counter ) ) {
111 self::$instance = new Post_Views_Counter;
112
113 // short init?
114 if ( defined( 'SHORTINIT' ) && SHORTINIT ) {
115 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-counter.php' );
116 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-crawler-detect.php' );
117 include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' );
118
119 self::$instance->counter = new Post_Views_Counter_Counter();
120 self::$instance->crawler_detect = new Post_Views_Counter_Crawler_Detect();
121 // regular setup
122 } else {
123 add_action( 'init', [ self::$instance, 'load_textdomain' ] );
124
125 self::$instance->includes();
126
127 // create settings API
128 self::$instance->settings_api = new Post_Views_Counter_Settings_API(
129 [
130 'object' => self::$instance,
131 'prefix' => 'post_views_counter',
132 'slug' => 'post-views-counter',
133 'domain' => 'post-views-counter',
134 'plugin' => 'Post Views Counter',
135 'plugin_url' => 'POST_VIEWS_COUNTER_URL'
136 ]
137 );
138
139 self::$instance->functions = new Post_Views_Counter_Functions();
140 self::$instance->update = new Post_Views_Counter_Update();
141 self::$instance->settings = new Post_Views_Counter_Settings();
142 self::$instance->admin = new Post_Views_Counter_Admin();
143 self::$instance->query = new Post_Views_Counter_Query();
144 self::$instance->cron = new Post_Views_Counter_Cron();
145 self::$instance->counter = new Post_Views_Counter_Counter();
146 self::$instance->columns = new Post_Views_Counter_Columns();
147 self::$instance->crawler_detect = new Post_Views_Counter_Crawler_Detect();
148 self::$instance->frontend = new Post_Views_Counter_Frontend();
149 self::$instance->dashboard = new Post_Views_Counter_Dashboard();
150 self::$instance->widgets = new Post_Views_Counter_Widgets();
151 }
152 }
153
154 return self::$instance;
155 }
156
157 /**
158 * Setup plugin constants.
159 *
160 * @return void
161 */
162 private function define_constants() {
163 // fix plugin_basename empty $wp_plugin_paths var
164 if ( ! ( defined( 'SHORTINIT' ) && SHORTINIT ) ) {
165 define( 'POST_VIEWS_COUNTER_URL', plugins_url( '', __FILE__ ) );
166 define( 'POST_VIEWS_COUNTER_BASENAME', plugin_basename( __FILE__ ) );
167 define( 'POST_VIEWS_COUNTER_REL_PATH', dirname( POST_VIEWS_COUNTER_BASENAME ) . '/' );
168 }
169
170 define( 'POST_VIEWS_COUNTER_PATH', plugin_dir_path( __FILE__ ) );
171 }
172
173 /**
174 * Include required files.
175 *
176 * @return void
177 */
178 private function includes() {
179 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-functions.php' );
180 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-update.php' );
181 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-settings-api.php' );
182 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-settings.php' );
183 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-admin.php' );
184 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-columns.php' );
185 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-query.php' );
186 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-cron.php' );
187 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-counter.php' );
188 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-crawler-detect.php' );
189 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-frontend.php' );
190 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-dashboard.php' );
191 include_once( POST_VIEWS_COUNTER_PATH . 'includes/class-widgets.php' );
192 }
193
194 /**
195 * Class constructor.
196 *
197 * @return void
198 */
199 public function __construct() {
200 // define plugin constants
201 $this->define_constants();
202
203 // short init?
204 if ( defined( 'SHORTINIT' ) && SHORTINIT ) {
205 $this->options = [
206 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ),
207 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) )
208 ];
209
210 return;
211 }
212
213 // activation hooks
214 register_activation_hook( __FILE__, [ $this, 'activate_plugin' ] );
215 register_deactivation_hook( __FILE__, [ $this, 'deactivate_plugin' ] );
216
217 // settings
218 $this->options = [
219 'general' => array_merge( $this->defaults['general'], get_option( 'post_views_counter_settings_general', $this->defaults['general'] ) ),
220 'display' => array_merge( $this->defaults['display'], get_option( 'post_views_counter_settings_display', $this->defaults['display'] ) )
221 ];
222
223 // actions
224 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
225 add_action( 'wp_loaded', [ $this, 'load_pluggable_functions' ] );
226 add_action( 'admin_init', [ $this, 'update_notice' ] );
227 add_action( 'wp_initialize_site', [ $this, 'initialize_new_network_site' ] );
228 add_action( 'wp_ajax_pvc_dismiss_notice', [ $this, 'dismiss_notice' ] );
229
230 // filters
231 add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
232 add_filter( 'plugin_action_links', [ $this, 'plugin_action_links' ], 10, 2 );
233 }
234
235 /**
236 * Update notice.
237 *
238 * @return void
239 */
240 public function update_notice() {
241 if ( ! current_user_can( 'install_plugins' ) )
242 return;
243
244 $current_update = 2;
245
246 // get current time
247 $current_time = time();
248
249 if ( $this->options['general']['update_version'] < $current_update ) {
250 // check version, if update version is lower than plugin version, set update notice to true
251 $this->options['general'] = array_merge(
252 $this->options['general'],
253 [
254 'update_version' => $current_update,
255 'update_notice' => true
256 ]
257 );
258
259 update_option( 'post_views_counter_settings_general', $this->options['general'] );
260
261 // set activation date
262 $activation_date = get_option( 'post_views_counter_activation_date' );
263
264 if ( $activation_date === false )
265 update_option( 'post_views_counter_activation_date', $current_time );
266 }
267
268 // display current version notice
269 if ( $this->options['general']['update_notice'] === true ) {
270 // include notice js, only if needed
271 add_action( 'admin_print_scripts', [ $this, 'admin_inline_js' ], 999 );
272
273 // get activation date
274 $activation_date = get_option( 'post_views_counter_activation_date' );
275
276 if ( (int) $this->options['general']['update_delay_date'] === 0 ) {
277 if ( $activation_date + 1209600 > $current_time )
278 $this->options['general']['update_delay_date'] = $activation_date + 1209600;
279 else
280 $this->options['general']['update_delay_date'] = $current_time;
281
282 update_option( 'post_views_counter_settings_general', $this->options['general'] );
283 }
284
285 if ( ( ! empty( $this->options['general']['update_delay_date'] ) ? (int) $this->options['general']['update_delay_date'] : $current_time ) <= $current_time )
286 $this->add_notice( sprintf( __( "Hey, you've been using <strong>Post Views Counter</strong> for more than %s.", 'post-views-counter' ), human_time_diff( $activation_date, $current_time ) ) . '<br />' . __( 'Could you please do me a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation.', 'post-views-counter' ) . '<br /><br />' . __( 'Your help is much appreciated. Thank you very much', 'post-views-counter' ) . ' ~ <strong>Bartosz Arendt</strong>, ' . sprintf( __( 'founder of <a href="%s" target="_blank">Digital Factory</a> plugins.', 'post-views-counter' ), 'https://dfactory.co/' ) . '<br /><br />' . sprintf( __( '<a href="%s" class="pvc-dismissible-notice" target="_blank" rel="noopener">Ok, you deserve it</a><br /><a href="javascript:void(0);" class="pvc-dismissible-notice pvc-delay-notice" rel="noopener">Nope, maybe later</a><br /><a href="javascript:void(0);" class="pvc-dismissible-notice" rel="noopener">I already did</a>', 'post-views-counter' ), 'https://wordpress.org/support/plugin/post-views-counter/reviews/?filter=5#new-post' ), 'notice notice-info is-dismissible pvc-notice' );
287 }
288 }
289
290 /**
291 * Add admin notices.
292 *
293 * @param string $html
294 * @param string $status
295 * @param bool $paragraph
296 * @return void
297 */
298 public function add_notice( $html = '', $status = 'error', $paragraph = true ) {
299 $this->notices[] = [
300 'html' => $html,
301 'status' => $status,
302 'paragraph' => $paragraph
303 ];
304
305 add_action( 'admin_notices', [ $this, 'display_notice' ] );
306 }
307
308 /**
309 * Print admin notices.
310 *
311 * @return void
312 */
313 public function display_notice() {
314 foreach( $this->notices as $notice ) {
315 echo '
316 <div class="' . $notice['status'] . '">
317 ' . ( $notice['paragraph'] ? '<p>' : '' ) . '
318 ' . $notice['html'] . '
319 ' . ( $notice['paragraph'] ? '</p>' : '' ) . '
320 </div>';
321 }
322 }
323
324 /**
325 * Print admin scripts.
326 *
327 * @return void
328 */
329 public function admin_inline_js() {
330 if ( ! current_user_can( 'install_plugins' ) )
331 return;
332 ?>
333 <script type="text/javascript">
334 ( function ( $ ) {
335 $( document ).ready( function () {
336 // save dismiss state
337 $( '.pvc-notice.is-dismissible' ).on( 'click', '.notice-dismiss, .pvc-dismissible-notice', function ( e ) {
338 var notice_action = 'hide';
339
340 if ( $( e.currentTarget ).hasClass( 'pvc-delay-notice' ) ) {
341 notice_action = 'delay'
342 }
343
344 $.post( ajaxurl, {
345 action: 'pvc_dismiss_notice',
346 notice_action: notice_action,
347 url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
348 nonce: '<?php echo wp_create_nonce( 'pvc_dismiss_notice' ); ?>'
349 } );
350
351 $( e.delegateTarget ).slideUp( 'fast' );
352 } );
353 } );
354 } )( jQuery );
355 </script>
356 <?php
357 }
358
359 /**
360 * Dismiss notice.
361 *
362 * @return void
363 */
364 public function dismiss_notice() {
365 if ( ! current_user_can( 'install_plugins' ) )
366 return;
367
368 if ( wp_verify_nonce( esc_attr( $_REQUEST['nonce'] ), 'pvc_dismiss_notice' ) ) {
369 $notice_action = empty( $_REQUEST['notice_action'] ) || $_REQUEST['notice_action'] === 'hide' ? 'hide' : sanitize_text_field( $_REQUEST['notice_action'] );
370
371 switch ( $notice_action ) {
372 // delay notice
373 case 'delay':
374 // set delay period to 1 week from now
375 $this->options['general'] = array_merge(
376 $this->options['general'],
377 [
378 'update_delay_date' => time() + 1209600
379 ]
380 );
381 update_option( 'post_views_counter_settings_general', $this->options['general'] );
382 break;
383
384 // hide notice
385 default:
386 $this->options['general'] = array_merge(
387 $this->options['general'],
388 [
389 'update_notice' => false
390 ]
391 );
392 $this->options['general'] = array_merge(
393 $this->options['general'],
394 [
395 'update_delay_date' => 0
396 ]
397 );
398
399 update_option( 'post_views_counter_settings_general', $this->options['general'] );
400 }
401 }
402
403 exit;
404 }
405
406 /**
407 * Plugin activation.
408 *
409 * @global object $wpdb
410 *
411 * @param bool $network
412 * @return void
413 */
414 public function activate_plugin( $network ) {
415 if ( is_multisite() && $network ) {
416 global $wpdb;
417
418 $activated_blogs = [];
419
420 // get available sites
421 $blogs_ids = $wpdb->get_col( 'SELECT blog_id FROM ' . $wpdb->blogs );
422
423 foreach ( $blogs_ids as $blog_id ) {
424 switch_to_blog( $blog_id );
425
426 $this->activate_single();
427
428 $activated_blogs[] = (int) $blog_id;
429
430 restore_current_blog();
431 }
432
433 update_site_option( 'post_views_counter_activated_blogs', $activated_blogs );
434 } else
435 $this->activate_single();
436 }
437
438 /**
439 * Single site activation.
440 *
441 * @global object $wpdb
442 * @global string $charset_collate
443 *
444 * @return void
445 */
446 public function activate_single() {
447 global $wpdb, $charset_collate;
448
449 // required for dbdelta
450 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
451
452 // create post views table
453 dbDelta( '
454 CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'post_views (
455 `id` bigint unsigned NOT NULL,
456 `type` tinyint(1) unsigned NOT NULL,
457 `period` varchar(8) NOT NULL,
458 `count` bigint unsigned NOT NULL,
459 PRIMARY KEY (type, period, id),
460 UNIQUE INDEX id_type_period_count (id, type, period, count) USING BTREE,
461 INDEX type_period_count (type, period, count) USING BTREE
462 ) ' . $charset_collate . ';'
463 );
464
465 // add default options
466 add_option( 'post_views_counter_settings_general', $this->defaults['general'], null, false );
467 add_option( 'post_views_counter_settings_display', $this->defaults['display'], null, false );
468 add_option( 'post_views_counter_version', $this->defaults['version'], null, false );
469
470 // schedule cache flush
471 $this->schedule_cache_flush();
472 }
473
474 /**
475 * Plugin deactivation.
476 *
477 * @global object $wpdb
478 *
479 * @param bool $network
480 * @return void
481 */
482 public function deactivate_plugin( $network ) {
483 if ( is_multisite() && $network ) {
484 global $wpdb;
485
486 // get available sites
487 $blogs_ids = $wpdb->get_col( 'SELECT blog_id FROM ' . $wpdb->blogs );
488
489 // get activated sites
490 $activated_blogs = get_site_option( 'post_views_counter_activated_blogs', [] );
491
492 foreach ( $blogs_ids as $blog_id ) {
493 switch_to_blog( $blog_id );
494
495 $this->deactivate_single( true );
496
497 if ( in_array( (int) $blog_id, $activated_blogs, true ) )
498 unset( $activated_blogs[array_search( $blog_id, $activated_blogs )] );
499
500 restore_current_blog();
501 }
502
503 update_site_option( 'post_views_counter_activated_blogs', $activated_blogs );
504 } else
505 $this->deactivate_single();
506 }
507
508 /**
509 * Single site deactivation.
510 *
511 * @global object $wpdb
512 *
513 * @param bool $network
514 * @return void
515 */
516 public function deactivate_single( $network = false ) {
517 if ( $network ) {
518 $options = get_option( 'post_views_counter_settings_general' );
519 $check = $options['deactivation_delete'];
520 } else
521 $check = $this->options['general']['deactivation_delete'];
522
523 // delete default options
524 if ( $check ) {
525 // delete options
526 delete_option( 'post_views_counter_settings_general' );
527 delete_option( 'post_views_counter_settings_display' );
528 delete_option( 'post_views_counter_activation_date' );
529 delete_option( 'post_views_counter_version' );
530
531 // delete transients
532 delete_transient( 'post_views_counter_ip_cache' );
533
534 global $wpdb;
535
536 // delete table from database
537 $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post_views' );
538 }
539
540 // remove schedule
541 wp_clear_scheduled_hook( 'pvc_reset_counts' );
542
543 remove_action( 'pvc_reset_counts', [ Post_Views_Counter()->cron, 'reset_counts' ] );
544
545 $this->remove_cache_flush();
546 }
547
548 /**
549 * Initialize new network site.
550 *
551 * @param object $site
552 * @return void
553 */
554 public function initialize_new_network_site( $site ) {
555 if ( is_multisite() ) {
556 // get active sites
557 $active_blogs = get_site_option( 'post_views_counter_activated_blogs', [] );
558
559 switch_to_blog( $site->blog_id );
560
561 // activate new site
562 $this->activate_single();
563
564 $active_blogs[] = (int) $site->blog_id;
565
566 restore_current_blog();
567
568 update_site_option( 'post_views_counter_activated_blogs', $active_blogs );
569 }
570 }
571
572 /**
573 * Schedule cache flushing if it's not already scheduled.
574 *
575 * @param bool $forced
576 * @return void
577 */
578 public function schedule_cache_flush( $forced = true ) {
579 if ( $forced || ! wp_next_scheduled( 'pvc_flush_cached_counts' ) )
580 wp_schedule_event( time(), 'post_views_counter_flush_interval', 'pvc_flush_cached_counts' );
581 }
582
583 /**
584 * Remove scheduled cache flush and the corresponding action.
585 *
586 * @return void
587 */
588 public function remove_cache_flush() {
589 wp_clear_scheduled_hook( 'pvc_flush_cached_counts' );
590 remove_action( 'pvc_flush_cached_counts', [ Post_Views_Counter()->cron, 'flush_cached_counts' ] );
591 }
592
593 /**
594 * Load text domain.
595 *
596 * @return void
597 */
598 public function load_textdomain() {
599 load_plugin_textdomain( 'post-views-counter', false, POST_VIEWS_COUNTER_REL_PATH . 'languages/' );
600 }
601
602 /**
603 * Load pluggable template functions.
604 *
605 * @return void
606 */
607 public function load_pluggable_functions() {
608 include_once( POST_VIEWS_COUNTER_PATH . 'includes/functions.php' );
609 }
610
611 /**
612 * Enqueue admin scripts and styles.
613 *
614 * @global string $post_type
615 *
616 * @param string $page
617 * @return void
618 */
619 public function admin_enqueue_scripts( $page ) {
620 // register styles
621 wp_register_style( 'pvc-admin', POST_VIEWS_COUNTER_URL . '/css/admin.min.css' );
622
623 // register scripts
624 wp_register_script( 'pvc-admin-settings', POST_VIEWS_COUNTER_URL . '/js/admin-settings.js', [ 'jquery' ], $this->defaults['version'] );
625 wp_register_script( 'pvc-admin-post', POST_VIEWS_COUNTER_URL . '/js/admin-post.js', [ 'jquery' ], $this->defaults['version'] );
626 wp_register_script( 'pvc-admin-quick-edit', POST_VIEWS_COUNTER_URL . '/js/admin-quick-edit.js', [ 'jquery', 'inline-edit-post' ], $this->defaults['version'] );
627
628 // load on pvc settings page
629 if ( $page === 'settings_page_post-views-counter' ) {
630 wp_enqueue_script( 'pvc-admin-settings' );
631
632 wp_localize_script(
633 'pvc-admin-settings', 'pvcArgsSettings', [
634 'resetToDefaults' => __( 'Are you sure you want to reset these settings to defaults?', 'post-views-counter' ),
635 'resetViews' => __( 'Are you sure you want to delete all existing data?', 'post-views-counter' )
636 ]
637 );
638
639 wp_enqueue_style( 'pvc-admin' );
640 // load on single post page
641 } elseif ( $page === 'post.php' || $page === 'post-new.php' ) {
642 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
643
644 global $post_type;
645
646 if ( ! in_array( $post_type, (array) $post_types ) )
647 return;
648
649 wp_enqueue_style( 'pvc-admin' );
650 wp_enqueue_script( 'pvc-admin-post' );
651 // edit post
652 } elseif ( $page === 'edit.php' ) {
653 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
654
655 global $post_type;
656
657 if ( ! in_array( $post_type, (array) $post_types ) )
658 return;
659
660 wp_enqueue_style( 'pvc-admin' );
661
662 // woocommerce
663 if ( get_post_type() !== 'product' )
664 wp_enqueue_script( 'pvc-admin-quick-edit' );
665 // widgets
666 } elseif ( $page === 'widgets.php' )
667 wp_enqueue_script( 'pvc-admin-widgets', POST_VIEWS_COUNTER_URL . '/js/admin-widgets.js', [ 'jquery' ], $this->defaults['version'] );
668 // media
669 elseif ( $page === 'upload.php' )
670 wp_enqueue_style( 'pvc-admin' );
671 }
672
673 /**
674 * Add links to plugin support forum.
675 *
676 * @param array $links
677 * @param string $file
678 * @return array
679 */
680 public function plugin_row_meta( $links, $file ) {
681 if ( ! current_user_can( 'install_plugins' ) )
682 return $links;
683
684 if ( $file === POST_VIEWS_COUNTER_BASENAME ) {
685 return array_merge(
686 $links,
687 [
688 sprintf( '<a href="http://www.dfactory.co/support/forum/post-views-counter/" target="_blank">%s</a>', __( 'Support', 'post-views-counter' ) )
689 ]
690 );
691 }
692
693 return $links;
694 }
695
696 /**
697 * Add link to settings page.
698 *
699 * @param array $links
700 * @param string $file
701 * @return array
702 */
703 public function plugin_action_links( $links, $file ) {
704 if ( ! current_user_can( 'manage_options' ) )
705 return $links;
706
707 if ( $file === POST_VIEWS_COUNTER_BASENAME ) {
708 $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=post-views-counter' ), __( 'Settings', 'post-views-counter' ) );
709
710 array_unshift( $links, $settings_link );
711 }
712
713 return $links;
714 }
715 }
716 }
717
718 /**
719 * Initialise Post Views Counter.
720 *
721 * @return object
722 */
723 function Post_Views_Counter() {
724 static $instance;
725
726 // first call to instance() initializes the plugin
727 if ( $instance === null || ! ( $instance instanceof Post_Views_Counter ) )
728 $instance = Post_Views_Counter::instance();
729
730 return $instance;
731 }
732
733 Post_Views_Counter();