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 |