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 / includes / class-frontend.php
post-views-counter / includes Last commit date
class-admin.php 2 years ago class-columns.php 2 years ago class-counter.php 2 years ago class-crawler-detect.php 2 years ago class-cron.php 2 years ago class-dashboard.php 2 years ago class-frontend.php 2 years ago class-functions.php 2 years ago class-query.php 2 years ago class-settings-api.php 2 years ago class-settings.php 2 years ago class-update.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-frontend.php
289 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 private $script_args = [];
14
15 /**
16 * Class constructor.
17 *
18 * @return void
19 */
20 public function __construct() {
21 // actions
22 add_action( 'after_setup_theme', [ $this, 'register_shortcode' ] );
23 add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] );
24 add_action( 'wp', [ $this, 'run' ] );
25 }
26
27 /**
28 * Register post-views shortcode function.
29 *
30 * @return void
31 */
32 public function register_shortcode() {
33 add_shortcode( 'post-views', [ $this, 'post_views_shortcode' ] );
34 }
35
36 /**
37 * Post views shortcode function.
38 *
39 * @param array $args
40 * @return string
41 */
42 public function post_views_shortcode( $args ) {
43 $views = 0;
44
45 $defaults = [
46 'id' => get_the_ID(),
47 'type' => 'post'
48 ];
49
50 // main item?
51 if ( ! in_the_loop() ) {
52 // get current object
53 $object = get_queried_object();
54
55 // post?
56 if ( is_a( $object, 'WP_Post' ) ) {
57 $defaults['id'] = $object->ID;
58 $defaults['type'] = 'post';
59 // term?
60 } elseif ( is_a( $object, 'WP_Term' ) ) {
61 $defaults['id'] = $object->term_id;
62 $defaults['type'] = 'term';
63 // user?
64 } elseif ( is_a( $object, 'WP_User' ) ) {
65 $defaults['id'] = $object->ID;
66 $defaults['type'] = 'user';
67 }
68 }
69
70 // combine attributes
71 $args = shortcode_atts( $defaults, $args );
72
73 // default type?
74 if ( $args['type'] === 'post' ) {
75 $views = pvc_post_views( $args['id'], false );
76 }
77
78 return apply_filters( 'pvc_post_views_shortcode', $views, $args );
79 }
80
81 /**
82 * Display number of post views.
83 *
84 * @return void
85 */
86 public function run() {
87 if ( is_admin() && ! wp_doing_ajax() )
88 return;
89
90 $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] );
91
92 // valid filter?
93 if ( ! empty( $filter ) && in_array( $filter, [ 'before', 'after' ] ) ) {
94 // post content
95 add_filter( 'the_content', [ $this, 'add_post_views_count' ] );
96
97 // bbpress support
98 add_action( 'bbp_template_' . $filter . '_single_topic', [ $this, 'display_bbpress_post_views' ] );
99 add_action( 'bbp_template_' . $filter . '_single_forum', [ $this, 'display_bbpress_post_views' ] );
100 // custom
101 } elseif ( $filter !== 'manual' && is_string( $filter ) )
102 add_filter( $filter, [ $this, 'add_post_views_count' ] );
103 }
104
105 /**
106 * Add post views counter to forum/topic of bbPress.
107 *
108 * @return void
109 */
110 public function display_bbpress_post_views() {
111 $post_id = get_the_ID();
112
113 // check only for forums and topics
114 if ( bbp_is_forum( $post_id ) || bbp_is_topic( $post_id ) )
115 echo $this->add_post_views_count( '' );
116 }
117
118 /**
119 * Add post views counter to content.
120 *
121 * @param string $content
122 * @return string
123 */
124 public function add_post_views_count( $content = '' ) {
125 // get main instance
126 $pvc = Post_Views_Counter();
127
128 $display = false;
129
130 // post type check
131 if ( ! empty( $pvc->options['display']['post_types_display'] ) )
132 $display = is_singular( $pvc->options['display']['post_types_display'] );
133
134 // page visibility check
135 if ( ! empty( $pvc->options['display']['page_types_display'] ) ) {
136 foreach ( $pvc->options['display']['page_types_display'] as $page ) {
137 switch ( $page ) {
138 case 'singular':
139 if ( is_singular( $pvc->options['display']['post_types_display'] ) )
140 $display = true;
141 break;
142
143 case 'archive':
144 if ( is_archive() )
145 $display = true;
146 break;
147
148 case 'search':
149 if ( is_search() )
150 $display = true;
151 break;
152
153 case 'home':
154 if ( is_home() || is_front_page() )
155 $display = true;
156 break;
157 }
158 }
159 }
160
161 // get groups to check it faster
162 $groups = $pvc->options['display']['restrict_display']['groups'];
163
164 // whether to display views
165 if ( is_user_logged_in() ) {
166 // exclude logged in users?
167 if ( in_array( 'users', $groups, true ) )
168 $display = false;
169 // exclude specific roles?
170 elseif ( in_array( 'roles', $groups, true ) && $pvc->counter->is_user_role_excluded( get_current_user_id(), $pvc->options['display']['restrict_display']['roles'] ) )
171 $display = false;
172 // exclude guests?
173 } elseif ( in_array( 'guests', $groups, true ) )
174 $display = false;
175
176 // we don't want to mess custom loops
177 if ( ! in_the_loop() && ! class_exists( 'bbPress' ) )
178 $display = false;
179
180 if ( (bool) apply_filters( 'pvc_display_views_count', $display ) === true ) {
181 $filter = apply_filters( 'pvc_shortcode_filter_hook', $pvc->options['display']['position'] );
182
183 switch ( $filter ) {
184 case 'after':
185 $content = $content . do_shortcode( '[post-views]' );
186 break;
187
188 case 'before':
189 $content = do_shortcode( '[post-views]' ) . $content;
190 break;
191
192 case 'manual':
193 default:
194 break;
195 }
196 }
197
198 return $content;
199 }
200
201 /**
202 * Get frontend script arguments.
203 *
204 * @return array
205 */
206 public function get_frontend_script_args() {
207 return $this->script_args;
208 }
209
210 /**
211 * Enqueue frontend scripts and styles.
212 *
213 * @return void
214 */
215 public function wp_enqueue_scripts() {
216 // get main instance
217 $pvc = Post_Views_Counter();
218
219 // enable styles?
220 if ( (bool) apply_filters( 'pvc_enqueue_styles', true ) === true ) {
221 // load dashicons
222 wp_enqueue_style( 'dashicons' );
223
224 // load style
225 wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', [], $pvc->defaults['version'] );
226 }
227
228 // skip special requests
229 if ( is_preview() || is_feed() || is_trackback() || ( function_exists( 'is_favicon' ) && is_favicon() ) || is_customize_preview() )
230 return;
231
232 // get countable post types
233 $post_types = $pvc->options['general']['post_types_count'];
234
235 // whether to count this post type or not
236 if ( empty( $post_types ) || ! is_singular( $post_types ) )
237 return;
238
239 // get current post id
240 $post_id = (int) get_the_ID();
241
242 // allow to run check post?
243 if ( ! (bool) apply_filters( 'pvc_run_check_post', true, $post_id ) )
244 return;
245
246 // get counter mode
247 $mode = $pvc->options['general']['counter_mode'];
248
249 // specific counter mode?
250 if ( in_array( $mode, [ 'js', 'rest_api' ], true ) ) {
251 wp_enqueue_script( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', [], $pvc->defaults['version'], false );
252
253 // prepare args
254 $args = [
255 'mode' => $mode,
256 'postID' => $post_id,
257 'requestURL' => '',
258 'nonce' => '',
259 'dataStorage' => $pvc->options['general']['data_storage'],
260 'multisite' => ( is_multisite() ? (int) get_current_blog_id() : false ),
261 'path' => empty( COOKIEPATH ) || ! is_string( COOKIEPATH ) ? '/' : COOKIEPATH,
262 'domain' => empty( COOKIE_DOMAIN ) || ! is_string( COOKIE_DOMAIN ) ? '' : COOKIE_DOMAIN
263 ];
264
265 switch ( $mode ) {
266 // rest api
267 case 'rest_api':
268 $args['requestURL'] = rest_url( 'post-views-counter/view-post/' . $args['postID'] );
269 $args['nonce'] = wp_create_nonce( 'wp_rest' );
270 break;
271
272 // javascript
273 case 'js':
274 default:
275 $args['requestURL'] = admin_url( 'admin-ajax.php' );
276 $args['nonce'] = wp_create_nonce( 'pvc-check-post' );
277 }
278
279 // make it safe
280 $args['requestURL'] = esc_url_raw( $args['requestURL'] );
281
282 // set script args
283 $this->script_args = apply_filters( 'pvc_frontend_script_args', $args, 'standard' );
284
285 wp_add_inline_script( 'post-views-counter-frontend', 'var pvcArgsFrontend = ' . wp_json_encode( $this->script_args ) . ";\n", 'before' );
286 }
287 }
288 }
289