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