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