PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.2
Post Views Counter v1.4.2
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
281 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 // page visibility check
134 if ( ! empty( $pvc->options['display']['page_types_display'] ) ) {
135 foreach ( $pvc->options['display']['page_types_display'] as $page ) {
136 switch ( $page ) {
137 case 'singular':
138 if ( is_singular( $pvc->options['display']['post_types_display'] ) )
139 $display = true;
140 break;
141
142 case 'archive':
143 if ( is_archive() )
144 $display = true;
145 break;
146
147 case 'search':
148 if ( is_search() )
149 $display = true;
150 break;
151
152 case 'home':
153 if ( is_home() || is_front_page() )
154 $display = true;
155 break;
156 }
157 }
158 }
159
160 // get groups to check it faster
161 $groups = $pvc->options['display']['restrict_display']['groups'];
162
163 // whether to display views
164 if ( is_user_logged_in() ) {
165 // exclude logged in users?
166 if ( in_array( 'users', $groups, true ) )
167 $display = false;
168 // exclude specific roles?
169 elseif ( in_array( 'roles', $groups, true ) && $pvc->counter->is_user_role_excluded( get_current_user_id(), $pvc->options['display']['restrict_display']['roles'] ) )
170 $display = false;
171 // exclude guests?
172 } elseif ( in_array( 'guests', $groups, true ) )
173 $display = false;
174
175 // we don't want to mess custom loops
176 if ( ! in_the_loop() && ! class_exists( 'bbPress' ) )
177 $display = false;
178
179 if ( (bool) apply_filters( 'pvc_display_views_count', $display ) === true ) {
180 $filter = apply_filters( 'pvc_shortcode_filter_hook', $pvc->options['display']['position'] );
181
182 switch ( $filter ) {
183 case 'after':
184 $content = $content . do_shortcode( '[post-views]' );
185 break;
186
187 case 'before':
188 $content = do_shortcode( '[post-views]' ) . $content;
189 break;
190
191 case 'manual':
192 default:
193 break;
194 }
195 }
196
197 return $content;
198 }
199
200 /**
201 * Get frontend script arguments.
202 *
203 * @return array
204 */
205 public function get_frontend_script_args() {
206 return $this->script_args;
207 }
208
209 /**
210 * Enqueue frontend scripts and styles.
211 *
212 * @return void
213 */
214 public function wp_enqueue_scripts() {
215 // get main instance
216 $pvc = Post_Views_Counter();
217
218 // enable styles?
219 if ( (bool) apply_filters( 'pvc_enqueue_styles', true ) === true ) {
220 // load dashicons
221 wp_enqueue_style( 'dashicons' );
222
223 // load style
224 wp_enqueue_style( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/css/frontend' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', [], $pvc->defaults['version'] );
225 }
226
227 // skip special requests
228 if ( is_preview() || is_feed() || is_trackback() || is_favicon() || is_customize_preview() )
229 return;
230
231 // get countable post types
232 $post_types = $pvc->options['general']['post_types_count'];
233
234 // whether to count this post type or not
235 if ( empty( $post_types ) || ! is_singular( $post_types ) )
236 return;
237
238 // get counter mode
239 $mode = $pvc->options['general']['counter_mode'];
240
241 // specific counter mode?
242 if ( in_array( $mode, [ 'js', 'rest_api' ], true ) ) {
243 wp_enqueue_script( 'post-views-counter-frontend', POST_VIEWS_COUNTER_URL . '/js/frontend' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', [], $pvc->defaults['version'], true );
244
245 // prepare args
246 $args = [
247 'mode' => $mode,
248 'postID' => get_the_ID(),
249 'requestURL' => '',
250 'nonce' => '',
251 'dataStorage' => $pvc->options['general']['data_storage'],
252 'multisite' => ( is_multisite() ? (int) get_current_blog_id() : false ),
253 'path' => empty( COOKIEPATH ) || ! is_string( COOKIEPATH ) ? '/' : COOKIEPATH,
254 'domain' => empty( COOKIE_DOMAIN ) || ! is_string( COOKIE_DOMAIN ) ? '' : COOKIE_DOMAIN
255 ];
256
257 switch ( $mode ) {
258 // rest api
259 case 'rest_api':
260 $args['requestURL'] = rest_url( 'post-views-counter/view-post/' . $args['postID'] );
261 $args['nonce'] = wp_create_nonce( 'wp_rest' );
262 break;
263
264 // javascript
265 case 'js':
266 default:
267 $args['requestURL'] = admin_url( 'admin-ajax.php' );
268 $args['nonce'] = wp_create_nonce( 'pvc-check-post' );
269 }
270
271 // make it safe
272 $args['requestURL'] = esc_url_raw( $args['requestURL'] );
273
274 // set script args
275 $this->script_args = apply_filters( 'pvc_frontend_script_args', $args, 'standard' );
276
277 wp_add_inline_script( 'post-views-counter-frontend', 'var pvcArgsFrontend = ' . wp_json_encode( $this->script_args ) . ";\n", 'before' );
278 }
279 }
280 }
281