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