PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.12
Post Views Counter v1.3.12
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 3 years ago class-columns.php 3 years ago class-counter.php 3 years ago class-crawler-detect.php 3 years ago class-cron.php 3 years ago class-dashboard.php 3 years ago class-frontend.php 3 years ago class-functions.php 3 years ago class-query.php 3 years ago class-settings-api.php 3 years ago class-settings.php 3 years ago class-update.php 3 years ago class-widgets.php 3 years ago functions.php 3 years ago
functions.php
651 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 // get icon class
408 $icon_class = ( $options['icon_class'] !== '' ? esc_attr( $options['icon_class'] ) : '' );
409
410 // add dashicons class if needed
411 $icon_class = strpos( $icon_class, 'dashicons ' ) === 0 ? $icon_class : 'dashicons ' . $icon_class;
412
413 // prepare icon output
414 $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon ' . $icon_class . '"></span> ', $post_id );
415
416 $html = apply_filters(
417 'pvc_post_views_html',
418 '<div class="' . esc_attr( $class ) . '">
419 ' . ( $options['display_style']['icon'] && $icon_class !== '' ? $icon : '' )
420 . ( $options['display_style']['text'] && $label !== '' ? '<span class="post-views-label">' . esc_html( $label ) . '</span> ' : '' )
421 . '<span class="post-views-count">' . number_format_i18n( $views ) . '</span>
422 </div>',
423 $post_id,
424 $views,
425 $label,
426 $icon
427 );
428
429 if ( $display )
430 echo $html;
431 else
432 return $html;
433 }
434
435 }
436
437 /**
438 * Get most viewed posts.
439 *
440 * @param array $args
441 * @return array
442 */
443 if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
444
445 function pvc_get_most_viewed_posts( $args = [] ) {
446 $args = array_merge(
447 [
448 'posts_per_page' => 10,
449 'order' => 'desc',
450 'post_type' => 'post',
451 'fields' => ''
452 ],
453 $args
454 );
455
456 $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
457
458 // force to use filters
459 $args['suppress_filters'] = false;
460
461 // force to use post views as order
462 $args['orderby'] = 'post_views';
463
464 return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args );
465 }
466
467 }
468
469 /**
470 * Display a list of most viewed posts.
471 *
472 * @param array $post_id
473 * @param bool $display
474 * @return mixed
475 */
476 if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
477
478 function pvc_most_viewed_posts( $args = [], $display = true ) {
479 $defaults = [
480 'number_of_posts' => 5,
481 'post_type' => [ 'post' ],
482 'order' => 'desc',
483 'thumbnail_size' => 'thumbnail',
484 'list_type' => 'unordered',
485 'show_post_views' => true,
486 'show_post_thumbnail' => false,
487 'show_post_author' => false,
488 'show_post_excerpt' => false,
489 'no_posts_message' => __( 'No Posts', 'post-views-counter' ),
490 'item_before' => '',
491 'item_after' => ''
492 ];
493
494 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
495
496 $args['show_post_views'] = (bool) $args['show_post_views'];
497 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
498 $args['show_post_author'] = (bool) $args['show_post_author'];
499 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
500
501 $posts = pvc_get_most_viewed_posts(
502 [
503 'posts_per_page' => ( isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts'] ),
504 'order' => ( isset( $args['order'] ) ? $args['order'] : $defaults['order'] ),
505 'post_type' => ( isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'] )
506 ]
507 );
508
509 if ( ! empty( $posts ) ) {
510 $html = ( $args['list_type'] === 'unordered' ? '<ul>' : '<ol>' );
511
512 foreach ( $posts as $post ) {
513 setup_postdata( $post );
514
515 $html .= '
516 <li>';
517
518 $html .= apply_filters( 'pvc_most_viewed_posts_item_before', $args['item_before'], $post );
519
520 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
521 $html .= '
522 <span class="post-thumbnail">
523 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
524 </span>';
525 }
526
527 $html .= '
528 <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>' : '' );
529
530 $excerpt = '';
531
532 if ( $args['show_post_excerpt'] ) {
533 if ( empty( $post->post_excerpt ) )
534 $text = $post->post_content;
535 else
536 $text = $post->post_excerpt;
537
538 if ( ! empty( $text ) )
539 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
540 }
541
542 if ( ! empty( $excerpt ) )
543 $html .= '
544
545 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
546
547 $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post );
548
549 $html .= '
550 </li>';
551 }
552
553 wp_reset_postdata();
554
555 $html .= ( $args['list_type'] === 'unordered' ? '</ul>' : '</ol>' );
556 } else
557 $html = $args['no_posts_message'];
558
559 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
560
561 if ( $display )
562 echo $html;
563 else
564 return $html;
565 }
566
567 }
568
569 /**
570 * Update total number of post views for a post.
571 *
572 * @global object $wpdb
573 *
574 * @param int $post_id Post ID
575 * @param int $post_views Number of post views
576 * @return bool|int
577 */
578 function pvc_update_post_views( $post_id = 0, $post_views = 0 ) {
579 // cast post ID
580 $post_id = (int) $post_id;
581
582 // get post
583 $post = get_post( $post_id );
584
585 // check if post exists
586 if ( empty( $post ) )
587 return false;
588
589 // cast number of views
590 $post_views = (int) $post_views;
591 $post_views = $post_views < 0 ? 0 : $post_views;
592
593 global $wpdb;
594
595 // change post views?
596 $post_views = apply_filters( 'pvc_update_post_views_count', $post_views, $post_id );
597
598 // insert or update database post views count
599 $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 ) );
600
601 // query fails only if it returns false
602 return apply_filters( 'pvc_update_post_views', $post_id );
603 }
604
605 /**
606 * View post manually function.
607 *
608 * By default this function has limitations. It works properly only between
609 * wp_loaded (minimum priority 10) and wp_head (maximum priority 6) actions and
610 * it can handle only one function execution per site request.
611 *
612 * To bypass these limitations there is a $bypass_content argument. It requires
613 * JavaScript or REST API as counter mode but it extends the ability to use
614 * pvc_view_post up to wp_print_footer_scripts (maximum priority 10) action. It
615 * also bypass one function execution limitation to allow multiple function
616 * calls during one site request. This also includes the correct saving of
617 * cookies.
618 *
619 * @since 1.2.0
620 *
621 * @param int $post_id
622 * @param bool $bypass_content
623 * @return bool
624 */
625 function pvc_view_post( $post_id = 0, $bypass_content = false ) {
626 // no post id?
627 if ( empty( $post_id ) ) {
628 // get current id
629 $post_id = get_the_ID();
630 } else {
631 // cast post id
632 $post_id = (int) $post_id;
633 }
634
635 // get post
636 $post = get_post( $post_id );
637
638 // invalid post?
639 if ( ! is_a( $post, 'WP_Post' ) )
640 return false;
641
642 // get main instance
643 $pvc = Post_Views_Counter();
644
645 if ( $bypass_content )
646 $pvc->counter->add_to_queue( $post_id );
647 else
648 $pvc->counter->check_post( $post_id );
649
650 return true;
651 }