PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.13
Post Views Counter v1.3.13
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 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
class-frontend.php
273 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 $defaults = [
44 'id' => get_the_ID(),
45 'type' => 'post'
46 ];
47
48 // main item?
49 if ( ! in_the_loop() ) {
50 // get current object
51 $object = get_queried_object();
52
53 // post?
54 if ( is_a( $object, 'WP_Post' ) ) {
55 $defaults['id'] = $object->ID;
56 $defaults['type'] = 'post';
57 // term?
58 } elseif ( is_a( $object, 'WP_Term' ) ) {
59 $defaults['id'] = $object->term_id;
60 $defaults['type'] = 'term';
61 // user?
62 } elseif ( is_a( $object, 'WP_User' ) ) {
63 $defaults['id'] = $object->ID;
64 $defaults['type'] = 'user';
65 }
66 }
67
68 // combine attributes
69 $args = shortcode_atts( $defaults, $args );
70
71 // default type?
72 if ( $args['type'] === 'post' )
73 $views = pvc_post_views( $args['id'], false );
74 else
75 $views = apply_filters( 'pvc_post_views_shortcode', '', $args );
76
77 return $views;
78 }
79
80 /**
81 * Display number of post views.
82 *
83 * @return void
84 */
85 public function run() {
86 if ( is_admin() && ! wp_doing_ajax() )
87 return;
88
89 $filter = apply_filters( 'pvc_shortcode_filter_hook', Post_Views_Counter()->options['display']['position'] );
90
91 // valid filter?
92 if ( ! empty( $filter ) && in_array( $filter, [ 'before', 'after' ] ) ) {
93 // post content
94 add_filter( 'the_content', [ $this, 'add_post_views_count' ] );
95
96 // bbpress support
97 add_action( 'bbp_template_' . $filter . '_single_topic', [ $this, 'display_bbpress_post_views' ] );
98 add_action( 'bbp_template_' . $filter . '_single_forum', [ $this, 'display_bbpress_post_views' ] );
99 // custom
100 } elseif ( $filter !== 'manual' && is_string( $filter ) )
101 add_filter( $filter, [ $this, 'add_post_views_count' ] );
102 }
103
104 /**
105 * Add post views counter to forum/topic of bbPress.
106 *
107 * @return void
108 */
109 public function display_bbpress_post_views() {
110 $post_id = get_the_ID();
111
112 // check only for forums and topics
113 if ( bbp_is_forum( $post_id ) || bbp_is_topic( $post_id ) )
114 echo $this->add_post_views_count( '' );
115 }
116
117 /**
118 * Add post views counter to content.
119 *
120 * @param string $content
121 * @return string
122 */
123 public function add_post_views_count( $content = '' ) {
124 // get main instance
125 $pvc = Post_Views_Counter();
126
127 $display = false;
128
129 // post type check
130 if ( ! empty( $pvc->options['display']['post_types_display'] ) ) {
131 $display = is_singular( $pvc->options['display']['post_types_display'] );
132 }
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 // get countable post types
229 $post_types = $pvc->options['general']['post_types_count'];
230
231 // whether to count this post type or not
232 if ( empty( $post_types ) || ! is_singular( $post_types ) )
233 return;
234
235 // get counter mode
236 $mode = $pvc->options['general']['counter_mode'];
237
238 // specific counter mode?
239 if ( in_array( $mode, [ 'js', 'rest_api' ], true ) ) {
240 wp_enqueue_script( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', [], $pvc->defaults['version'], true );
241
242 // prepare args
243 $args = [
244 'mode' => $mode,
245 'postID' => get_the_ID(),
246 'requestURL' => '',
247 'nonce' => ''
248 ];
249
250 switch ( $mode ) {
251 // rest api
252 case 'rest_api':
253 $args['requestURL'] = rest_url( 'post-views-counter/view-post/' . $args['postID'] );
254 $args['nonce'] = wp_create_nonce( 'wp_rest' );
255 break;
256
257 // javascript
258 case 'js':
259 default:
260 $args['requestURL'] = admin_url( 'admin-ajax.php' );
261 $args['nonce'] = wp_create_nonce( 'pvc-check-post' );
262 }
263
264 // make it safe
265 $args['requestURL'] = esc_url_raw( $args['requestURL'] );
266
267 // set script args
268 $this->script_args = apply_filters( 'pvc_frontend_script_args', $args, 'standard' );
269
270 wp_add_inline_script( 'post-views-counter-frontend', 'var pvcArgsFrontend = ' . wp_json_encode( $this->script_args ) . ";\n", 'before' );
271 }
272 }
273 }