PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.8
Post Views Counter v1.3.8
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 / class-frontend.php
post-views-counter / includes Last commit date
ajax.php 7 years ago class-admin.php 4 years ago class-columns.php 4 years ago class-counter.php 4 years ago class-crawler-detect.php 4 years ago class-cron.php 4 years ago class-dashboard.php 4 years ago class-frontend.php 4 years ago class-functions.php 4 years ago class-query.php 4 years ago class-settings-api.php 4 years ago class-settings.php 4 years ago class-update.php 4 years ago class-widgets.php 4 years ago columns.php 6 years ago counter.php 6 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 6 years ago frontend.php 6 years ago functions.php 4 years ago query.php 6 years ago settings.php 6 years ago update.php 6 years ago widgets.php 6 years ago
class-frontend.php
222 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 class Post_Views_Counter_Frontend {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'after_setup_theme', [ $this, 'register_shortcode' ] );
21 add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] );
22 add_action( 'wp', [ $this, 'run' ] );
23 }
24
25 /**
26 * Register post-views shortcode function.
27 *
28 * @return void
29 */
30 public function register_shortcode() {
31 add_shortcode( 'post-views', [ $this, 'post_views_shortcode' ] );
32 }
33
34 /**
35 * Post views shortcode function.
36 *
37 * @param array $args
38 * @return string
39 */
40 public function post_views_shortcode( $args ) {
41 $defaults = [
42 'id' => get_the_ID()
43 ];
44
45 $args = shortcode_atts( $defaults, $args );
46
47 return pvc_post_views( $args['id'], false );
48 }
49
50 /**
51 * Set up plugin hooks.
52 *
53 * @return void
54 */
55 public function run() {
56 if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
57 return;
58
59 $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] );
60
61 if ( ! empty( $filter ) && in_array( $filter, [ 'before', 'after' ] ) ) {
62 // post content
63 add_filter( 'the_content', [ $this, 'add_post_views_count' ] );
64
65 // bbpress support
66 add_action( 'bbp_template_' . $filter . '_single_topic', [ $this, 'display_bbpress_post_views' ] );
67 add_action( 'bbp_template_' . $filter . '_single_forum', [ $this, 'display_bbpress_post_views' ] );
68 } else {
69 // custom
70 if ( $filter !== 'manual' && is_string( $filter ) )
71 add_filter( $filter, [ $this, 'add_post_views_count' ] );
72 }
73 }
74
75 /**
76 * Add post views counter to forum/topic of bbPress.
77 *
78 * @return void
79 */
80 public function display_bbpress_post_views() {
81 $post_id = get_the_ID();
82
83 // check only for forums and topics
84 if ( bbp_is_forum( $post_id ) || bbp_is_topic( $post_id ) )
85 echo $this->add_post_views_count( '' );
86 }
87
88 /**
89 * Add post views counter to content.
90 *
91 * @param string $content
92 * @return string
93 */
94 public function add_post_views_count( $content = '' ) {
95 // get main instance
96 $pvc = Post_Views_Counter();
97
98 $display = false;
99
100 // page visibility check
101 if ( ! empty( $pvc->options['display']['page_types_display'] ) ) {
102 foreach ( $pvc->options['display']['page_types_display'] as $page ) {
103 switch ( $page ) {
104 case 'singular':
105 if ( is_singular( $pvc->options['display']['post_types_display'] ) )
106 $display = true;
107 break;
108
109 case 'archive':
110 if ( is_archive() )
111 $display = true;
112 break;
113
114 case 'search':
115 if ( is_search() )
116 $display = true;
117 break;
118
119 case 'home':
120 if ( is_home() || is_front_page() )
121 $display = true;
122 break;
123 }
124 }
125 }
126
127 // get groups to check it faster
128 $groups = $pvc->options['display']['restrict_display']['groups'];
129
130 // whether to display views
131 if ( is_user_logged_in() ) {
132 // exclude logged in users?
133 if ( in_array( 'users', $groups, true ) )
134 $display = false;
135 // exclude specific roles?
136 elseif ( in_array( 'roles', $groups, true ) && $pvc->counter->is_user_role_excluded( get_current_user_id(), $pvc->options['display']['restrict_display']['roles'] ) )
137 $display = false;
138 // exclude guests?
139 } elseif ( in_array( 'guests', $groups, true ) )
140 $display = false;
141
142 // we don't want to mess custom loops
143 if ( ! in_the_loop() && ! class_exists( 'bbPress' ) )
144 $display = false;
145
146 if ( (bool) apply_filters( 'pvc_display_views_count', $display ) === true ) {
147 $filter = apply_filters( 'pvc_shortcode_filter_hook', $pvc->options['display']['position'] );
148
149 switch ( $filter ) {
150 case 'after':
151 $content = $content . do_shortcode( '[post-views]' );
152 break;
153
154 case 'before':
155 $content = do_shortcode( '[post-views]' ) . $content;
156 break;
157
158 case 'manual':
159 default:
160 break;
161 }
162 }
163
164 return $content;
165 }
166
167 /**
168 * Enqueue frontend scripts and styles.
169 *
170 * @return void
171 */
172 public function wp_enqueue_scripts() {
173 // get main instance
174 $pvc = Post_Views_Counter();
175
176 // enable styles?
177 if ( (bool) apply_filters( 'pvc_enqueue_styles', true ) === true ) {
178 // load dashicons
179 wp_enqueue_style( 'dashicons' );
180
181 // load style
182 wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend.css', [], $pvc->defaults['version'] );
183 }
184
185 // get countable post types
186 $post_types = $pvc->options['general']['post_types_count'];
187
188 // whether to count this post type or not
189 if ( empty( $post_types ) || ! is_singular( $post_types ) )
190 return;
191
192 // get counter mode
193 $mode = $pvc->options['general']['counter_mode'];
194
195 // specific counter mode?
196 if ( in_array( $mode, [ 'js', 'rest_api' ], true ) ) {
197 wp_enqueue_script( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend.js', [ 'jquery' ], $pvc->defaults['version'], true );
198
199 $args = [
200 'mode' => $mode,
201 'postID' => get_the_ID(),
202 'nonce' => ( $mode === 'rest_api' ? wp_create_nonce( 'wp_rest' ) : wp_create_nonce( 'pvc-check-post' ) )
203 ];
204
205 switch ( $mode ) {
206 case 'rest_api':
207 $args['requestURL'] = rest_url( 'post-views-counter/view-post/' );
208 break;
209
210 case 'js':
211 default:
212 $args['requestURL'] = admin_url( 'admin-ajax.php' );
213 break;
214 }
215
216 // make it safe
217 $args['requestURL'] = esc_url_raw( $args['requestURL'] );
218
219 wp_localize_script( 'post-views-counter-frontend', 'pvcArgsFrontend', apply_filters( 'pvc_frontend_script_args', $args ) );
220 }
221 }
222 }