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