PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.2.4
Post Views Counter v1.2.4
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
224 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 * @param int|array $post_id
19 * @return int
20 */
21 if ( ! function_exists( 'pvc_get_post_views' ) ) {
22
23 function pvc_get_post_views( $post_id = 0 ) {
24 if ( empty( $post_id ) )
25 $post_id = get_the_ID();
26
27 if ( is_array( $post_id ) )
28 $post_id = implode( ',', array_map( 'intval', $post_id ) );
29 else
30 $post_id = (int) $post_id;
31
32 global $wpdb;
33
34 $post_views = (int) $wpdb->get_var( "
35 SELECT SUM(count) AS views
36 FROM " . $wpdb->prefix . "post_views
37 WHERE id IN (" . $post_id . ") AND type = 4"
38 );
39
40 return apply_filters( 'pvc_get_post_views', $post_views, $post_id );
41 }
42
43 }
44
45 /**
46 * Display post views for a given post.
47 *
48 * @param int|array $post_id
49 * @param bool $display
50 * @return mixed
51 */
52 if ( ! function_exists( 'pvc_post_views' ) ) {
53
54 function pvc_post_views( $post_id = 0, $echo = true ) {
55
56 // get all data
57 $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
58 $options = Post_Views_Counter()->options['display'];
59 $views = pvc_get_post_views( $post_id );
60
61 // prepares display
62 $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 );
63 $icon_class = ($options['icon_class'] !== '' ? ' ' . esc_attr( $options['icon_class'] ) : '');
64 $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon dashicons ' . $icon_class . '"></span>', $post_id );
65
66 $html = apply_filters(
67 'pvc_post_views_html', '<div class="post-views post-' . $post_id . ' entry-meta">
68 ' . ($options['display_style']['icon'] && $icon_class !== '' ? $icon : '') . '
69 ' . ($options['display_style']['text'] ? '<span class="post-views-label">' . $label . ' </span>' : '') . '
70 <span class="post-views-count">' . number_format_i18n( $views ) . '</span>
71 </div>', $post_id, $views, $label, $icon
72 );
73
74 if ( $echo )
75 echo $html;
76 else
77 return $html;
78 }
79
80 }
81
82 /**
83 * Get most viewed posts.
84 *
85 * @param array $args
86 * @return array
87 */
88 if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
89
90 function pvc_get_most_viewed_posts( $args = array() ) {
91 $args = array_merge(
92 array(
93 'posts_per_page' => 10,
94 'order' => 'desc',
95 'post_type' => 'post'
96 ), $args
97 );
98
99 $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
100
101 // force to use filters
102 $args['suppress_filters'] = false;
103
104 // force to use post views as order
105 $args['orderby'] = 'post_views';
106
107 // force to get all fields
108 $args['fields'] = '';
109
110 return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args );
111 }
112
113 }
114
115 /**
116 * Display a list of most viewed posts.
117 *
118 * @param array $post_id
119 * @param bool $display
120 * @return mixed
121 */
122 if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
123
124 function pvc_most_viewed_posts( $args = array(), $display = true ) {
125 $defaults = array(
126 'number_of_posts' => 5,
127 'post_type' => array( 'post' ),
128 'order' => 'desc',
129 'thumbnail_size' => 'thumbnail',
130 'show_post_views' => true,
131 'show_post_thumbnail' => false,
132 'show_post_excerpt' => false,
133 'no_posts_message' => __( 'No Posts', 'post-views-counter' )
134 );
135
136 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
137
138 $args['show_post_views'] = (bool) $args['show_post_views'];
139 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
140 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
141
142 $posts = pvc_get_most_viewed_posts(
143 array(
144 'posts_per_page' => (isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts']),
145 'order' => (isset( $args['order'] ) ? $args['order'] : $defaults['order']),
146 'post_type' => (isset( $args['post_type'] ) ? $args['post_type'] : $defaults['post_type'])
147 )
148 );
149
150 if ( ! empty( $posts ) ) {
151 $html = '
152 <ul>';
153
154 foreach ( $posts as $post ) {
155 setup_postdata( $post );
156
157 $html .= '
158 <li>';
159
160 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
161 $html .= '
162 <span class="post-thumbnail">
163 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
164 </span>';
165 }
166
167 $html .= '
168 <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>' : '');
169
170 $excerpt = '';
171
172 if ( $args['show_post_excerpt'] ) {
173 if ( empty( $post->post_excerpt ) )
174 $text = $post->post_content;
175 else
176 $text = $post->post_excerpt;
177
178 if ( ! empty( $text ) )
179 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
180 }
181
182 if ( ! empty( $excerpt ) )
183 $html .= '
184 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
185
186 $html .= '
187 </li>';
188 }
189
190 wp_reset_postdata();
191
192 $html .= '
193 </ul>';
194 } else
195 $html = $args['no_posts_message'];
196
197 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
198
199 if ( $display )
200 echo $html;
201 else
202 return $html;
203 }
204
205 }
206
207 /**
208 * View post manually function.
209 *
210 * @since 1.2.0
211 * @param int $post_id
212 * @return bool
213 */
214 function pvc_view_post( $post_id = 0 ) {
215 $post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
216
217 if ( ! $post_id )
218 return false;
219
220 Post_Views_Counter()->counter->check_post( $post_id );
221
222 return true;
223 }
224