PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.0
Post Views Counter v1.1.0
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
200 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 // forces to use filters and post views as order
101 $args['suppress_filters'] = false;
102 $args['orderby'] = 'post_views';
103
104 return get_posts( $args );
105 }
106
107 }
108
109 /**
110 * Display a list of most viewed posts.
111 *
112 * @param array $post_id
113 * @param bool $display
114 * @return mixed
115 */
116 if ( ! function_exists( 'pvc_most_viewed_posts' ) ) {
117
118 function pvc_most_viewed_posts( $args = array(), $display = true ) {
119 $defaults = array(
120 'number_of_posts' => 5,
121 'post_types' => array( 'post' ),
122 'order' => 'desc',
123 'thumbnail_size' => 'thumbnail',
124 'show_post_views' => true,
125 'show_post_thumbnail' => false,
126 'show_post_excerpt' => false,
127 'no_posts_message' => __( 'No Posts', 'post-views-counter' )
128 );
129
130 $args = apply_filters( 'pvc_most_viewed_posts_args', wp_parse_args( $args, $defaults ) );
131
132 $args['show_post_views'] = (bool) $args['show_post_views'];
133 $args['show_post_thumbnail'] = (bool) $args['show_post_thumbnail'];
134 $args['show_post_excerpt'] = (bool) $args['show_post_excerpt'];
135
136 $posts = pvc_get_most_viewed_posts(
137 array(
138 'posts_per_page' => (isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $defaults['number_of_posts']),
139 'order' => (isset( $args['order'] ) ? $args['order'] : $defaults['order']),
140 'post_type' => (isset( $args['post_types'] ) ? $args['post_types'] : $defaults['post_types'])
141 )
142 );
143
144 if ( ! empty( $posts ) ) {
145 $html = '
146 <ul>';
147
148 foreach ( $posts as $post ) {
149 setup_postdata( $post );
150
151 $html .= '
152 <li>';
153
154 if ( $args['show_post_thumbnail'] && has_post_thumbnail( $post->ID ) ) {
155 $html .= '
156 <span class="post-thumbnail">
157 ' . get_the_post_thumbnail( $post->ID, $args['thumbnail_size'] ) . '
158 </span>';
159 }
160
161 $html .= '
162 <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>' : '');
163
164 $excerpt = '';
165
166 if ( $args['show_post_excerpt'] ) {
167 if ( empty( $post->post_excerpt ) )
168 $text = $post->post_content;
169 else
170 $text = $post->post_excerpt;
171
172 if ( ! empty( $text ) )
173 $excerpt = wp_trim_words( str_replace( ']]>', ']]&gt;', strip_shortcodes( $text ) ), apply_filters( 'excerpt_length', 55 ), apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ) );
174 }
175
176 if ( ! empty( $excerpt ) )
177 $html .= '
178 <div class="post-excerpt">' . esc_html( $excerpt ) . '</div>';
179
180 $html .= '
181 </li>';
182 }
183
184 wp_reset_postdata();
185
186 $html .= '
187 </ul>';
188 } else
189 $html = $args['no_posts_message'];
190
191 $html = apply_filters( 'pvc_most_viewed_posts_html', $html, $args );
192
193 if ( $display )
194 echo $html;
195 else
196 return $html;
197 }
198
199 }
200