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