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