WC_Admin_Report

Admin Report.

Defined (1)

The class is defined in the following location(s).

/includes/admin/reports/class-wc-admin-report.php  
  1. class WC_Admin_Report { 
  2.  
  3. /** 
  4. * The chart interval. 
  5. * @var int 
  6. */ 
  7. public $chart_interval; 
  8.  
  9. /** 
  10. * Group by SQL query. 
  11. * @var string 
  12. */ 
  13. public $group_by_query; 
  14.  
  15. /** 
  16. * The bar width. 
  17. * @var int 
  18. */ 
  19. public $barwidth; 
  20.  
  21. /** 
  22. * Group chart item by day or month. 
  23. * @var string 
  24. */ 
  25. public $chart_groupby; 
  26.  
  27. /** 
  28. * The start date of the report. 
  29. * @var int timestamp 
  30. */ 
  31. public $start_date; 
  32.  
  33. /** 
  34. * The end date of the report. 
  35. * @var int timestamp 
  36. */ 
  37. public $end_date; 
  38.  
  39. /** 
  40. * Get report totals such as order totals and discount amounts. 
  41. * Data example: 
  42. * '_order_total' => array( 
  43. * 'type' => 'meta',  
  44. * 'function' => 'SUM',  
  45. * 'name' => 'total_sales' 
  46. * ) 
  47. * @param array $args 
  48. * @return mixed depending on query_type 
  49. */ 
  50. public function get_order_report_data( $args = array() ) { 
  51. global $wpdb; 
  52.  
  53. $default_args = array( 
  54. 'data' => array(),  
  55. 'where' => array(),  
  56. 'where_meta' => array(),  
  57. 'query_type' => 'get_row',  
  58. 'group_by' => '',  
  59. 'order_by' => '',  
  60. 'limit' => '',  
  61. 'filter_range' => false,  
  62. 'nocache' => false,  
  63. 'debug' => false,  
  64. 'order_types' => wc_get_order_types( 'reports' ),  
  65. 'order_status' => array( 'completed', 'processing', 'on-hold' ),  
  66. 'parent_order_status' => false,  
  67. ); 
  68. $args = apply_filters( 'woocommerce_reports_get_order_report_data_args', $args ); 
  69. $args = wp_parse_args( $args, $default_args ); 
  70.  
  71. extract( $args ); 
  72.  
  73. if ( empty( $data ) ) { 
  74. return ''; 
  75.  
  76. $order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status ); 
  77.  
  78. $query = array(); 
  79. $select = array(); 
  80.  
  81. foreach ( $data as $key => $value ) { 
  82. $distinct = ''; 
  83.  
  84. if ( isset( $value['distinct'] ) ) { 
  85. $distinct = 'DISTINCT'; 
  86.  
  87. switch ( $value['type'] ) { 
  88. case 'meta' : 
  89. $get_key = "meta_{$key}.meta_value"; 
  90. break; 
  91. case 'parent_meta' : 
  92. $get_key = "parent_meta_{$key}.meta_value"; 
  93. break; 
  94. case 'post_data' : 
  95. $get_key = "posts.{$key}"; 
  96. break; 
  97. case 'order_item_meta' : 
  98. $get_key = "order_item_meta_{$key}.meta_value"; 
  99. break; 
  100. case 'order_item' : 
  101. $get_key = "order_items.{$key}"; 
  102. break; 
  103. default : 
  104. continue; 
  105.  
  106. if ( $value['function'] ) { 
  107. $get = "{$value['function']}({$distinct} {$get_key})"; 
  108. } else { 
  109. $get = "{$distinct} {$get_key}"; 
  110.  
  111. $select[] = "{$get} as {$value['name']}"; 
  112.  
  113. $query['select'] = "SELECT " . implode( ', ', $select ); 
  114. $query['from'] = "FROM {$wpdb->posts} AS posts"; 
  115.  
  116. // Joins 
  117. $joins = array(); 
  118.  
  119. foreach ( ( $data + $where ) as $key => $value ) { 
  120. $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; 
  121. $type = isset( $value['type'] ) ? $value['type'] : false; 
  122.  
  123. switch ( $type ) { 
  124. case 'meta' : 
  125. $joins["meta_{$key}"] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON ( posts.ID = meta_{$key}.post_id AND meta_{$key}.meta_key = '{$key}' )"; 
  126. break; 
  127. case 'parent_meta' : 
  128. $joins["parent_meta_{$key}"] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$key}')"; 
  129. break; 
  130. case 'order_item_meta' : 
  131. $joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON (posts.ID = order_items.order_id)"; 
  132.  
  133. if ( ! empty( $value['order_item_type'] ) ) { 
  134. $joins["order_items"] .= " AND (order_items.order_item_type = '{$value['order_item_type']}')"; 
  135.  
  136. $joins["order_item_meta_{$key}"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON " . 
  137. "(order_items.order_item_id = order_item_meta_{$key}.order_item_id) " . 
  138. " AND (order_item_meta_{$key}.meta_key = '{$key}')"; 
  139. break; 
  140. case 'order_item' : 
  141. $joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id"; 
  142. break; 
  143.  
  144. if ( ! empty( $where_meta ) ) { 
  145. foreach ( $where_meta as $value ) { 
  146. if ( ! is_array( $value ) ) { 
  147. continue; 
  148. $join_type = isset( $value['join_type'] ) ? $value['join_type'] : 'INNER'; 
  149. $type = isset( $value['type'] ) ? $value['type'] : false; 
  150. $key = is_array( $value['meta_key'] ) ? $value['meta_key'][0] . '_array' : $value['meta_key']; 
  151.  
  152. if ( 'order_item_meta' === $type ) { 
  153.  
  154. $joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id"; 
  155. $joins["order_item_meta_{$key}"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items.order_item_id = order_item_meta_{$key}.order_item_id"; 
  156.  
  157. } else { 
  158. // If we have a where clause for meta, join the postmeta table 
  159. $joins["meta_{$key}"] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON posts.ID = meta_{$key}.post_id"; 
  160.  
  161. if ( ! empty( $parent_order_status ) ) { 
  162. $joins["parent"] = "LEFT JOIN {$wpdb->posts} AS parent ON posts.post_parent = parent.ID"; 
  163.  
  164. $query['join'] = implode( ' ', $joins ); 
  165.  
  166. $query['where'] = " 
  167. WHERE posts.post_type IN ( '" . implode( "', '", $order_types ) . "' ) 
  168. "; 
  169.  
  170. if ( ! empty( $order_status ) ) { 
  171. $query['where'] .= " 
  172. AND posts.post_status IN ( 'wc-" . implode( "', 'wc-", $order_status ) . "') 
  173. "; 
  174.  
  175. if ( ! empty( $parent_order_status ) ) { 
  176. if ( ! empty( $order_status ) ) { 
  177. $query['where'] .= " AND ( parent.post_status IN ( 'wc-" . implode( "', 'wc-", $parent_order_status ) . "') OR parent.ID IS NULL ) "; 
  178. } else { 
  179. $query['where'] .= " AND parent.post_status IN ( 'wc-" . implode( "', 'wc-", $parent_order_status ) . "') "; 
  180.  
  181. if ( $filter_range ) { 
  182.  
  183. $query['where'] .= " 
  184. AND posts.post_date >= '" . date('Y-m-d', $this->start_date ) . "' 
  185. AND posts.post_date < '" . date('Y-m-d', strtotime( '+1 DAY', $this->end_date ) ) . "' 
  186. "; 
  187.  
  188.  
  189. if ( ! empty( $where_meta ) ) { 
  190.  
  191. $relation = isset( $where_meta['relation'] ) ? $where_meta['relation'] : 'AND'; 
  192.  
  193. $query['where'] .= " AND ("; 
  194.  
  195. foreach ( $where_meta as $index => $value ) { 
  196.  
  197. if ( ! is_array( $value ) ) { 
  198. continue; 
  199.  
  200. $key = is_array( $value['meta_key'] ) ? $value['meta_key'][0] . '_array' : $value['meta_key']; 
  201.  
  202. if ( strtolower( $value['operator'] ) == 'in' ) { 
  203.  
  204. if ( is_array( $value['meta_value'] ) ) { 
  205. $value['meta_value'] = implode( "', '", $value['meta_value'] ); 
  206.  
  207. if ( ! empty( $value['meta_value'] ) ) { 
  208. $where_value = "IN ('{$value['meta_value']}')"; 
  209. } else { 
  210. $where_value = "{$value['operator']} '{$value['meta_value']}'"; 
  211.  
  212. if ( ! empty( $where_value ) ) { 
  213. if ( $index > 0 ) { 
  214. $query['where'] .= ' ' . $relation; 
  215.  
  216. if ( isset( $value['type'] ) && $value['type'] == 'order_item_meta' ) { 
  217.  
  218. if ( is_array( $value['meta_key'] ) ) { 
  219. $query['where'] .= " ( order_item_meta_{$key}.meta_key IN ('" . implode( "', '", $value['meta_key'] ) . "')"; 
  220. } else { 
  221. $query['where'] .= " ( order_item_meta_{$key}.meta_key = '{$value['meta_key']}'"; 
  222.  
  223. $query['where'] .= " AND order_item_meta_{$key}.meta_value {$where_value} )"; 
  224. } else { 
  225.  
  226. if ( is_array( $value['meta_key'] ) ) { 
  227. $query['where'] .= " ( meta_{$key}.meta_key IN ('" . implode( "', '", $value['meta_key'] ) . "')"; 
  228. } else { 
  229. $query['where'] .= " ( meta_{$key}.meta_key = '{$value['meta_key']}'"; 
  230.  
  231. $query['where'] .= " AND meta_{$key}.meta_value {$where_value} )"; 
  232.  
  233. $query['where'] .= ")"; 
  234.  
  235. if ( ! empty( $where ) ) { 
  236.  
  237. foreach ( $where as $value ) { 
  238.  
  239. if ( strtolower( $value['operator'] ) == 'in' ) { 
  240.  
  241. if ( is_array( $value['value'] ) ) { 
  242. $value['value'] = implode( "', '", $value['value'] ); 
  243.  
  244. if ( ! empty( $value['value'] ) ) { 
  245. $where_value = "IN ('{$value['value']}')"; 
  246. } else { 
  247. $where_value = "{$value['operator']} '{$value['value']}'"; 
  248.  
  249. if ( ! empty( $where_value ) ) 
  250. $query['where'] .= " AND {$value['key']} {$where_value}"; 
  251.  
  252. if ( $group_by ) { 
  253. $query['group_by'] = "GROUP BY {$group_by}"; 
  254.  
  255. if ( $order_by ) { 
  256. $query['order_by'] = "ORDER BY {$order_by}"; 
  257.  
  258. if ( $limit ) { 
  259. $query['limit'] = "LIMIT {$limit}"; 
  260.  
  261. $query = apply_filters( 'woocommerce_reports_get_order_report_query', $query ); 
  262. $query = implode( ' ', $query ); 
  263. $query_hash = md5( $query_type . $query ); 
  264. $cached_results = get_transient( strtolower( get_class( $this ) ) ); 
  265.  
  266. if ( $debug ) { 
  267. echo '<pre>'; 
  268. print_r( $query ); 
  269. echo '</pre>'; 
  270.  
  271. if ( $debug || $nocache || false === $cached_results || ! isset( $cached_results[ $query_hash ] ) ) { 
  272. // Enable big selects for reports 
  273. $wpdb->query( 'SET SESSION SQL_BIG_SELECTS=1' ); 
  274. $cached_results[ $query_hash ] = apply_filters( 'woocommerce_reports_get_order_report_data', $wpdb->$query_type( $query ), $data ); 
  275. set_transient( strtolower( get_class( $this ) ), $cached_results, DAY_IN_SECONDS ); 
  276.  
  277. $result = $cached_results[ $query_hash ]; 
  278.  
  279. return $result; 
  280.  
  281. /** 
  282. * Put data with post_date's into an array of times. 
  283. * @param array $data array of your data 
  284. * @param string $date_key key for the 'date' field. e.g. 'post_date' 
  285. * @param string $data_key key for the data you are charting 
  286. * @param int $interval 
  287. * @param string $start_date 
  288. * @param string $group_by 
  289. * @return array 
  290. */ 
  291. public function prepare_chart_data( $data, $date_key, $data_key, $interval, $start_date, $group_by ) { 
  292. $prepared_data = array(); 
  293.  
  294. // Ensure all days (or months) have values first in this range 
  295. for ( $i = 0; $i <= $interval; $i ++ ) { 
  296. switch ( $group_by ) { 
  297. case 'day' : 
  298. $time = strtotime( date( 'Ymd', strtotime( "+{$i} DAY", $start_date ) ) ) . '000'; 
  299. break; 
  300. case 'month' : 
  301. default : 
  302. $time = strtotime( date( 'Ym', strtotime( "+{$i} MONTH", $start_date ) ) . '01' ) . '000'; 
  303. break; 
  304.  
  305. if ( ! isset( $prepared_data[ $time ] ) ) { 
  306. $prepared_data[ $time ] = array( esc_js( $time ), 0 ); 
  307.  
  308. foreach ( $data as $d ) { 
  309. switch ( $group_by ) { 
  310. case 'day' : 
  311. $time = strtotime( date( 'Ymd', strtotime( $d->$date_key ) ) ) . '000'; 
  312. break; 
  313. case 'month' : 
  314. default : 
  315. $time = strtotime( date( 'Ym', strtotime( $d->$date_key ) ) . '01' ) . '000'; 
  316. break; 
  317.  
  318. if ( ! isset( $prepared_data[ $time ] ) ) { 
  319. continue; 
  320.  
  321. if ( $data_key ) { 
  322. $prepared_data[ $time ][1] += $d->$data_key; 
  323. } else { 
  324. $prepared_data[ $time ][1] ++; 
  325.  
  326. return $prepared_data; 
  327.  
  328. /** 
  329. * Prepares a sparkline to show sales in the last X days. 
  330. * @param int $id ID of the product to show. Blank to get all orders. 
  331. * @param int $days Days of stats to get. 
  332. * @param string $type Type of sparkline to get. Ignored if ID is not set. 
  333. * @return string 
  334. */ 
  335. public function sales_sparkline( $id = '', $days = 7, $type = 'sales' ) { 
  336.  
  337. if ( $id ) { 
  338. $meta_key = $type == 'sales' ? '_line_total' : '_qty'; 
  339.  
  340. $data = $this->get_order_report_data( array( 
  341. 'data' => array( 
  342. '_product_id' => array( 
  343. 'type' => 'order_item_meta',  
  344. 'order_item_type' => 'line_item',  
  345. 'function' => '',  
  346. 'name' => 'product_id' 
  347. ),  
  348. $meta_key => array( 
  349. 'type' => 'order_item_meta',  
  350. 'order_item_type' => 'line_item',  
  351. 'function' => 'SUM',  
  352. 'name' => 'sparkline_value' 
  353. ),  
  354. 'post_date' => array( 
  355. 'type' => 'post_data',  
  356. 'function' => '',  
  357. 'name' => 'post_date' 
  358. ),  
  359. ),  
  360. 'where' => array( 
  361. array( 
  362. 'key' => 'post_date',  
  363. 'value' => date( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),  
  364. 'operator' => '>' 
  365. ),  
  366. array( 
  367. 'key' => 'order_item_meta__product_id.meta_value',  
  368. 'value' => $id,  
  369. 'operator' => '=' 
  370. ),  
  371. 'group_by' => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',  
  372. 'query_type' => 'get_results',  
  373. 'filter_range' => false 
  374. ) ); 
  375. } else { 
  376.  
  377. $data = $this->get_order_report_data( array( 
  378. 'data' => array( 
  379. '_order_total' => array( 
  380. 'type' => 'meta',  
  381. 'function' => 'SUM',  
  382. 'name' => 'sparkline_value' 
  383. ),  
  384. 'post_date' => array( 
  385. 'type' => 'post_data',  
  386. 'function' => '',  
  387. 'name' => 'post_date' 
  388. ),  
  389. ),  
  390. 'where' => array( 
  391. array( 
  392. 'key' => 'post_date',  
  393. 'value' => date( 'Y-m-d', strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ) ),  
  394. 'operator' => '>' 
  395. ),  
  396. 'group_by' => 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)',  
  397. 'query_type' => 'get_results',  
  398. 'filter_range' => false 
  399. ) ); 
  400.  
  401. $total = 0; 
  402. foreach ( $data as $d ) { 
  403. $total += $d->sparkline_value; 
  404.  
  405. if ( $type == 'sales' ) { 
  406. $tooltip = sprintf( __( 'Sold %s worth in the last %d days', 'woocommerce' ), strip_tags( wc_price( $total ) ), $days ); 
  407. } else { 
  408. $tooltip = sprintf( _n( 'Sold 1 item in the last %d days', 'Sold %d items in the last %d days', $total, 'woocommerce' ), $total, $days ); 
  409.  
  410. $sparkline_data = array_values( $this->prepare_chart_data( $data, 'post_date', 'sparkline_value', $days - 1, strtotime( 'midnight -' . ( $days - 1 ) . ' days', current_time( 'timestamp' ) ), 'day' ) ); 
  411.  
  412. return '<span class="wc_sparkline ' . ( $type == 'sales' ? 'lines' : 'bars' ) . ' tips" data-color="#777" data-tip="' . esc_attr( $tooltip ) . '" data-barwidth="' . 60*60*16*1000 . '" data-sparkline="' . esc_attr( json_encode( $sparkline_data ) ) . '"></span>'; 
  413.  
  414. /** 
  415. * Get the current range and calculate the start and end dates. 
  416. * @param string $current_range 
  417. */ 
  418. public function calculate_current_range( $current_range ) { 
  419.  
  420. switch ( $current_range ) { 
  421.  
  422. case 'custom' : 
  423. $this->start_date = strtotime( sanitize_text_field( $_GET['start_date'] ) ); 
  424. $this->end_date = strtotime( 'midnight', strtotime( sanitize_text_field( $_GET['end_date'] ) ) ); 
  425.  
  426. if ( ! $this->end_date ) { 
  427. $this->end_date = current_time( 'timestamp' ); 
  428.  
  429. $interval = 0; 
  430. $min_date = $this->start_date; 
  431.  
  432. while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) { 
  433. $interval ++; 
  434.  
  435. // 3 months max for day view 
  436. if ( $interval > 3 ) { 
  437. $this->chart_groupby = 'month'; 
  438. } else { 
  439. $this->chart_groupby = 'day'; 
  440. break; 
  441.  
  442. case 'year' : 
  443. $this->start_date = strtotime( date( 'Y-01-01', current_time('timestamp') ) ); 
  444. $this->end_date = strtotime( 'midnight', current_time( 'timestamp' ) ); 
  445. $this->chart_groupby = 'month'; 
  446. break; 
  447.  
  448. case 'last_month' : 
  449. $first_day_current_month = strtotime( date( 'Y-m-01', current_time( 'timestamp' ) ) ); 
  450. $this->start_date = strtotime( date( 'Y-m-01', strtotime( '-1 DAY', $first_day_current_month ) ) ); 
  451. $this->end_date = strtotime( date( 'Y-m-t', strtotime( '-1 DAY', $first_day_current_month ) ) ); 
  452. $this->chart_groupby = 'day'; 
  453. break; 
  454.  
  455. case 'month' : 
  456. $this->start_date = strtotime( date( 'Y-m-01', current_time('timestamp') ) ); 
  457. $this->end_date = strtotime( 'midnight', current_time( 'timestamp' ) ); 
  458. $this->chart_groupby = 'day'; 
  459. break; 
  460.  
  461. case '7day' : 
  462. $this->start_date = strtotime( '-6 days', current_time( 'timestamp' ) ); 
  463. $this->end_date = strtotime( 'midnight', current_time( 'timestamp' ) ); 
  464. $this->chart_groupby = 'day'; 
  465. break; 
  466.  
  467. // Group by 
  468. switch ( $this->chart_groupby ) { 
  469.  
  470. case 'day' : 
  471. $this->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; 
  472. $this->chart_interval = absint( ceil( max( 0, ( $this->end_date - $this->start_date ) / ( 60 * 60 * 24 ) ) ) ); 
  473. $this->barwidth = 60 * 60 * 24 * 1000; 
  474. break; 
  475.  
  476. case 'month' : 
  477. $this->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date)'; 
  478. $this->chart_interval = 0; 
  479. $min_date = $this->start_date; 
  480.  
  481. while ( ( $min_date = strtotime( "+1 MONTH", $min_date ) ) <= $this->end_date ) { 
  482. $this->chart_interval ++; 
  483.  
  484. $this->barwidth = 60 * 60 * 24 * 7 * 4 * 1000; 
  485. break; 
  486.  
  487. /** 
  488. * Return currency tooltip JS based on WooCommerce currency position settings. 
  489. * @return string 
  490. */ 
  491. public function get_currency_tooltip() { 
  492. switch( get_option( 'woocommerce_currency_pos' ) ) { 
  493. case 'right': 
  494. $currency_tooltip = 'append_tooltip: "' . get_woocommerce_currency_symbol() . '"'; break; 
  495. case 'right_space': 
  496. $currency_tooltip = 'append_tooltip: " ' . get_woocommerce_currency_symbol() . '"'; break; 
  497. case 'left': 
  498. $currency_tooltip = 'prepend_tooltip: "' . get_woocommerce_currency_symbol() . '"'; break; 
  499. case 'left_space': 
  500. default: 
  501. $currency_tooltip = 'prepend_tooltip: "' . get_woocommerce_currency_symbol() . ' "'; break; 
  502.  
  503. return $currency_tooltip; 
  504.  
  505. /** 
  506. * Get the main chart. 
  507. * @return string 
  508. */ 
  509. public function get_main_chart() {} 
  510.  
  511. /** 
  512. * Get the legend for the main chart sidebar. 
  513. * @return array 
  514. */ 
  515. public function get_chart_legend() { 
  516. return array(); 
  517.  
  518. /** 
  519. * Get chart widgets. 
  520. * @return array 
  521. */ 
  522. public function get_chart_widgets() { 
  523. return array(); 
  524.  
  525. /** 
  526. * Get an export link if needed. 
  527. */ 
  528. public function get_export_button() {} 
  529.  
  530. /** 
  531. * Output the report. 
  532. */ 
  533. public function output_report() {}