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