PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.12
Post Views Counter v1.2.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
columns.php 8 years ago counter.php 8 years ago crawler-detect.php 8 years ago cron.php 8 years ago dashboard.php 8 years ago frontend.php 8 years ago functions.php 8 years ago query.php 8 years ago settings.php 8 years ago update.php 8 years ago widgets.php 8 years ago
functions.php
444 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 'show_post_views' => true,
344 'show_post_thumbnail' => false,
345 'show_post_excerpt' => false,
346 'no_posts_message' => __( 'No Posts', 'post-views-counter' ),
347 'item_before' => '',
348 'item_after' => ''
349 );
350
351 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
352
353 $args['show_post_views'] = (bool) $args['show_post_views'];
354 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
355 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
356
357 $posts = pvc_get_most_viewed_posts(
358 array(
359 'posts_per_page' => (isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts']),
360 'order' => (isset( $args['order'] ) ? $args['order'] : $defaults['order']),
361 'post_type' => (isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'])
362 )
363 );
364
365 if ( ! empty( $posts ) ) {
366 $html = '
367 <ul>';
368
369 foreach ( $posts as $post ) {
370 setup_postdata( $post );
371
372 $html .= '
373 <li>';
374
375 $html .= apply_filters( 'pvc_most_viewed_posts_item_before', $args['item_before'], $post );
376
377 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
378 $html .= '
379 <span class="post-thumbnail">
380 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
381 </span>';
382 }
383
384 $html .= '
385 <a class="post-title" href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a>' . ($args['show_post_views'] ? ' <span class="count">(' . number_format_i18n( pvc_get_post_views( $post->ID ) ) . ')</span>' : '');
386
387 $excerpt = '';
388
389 if ( $args['show_post_excerpt'] ) {
390 if ( empty( $post->post_excerpt ) )
391 $text = $post->post_content;
392 else
393 $text = $post->post_excerpt;
394
395 if ( ! empty( $text ) )
396 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
397 }
398
399 if ( ! empty( $excerpt ) )
400 $html .= '
401
402 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
403
404 $html .= apply_filters( 'pvc_most_viewed_posts_item_after', $args['item_after'], $post );
405
406 $html .= '
407 </li>';
408 }
409
410 wp_reset_postdata();
411
412 $html .= '
413 </ul>';
414 } else
415 $html = $args['no_posts_message'];
416
417 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
418
419 if ( $display )
420 echo $html;
421 else
422 return $html;
423 }
424
425 }
426
427 /**
428 * View post manually function.
429 *
430 * @since 1.2.0
431 * @param int $post_id
432 * @return bool
433 */
434 function pvc_view_post( $post_id = 0 ) {
435 $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
436
437 if ( ! $post_id )
438 return false;
439
440 Post_Views_Counter()->counter->check_post( $post_id );
441
442 return true;
443 }
444