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