PluginProbe
Stream – Activity Log & Audit Trail / 4.0.0
Stream – Activity Log & Audit Trail v4.0.0
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-query.php

class-query.php in Stream – Activity Log & Audit Trail 4.0.0, at classes/class-query.php

281 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Queries the database for stream records.
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Query
12 */
13 class Query {
14 /**
15 * Hold the number of records found
16 *
17 * @var int
18 */
19 public $found_records = 0;
20
21 /**
22 * Query records
23 *
24 * @param array $args Arguments to filter the records by.
25 *
26 * @return array Stream Records
27 */
28 public function query( $args ) {
29 global $wpdb;
30
31 $join = '';
32 $where = '';
33
34 /**
35 * PARSE CORE PARAMS
36 */
37 if ( is_numeric( $args['site_id'] ) ) {
38 $where .= $wpdb->prepare( " AND $wpdb->stream.site_id = %d", $args['site_id'] );
39 }
40
41 if ( is_numeric( $args['blog_id'] ) ) {
42 $where .= $wpdb->prepare( " AND $wpdb->stream.blog_id = %d", $args['blog_id'] );
43 }
44
45 if ( is_numeric( $args['object_id'] ) ) {
46 $where .= $wpdb->prepare( " AND $wpdb->stream.object_id = %d", $args['object_id'] );
47 }
48
49 if ( is_numeric( $args['user_id'] ) ) {
50 $where .= $wpdb->prepare( " AND $wpdb->stream.user_id = %d", $args['user_id'] );
51 }
52
53 if ( ! empty( $args['user_role'] ) ) {
54 $where .= $wpdb->prepare( " AND $wpdb->stream.user_role = %s", $args['user_role'] );
55 }
56
57 if ( ! empty( $args['search'] ) ) {
58 $field = ! empty( $args['search_field'] ) ? $args['search_field'] : 'summary';
59
60 // Sanitize field.
61 $allowed_fields = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
62 if ( in_array( $field, $allowed_fields, true ) ) {
63 $where .= $wpdb->prepare( " AND $wpdb->stream.{$field} LIKE %s", "%{$args['search']}%" ); // @codingStandardsIgnoreLine can't prepare column name
64 }
65 }
66
67 if ( ! empty( $args['connector'] ) ) {
68 $where .= $wpdb->prepare( " AND $wpdb->stream.connector = %s", $args['connector'] );
69 }
70
71 if ( ! empty( $args['context'] ) ) {
72 $where .= $wpdb->prepare( " AND $wpdb->stream.context = %s", $args['context'] );
73 }
74
75 if ( ! empty( $args['action'] ) ) {
76 $where .= $wpdb->prepare( " AND $wpdb->stream.action = %s", $args['action'] );
77 }
78
79 if ( ! empty( $args['ip'] ) ) {
80 $where .= $wpdb->prepare( " AND $wpdb->stream.ip = %s", wp_stream_filter_var( $args['ip'], FILTER_VALIDATE_IP ) );
81 }
82
83 /**
84 * PARSE DATE PARAM FAMILY
85 */
86 if ( ! empty( $args['date'] ) ) {
87 $args['date_from'] = $args['date'];
88 $args['date_to'] = $args['date'];
89 }
90
91 if ( ! empty( $args['date_from'] ) ) {
92 $date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_from'] . ' 00:00:00' ) ) );
93 $where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) >= %s", $date );
94 }
95
96 if ( ! empty( $args['date_to'] ) ) {
97 $date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_to'] . ' 23:59:59' ) ) );
98 $where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) <= %s", $date );
99 }
100
101 if ( ! empty( $args['date_after'] ) ) {
102 $date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_after'] ) ) );
103 $where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) > %s", $date );
104 }
105
106 if ( ! empty( $args['date_before'] ) ) {
107 $date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_before'] ) ) );
108 $where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) < %s", $date );
109 }
110
111 /**
112 * PARSE __IN PARAM FAMILY
113 */
114 $ins = array();
115
116 foreach ( $args as $arg => $value ) {
117 if ( '__in' === substr( $arg, -4 ) ) {
118 $ins[ $arg ] = $value;
119 }
120 }
121
122 if ( ! empty( $ins ) ) {
123 foreach ( $ins as $key => $value ) {
124 if ( empty( $value ) || ! is_array( $value ) ) {
125 continue;
126 }
127
128 $field = str_replace( array( 'record_', '__in' ), '', $key );
129 $field = empty( $field ) ? 'ID' : $field;
130 $type = is_numeric( array_shift( $value ) ) ? '%d' : '%s';
131
132 if ( ! empty( $value ) ) {
133 $format = '(' . join( ',', array_fill( 0, count( $value ), $type ) ) . ')';
134 $where .= $wpdb->prepare( " AND $wpdb->stream.%s IN {$format}", $field, $value ); // @codingStandardsIgnoreLine prepare okay
135 }
136 }
137 }
138
139 /**
140 * PARSE __NOT_IN PARAM FAMILY
141 */
142 $not_ins = array();
143
144 foreach ( $args as $arg => $value ) {
145 if ( '__not_in' === substr( $arg, -8 ) ) {
146 $not_ins[ $arg ] = $value;
147 }
148 }
149
150 if ( ! empty( $not_ins ) ) {
151 foreach ( $not_ins as $key => $value ) {
152 if ( empty( $value ) || ! is_array( $value ) ) {
153 continue;
154 }
155
156 $field = str_replace( array( 'record_', '__not_in' ), '', $key );
157 $field = empty( $field ) ? 'ID' : $field;
158 $type = is_numeric( array_shift( $value ) ) ? '%d' : '%s';
159
160 if ( ! empty( $value ) ) {
161 $format = '(' . join( ',', array_fill( 0, count( $value ), $type ) ) . ')';
162 $where .= $wpdb->prepare( " AND $wpdb->stream.%s NOT IN {$format}", $field, $value ); // @codingStandardsIgnoreLine prepare okay
163 }
164 }
165 }
166
167 /**
168 * PARSE PAGINATION PARAMS
169 */
170 $limits = '';
171 $page = absint( $args['paged'] );
172 $per_page = absint( $args['records_per_page'] );
173
174 if ( $per_page >= 0 ) {
175 $offset = absint( ( $page - 1 ) * $per_page );
176 $limits = "LIMIT {$offset}, {$per_page}";
177 }
178
179 /**
180 * PARSE ORDER PARAMS
181 */
182 $orderable = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'summary', 'created', 'connector', 'context', 'action' );
183
184 // Default to sorting by record ID.
185 $orderby = "$wpdb->stream.ID";
186
187 if ( in_array( $args['orderby'], $orderable, true ) ) {
188 $orderby = sprintf( '%s.%s', $wpdb->stream, $args['orderby'] );
189 } elseif ( 'meta_value_num' === $args['orderby'] && ! empty( $args['meta_key'] ) ) {
190 $orderby = "CAST($wpdb->streammeta.meta_value AS SIGNED)";
191 } elseif ( 'meta_value' === $args['orderby'] && ! empty( $args['meta_key'] ) ) {
192 $orderby = "$wpdb->streammeta.meta_value";
193 }
194
195 // Show the recent records first by default.
196 $order = 'DESC';
197 if ( 'ASC' === strtoupper( $args['order'] ) ) {
198 $order = 'ASC';
199 }
200
201 $orderby = sprintf( 'ORDER BY %s %s', $orderby, $order );
202
203 /**
204 * PARSE FIELDS PARAMETER
205 */
206 $fields = (array) $args['fields'];
207 $selects = array();
208
209 if ( ! empty( $fields ) ) {
210 foreach ( $fields as $field ) {
211 // We'll query the meta table later.
212 if ( 'meta' === $field ) {
213 continue;
214 }
215
216 $selects[] = sprintf( "$wpdb->stream.%s", $field );
217 }
218 } else {
219 $selects[] = "$wpdb->stream.*";
220 }
221
222 $select = implode( ', ', $selects );
223
224 /**
225 * Filters query WHERE statement as an alternative to filtering
226 * the $query using the hook below.
227 *
228 * @param string $where WHERE statement.
229 *
230 * @return string
231 */
232 $where = apply_filters( 'wp_stream_db_query_where', $where );
233
234 /**
235 * BUILD THE FINAL QUERY
236 */
237 $query = "SELECT {$select}
238 FROM $wpdb->stream
239 {$join}
240 WHERE 1=1 {$where}
241 {$orderby}
242 {$limits}";
243
244 /**
245 * Filter allows the final query to be modified before execution
246 *
247 * @param string $query
248 * @param array $args
249 *
250 * @return string
251 */
252 $query = apply_filters( 'wp_stream_db_query', $query, $args );
253
254 // Build result count query.
255 $count_query = "SELECT COUNT(*) as found
256 FROM $wpdb->stream
257 {$join}
258 WHERE 1=1 {$where}";
259
260 /**
261 * Filter allows the result count query to be modified before execution.
262 *
263 * @param string $query
264 * @param array $args
265 *
266 * @return string
267 */
268 $count_query = apply_filters( 'wp_stream_db_count_query', $count_query, $args );
269
270 /**
271 * QUERY THE DATABASE FOR RESULTS
272 */
273 $result = array(
274 'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
275 'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
276 );
277
278 return $result;
279 }
280 }
281