PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.6
Post Views Counter v1.4.6
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 / functions.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
functions.php
664 lines
1 <?php
2 /**
3 * Post Views Counter pluggable template functions
4 *
5 * Override any of those functions by copying it to your theme or replace it via plugin
6 *
7 * @author Digital Factory
8 * @package Post Views Counter
9 * @since 1.0.0
10 */
11 // exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) )
13 exit;
14
15 /**
16 * Get post views for a post or array of posts.
17 *
18 * @global object $wpdb
19 *
20 * @param int|array $post_id
21 * @param string $period
22 * @return int
23 */
24 if ( ! function_exists( 'pvc_get_post_views' ) ) {
25
26 function pvc_get_post_views( $post_id = 0, $period = 'total' ) {
27 global $wpdb;
28
29 // sanitize period
30 $period = sanitize_key( $period );
31
32 if ( empty( $post_id ) )
33 $post_id = get_the_ID();
34
35 if ( is_array( $post_id ) )
36 $post_id = implode( ',', array_map( 'intval', $post_id ) );
37 else
38 $post_id = (int) $post_id;
39
40 // set where clause
41 $where = [ 'type' => 'type = 4' ];
42
43 // update where clause
44 $where = apply_filters( 'pvc_get_post_views_period_where', $where, $period, $post_id );
45
46 $query = "SELECT SUM(count) AS views
47 FROM " . $wpdb->prefix . "post_views
48 WHERE id IN (" . $post_id . ") AND " . implode( ' AND ', $where );
49
50 // calculate query hash
51 $query_hash = md5( $query );
52
53 // get cached data
54 $post_views = wp_cache_get( $query_hash, 'pvc-get_post_views' );
55
56 // cached data not found?
57 if ( $post_views === false ) {
58 // get post views
59 $post_views = (int) $wpdb->get_var( $query );
60
61 // set the cache expiration, 5 minutes by default
62 $expire = absint( apply_filters( 'pvc_object_cache_expire', 300 ) );
63
64 // add cached post views
65 wp_cache_add( $query_hash, $post_views, 'pvc-get_post_views', $expire );
66 }
67
68 return (int) apply_filters( 'pvc_get_post_views', $post_views, $post_id, $period );
69 }
70
71 }
72
73 /**
74 * Get views query.
75 *
76 * @global object $wpdb
77 *
78 * @param array $args
79 * @return int|array
80 */
81 if ( ! function_exists( 'pvc_get_views' ) ) {
82
83 function pvc_get_views( $args = [] ) {
84 $range = [];
85 $defaults = [
86 'fields' => 'views',
87 'post_id' => '',
88 'post_type' => '',
89 'views_query' => [
90 'year' => '',
91 'month' => '',
92 'week' => '',
93 'day' => '',
94 'after' => '', // string or array
95 'before' => '', // string or array
96 'inclusive' => true
97 ]
98 ];
99
100 // merge default options with new arguments
101 $args = array_merge( $defaults, $args );
102
103 // check views query
104 if ( ! is_array( $args['views_query'] ) )
105 $args['views_query'] = $defaults['views_query'];
106
107 // merge views query too
108 $args['views_query'] = array_merge( $defaults['views_query'], $args['views_query'] );
109
110 $args = apply_filters( 'pvc_get_views_args', $args );
111
112 // check post types
113 if ( is_array( $args['post_type'] ) && ! empty( $args['post_type'] ) ) {
114 $post_types = [];
115
116 foreach ( $args['post_type'] as $post_type ) {
117 $post_types[] = "'" . $post_type . "'";
118 }
119
120 $args['post_type'] = implode( ', ', $post_types );
121 } elseif ( ! is_string( $args['post_type'] ) )
122 $args['post_type'] = $defaults['post_type'];
123 else
124 $args['post_type'] = "'" . $args['post_type'] . "'";
125
126 // check post ids
127 if ( is_array( $args['post_id'] ) && ! empty( $args['post_id'] ) )
128 $args['post_id'] = implode( ', ', array_unique( array_map( 'intval', $args['post_id'] ) ) );
129 else
130 $args['post_id'] = (int) $args['post_id'];
131
132 // check fields
133 if ( ! in_array( $args['fields'], [ 'views', 'date=>views' ], true ) )
134 $args['fields'] = $defaults['fields'];
135
136 $query_chunks = [];
137 $views_query = '';
138
139 // views query after/before parameters work only when fields == views
140 if ( $args['fields'] === 'views' ) {
141 // check views query inclusive
142 if ( ! isset( $args['views_query']['inclusive'] ) )
143 $args['views_query']['inclusive'] = $defaults['views_query']['inclusive'];
144 else
145 $args['views_query']['inclusive'] = (bool) $args['views_query']['inclusive'];
146
147 // check after and before dates
148 foreach ( [ 'after' => '>', 'before' => '<' ] as $date => $type ) {
149 $year_ = null;
150 $month_ = null;
151 $week_ = null;
152 $day_ = null;
153
154 // check views query date
155 if ( ! empty( $args['views_query'][$date] ) ) {
156 // is it a date array?
157 if ( is_array( $args['views_query'][$date] ) ) {
158 // check views query $date date year
159 if ( ! empty( $args['views_query'][$date]['year'] ) )
160 $year_ = str_pad( (int) $args['views_query'][$date]['year'], 4, 0, STR_PAD_LEFT );
161
162 // check views query date month
163 if ( ! empty( $args['views_query'][$date]['month'] ) )
164 $month_ = str_pad( (int) $args['views_query'][$date]['month'], 2, 0, STR_PAD_LEFT );
165
166 // check views query date week
167 if ( ! empty( $args['views_query'][$date]['week'] ) )
168 $week_ = str_pad( (int) $args['views_query'][$date]['week'], 2, 0, STR_PAD_LEFT );
169
170 // check views query date day
171 if ( ! empty( $args['views_query'][$date]['day'] ) )
172 $day_ = str_pad( (int) $args['views_query'][$date]['day'], 2, 0, STR_PAD_LEFT );
173 // is it a date string?
174 } elseif ( is_string( $args['views_query'][$date] ) ) {
175 $time_ = strtotime( $args['views_query'][$date] );
176
177 // valid datetime?
178 if ( $time_ !== false ) {
179 // week does not exists here, string dates are always treated as year + month + day
180 list( $day_, $month_, $year_ ) = explode( ' ', date( "d m Y", $time_ ) );
181 }
182 }
183
184 // valid date?
185 if ( ! ( $year_ === null && $month_ === null && $week_ === null && $day_ === null ) ) {
186 $query_chunks[] = [
187 'year' => $year_,
188 'month' => $month_,
189 'day' => $day_,
190 'week' => $week_,
191 'type' => $type . ( $args['views_query']['inclusive'] ? '=' : '' )
192 ];
193 }
194 }
195 }
196
197 if ( ! empty( $query_chunks ) ) {
198 $valid_dates = true;
199
200 // after and before?
201 if ( count( $query_chunks ) === 2 ) {
202 // before and after dates should be the same
203 foreach ( [ 'year', 'month', 'day', 'week' ] as $date_type ) {
204 if ( ! ( ( $query_chunks[0][$date_type] !== null && $query_chunks[1][$date_type] !== null ) || ( $query_chunks[0][$date_type] === null && $query_chunks[1][$date_type] === null ) ) )
205 $valid_dates = false;
206 }
207 }
208
209 if ( $valid_dates ) {
210 foreach ( $query_chunks as $chunk ) {
211 // year
212 if ( isset( $chunk['year'] ) ) {
213 // year, week
214 if ( isset( $chunk['week'] ) )
215 $views_query .= " AND pvc.type = 1 AND pvc.period " . $chunk['type'] . " '" . $chunk['year'] . $chunk['week'] . "'";
216 // year, month
217 elseif ( isset( $chunk['month'] ) ) {
218 // year, month, day
219 if ( isset( $chunk['day'] ) )
220 $views_query .= " AND pvc.type = 0 AND pvc.period " . $chunk['type'] . " '" . $chunk['year'] . $chunk['month'] . $chunk['day'] . "'";
221 // year, month
222 else
223 $views_query .= " AND pvc.type = 2 AND pvc.period " . $chunk['type'] . " '" . $chunk['year'] . $chunk['month'] . "'";
224 // year
225 } else
226 $views_query .= " AND pvc.type = 3 AND pvc.period " . $chunk['type'] . " '" . $chunk['year'] . "'";
227 // month
228 } elseif ( isset( $chunk['month'] ) ) {
229 // month, day
230 if ( isset( $chunk['day'] ) ) {
231 $views_query .= " AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) " . $chunk['type'] . " '" . $chunk['month'] . $chunk['day'] . "'";
232 // month
233 } else
234 $views_query .= " AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) " . $chunk['type'] . " '" . $chunk['month'] . "'";
235 // week
236 } elseif ( isset( $chunk['week'] ) )
237 $views_query .= " AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) " . $chunk['type'] . " '" . $chunk['week'] . "'";
238 // day
239 elseif ( isset( $chunk['day'] ) )
240 $views_query .= " AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) " . $chunk['type'] . " '" . $chunk['day'] . "'";
241 }
242 }
243 }
244 }
245
246 $special_views_query = ( $views_query !== '' );
247
248 if ( $args['fields'] === 'date=>views' || $views_query === '' ) {
249 // check views query year
250 if ( ! empty( $args['views_query']['year'] ) )
251 $year = str_pad( (int) $args['views_query']['year'], 4, 0, STR_PAD_LEFT );
252
253 // check views query month
254 if ( ! empty( $args['views_query']['month'] ) )
255 $month = str_pad( (int) $args['views_query']['month'], 2, 0, STR_PAD_LEFT );
256
257 // check views query week
258 if ( ! empty( $args['views_query']['week'] ) )
259 $week = str_pad( (int) $args['views_query']['week'], 2, 0, STR_PAD_LEFT );
260
261 // check views query day
262 if ( ! empty( $args['views_query']['day'] ) )
263 $day = str_pad( (int) $args['views_query']['day'], 2, 0, STR_PAD_LEFT );
264
265 // year
266 if ( isset( $year ) ) {
267 // year, week
268 if ( isset( $week ) ) {
269 if ( $args['fields'] === 'date=>views' ) {
270 // create date based on week number
271 $date = new DateTime( $year . 'W' . $week );
272
273 // get monday
274 $monday = $date->format( 'd' );
275
276 // get month of monday
277 $monday_month = $date->format( 'm' );
278
279 // prepare range
280 for( $i = 1; $i <= 6; $i++ ) {
281 $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0;
282
283 $date->modify( '+1days' );
284 }
285
286 $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0;
287
288 // get month of sunday
289 $sunday_month = $date->format( 'm' );
290
291 $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $monday_month . $monday . "' AND pvc.period <= '" . $date->format( 'Y' ) . $sunday_month . $date->format( 'd' ) . "'";
292 } else
293 $views_query = " AND pvc.type = 1 AND pvc.period = '" . $year . $week . "'";
294 // year, month
295 } elseif ( isset( $month ) ) {
296 // year, month, day
297 if ( isset( $day ) ) {
298 if ( $args['fields'] === 'date=>views' )
299 // prepare range
300 $range[(string) ( $year . $month . $day )] = 0;
301
302 $views_query = " AND pvc.type = 0 AND pvc.period = '" . $year . $month . $day . "'";
303 // year, month
304 } else {
305 if ( $args['fields'] === 'date=>views' ) {
306 // create date
307 $date = new DateTime( $year . '-' . $month . '-01' );
308
309 // get last day
310 $last = $date->format( 't' );
311
312 // prepare range
313 for( $i = 1; $i <= $last; $i++ ) {
314 $range[(string) ( $year . $month . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0;
315 }
316
317 $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $month . "01' AND pvc.period <= '" . $year . $month . $last . "'";
318 } else
319 $views_query = " AND pvc.type = 2 AND pvc.period = '" . $year . $month . "'";
320 }
321 // year
322 } else {
323 if ( $args['fields'] === 'date=>views' ) {
324 // prepare range
325 for( $i = 1; $i <= 12; $i++ ) {
326 $range[(string) ( $year . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0;
327 }
328
329 // create date
330 $date = new DateTime( $year . '-12-01' );
331
332 $views_query = " AND pvc.type = 2 AND pvc.period >= '" . $year . "01' AND pvc.period <= '" . $year . "12'";
333 } else
334 $views_query = " AND pvc.type = 3 AND pvc.period = '" . $year . "'";
335 }
336 // month
337 } elseif ( isset( $month ) ) {
338 // month, day
339 if ( isset( $day ) ) {
340 $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) = '" . $month . $day . "'";
341 // month
342 } else {
343 $views_query = " AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) = '" . $month . "'";
344 }
345 // week
346 } elseif ( isset( $week ) ) {
347 $views_query = " AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) = '" . $week . "'";
348 // day
349 } elseif ( isset( $day ) ) {
350 $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) = '" . $day . "'";
351 }
352 }
353
354 global $wpdb;
355
356 $query = "SELECT " . ( $args['fields'] === 'date=>views' ? 'pvc.period, ' : '' ) . "SUM( COALESCE( pvc.count, 0 ) ) AS post_views
357 FROM " . $wpdb->prefix . "posts wpp
358 LEFT JOIN " . $wpdb->prefix . "post_views pvc ON pvc.id = wpp.ID" . ( $views_query !== '' ? ' ' . $views_query : ' AND pvc.type = 4' ) . ( ! empty( $args['post_id'] ) ? ' AND pvc.id IN (' . $args['post_id'] . ')' : '' ) . "
359 " . ( $args['post_type'] !== '' ? "WHERE wpp.post_type IN (" . $args['post_type'] . ")" : '' ) . "
360 " . ( $views_query !== '' && $special_views_query === false ? 'GROUP BY pvc.period' : '' ) . "
361 HAVING post_views > 0";
362
363 // get cached data
364 $post_views = wp_cache_get( md5( $query ), 'pvc-get_views' );
365
366 // cached data not found?
367 if ( $post_views === false ) {
368 if ( $args['fields'] === 'date=>views' && ! empty( $range ) ) {
369 $results = $wpdb->get_results( $query );
370
371 if ( ! empty( $results ) ) {
372 foreach ( $results as $row ) {
373 $range[$row->period] = (int) $row->post_views;
374 }
375 }
376
377 $post_views = $range;
378 } else
379 $post_views = (int) $wpdb->get_var( $query );
380
381 // set the cache expiration, 5 minutes by default
382 $expire = absint( apply_filters( 'pvc_object_cache_expire', 300 ) );
383
384 wp_cache_add( md5( $query ), $post_views, 'pvc-get_views', $expire );
385 }
386
387 return apply_filters( 'pvc_get_views', $post_views );
388 }
389
390 }
391
392 /**
393 * Display post views for a given post.
394 *
395 * @param int|array $post_id
396 * @param bool $display
397 * @return string|void
398 */
399 if ( ! function_exists( 'pvc_post_views' ) ) {
400
401 function pvc_post_views( $post_id = 0, $display = true ) {
402 // get all data
403 $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
404
405 // get display options
406 $options = Post_Views_Counter()->options['display'];
407
408 // get post views
409 $views = pvc_get_post_views( $post_id, $options['display_period'] );
410
411 // use number format?
412 $views = $options['use_format'] ? number_format_i18n( $views ) : $views;
413
414 // container class
415 $class = apply_filters( 'pvc_post_views_class', 'post-views content-post post-' . $post_id . ' entry-meta', $post_id );
416
417 // prepare display
418 $label = apply_filters( 'pvc_post_views_label', ( function_exists( 'icl_t' ) ? icl_t( 'Post Views Counter', 'Post Views Label', $options['label'] ) : $options['label'] ), $post_id );
419
420 // add dashicons class if needed
421 $icon_class = strpos( $options['icon_class'], 'dashicons' ) === false ? $options['icon_class'] : 'dashicons ' . $options['icon_class'];
422
423 // prepare icon output
424 $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon ' . esc_attr( $icon_class ) . '"></span> ', $post_id );
425
426 // final views
427 $views = apply_filters( 'pvc_post_views_number_format', $views, $post_id );
428
429 $html = apply_filters(
430 'pvc_post_views_html',
431 '<div class="' . esc_attr( $class ) . '">
432 ' . ( $options['display_style']['icon'] ? $icon : '' )
433 . ( $options['display_style']['text'] ? '<span class="post-views-label">' . esc_html( $label ) . '</span> ' : '' )
434 . '<span class="post-views-count">' . $views . '</span>
435 </div>',
436 $post_id,
437 $views,
438 $label,
439 $icon
440 );
441
442 if ( $display )
443 echo $html;
444 else
445 return $html;
446 }
447
448 }
449
450 /**
451 * Get most viewed posts.
452 *
453 * @param array $args
454 * @return array
455 */
456 if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
457
458 function pvc_get_most_viewed_posts( $args = [] ) {
459 $args = array_merge(
460 [
461 'posts_per_page' => 10,
462 'order' => 'desc',
463 'post_type' => 'post',
464 'fields' => ''
465 ],
466 $args
467 );
468
469 $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
470
471 // force to use filters
472 $args['suppress_filters'] = false;
473
474 // force to use post views as order
475 $args['orderby'] = 'post_views';
476
477 return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args );
478 }
479
480 }
481
482 /**
483 * Display a list of most viewed posts.
484 *
485 * @param array $post_id
486 * @param bool $display
487 * @return mixed
488 */
489 if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
490
491 function pvc_most_viewed_posts( $args = [], $display = true ) {
492 $defaults = [
493 'number_of_posts' => 5,
494 'post_type' => [ 'post' ],
495 'order' => 'desc',
496 'thumbnail_size' => 'thumbnail',
497 'list_type' => 'unordered',
498 'show_post_views' => true,
499 'show_post_thumbnail' => false,
500 'show_post_author' => false,
501 'show_post_excerpt' => false,
502 'no_posts_message' => __( 'No Posts', 'post-views-counter' ),
503 'item_before' => '',
504 'item_after' => ''
505 ];
506
507 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
508
509 $args['show_post_views'] = (bool) $args['show_post_views'];
510 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
511 $args['show_post_author'] = (bool) $args['show_post_author'];
512 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
513
514 $posts = pvc_get_most_viewed_posts(
515 [
516 'posts_per_page' => ( isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts'] ),
517 'order' => ( isset( $args['order'] ) ? $args['order'] : $defaults['order'] ),
518 'post_type' => ( isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'] )
519 ]
520 );
521
522 if ( ! empty( $posts ) ) {
523 $html = ( $args['list_type'] === 'unordered' ? '<ul>' : '<ol>' );
524
525 foreach ( $posts as $post ) {
526 setup_postdata( $post );
527
528 $html .= '
529 <li>';
530
531 $html .= apply_filters( 'pvc_most_viewed_posts_item_before', $args['item_before'], $post );
532
533 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
534 $html .= '
535 <span class="post-thumbnail">
536 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
537 </span>';
538 }
539
540 $html .= '
541 <a class="post-title" href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a>' . ( $args['show_post_author'] ? ' <span class="author">(' . get_the_author_meta( 'display_name', $post->post_author ) . ')</span> ' : '' ) . ( $args['show_post_views'] ? ' <span class="count">(' . number_format_i18n( pvc_get_post_views( $post->ID ) ) . ')</span>' : '' );
542
543 $excerpt = '';
544
545 if ( $args['show_post_excerpt'] ) {
546 if ( empty( $post->post_excerpt ) )
547 $text = $post->post_content;
548 else
549 $text = $post->post_excerpt;
550
551 if ( ! empty( $text ) )
552 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
553 }
554
555 if ( ! empty( $excerpt ) )
556 $html .= '
557
558 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
559
560 $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post );
561
562 $html .= '
563 </li>';
564 }
565
566 wp_reset_postdata();
567
568 $html .= ( $args['list_type'] === 'unordered' ? '</ul>' : '</ol>' );
569 } else
570 $html = $args['no_posts_message'];
571
572 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
573
574 if ( $display )
575 echo $html;
576 else
577 return $html;
578 }
579
580 }
581
582 /**
583 * Update total number of post views for a post.
584 *
585 * @global object $wpdb
586 *
587 * @param int $post_id Post ID
588 * @param int $post_views Number of post views
589 * @return bool|int
590 */
591 function pvc_update_post_views( $post_id = 0, $post_views = 0 ) {
592 // cast post ID
593 $post_id = (int) $post_id;
594
595 // get post
596 $post = get_post( $post_id );
597
598 // check if post exists
599 if ( empty( $post ) )
600 return false;
601
602 // cast number of views
603 $post_views = (int) $post_views;
604 $post_views = $post_views < 0 ? 0 : $post_views;
605
606 global $wpdb;
607
608 // change post views?
609 $post_views = apply_filters( 'pvc_update_post_views_count', $post_views, $post_id );
610
611 // insert or update database post views count
612 $wpdb->query( $wpdb->prepare( "INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count) VALUES (%d, %d, %s, %d) ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $post_views, $post_views ) );
613
614 // query fails only if it returns false
615 return apply_filters( 'pvc_update_post_views', $post_id );
616 }
617
618 /**
619 * View post manually function.
620 *
621 * By default this function has limitations. It works properly only between
622 * wp_loaded (minimum priority 10) and wp_head (maximum priority 6) actions and
623 * it can handle only one function execution per site request.
624 *
625 * To bypass these limitations there is a $bypass_content argument. It requires
626 * JavaScript or REST API as counter mode but it extends the ability to use
627 * pvc_view_post up to wp_print_footer_scripts (maximum priority 10) action. It
628 * also bypass one function execution limitation to allow multiple function
629 * calls during one site request. This also includes the correct saving of
630 * cookies.
631 *
632 * @since 1.2.0
633 *
634 * @param int $post_id
635 * @param bool $bypass_content
636 * @return bool
637 */
638 function pvc_view_post( $post_id = 0, $bypass_content = false ) {
639 // no post id?
640 if ( empty( $post_id ) ) {
641 // get current id
642 $post_id = get_the_ID();
643 } else {
644 // cast post id
645 $post_id = (int) $post_id;
646 }
647
648 // get post
649 $post = get_post( $post_id );
650
651 // invalid post?
652 if ( ! is_a( $post, 'WP_Post' ) )
653 return false;
654
655 // get main instance
656 $pvc = Post_Views_Counter();
657
658 if ( $bypass_content )
659 $pvc->counter->add_to_queue( $post_id );
660 else
661 $pvc->counter->check_post( $post_id );
662
663 return true;
664 }