PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.0
Post Views Counter v1.2.0
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 / includes / frontend.php
post-views-counter / includes Last commit date
columns.php 10 years ago counter.php 10 years ago cron.php 10 years ago dashboard.php 10 years ago frontend.php 10 years ago functions.php 10 years ago query.php 10 years ago settings.php 10 years ago update.php 10 years ago widgets.php 10 years ago
frontend.php
142 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Frontend class.
8 */
9 class Post_Views_Counter_Frontend {
10
11 public function __construct() {
12 // actions
13 add_action( 'after_setup_theme', array( $this, 'register_shortcode' ) );
14 add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts_styles' ) );
15
16 // filters
17 add_filter( 'the_content', array( $this, 'add_post_views_count' ) );
18 add_filter( 'the_excerpt', array( $this, 'remove_post_views_count' ) );
19 }
20
21 /**
22 * Register post-views shortcode function.
23 */
24 public function register_shortcode() {
25 add_shortcode( 'post-views', array( $this, 'post_views_shortcode' ) );
26 }
27
28 /**
29 * Post views shortcode function.
30 *
31 * @param array $args
32 * @return mixed
33 */
34 public function post_views_shortcode( $args ) {
35 $defaults = array(
36 'id' => get_the_ID()
37 );
38
39 $args = shortcode_atts( $defaults, $args );
40
41 return pvc_post_views( $args['id'], false );
42 }
43
44 /**
45 * Add post views counter to content.
46 *
47 * @param mixed $content
48 * @return mixed
49 */
50 public function add_post_views_count( $content ) {
51 $display = false;
52
53 if ( is_singular() && in_array( get_post_type(), Post_Views_Counter()->options['display']['post_types_display'], true ) )
54 $display = true;
55
56 // get groups to check it faster
57 $groups = Post_Views_Counter()->options['display']['restrict_display']['groups'];
58
59 // whether to display views
60 if ( is_user_logged_in() ) {
61 // exclude logged in users?
62 if ( in_array( 'users', $groups, true ) )
63 $display = false;
64 // exclude specific roles?
65 elseif ( in_array( 'roles', $groups, true ) && Post_Views_Counter()->counter->is_user_roles_excluded( Post_Views_Counter()->options['display']['restrict_display']['roles'] ) )
66 $display = false;
67 }
68 // exclude guests?
69 elseif ( in_array( 'guests', $groups, true ) )
70 $display = false;
71
72
73 if ( apply_filters( 'pvc_display_views_count', $display ) === true ) {
74 switch ( Post_Views_Counter()->options['display']['position'] ) {
75 case 'after':
76 $content = $content . '[post-views]';
77 break;
78
79 case 'before':
80 $content = '[post-views]' . $content;
81 break;
82
83 case 'manual':
84 default:
85 break;
86 }
87 }
88
89 return $content;
90 }
91
92 /**
93 * Remove post views shortcode from excerpt.
94 *
95 * @param mixed $excerpt
96 * @return mixed
97 */
98 public function remove_post_views_count( $excerpt ) {
99 remove_shortcode( 'post-views' );
100 $excerpt = preg_replace( '/\[post-views[^\]]*\]/', '', $excerpt );
101 return $excerpt;
102 }
103
104 /**
105 * Enqueue frontend scripts and styles.
106 */
107 public function frontend_scripts_styles() {
108 $post_types = Post_Views_Counter()->options['display']['post_types_display'];
109
110 // load dashicons
111 wp_enqueue_style( 'dashicons' );
112
113 // load style
114 wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css' );
115
116 if ( Post_Views_Counter()->options['general']['counter_mode'] === 'js' ) {
117 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
118
119 // whether to count this post type or not
120 if ( empty( $post_types ) || ! is_singular( $post_types ) )
121 return;
122
123 wp_register_script(
124 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', array( 'jquery' )
125 );
126
127 wp_enqueue_script( 'post-views-counter-frontend' );
128
129 wp_localize_script(
130 'post-views-counter-frontend',
131 'pvcArgsFrontend',
132 array(
133 'ajaxURL' => admin_url( 'admin-ajax.php' ),
134 'postID' => get_the_ID(),
135 'nonce' => wp_create_nonce( 'pvc-check-post' ),
136 'postType' => get_post_type()
137 )
138 );
139 }
140 }
141 }
142