PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.0.7
Post Views Counter v1.0.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 11 years ago counter.php 11 years ago cron.php 11 years ago frontend.php 11 years ago functions.php 11 years ago query.php 11 years ago settings.php 11 years ago update.php 11 years ago widgets.php 11 years ago
functions.php
198 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')) exit;
13
14 /**
15 * Get post views for a post or array of posts
16 *
17 * @param int|array $post_id
18 * @return int
19 */
20 if (!function_exists('pvc_get_post_views')) {
21 function pvc_get_post_views($post_id = 0) {
22 if (empty($post_id))
23 $post_id = get_the_ID();
24
25 if (is_array($post_id))
26 $post_id = implode(',', array_map('intval', $post_id));
27 else
28 $post_id = (int)$post_id;
29
30 global $wpdb;
31
32 $post_views = (int)$wpdb->get_var("
33 SELECT SUM(count) AS views
34 FROM ".$wpdb->prefix."post_views
35 WHERE id IN (".$post_id.") AND type = 4"
36 );
37
38 return apply_filters('pvc_get_post_views', $post_views, $post_id);
39 }
40 }
41
42 /**
43 * Display post views for a given post
44 *
45 * @param int|array $post_id
46 * @param bool $display
47 * @return mixed
48 */
49 if (!function_exists('pvc_post_views')) {
50 function pvc_post_views($post_id = 0, $display = true) {
51 // get all data
52 $post_id = (int)(empty($post_id) ? get_the_ID() : $post_id);
53 $options = Post_Views_Counter()->get_attribute('options', 'display');
54 $views = pvc_get_post_views($post_id);
55
56 // prepares display
57 $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);
58 $icon_class = ($options['icon_class'] !== '' ? ' '.esc_attr($options['icon_class']) : '');
59 $icon = apply_filters('pvc_post_views_icon', '<span class="post-views-icon dashicons '.$icon_class.'"></span>', $post_id);
60
61 $html = apply_filters(
62 'pvc_post_views_html',
63 '<div class="post-views post-'.$post_id.' entry-meta">
64 '.($options['display_style']['icon'] && $icon_class !== '' ? $icon : '').'
65 '.($options['display_style']['text'] ? '<span class="post-views-label">'.$label.' </span>' : '').'
66 <span class="post-views-count">'.number_format_i18n($views).'</span>
67 </div>',
68 $post_id,
69 $views,
70 $label,
71 $icon
72 );
73
74 if ($display)
75 echo $html;
76 else
77 return $html;
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 function pvc_get_most_viewed_posts($args = array()) {
89 $args = array_merge(
90 array(
91 'posts_per_page' => 10,
92 'order' => 'desc',
93 'post_type' => 'post'
94 ),
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 * Display a list of most viewed posts
110 *
111 * @param array $post_id
112 * @param bool $display
113 * @return mixed
114 */
115 if(!function_exists('pvc_most_viewed_posts')) {
116 function pvc_most_viewed_posts($args = array(), $display = true) {
117 $defaults = array(
118 'number_of_posts' => 5,
119 'post_types' => array('post'),
120 'order' => 'desc',
121 'thumbnail_size' => 'thumbnail',
122 'show_post_views' => true,
123 'show_post_thumbnail' => false,
124 'show_post_excerpt' => false,
125 'no_posts_message' => __('No Posts', 'post-views-counter')
126 );
127
128 $args = apply_filters('pvc_most_viewed_posts_args', wp_parse_args($args, $defaults));
129
130 $args['show_post_views'] = (bool)$args['show_post_views'];
131 $args['show_post_thumbnail'] = (bool)$args['show_post_thumbnail'];
132 $args['show_post_excerpt'] = (bool)$args['show_post_excerpt'];
133
134 $posts = pvc_get_most_viewed_posts(
135 array(
136 'posts_per_page' => (isset($args['number_of_posts']) ? (int)$args['number_of_posts'] : $defaults['number_of_posts']),
137 'order' => (isset($args['order']) ? $args['order'] : $defaults['order']),
138 'post_type' => (isset($args['post_types']) ? $args['post_types'] : $defaults['post_types'])
139 )
140 );
141
142 if(!empty($posts)) {
143 $html = '
144 <ul>';
145
146 foreach($posts as $post) {
147 setup_postdata($post);
148
149 $html .= '
150 <li>';
151
152 if($args['show_post_thumbnail'] && has_post_thumbnail($post->ID)) {
153 $html .= '
154 <span class="post-thumbnail">
155 '.get_the_post_thumbnail($post->ID, $args['thumbnail_size']).'
156 </span>';
157 }
158
159 $html .= '
160 <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>' : '');
161
162 $excerpt = '';
163
164 if($args['show_post_excerpt']) {
165 if(empty($post->post_excerpt))
166 $text = $post->post_content;
167 else
168 $text = $post->post_excerpt;
169
170 if(!empty($text))
171 $excerpt = wp_trim_words(str_replace(']]>', ']]&gt;', strip_shortcodes($text)), apply_filters('excerpt_length', 55), apply_filters('excerpt_more', ' '.'[&hellip;]'));
172 }
173
174 if(!empty($excerpt))
175 $html .= '
176 <div class="post-excerpt">'.esc_html($excerpt).'</div>';
177
178 $html .= '
179 </li>';
180 }
181
182 wp_reset_postdata();
183
184 $html .= '
185 </ul>';
186 }
187 else
188 $html = $args['no_posts_message'];
189
190 $html = apply_filters('pvc_most_viewed_posts_html', $html, $args);
191
192 if($display)
193 echo $html;
194 else
195 return $html;
196 }
197 }
198 ?>