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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / Downloads / DataStore.php
woocommerce / src / Admin / API / Reports / Downloads Last commit date
Files 4 years ago Stats 1 year ago Controller.php 1 year ago DataStore.php 1 year ago Query.php 1 year ago
DataStore.php
421 lines
1 <?php
2 /**
3 * API\Reports\Downloads\DataStore class file.
4 */
5
6 namespace Automattic\WooCommerce\Admin\API\Reports\Downloads;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
11 use Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
12 use Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
13 use Automattic\WooCommerce\Admin\API\Reports\SqlQuery;
14
15 /**
16 * API\Reports\Downloads\DataStore.
17 */
18 class DataStore extends ReportsDataStore implements DataStoreInterface {
19
20 /**
21 * Table used to get the data.
22 *
23 * @override ReportsDataStore::$table_name
24 *
25 * @var string
26 */
27 protected static $table_name = 'wc_download_log';
28
29 /**
30 * Cache identifier.
31 *
32 * @override ReportsDataStore::$cache_key
33 *
34 * @var string
35 */
36 protected $cache_key = 'downloads';
37
38 /**
39 * Mapping columns to data type to return correct response types.
40 *
41 * @override ReportsDataStore::$column_types
42 *
43 * @var array
44 */
45 protected $column_types = array(
46 'id' => 'intval',
47 'date' => 'strval',
48 'date_gmt' => 'strval',
49 'download_id' => 'strval', // String because this can sometimes be a hash.
50 'file_name' => 'strval',
51 'product_id' => 'intval',
52 'order_id' => 'intval',
53 'user_id' => 'intval',
54 'ip_address' => 'strval',
55 );
56
57 /**
58 * Data store context used to pass to filters.
59 *
60 * @override ReportsDataStore::$context
61 *
62 * @var string
63 */
64 protected $context = 'downloads';
65
66 /**
67 * Assign report columns once full table name has been assigned.
68 *
69 * @override ReportsDataStore::assign_report_columns()
70 */
71 protected function assign_report_columns() {
72 $this->report_columns = array(
73 'id' => 'download_log_id as id',
74 'date' => 'timestamp as date_gmt',
75 'download_id' => 'product_permissions.download_id',
76 'product_id' => 'product_permissions.product_id',
77 'order_id' => 'product_permissions.order_id',
78 'user_id' => 'product_permissions.user_id',
79 'ip_address' => 'user_ip_address as ip_address',
80 );
81 }
82
83 /**
84 * Updates the database query with parameters used for downloads report.
85 *
86 * @param array $query_args Query arguments supplied by the user.
87 */
88 protected function add_sql_query_params( $query_args ) {
89 global $wpdb;
90
91 $lookup_table = self::get_db_table_name();
92 $permission_table = $wpdb->prefix . 'woocommerce_downloadable_product_permissions';
93 $operator = $this->get_match_operator( $query_args );
94 $where_filters = array();
95 $join = "JOIN {$permission_table} as product_permissions ON {$lookup_table}.permission_id = product_permissions.permission_id";
96
97 $where_time = $this->add_time_period_sql_params( $query_args, $lookup_table );
98 if ( $where_time ) {
99 if ( isset( $this->subquery ) ) {
100 $this->subquery->add_sql_clause( 'where_time', $where_time );
101 } else {
102 $this->interval_query->add_sql_clause( 'where_time', $where_time );
103 }
104 }
105 $this->get_limit_sql_params( $query_args );
106
107 $where_filters[] = $this->get_object_where_filter(
108 $lookup_table,
109 'permission_id',
110 $permission_table,
111 'product_id',
112 'IN',
113 $this->get_included_products( $query_args )
114 );
115 $where_filters[] = $this->get_object_where_filter(
116 $lookup_table,
117 'permission_id',
118 $permission_table,
119 'product_id',
120 'NOT IN',
121 $this->get_excluded_products( $query_args )
122 );
123 $where_filters[] = $this->get_object_where_filter(
124 $lookup_table,
125 'permission_id',
126 $permission_table,
127 'order_id',
128 'IN',
129 $this->get_included_orders( $query_args )
130 );
131 $where_filters[] = $this->get_object_where_filter(
132 $lookup_table,
133 'permission_id',
134 $permission_table,
135 'order_id',
136 'NOT IN',
137 $this->get_excluded_orders( $query_args )
138 );
139
140 $customer_lookup_table = $wpdb->prefix . 'wc_customer_lookup';
141 $customer_lookup = "SELECT {$customer_lookup_table}.user_id FROM {$customer_lookup_table} WHERE {$customer_lookup_table}.customer_id IN (%s)";
142 $included_customers = $this->get_included_customers( $query_args );
143 $excluded_customers = $this->get_excluded_customers( $query_args );
144 if ( $included_customers ) {
145 $where_filters[] = $this->get_object_where_filter(
146 $lookup_table,
147 'permission_id',
148 $permission_table,
149 'user_id',
150 'IN',
151 sprintf( $customer_lookup, $included_customers )
152 );
153 }
154
155 if ( $excluded_customers ) {
156 $where_filters[] = $this->get_object_where_filter(
157 $lookup_table,
158 'permission_id',
159 $permission_table,
160 'user_id',
161 'NOT IN',
162 sprintf( $customer_lookup, $excluded_customers )
163 );
164 }
165
166 $included_ip_addresses = $this->get_included_ip_addresses( $query_args );
167 $excluded_ip_addresses = $this->get_excluded_ip_addresses( $query_args );
168 if ( $included_ip_addresses ) {
169 $where_filters[] = "{$lookup_table}.user_ip_address IN ('{$included_ip_addresses}')";
170 }
171
172 if ( $excluded_ip_addresses ) {
173 $where_filters[] = "{$lookup_table}.user_ip_address NOT IN ('{$excluded_ip_addresses}')";
174 }
175
176 $where_filters = array_filter( $where_filters );
177 $where_subclause = implode( " $operator ", $where_filters );
178 if ( $where_subclause ) {
179 if ( isset( $this->subquery ) ) {
180 $this->subquery->add_sql_clause( 'where', "AND ( $where_subclause )" );
181 } else {
182 $this->interval_query->add_sql_clause( 'where', "AND ( $where_subclause )" );
183 }
184 }
185
186 if ( isset( $this->subquery ) ) {
187 $this->subquery->add_sql_clause( 'join', $join );
188 } else {
189 $this->interval_query->add_sql_clause( 'join', $join );
190 }
191 $this->add_order_by( $query_args );
192 }
193
194 /**
195 * Returns comma separated ids of included ip address, based on query arguments from the user.
196 *
197 * @param array $query_args Parameters supplied by the user.
198 * @return string
199 */
200 protected function get_included_ip_addresses( $query_args ) {
201 return $this->get_filtered_ip_addresses( $query_args, 'ip_address_includes' );
202 }
203
204 /**
205 * Returns comma separated ids of excluded ip address, based on query arguments from the user.
206 *
207 * @param array $query_args Parameters supplied by the user.
208 * @return string
209 */
210 protected function get_excluded_ip_addresses( $query_args ) {
211 return $this->get_filtered_ip_addresses( $query_args, 'ip_address_excludes' );
212 }
213
214 /**
215 * Returns filtered comma separated ids, based on query arguments from the user.
216 *
217 * @param array $query_args Parameters supplied by the user.
218 * @param string $field Query field to filter.
219 * @return string
220 */
221 protected function get_filtered_ip_addresses( $query_args, $field ) {
222 if ( isset( $query_args[ $field ] ) && is_array( $query_args[ $field ] ) && count( $query_args[ $field ] ) > 0 ) {
223 $ip_addresses = array_map( 'esc_sql', $query_args[ $field ] );
224
225 /**
226 * Filter the IDs before retrieving report data.
227 *
228 * Allows filtering of the objects included or excluded from reports.
229 *
230 * @param array $ids List of object Ids.
231 * @param array $query_args The original arguments for the request.
232 * @param string $field The object type.
233 * @param string $context The data store context.
234 */
235 $ip_addresses = apply_filters( 'woocommerce_analytics_' . $field, $ip_addresses, $query_args, $field, $this->context );
236
237 return implode( "','", $ip_addresses );
238 }
239 return '';
240 }
241
242 /**
243 * Returns comma separated ids of included customers, based on query arguments from the user.
244 *
245 * @param array $query_args Parameters supplied by the user.
246 * @return string
247 */
248 protected function get_included_customers( $query_args ) {
249 return self::get_filtered_ids( $query_args, 'customer_includes' );
250 }
251
252 /**
253 * Returns comma separated ids of excluded customers, based on query arguments from the user.
254 *
255 * @param array $query_args Parameters supplied by the user.
256 * @return string
257 */
258 protected function get_excluded_customers( $query_args ) {
259 return self::get_filtered_ids( $query_args, 'customer_excludes' );
260 }
261
262 /**
263 * Gets WHERE time clause of SQL request with date-related constraints.
264 *
265 * @override ReportsDataStore::add_time_period_sql_params()
266 *
267 * @param array $query_args Parameters supplied by the user.
268 * @param string $table_name Name of the db table relevant for the date constraint.
269 * @return string
270 */
271 protected function add_time_period_sql_params( $query_args, $table_name ) {
272 $where_time = '';
273 if ( $query_args['before'] ) {
274 $datetime_str = $query_args['before']->format( TimeInterval::$sql_datetime_format );
275 $where_time .= " AND {$table_name}.timestamp <= '$datetime_str'";
276
277 }
278
279 if ( $query_args['after'] ) {
280 $datetime_str = $query_args['after']->format( TimeInterval::$sql_datetime_format );
281 $where_time .= " AND {$table_name}.timestamp >= '$datetime_str'";
282 }
283
284 return $where_time;
285 }
286
287 /**
288 * Fills ORDER BY clause of SQL request based on user supplied parameters.
289 *
290 * @param array $query_args Parameters supplied by the user.
291 */
292 protected function add_order_by( $query_args ) {
293 global $wpdb;
294 $this->clear_sql_clause( 'order_by' );
295 $order_by = '';
296 if ( isset( $query_args['orderby'] ) ) {
297 $order_by = $this->normalize_order_by( esc_sql( $query_args['orderby'] ) );
298 $this->add_sql_clause( 'order_by', $order_by );
299 }
300
301 if ( false !== strpos( $order_by, '_products' ) ) {
302 $this->subquery->add_sql_clause( 'join', "JOIN {$wpdb->posts} AS _products ON product_permissions.product_id = _products.ID" );
303 }
304
305 $this->add_orderby_order_clause( $query_args, $this );
306 }
307
308 /**
309 * Get the default query arguments to be used by get_data().
310 * These defaults are only partially applied when used via REST API, as that has its own defaults.
311 *
312 * @override ReportsDataStore::get_default_query_vars()
313 *
314 * @return array Query parameters.
315 */
316 public function get_default_query_vars() {
317 $defaults = parent::get_default_query_vars();
318 $defaults['orderby'] = 'timestamp';
319
320 return $defaults;
321 }
322
323 /**
324 * Returns the report data based on normalized parameters.
325 * Will be called by `get_data` if there is no data in cache.
326 *
327 * @override ReportsDataStore::get_noncached_data()
328 *
329 * @see get_data
330 * @param array $query_args Query parameters.
331 * @return stdClass|WP_Error Data object `{ totals: *, intervals: array, total: int, pages: int, page_no: int }`, or error.
332 */
333 public function get_noncached_data( $query_args ) {
334 global $wpdb;
335
336 $this->initialize_queries();
337
338 $data = (object) array(
339 'data' => array(),
340 'total' => 0,
341 'pages' => 0,
342 'page_no' => 0,
343 );
344
345 $selections = $this->selected_columns( $query_args );
346 $this->add_sql_query_params( $query_args );
347
348 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
349 $db_records_count = (int) $wpdb->get_var(
350 "SELECT COUNT(*) FROM (
351 {$this->subquery->get_query_statement()}
352 ) AS tt"
353 );
354 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
355
356 $params = $this->get_limit_params( $query_args );
357 $total_pages = (int) ceil( $db_records_count / $params['per_page'] );
358 if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
359 return $data;
360 }
361
362 $this->subquery->clear_sql_clause( 'select' );
363 $this->subquery->add_sql_clause( 'select', $selections );
364 $this->subquery->add_sql_clause( 'order_by', $this->get_sql_clause( 'order_by' ) );
365 $this->subquery->add_sql_clause( 'limit', $this->get_sql_clause( 'limit' ) );
366
367 $download_data = $wpdb->get_results(
368 $this->subquery->get_query_statement(), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
369 ARRAY_A
370 );
371
372 if ( null === $download_data ) {
373 return $data;
374 }
375
376 $download_data = array_map( array( $this, 'cast_numbers' ), $download_data );
377 $data = (object) array(
378 'data' => $download_data,
379 'total' => $db_records_count,
380 'pages' => $total_pages,
381 'page_no' => (int) $query_args['page'],
382 );
383
384 return $data;
385 }
386
387 /**
388 * Maps ordering specified by the user to columns in the database/fields in the data.
389 *
390 * @override ReportsDataStore::normalize_order_by()
391 *
392 * @param string $order_by Sorting criterion.
393 * @return string
394 */
395 protected function normalize_order_by( $order_by ) {
396 global $wpdb;
397
398 if ( 'date' === $order_by ) {
399 return $wpdb->prefix . 'wc_download_log.timestamp';
400 }
401
402 if ( 'product' === $order_by ) {
403 return '_products.post_title';
404 }
405
406 return $order_by;
407 }
408
409 /**
410 * Initialize query objects.
411 */
412 protected function initialize_queries() {
413 $this->clear_all_clauses();
414 $table_name = self::get_db_table_name();
415 $this->subquery = new SqlQuery( $this->context . '_subquery' );
416 $this->subquery->add_sql_clause( 'from', $table_name );
417 $this->subquery->add_sql_clause( 'select', "{$table_name}.download_log_id" );
418 $this->subquery->add_sql_clause( 'group_by', "{$table_name}.download_log_id" );
419 }
420 }
421