PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.2
Post Views Counter v1.1.2
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 10 years ago counter.php 10 years ago cron.php 10 years ago frontend.php 10 years ago functions.php 10 years ago query.php 10 years ago settings.php 10 years ago update.php 10 years ago widgets.php 10 years ago
functions.php
205 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
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, $display = true ) {
55 // get all data
56 $post_id = (int) (empty( $post_id ) ? get_the_ID() : $post_id);
57 $options = Post_Views_Counter()->get_attribute( 'options', 'display' );
58 $views = pvc_get_post_views( $post_id );
59
60 // prepares display
61 $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 );
62 $icon_class = ($options['icon_class'] !== '' ? ' ' . esc_attr( $options['icon_class'] ) : '');
63 $icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon dashicons ' . $icon_class . '"></span>', $post_id );
64
65 $html = apply_filters(
66 'pvc_post_views_html', '<div class="post-views post-' . $post_id . ' entry-meta">
67 ' . ($options['display_style']['icon'] && $icon_class !== '' ? $icon : '') . '
68 ' . ($options['display_style']['text'] ? '<span class="post-views-label">' . $label . ' </span>' : '') . '
69 <span class="post-views-count">' . number_format_i18n( $views ) . '</span>
70 </div>', $post_id, $views, $label, $icon
71 );
72
73 if ( $display )
74 echo $html;
75 else
76 return $html;
77 }
78
79 }
80
81 /**
82 * Get most viewed posts.
83 *
84 * @param array $args
85 * @return array
86 */
87 if ( ! function_exists( 'pvc_get_most_viewed_posts' ) ) {
88
89 function pvc_get_most_viewed_posts( $args = array() ) {
90 $args = array_merge(
91 array(
92 'posts_per_page' => 10,
93 'order' => 'desc',
94 'post_type' => 'post'
95 ), $args
96 );
97
98 $args = apply_filters( 'pvc_get_most_viewed_posts_args', $args );
99
100 // force to use filters
101 $args['suppress_filters'] = false;
102
103 // force to use post views as order
104 $args['orderby'] = 'post_views';
105
106 // force to get all fields
107 $args['fields'] = '';
108
109 return apply_filters( 'pvc_get_most_viewed_posts', get_posts( $args ), $args );
110 }
111
112 }
113
114 /**
115 * Display a list of most viewed posts.
116 *
117 * @param array $post_id
118 * @param bool $display
119 * @return mixed
120 */
121 if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
122
123 function pvc_most_viewed_posts( $args = array(), $display = true ) {
124 $defaults = array(
125 'number_of_posts' => 5,
126 'post_types' => array( 'post' ),
127 'order' => 'desc',
128 'thumbnail_size' => 'thumbnail',
129 'show_post_views' => true,
130 'show_post_thumbnail' => false,
131 'show_post_excerpt' => false,
132 'no_posts_message' => __( 'No Posts', 'post-views-counter' )
133 );
134
135 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
136
137 $args['show_post_views'] = (bool) $args['show_post_views'];
138 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
139 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
140
141 $posts = pvc_get_most_viewed_posts(
142 array(
143 'posts_per_page' => (isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts']),
144 'order' => (isset( $args['order'] ) ? $args['order'] : $defaults['order']),
145 'post_type' => (isset( $args['post_types'] ) ? $args['post_types'] : $defaults['post_types'])
146 )
147 );
148
149 if ( ! empty( $posts ) ) {
150 $html = '
151 <ul>';
152
153 foreach ( $posts as $post ) {
154 setup_postdata( $post );
155
156 $html .= '
157 <li>';
158
159 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
160 $html .= '
161 <span class="post-thumbnail">
162 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
163 </span>';
164 }
165
166 $html .= '
167 <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>' : '');
168
169 $excerpt = '';
170
171 if ( $args['show_post_excerpt'] ) {
172 if ( empty( $post->post_excerpt ) )
173 $text = $post->post_content;
174 else
175 $text = $post->post_excerpt;
176
177 if ( ! empty( $text ) )
178 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
179 }
180
181 if ( ! empty( $excerpt ) )
182 $html .= '
183 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
184
185 $html .= '
186 </li>';
187 }
188
189 wp_reset_postdata();
190
191 $html .= '
192 </ul>';
193 } else
194 $html = $args['no_posts_message'];
195
196 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
197
198 if ( $display )
199 echo $html;
200 else
201 return $html;
202 }
203
204 }
205