Segmenter.php
181 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for adding segmenting support without cluttering the data stores. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports\Variations\Stats; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Segmenter as ReportsSegmenter; |
| 11 | use Automattic\WooCommerce\Admin\API\Reports\ParameterException; |
| 12 | |
| 13 | /** |
| 14 | * Date & time interval and numeric range handling class for Reporting API. |
| 15 | */ |
| 16 | class Segmenter extends ReportsSegmenter { |
| 17 | |
| 18 | /** |
| 19 | * Returns column => query mapping to be used for product-related product-level segmenting query |
| 20 | * (e.g. products sold, revenue from product X when segmenting by category). |
| 21 | * |
| 22 | * @param string $products_table Name of SQL table containing the product-level segmenting info. |
| 23 | * |
| 24 | * @return array Column => SELECT query mapping. |
| 25 | */ |
| 26 | protected function get_segment_selections_product_level( $products_table ) { |
| 27 | $columns_mapping = array( |
| 28 | 'items_sold' => "SUM($products_table.product_qty) as items_sold", |
| 29 | 'net_revenue' => "SUM($products_table.product_net_revenue ) AS net_revenue", |
| 30 | 'orders_count' => "COUNT( DISTINCT $products_table.order_id ) AS orders_count", |
| 31 | 'variations_count' => "COUNT( DISTINCT $products_table.variation_id ) AS variations_count", |
| 32 | ); |
| 33 | |
| 34 | return $columns_mapping; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Calculate segments for totals where the segmenting property is bound to product (e.g. category, product_id, variation_id). |
| 39 | * |
| 40 | * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. |
| 41 | * @param string $segmenting_from FROM part of segmenting SQL query. |
| 42 | * @param string $segmenting_where WHERE part of segmenting SQL query. |
| 43 | * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. |
| 44 | * @param string $segmenting_dimension_name Name of the segmenting dimension. |
| 45 | * @param string $table_name Name of SQL table which is the stats table for orders. |
| 46 | * @param array $totals_query Array of SQL clauses for totals query. |
| 47 | * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | protected function get_product_related_totals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $totals_query, $unique_orders_table ) { |
| 52 | global $wpdb; |
| 53 | |
| 54 | $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; |
| 55 | |
| 56 | // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). |
| 57 | // Product-level numbers. |
| 58 | /* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */ |
| 59 | $segments_products = $wpdb->get_results( |
| 60 | "SELECT |
| 61 | $segmenting_groupby AS $segmenting_dimension_name |
| 62 | {$segmenting_selections['product_level']} |
| 63 | FROM |
| 64 | $table_name |
| 65 | $segmenting_from |
| 66 | {$totals_query['from_clause']} |
| 67 | WHERE |
| 68 | 1=1 |
| 69 | {$totals_query['where_time_clause']} |
| 70 | {$totals_query['where_clause']} |
| 71 | $segmenting_where |
| 72 | GROUP BY |
| 73 | $segmenting_groupby", |
| 74 | ARRAY_A |
| 75 | ); |
| 76 | /* phpcs:enable */ |
| 77 | |
| 78 | $totals_segments = $this->merge_segment_totals_results( $segmenting_dimension_name, $segments_products, array() ); |
| 79 | return $totals_segments; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Calculate segments for intervals where the segmenting property is bound to product (e.g. category, product_id, variation_id). |
| 84 | * |
| 85 | * @param array $segmenting_selections SELECT part of segmenting SQL query--one for 'product_level' and one for 'order_level'. |
| 86 | * @param string $segmenting_from FROM part of segmenting SQL query. |
| 87 | * @param string $segmenting_where WHERE part of segmenting SQL query. |
| 88 | * @param string $segmenting_groupby GROUP BY part of segmenting SQL query. |
| 89 | * @param string $segmenting_dimension_name Name of the segmenting dimension. |
| 90 | * @param string $table_name Name of SQL table which is the stats table for orders. |
| 91 | * @param array $intervals_query Array of SQL clauses for intervals query. |
| 92 | * @param string $unique_orders_table Name of temporary SQL table that holds unique orders. |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | protected function get_product_related_intervals_segments( $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $intervals_query, $unique_orders_table ) { |
| 97 | global $wpdb; |
| 98 | |
| 99 | $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; |
| 100 | |
| 101 | // LIMIT offset, rowcount needs to be updated to a multiple of the number of segments. |
| 102 | preg_match( '/LIMIT (\d+)\s?,\s?(\d+)/', $intervals_query['limit'], $limit_parts ); |
| 103 | $segment_count = count( $this->get_all_segments() ); |
| 104 | $orig_offset = intval( $limit_parts[1] ); |
| 105 | $orig_rowcount = intval( $limit_parts[2] ); |
| 106 | $segmenting_limit = $wpdb->prepare( 'LIMIT %d, %d', $orig_offset * $segment_count, $orig_rowcount * $segment_count ); |
| 107 | |
| 108 | // Can't get all the numbers from one query, so split it into one query for product-level numbers and one for order-level numbers (which first need to have orders uniqued). |
| 109 | // Product-level numbers. |
| 110 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 111 | $segments_products = $wpdb->get_results( |
| 112 | "SELECT |
| 113 | {$intervals_query['select_clause']} AS time_interval, |
| 114 | $segmenting_groupby AS $segmenting_dimension_name |
| 115 | {$segmenting_selections['product_level']} |
| 116 | FROM |
| 117 | $table_name |
| 118 | $segmenting_from |
| 119 | {$intervals_query['from_clause']} |
| 120 | WHERE |
| 121 | 1=1 |
| 122 | {$intervals_query['where_time_clause']} |
| 123 | {$intervals_query['where_clause']} |
| 124 | $segmenting_where |
| 125 | GROUP BY |
| 126 | time_interval, $segmenting_groupby |
| 127 | $segmenting_limit", |
| 128 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 129 | ARRAY_A |
| 130 | ); |
| 131 | |
| 132 | $intervals_segments = $this->merge_segment_intervals_results( $segmenting_dimension_name, $segments_products, array() ); |
| 133 | return $intervals_segments; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Return array of segments formatted for REST response. |
| 138 | * |
| 139 | * @param string $type Type of segments to return--'totals' or 'intervals'. |
| 140 | * @param array $query_params SQL query parameter array. |
| 141 | * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). |
| 142 | * |
| 143 | * @return array|null |
| 144 | * @throws \Automattic\WooCommerce\Admin\API\Reports\ParameterException In case of segmenting by variations, when no parent product is specified. |
| 145 | */ |
| 146 | protected function get_segments( $type, $query_params, $table_name ) { |
| 147 | global $wpdb; |
| 148 | if ( ! isset( $this->query_args['segmentby'] ) || '' === $this->query_args['segmentby'] ) { |
| 149 | return array(); |
| 150 | } |
| 151 | $segments = null; |
| 152 | $product_segmenting_table = $wpdb->prefix . 'wc_order_product_lookup'; |
| 153 | $unique_orders_table = 'uniq_orders'; |
| 154 | $segmenting_where = ''; |
| 155 | |
| 156 | // Product, variation, and category are bound to product, so here product segmenting table is required, |
| 157 | // while coupon and customer are bound to order, so we don't need the extra JOIN for those. |
| 158 | // This also means that segment selections need to be calculated differently. |
| 159 | if ( 'variation' === $this->query_args['segmentby'] ) { |
| 160 | $product_level_columns = $this->get_segment_selections_product_level( $product_segmenting_table ); |
| 161 | $segmenting_selections = array( |
| 162 | 'product_level' => $this->prepare_selections( $product_level_columns ), |
| 163 | ); |
| 164 | $this->report_columns = $product_level_columns; |
| 165 | $segmenting_from = ''; |
| 166 | $segmenting_groupby = $product_segmenting_table . '.variation_id'; |
| 167 | $segmenting_dimension_name = 'variation_id'; |
| 168 | |
| 169 | // Restrict our search space for variation comparisons. |
| 170 | if ( isset( $this->query_args['variation_includes'] ) ) { |
| 171 | $variation_ids = implode( ',', $this->get_all_segments() ); |
| 172 | $segmenting_where = " AND $product_segmenting_table.variation_id IN ( $variation_ids )"; |
| 173 | } |
| 174 | |
| 175 | $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); |
| 176 | } |
| 177 | |
| 178 | return $segments; |
| 179 | } |
| 180 | } |
| 181 |