ajax.php
6 years ago
columns.php
6 years ago
counter.php
6 years ago
crawler-detect.php
6 years ago
cron.php
6 years ago
dashboard.php
6 years ago
frontend.php
6 years ago
functions.php
6 years ago
query.php
6 years ago
settings.php
6 years ago
update.php
6 years ago
widgets.php
6 years ago
functions.php
445 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', 5 * 60 ) ); |
| 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 = array() ) { |
| 67 | $range = array(); |
| 68 | $defaults = array( |
| 69 | 'fields' => 'views', |
| 70 | 'post_id' => '', |
| 71 | 'post_type' => '', |
| 72 | 'views_query' => array( |
| 73 | 'year' => '', |
| 74 | 'month' => '', |
| 75 | 'week' => '', |
| 76 | 'day' => '' |
| 77 | ) |
| 78 | ); |
| 79 | |
| 80 | $args = apply_filters( 'pvc_get_views_args', array_merge( $defaults, $args ) ); |
| 81 | |
| 82 | // check post types |
| 83 | if ( is_array( $args['post_type'] ) && ! empty( $args['post_type'] ) ) { |
| 84 | $post_types = array(); |
| 85 | |
| 86 | foreach( $args['post_type'] as $post_type ) { |
| 87 | $post_types[] = "'" . $post_type . "'"; |
| 88 | } |
| 89 | |
| 90 | $args['post_type'] = implode( ', ', $post_types ); |
| 91 | } elseif ( ! is_string( $args['post_type'] ) ) |
| 92 | $args['post_type'] = $defaults['post_type']; |
| 93 | else |
| 94 | $args['post_type'] = "'" . $args['post_type'] . "'"; |
| 95 | |
| 96 | // check post ids |
| 97 | if ( is_array( $args['post_id'] ) && ! empty( $args['post_id'] ) ) |
| 98 | $args['post_id'] = implode( ', ', array_unique( array_map( 'intval', $args['post_id'] ) ) ); |
| 99 | else |
| 100 | $args['post_id'] = (int) $args['post_id']; |
| 101 | |
| 102 | // check fields |
| 103 | if ( ! in_array( $args['fields'], array( 'views', 'date=>views' ), true ) ) |
| 104 | $args['fields'] = $defaults['fields']; |
| 105 | |
| 106 | // check views query |
| 107 | if ( ! is_array( $args['views_query'] ) ) |
| 108 | $args['views_query'] = $defaults['views_query']; |
| 109 | |
| 110 | // check views query year |
| 111 | if ( ! empty( $args['views_query']['year'] ) ) |
| 112 | $year = str_pad( (int) $args['views_query']['year'], 4, 0, STR_PAD_LEFT ); |
| 113 | |
| 114 | // check views query month |
| 115 | if ( ! empty( $args['views_query']['month'] ) ) |
| 116 | $month = str_pad( (int) $args['views_query']['month'], 2, 0, STR_PAD_LEFT ); |
| 117 | |
| 118 | // check views query week |
| 119 | if ( ! empty( $args['views_query']['week'] ) ) |
| 120 | $week = str_pad( (int) $args['views_query']['week'], 2, 0, STR_PAD_LEFT ); |
| 121 | |
| 122 | // check views query day |
| 123 | if ( ! empty( $args['views_query']['day'] ) ) |
| 124 | $day = str_pad( (int) $args['views_query']['day'], 2, 0, STR_PAD_LEFT ); |
| 125 | |
| 126 | $views_query = ''; |
| 127 | |
| 128 | // year |
| 129 | if ( isset( $year ) ) { |
| 130 | // year, week |
| 131 | if ( isset( $week ) ) { |
| 132 | if ( $args['fields'] === 'date=>views' ) { |
| 133 | // create date based on week number |
| 134 | $date = new DateTime( $year . 'W' . $week ); |
| 135 | |
| 136 | // get monday |
| 137 | $monday = $date->format( 'd' ); |
| 138 | |
| 139 | // get month of monday |
| 140 | $monday_month = $date->format( 'm' ); |
| 141 | |
| 142 | // prepare range |
| 143 | for( $i = 1; $i <= 6; $i++ ) { |
| 144 | $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0; |
| 145 | |
| 146 | $date->modify( '+1days' ); |
| 147 | } |
| 148 | |
| 149 | $range[(string) ( $date->format( 'Y' ) . $date->format( 'm' ) . $date->format( 'd' ) )] = 0; |
| 150 | |
| 151 | // get month of sunday |
| 152 | $sunday_month = $date->format( 'm' ); |
| 153 | |
| 154 | $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $monday_month . $monday . "' AND pvc.period <= '" . $date->format( 'Y' ) . $sunday_month . $date->format( 'd' ) . "'"; |
| 155 | } else |
| 156 | $views_query = " AND pvc.type = 1 AND pvc.period = '" . $year . $week . "'"; |
| 157 | // year, month |
| 158 | } elseif ( isset( $month ) ) { |
| 159 | // year, month, day |
| 160 | if ( isset( $day ) ) { |
| 161 | if ( $args['fields'] === 'date=>views' ) |
| 162 | // prepare range |
| 163 | $range[(string) ( $year . $month . $day )] = 0; |
| 164 | |
| 165 | $views_query = " AND pvc.type = 0 AND pvc.period = '" . $year . $month . $day . "'"; |
| 166 | // year, month |
| 167 | } else { |
| 168 | if ( $args['fields'] === 'date=>views' ) { |
| 169 | // create date |
| 170 | $date = new DateTime( $year . '-' . $month . '-01' ); |
| 171 | |
| 172 | // get last day |
| 173 | $last = $date->format( 't' ); |
| 174 | |
| 175 | // prepare range |
| 176 | for( $i = 1; $i <= $last; $i++ ) { |
| 177 | $range[(string) ( $year . $month . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0; |
| 178 | } |
| 179 | |
| 180 | $views_query = " AND pvc.type = 0 AND pvc.period >= '" . $year . $month . "01' AND pvc.period <= '" . $year . $month . $last . "'"; |
| 181 | } else |
| 182 | $views_query = " AND pvc.type = 2 AND pvc.period = '" . $year . $month . "'"; |
| 183 | } |
| 184 | // year |
| 185 | } else { |
| 186 | if ( $args['fields'] === 'date=>views' ) { |
| 187 | // prepare range |
| 188 | for( $i = 1; $i <= 12; $i++ ) { |
| 189 | $range[(string) ( $year . str_pad( $i, 2, 0, STR_PAD_LEFT ) )] = 0; |
| 190 | } |
| 191 | |
| 192 | // create date |
| 193 | $date = new DateTime( $year . '-12-01' ); |
| 194 | |
| 195 | $views_query = " AND pvc.type = 2 AND pvc.period >= '" . $year . "01' AND pvc.period <= '" . $year . "12'"; |
| 196 | } else |
| 197 | $views_query = " AND pvc.type = 3 AND pvc.period = '" . $year . "'"; |
| 198 | } |
| 199 | // month |
| 200 | } elseif ( isset( $month ) ) { |
| 201 | // month, day |
| 202 | if ( isset( $day ) ) { |
| 203 | $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) = '" . $month . $day . "'"; |
| 204 | // month |
| 205 | } else { |
| 206 | $views_query = " AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) = '" . $month . "'"; |
| 207 | } |
| 208 | // week |
| 209 | } elseif ( isset( $week ) ) { |
| 210 | $views_query = " AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) = '" . $week . "'"; |
| 211 | // day |
| 212 | } elseif ( isset( $day ) ) { |
| 213 | $views_query = " AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) = '" . $day . "'"; |
| 214 | } |
| 215 | |
| 216 | global $wpdb; |
| 217 | |
| 218 | $query = "SELECT " . ( $args['fields'] === 'date=>views' ? 'pvc.period, ' : '' ) . "SUM( IFNULL( pvc.count, 0 ) ) AS post_views |
| 219 | FROM " . $wpdb->prefix . "posts wpp |
| 220 | 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'] . ')' : '' ) . " |
| 221 | " . ( $args['post_type'] !== '' ? "WHERE wpp.post_type IN (" . $args['post_type'] . ")" : '' ) . " |
| 222 | " . ( $views_query !== '' ? 'GROUP BY pvc.period' : '' ) . " |
| 223 | HAVING post_views > 0"; |
| 224 | |
| 225 | // get cached data |
| 226 | $post_views = wp_cache_get( md5( $query ), 'pvc-get_views' ); |
| 227 | |
| 228 | // cached data not found? |
| 229 | if ( $post_views === false ) { |
| 230 | if ( $args['fields'] === 'date=>views' && ! empty( $range ) ) { |
| 231 | $results = $wpdb->get_results( $query ); |
| 232 | |
| 233 | if ( ! empty( $results ) ) { |
| 234 | foreach( $results as $row ) { |
| 235 | $range[$row->period] = (int) $row->post_views; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | $post_views = $range; |
| 240 | } else |
| 241 | $post_views = (int) $wpdb->get_var( $query ); |
| 242 | |
| 243 | // set the cache expiration, 5 minutes by default |
| 244 | $expire = absint( apply_filters( 'pvc_object_cache_expire', 5 * 60 ) ); |
| 245 | |
| 246 | wp_cache_add( md5( $query ), $post_views, 'pvc-get_views', $expire ); |
| 247 | } |
| 248 | |
| 249 | return apply_filters( 'pvc_get_views', $post_views ); |
| 250 | } |
| 251 | |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Display post views for a given post. |
| 256 | * |
| 257 | * @param int|array $post_id |
| 258 | * @param bool $display |
| 259 | * @return mixed |
| 260 | */ |
| 261 | if ( ! function_exists( 'pvc_post_views' ) ) { |
| 262 | |
| 263 | function pvc_post_views( $post_id = 0, $echo = true ) { |
| 264 | |
| 265 | // get all data |
| 266 | $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id ); |
| 267 | $options = Post_Views_Counter()->options['display']; |
| 268 | $views = pvc_get_post_views( $post_id ); |
| 269 | |
| 270 | // prepare display |
| 271 | $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 ); |
| 272 | // get icon |
| 273 | $icon_class = ( $options['icon_class'] !== '' ? esc_attr( $options['icon_class'] ) : '' ); |
| 274 | // add dashicons class if needed |
| 275 | $icon_class = strpos( $icon_class, 'dashicons' ) === 0 ? 'dashicons ' . $icon_class : $icon_class; |
| 276 | |
| 277 | $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon ' . $icon_class . '"></span>', $post_id ); |
| 278 | |
| 279 | $html = apply_filters( |
| 280 | 'pvc_post_views_html', '<div class="post-views post-' . $post_id . ' entry-meta"> |
| 281 | ' . ($options['display_style']['icon'] && $icon_class !== '' ? $icon : '') . ' |
| 282 | ' . ($options['display_style']['text'] ? '<span class="post-views-label">' . $label . ' </span>' : '') . ' |
| 283 | <span class="post-views-count">' . number_format_i18n( $views ) . '</span> |
| 284 | </div>', $post_id, $views, $label, $icon |
| 285 | ); |
| 286 | |
| 287 | if ( $echo ) |
| 288 | echo $html; |
| 289 | else |
| 290 | return $html; |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get most viewed posts. |
| 297 | * |
| 298 | * @param array $args |
| 299 | * @return array |
| 300 | */ |
| 301 | if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) { |
| 302 | |
| 303 | function pvc_get_most_viewed_posts( $args = array() ) { |
| 304 | $args = array_merge( |
| 305 | array( |
| 306 | 'posts_per_page' => 10, |
| 307 | 'order' => 'desc', |
| 308 | 'post_type' => 'post' |
| 309 | ), $args |
| 310 | ); |
| 311 | |
| 312 | $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args ); |
| 313 | |
| 314 | // force to use filters |
| 315 | $args['suppress_filters'] = false; |
| 316 | |
| 317 | // force to use post views as order |
| 318 | $args['orderby'] = 'post_views'; |
| 319 | |
| 320 | // force to get all fields |
| 321 | $args['fields'] = ''; |
| 322 | |
| 323 | return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args ); |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Display a list of most viewed posts. |
| 330 | * |
| 331 | * @param array $post_id |
| 332 | * @param bool $display |
| 333 | * @return mixed |
| 334 | */ |
| 335 | if ( ! function_exists( 'pvc_most_viewed_posts' ) ) { |
| 336 | |
| 337 | function pvc_most_viewed_posts( $args = array(), $display = true ) { |
| 338 | $defaults = array( |
| 339 | 'number_of_posts' => 5, |
| 340 | 'post_type' => array( 'post' ), |
| 341 | 'order' => 'desc', |
| 342 | 'thumbnail_size' => 'thumbnail', |
| 343 | 'list_type' => 'unordered', |
| 344 | 'show_post_views' => true, |
| 345 | 'show_post_thumbnail' => false, |
| 346 | 'show_post_author' => false, |
| 347 | 'show_post_excerpt' => false, |
| 348 | 'no_posts_message' => __( 'No Posts', 'post-views-counter' ), |
| 349 | 'item_before' => '', |
| 350 | 'item_after' => '' |
| 351 | ); |
| 352 | |
| 353 | $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) ); |
| 354 | |
| 355 | $args['show_post_views'] = (bool) $args['show_post_views']; |
| 356 | $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail']; |
| 357 | $args['show_post_author'] = (bool) $args['show_post_author']; |
| 358 | $args['show_post_excerpt'] = (bool) $args['show_post_excerpt']; |
| 359 | |
| 360 | $posts = pvc_get_most_viewed_posts( |
| 361 | array( |
| 362 | 'posts_per_page' => ( isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts'] ), |
| 363 | 'order' => ( isset( $args['order'] ) ? $args['order'] : $defaults['order'] ), |
| 364 | 'post_type' => ( isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'] ) |
| 365 | ) |
| 366 | ); |
| 367 | |
| 368 | if ( ! empty( $posts ) ) { |
| 369 | $html = ( $args['list_type'] === 'unordered' ? '<ul>' : '<ol>' ); |
| 370 | |
| 371 | foreach ( $posts as $post ) { |
| 372 | setup_postdata( $post ); |
| 373 | |
| 374 | $html .= ' |
| 375 | <li>'; |
| 376 | |
| 377 | $html .= apply_filters( 'pvc_most_viewed_posts_item_before', $args['item_before'], $post ); |
| 378 | |
| 379 | if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) { |
| 380 | $html .= ' |
| 381 | <span class="post-thumbnail"> |
| 382 | ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . ' |
| 383 | </span>'; |
| 384 | } |
| 385 | |
| 386 | $html .= ' |
| 387 | <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>' : '' ); |
| 388 | |
| 389 | $excerpt = ''; |
| 390 | |
| 391 | if ( $args['show_post_excerpt'] ) { |
| 392 | if ( empty( $post->post_excerpt ) ) |
| 393 | $text = $post->post_content; |
| 394 | else |
| 395 | $text = $post->post_excerpt; |
| 396 | |
| 397 | if ( ! empty( $text ) ) |
| 398 | $excerpt = wp_trim_words( str_replace( ']]>', ']]>', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[…]' ) ); |
| 399 | } |
| 400 | |
| 401 | if ( ! empty( $excerpt ) ) |
| 402 | $html .= ' |
| 403 | |
| 404 | <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>'; |
| 405 | |
| 406 | $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post ); |
| 407 | |
| 408 | $html .= ' |
| 409 | </li>'; |
| 410 | } |
| 411 | |
| 412 | wp_reset_postdata(); |
| 413 | |
| 414 | $html .= ( $args['list_type'] === 'unordered' ? '</ul>' : '</ol>' ); |
| 415 | } else |
| 416 | $html = $args['no_posts_message']; |
| 417 | |
| 418 | $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args ); |
| 419 | |
| 420 | if ( $display ) |
| 421 | echo $html; |
| 422 | else |
| 423 | return $html; |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * View post manually function. |
| 430 | * |
| 431 | * @since 1.2.0 |
| 432 | * @param int $post_id |
| 433 | * @return bool |
| 434 | */ |
| 435 | function pvc_view_post( $post_id = 0 ) { |
| 436 | $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id ); |
| 437 | |
| 438 | if ( ! $post_id ) |
| 439 | return false; |
| 440 | |
| 441 | Post_Views_Counter()->counter->check_post( $post_id ); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 |