PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.1.4
Post Views Counter v1.1.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 / query.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
query.php
225 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Query class.
8 */
9 class Post_Views_Counter_Query {
10
11 public function __construct() {
12 // actions
13 add_action( 'pre_get_posts', array( $this, 'extend_pre_query' ), 1 );
14
15 // filters
16 add_filter( 'query_vars', array( $this, 'query_vars' ) );
17 add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 );
18 add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 2 );
19 add_filter( 'posts_orderby', array( $this, 'posts_orderby' ), 10, 2 );
20 add_filter( 'posts_fields', array( $this, 'posts_fields' ), 10, 2 );
21 }
22
23 /**
24 * Regiseter views_query var.
25 *
26 * @param array $query_vars
27 * @return array
28 */
29 public function query_vars( $query_vars ) {
30 $query_vars[] = 'views_query';
31
32 return $query_vars;
33 }
34
35 /**
36 * Extend query with post_views orderby parameter.
37 *
38 * @param object $query
39 */
40 public function extend_pre_query( $query ) {
41 if ( isset( $query->query_vars['orderby'] ) && $query->query_vars['orderby'] === 'post_views' )
42 $query->pvc_orderby = true;
43 }
44
45 /**
46 * Modify the db query to use post_views parameter.
47 *
48 * @global object $wpdb
49 * @param string $join
50 * @param object $query
51 * @return string
52 */
53 public function posts_join( $join, $query ) {
54 $sql = '';
55
56 if ( ! empty( $query->query['views_query'] ) ) {
57 if ( isset( $query->query['views_query']['year'] ) )
58 $year = (int)$query->query['views_query']['year'];
59
60 if ( isset( $query->query['views_query']['month'] ) )
61 $month = (int)$query->query['views_query']['month'];
62
63 if ( isset( $query->query['views_query']['week'] ) )
64 $week = (int)$query->query['views_query']['week'];
65
66 if ( isset( $query->query['views_query']['day'] ) )
67 $day = (int)$query->query['views_query']['day'];
68
69 // year
70 if ( isset( $year ) ) {
71 // year, week
72 if ( isset( $week ) && $this->is_valid_date( 'yw', $year, 0, 0, $week ) ) {
73 $sql = " AND pvc.type = 1 AND pvc.period = '" . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $week, 2, 0, STR_PAD_LEFT ) . "'";
74 // year, month
75 } elseif ( isset( $month ) ) {
76 // year, month, day
77 if ( isset( $day ) && $this->is_valid_date( 'ymd', $year, $month, $day ) ) {
78 $sql = " AND pvc.type = 0 AND pvc.period = '" . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $month, 2, 0, STR_PAD_LEFT ) . str_pad( $day, 2, 0, STR_PAD_LEFT ) . "'";
79 // year, month
80 } elseif ( $this->is_valid_date( 'ym', $year, $month ) ) {
81 $sql = " AND pvc.type = 2 AND pvc.period = '" . str_pad( $year, 4, 0, STR_PAD_LEFT ) . str_pad( $month, 2, 0, STR_PAD_LEFT ) . "'";
82 }
83 // year
84 } elseif ( $this->is_valid_date( 'y', $year ) ) {
85 $sql = " AND pvc.type = 3 AND pvc.period = '" . str_pad( $year, 4, 0, STR_PAD_LEFT ) . "'";
86 }
87 // month
88 } elseif ( isset( $month ) ) {
89 // month, day
90 if ( isset( $day ) && $this->is_valid_date( 'md', 0, $month, $day ) ) {
91 $sql = " AND pvc.type = 0 AND RIGHT( pvc.period, 4 ) = '" . str_pad( $month, 2, 0, STR_PAD_LEFT ) . str_pad( $day, 2, 0, STR_PAD_LEFT ) . "'";
92 // month
93 } elseif ( $this->is_valid_date( 'm', 0, $month ) ) {
94 $sql = " AND pvc.type = 2 AND RIGHT( pvc.period, 2 ) = '" . str_pad( $month, 2, 0, STR_PAD_LEFT ) . "'";
95 }
96 // week
97 } elseif ( isset( $week ) && $this->is_valid_date( 'w', 0, 0, 0, $week ) ) {
98 $sql = " AND pvc.type = 1 AND RIGHT( pvc.period, 2 ) = '" . str_pad( $week, 2, 0, STR_PAD_LEFT ) . "'";
99 // day
100 } elseif ( isset( $day ) && $this->is_valid_date( 'd', 0, 0, $day ) ) {
101 $sql = " AND pvc.type = 0 AND RIGHT( pvc.period, 2 ) = '" . str_pad( $day, 2, 0, STR_PAD_LEFT ) . "'";
102 }
103
104 if ( $sql !== '' )
105 $query->pvc_query = true;
106 }
107
108 // is it sorted by post views?
109 if ( ( $sql === '' && isset( $query->pvc_orderby ) && $query->pvc_orderby ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true )
110 $sql = ' AND pvc.type = 4';
111
112 // add date range
113 if ( $sql !== '' ) {
114 global $wpdb;
115
116 $join .= " LEFT JOIN " . $wpdb->prefix . "post_views pvc ON pvc.id = " . $wpdb->prefix . "posts.ID" . $sql;
117 }
118
119 return $join;
120 }
121
122 /**
123 * Group posts using the post ID.
124 *
125 * @global object $wpdb
126 * @param string $groupby
127 * @param object $query
128 * @return string
129 */
130 public function posts_groupby( $groupby, $query ) {
131 // is it sorted by post views or views_query is used?
132 if ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) || ( isset( $query->pvc_query ) && $query->pvc_query ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true ) {
133 global $wpdb;
134
135 $groupby = trim( $groupby );
136
137 if ( strpos( $groupby, $wpdb->prefix . 'posts.ID' ) === false )
138 $groupby = ( $groupby !== '' ? $groupby . ', ' : '') . $wpdb->prefix . 'posts.ID';
139 }
140
141 return $groupby;
142 }
143
144 /**
145 * Order posts by post views.
146 *
147 * @global object $wpdb
148 * @param string $orderby
149 * @param object $query
150 * @return string
151 */
152 public function posts_orderby( $orderby, $query ) {
153 // is it sorted by post views?
154 if ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) ) {
155 global $wpdb;
156
157 $order = $query->get( 'order' );
158 $orderby = 'pvc.count ' . $order . ', ' . $wpdb->prefix . 'posts.ID ' . $order;
159 }
160
161 return $orderby;
162 }
163
164 /**
165 * Return post views in queried post objects.
166 *
167 * @param string $fields
168 * @param object $query
169 * @return string
170 */
171 public function posts_fields( $fields, $query ) {
172 if ( ( ! isset( $query->query['fields'] ) || $query->query['fields'] === '' ) && ( ( isset( $query->pvc_orderby ) && $query->pvc_orderby ) || ( isset( $query->pvc_query ) && $query->pvc_query ) || apply_filters( 'pvc_extend_post_object', false, $query ) === true ) )
173 $fields = $fields . ', IFNULL( pvc.count, 0 ) AS post_views';
174
175 return $fields;
176 }
177
178 /**
179 * Validate date helper function.
180 *
181 * @param string $type
182 * @param int $year
183 * @param int $month
184 * @param int $day
185 * @param int $week
186 * @return bool
187 */
188 private function is_valid_date( $type, $year = 0, $month = 0, $day = 0, $week = 0 ) {
189 switch( $type ) {
190 case 'y':
191 $bool = ( $year >= 1 && $year <= 32767 );
192 break;
193
194 case 'yw':
195 $bool = ( $year >= 1 && $year <= 32767 && $week >= 0 && $week <= 53 );
196 break;
197
198 case 'ym':
199 $bool = ( $year >= 1 && $year <= 32767 && $month >= 1 && $month <= 12 );
200 break;
201
202 case 'ymd':
203 $bool = checkdate( $month, $day, $year );
204 break;
205
206 case 'm':
207 $bool = ( $month >= 1 && $month <= 12 );
208 break;
209
210 case 'md':
211 $bool = ( $month >= 1 && $month <= 12 && $day >= 1 && $day <= 31 );
212 break;
213
214 case 'w':
215 $bool = ( $week >= 0 && $week <= 53 );
216 break;
217
218 case 'd':
219 $bool = ( $day >= 1 && $day <= 31 );
220 break;
221 }
222
223 return $bool;
224 }
225 }