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