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