class-wc-admin-reports.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Admin Reports
  4. *
  5. * Functions used for displaying sales and customer reports in admin.
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce/Admin/Reports
  10. * @version 2.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. if ( class_exists( 'WC_Admin_Reports', false ) ) {
  16. return;
  17. }
  18. /**
  19. * WC_Admin_Reports Class.
  20. */
  21. class WC_Admin_Reports {
  22. /**
  23. * Handles output of the reports page in admin.
  24. */
  25. public static function output() {
  26. $reports = self::get_reports();
  27. $first_tab = array_keys( $reports );
  28. $current_tab = ! empty( $_GET['tab'] ) ? sanitize_title( $_GET['tab'] ) : $first_tab[0];
  29. $current_report = isset( $_GET['report'] ) ? sanitize_title( $_GET['report'] ) : current( array_keys( $reports[ $current_tab ]['reports'] ) );
  30. include_once dirname( __FILE__ ) . '/reports/class-wc-admin-report.php';
  31. include_once dirname( __FILE__ ) . '/views/html-admin-page-reports.php';
  32. }
  33. /**
  34. * Returns the definitions for the reports to show in admin.
  35. *
  36. * @return array
  37. */
  38. public static function get_reports() {
  39. $reports = array(
  40. 'orders' => array(
  41. 'title' => __( 'Orders', 'woocommerce' ),
  42. 'reports' => array(
  43. 'sales_by_date' => array(
  44. 'title' => __( 'Sales by date', 'woocommerce' ),
  45. 'description' => '',
  46. 'hide_title' => true,
  47. 'callback' => array( __CLASS__, 'get_report' ),
  48. ),
  49. 'sales_by_product' => array(
  50. 'title' => __( 'Sales by product', 'woocommerce' ),
  51. 'description' => '',
  52. 'hide_title' => true,
  53. 'callback' => array( __CLASS__, 'get_report' ),
  54. ),
  55. 'sales_by_category' => array(
  56. 'title' => __( 'Sales by category', 'woocommerce' ),
  57. 'description' => '',
  58. 'hide_title' => true,
  59. 'callback' => array( __CLASS__, 'get_report' ),
  60. ),
  61. 'coupon_usage' => array(
  62. 'title' => __( 'Coupons by date', 'woocommerce' ),
  63. 'description' => '',
  64. 'hide_title' => true,
  65. 'callback' => array( __CLASS__, 'get_report' ),
  66. ),
  67. 'downloads' => array(
  68. 'title' => __( 'Customer downloads', 'woocommerce' ),
  69. 'description' => '',
  70. 'hide_title' => true,
  71. 'callback' => array( __CLASS__, 'get_report' ),
  72. ),
  73. ),
  74. ),
  75. 'customers' => array(
  76. 'title' => __( 'Customers', 'woocommerce' ),
  77. 'reports' => array(
  78. 'customers' => array(
  79. 'title' => __( 'Customers vs. guests', 'woocommerce' ),
  80. 'description' => '',
  81. 'hide_title' => true,
  82. 'callback' => array( __CLASS__, 'get_report' ),
  83. ),
  84. 'customer_list' => array(
  85. 'title' => __( 'Customer list', 'woocommerce' ),
  86. 'description' => '',
  87. 'hide_title' => true,
  88. 'callback' => array( __CLASS__, 'get_report' ),
  89. ),
  90. ),
  91. ),
  92. 'stock' => array(
  93. 'title' => __( 'Stock', 'woocommerce' ),
  94. 'reports' => array(
  95. 'low_in_stock' => array(
  96. 'title' => __( 'Low in stock', 'woocommerce' ),
  97. 'description' => '',
  98. 'hide_title' => true,
  99. 'callback' => array( __CLASS__, 'get_report' ),
  100. ),
  101. 'out_of_stock' => array(
  102. 'title' => __( 'Out of stock', 'woocommerce' ),
  103. 'description' => '',
  104. 'hide_title' => true,
  105. 'callback' => array( __CLASS__, 'get_report' ),
  106. ),
  107. 'most_stocked' => array(
  108. 'title' => __( 'Most stocked', 'woocommerce' ),
  109. 'description' => '',
  110. 'hide_title' => true,
  111. 'callback' => array( __CLASS__, 'get_report' ),
  112. ),
  113. ),
  114. ),
  115. );
  116. if ( wc_tax_enabled() ) {
  117. $reports['taxes'] = array(
  118. 'title' => __( 'Taxes', 'woocommerce' ),
  119. 'reports' => array(
  120. 'taxes_by_code' => array(
  121. 'title' => __( 'Taxes by code', 'woocommerce' ),
  122. 'description' => '',
  123. 'hide_title' => true,
  124. 'callback' => array( __CLASS__, 'get_report' ),
  125. ),
  126. 'taxes_by_date' => array(
  127. 'title' => __( 'Taxes by date', 'woocommerce' ),
  128. 'description' => '',
  129. 'hide_title' => true,
  130. 'callback' => array( __CLASS__, 'get_report' ),
  131. ),
  132. ),
  133. );
  134. }
  135. $reports = apply_filters( 'woocommerce_admin_reports', $reports );
  136. $reports = apply_filters( 'woocommerce_reports_charts', $reports ); // Backwards compatibility.
  137. foreach ( $reports as $key => $report_group ) {
  138. if ( isset( $reports[ $key ]['charts'] ) ) {
  139. $reports[ $key ]['reports'] = $reports[ $key ]['charts'];
  140. }
  141. foreach ( $reports[ $key ]['reports'] as $report_key => $report ) {
  142. if ( isset( $reports[ $key ]['reports'][ $report_key ]['function'] ) ) {
  143. $reports[ $key ]['reports'][ $report_key ]['callback'] = $reports[ $key ]['reports'][ $report_key ]['function'];
  144. }
  145. }
  146. }
  147. return $reports;
  148. }
  149. /**
  150. * Get a report from our reports subfolder.
  151. *
  152. * @param string $name
  153. */
  154. public static function get_report( $name ) {
  155. $name = sanitize_title( str_replace( '_', '-', $name ) );
  156. $class = 'WC_Report_' . str_replace( '-', '_', $name );
  157. include_once apply_filters( 'wc_admin_reports_path', 'reports/class-wc-report-' . $name . '.php', $name, $class );
  158. if ( ! class_exists( $class ) ) {
  159. return;
  160. }
  161. $report = new $class();
  162. $report->output_report();
  163. }
  164. }