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