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