/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/API/Reports/Products/Stats/Segmenter.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 209 lines · 124 code · 24 blank · 61 comment · 7 complexity · 1813fba50ef338bcefd7dd9b8ac40a3a MD5 · raw file

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