PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/admin/reports/class-wc-report-customers.php

https://gitlab.com/webkod3r/tripolis
PHP | 415 lines | 332 code | 42 blank | 41 comment | 11 complexity | d21991df13c78f272c833d48fdcee33a MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_Report_Customers
  7. *
  8. * @author WooThemes
  9. * @category Admin
  10. * @package WooCommerce/Admin/Reports
  11. * @version 2.1.0
  12. */
  13. class WC_Report_Customers extends WC_Admin_Report {
  14. /**
  15. * Chart colours.
  16. *
  17. * @var array
  18. */
  19. public $chart_colours = array();
  20. /**
  21. * Customers.
  22. *
  23. * @var array
  24. */
  25. public $customers = array();
  26. /**
  27. * Get the legend for the main chart sidebar.
  28. *
  29. * @return array
  30. */
  31. public function get_chart_legend() {
  32. $legend = array();
  33. $legend[] = array(
  34. 'title' => sprintf( __( '%s signups in this period', 'woocommerce' ), '<strong>' . sizeof( $this->customers ) . '</strong>' ),
  35. 'color' => $this->chart_colours['signups'],
  36. 'highlight_series' => 2
  37. );
  38. return $legend;
  39. }
  40. /**
  41. * Get chart widgets.
  42. *
  43. * @return array
  44. */
  45. public function get_chart_widgets() {
  46. $widgets = array();
  47. $widgets[] = array(
  48. 'title' => '',
  49. 'callback' => array( $this, 'customers_vs_guests' )
  50. );
  51. return $widgets;
  52. }
  53. /**
  54. * Output customers vs guests chart.
  55. */
  56. public function customers_vs_guests() {
  57. $customer_order_totals = $this->get_order_report_data( array(
  58. 'data' => array(
  59. 'ID' => array(
  60. 'type' => 'post_data',
  61. 'function' => 'COUNT',
  62. 'name' => 'total_orders'
  63. )
  64. ),
  65. 'where_meta' => array(
  66. array(
  67. 'meta_key' => '_customer_user',
  68. 'meta_value' => '0',
  69. 'operator' => '>'
  70. )
  71. ),
  72. 'filter_range' => true
  73. ) );
  74. $guest_order_totals = $this->get_order_report_data( array(
  75. 'data' => array(
  76. 'ID' => array(
  77. 'type' => 'post_data',
  78. 'function' => 'COUNT',
  79. 'name' => 'total_orders'
  80. )
  81. ),
  82. 'where_meta' => array(
  83. array(
  84. 'meta_key' => '_customer_user',
  85. 'meta_value' => '0',
  86. 'operator' => '='
  87. )
  88. ),
  89. 'filter_range' => true
  90. ) );
  91. ?>
  92. <div class="chart-container">
  93. <div class="chart-placeholder customers_vs_guests pie-chart" style="height:200px"></div>
  94. <ul class="pie-chart-legend">
  95. <li style="border-color: <?php echo $this->chart_colours['customers']; ?>"><?php _e( 'Customer Sales', 'woocommerce' ); ?></li>
  96. <li style="border-color: <?php echo $this->chart_colours['guests']; ?>"><?php _e( 'Guest Sales', 'woocommerce' ); ?></li>
  97. </ul>
  98. </div>
  99. <script type="text/javascript">
  100. jQuery(function(){
  101. jQuery.plot(
  102. jQuery('.chart-placeholder.customers_vs_guests'),
  103. [
  104. {
  105. label: '<?php _e( 'Customer Orders', 'woocommerce' ); ?>',
  106. data: "<?php echo $customer_order_totals->total_orders ?>",
  107. color: '<?php echo $this->chart_colours['customers']; ?>'
  108. },
  109. {
  110. label: '<?php _e( 'Guest Orders', 'woocommerce' ); ?>',
  111. data: "<?php echo $guest_order_totals->total_orders ?>",
  112. color: '<?php echo $this->chart_colours['guests']; ?>'
  113. }
  114. ],
  115. {
  116. grid: {
  117. hoverable: true
  118. },
  119. series: {
  120. pie: {
  121. show: true,
  122. radius: 1,
  123. innerRadius: 0.6,
  124. label: {
  125. show: false
  126. }
  127. },
  128. enable_tooltip: true,
  129. append_tooltip: "<?php echo ' ' . __( 'orders', 'woocommerce' ); ?>",
  130. },
  131. legend: {
  132. show: false
  133. }
  134. }
  135. );
  136. jQuery('.chart-placeholder.customers_vs_guests').resize();
  137. });
  138. </script>
  139. <?php
  140. }
  141. /**
  142. * Output the report.
  143. */
  144. public function output_report() {
  145. $ranges = array(
  146. 'year' => __( 'Year', 'woocommerce' ),
  147. 'last_month' => __( 'Last Month', 'woocommerce' ),
  148. 'month' => __( 'This Month', 'woocommerce' ),
  149. '7day' => __( 'Last 7 Days', 'woocommerce' )
  150. );
  151. $this->chart_colours = array(
  152. 'signups' => '#3498db',
  153. 'customers' => '#1abc9c',
  154. 'guests' => '#8fdece'
  155. );
  156. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  157. if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
  158. $current_range = '7day';
  159. }
  160. $this->calculate_current_range( $current_range );
  161. $admin_users = new WP_User_Query(
  162. array(
  163. 'role' => 'administrator',
  164. 'fields' => 'ID'
  165. )
  166. );
  167. $manager_users = new WP_User_Query(
  168. array(
  169. 'role' => 'shop_manager',
  170. 'fields' => 'ID'
  171. )
  172. );
  173. $users_query = new WP_User_Query(
  174. array(
  175. 'fields' => array( 'user_registered' ),
  176. 'exclude' => array_merge( $admin_users->get_results(), $manager_users->get_results() )
  177. )
  178. );
  179. $this->customers = $users_query->get_results();
  180. foreach ( $this->customers as $key => $customer ) {
  181. if ( strtotime( $customer->user_registered ) < $this->start_date || strtotime( $customer->user_registered ) > $this->end_date ) {
  182. unset( $this->customers[ $key ] );
  183. }
  184. }
  185. include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
  186. }
  187. /**
  188. * Output an export link.
  189. */
  190. public function get_export_button() {
  191. $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
  192. ?>
  193. <a
  194. href="#"
  195. download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo date_i18n( 'Y-m-d', current_time('timestamp') ); ?>.csv"
  196. class="export_csv"
  197. data-export="chart"
  198. data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>"
  199. data-groupby="<?php echo $this->chart_groupby; ?>"
  200. >
  201. <?php _e( 'Export CSV', 'woocommerce' ); ?>
  202. </a>
  203. <?php
  204. }
  205. /**
  206. * Output the main chart.
  207. */
  208. public function get_main_chart() {
  209. global $wp_locale;
  210. $customer_orders = $this->get_order_report_data( array(
  211. 'data' => array(
  212. 'ID' => array(
  213. 'type' => 'post_data',
  214. 'function' => 'COUNT',
  215. 'name' => 'total_orders'
  216. ),
  217. 'post_date' => array(
  218. 'type' => 'post_data',
  219. 'function' => '',
  220. 'name' => 'post_date'
  221. ),
  222. ),
  223. 'where_meta' => array(
  224. array(
  225. 'meta_key' => '_customer_user',
  226. 'meta_value' => '0',
  227. 'operator' => '>'
  228. )
  229. ),
  230. 'group_by' => $this->group_by_query,
  231. 'order_by' => 'post_date ASC',
  232. 'query_type' => 'get_results',
  233. 'filter_range' => true
  234. ) );
  235. $guest_orders = $this->get_order_report_data( array(
  236. 'data' => array(
  237. 'ID' => array(
  238. 'type' => 'post_data',
  239. 'function' => 'COUNT',
  240. 'name' => 'total_orders'
  241. ),
  242. 'post_date' => array(
  243. 'type' => 'post_data',
  244. 'function' => '',
  245. 'name' => 'post_date'
  246. ),
  247. ),
  248. 'where_meta' => array(
  249. array(
  250. 'meta_key' => '_customer_user',
  251. 'meta_value' => '0',
  252. 'operator' => '='
  253. )
  254. ),
  255. 'group_by' => $this->group_by_query,
  256. 'order_by' => 'post_date ASC',
  257. 'query_type' => 'get_results',
  258. 'filter_range' => true
  259. ) );
  260. $signups = $this->prepare_chart_data( $this->customers, 'user_registered', '', $this->chart_interval, $this->start_date, $this->chart_groupby );
  261. $customer_orders = $this->prepare_chart_data( $customer_orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
  262. $guest_orders = $this->prepare_chart_data( $guest_orders, 'post_date', 'total_orders', $this->chart_interval, $this->start_date, $this->chart_groupby );
  263. // Encode in json format
  264. $chart_data = json_encode( array(
  265. 'signups' => array_values( $signups ),
  266. 'customer_orders' => array_values( $customer_orders ),
  267. 'guest_orders' => array_values( $guest_orders )
  268. ) );
  269. ?>
  270. <div class="chart-container">
  271. <div class="chart-placeholder main"></div>
  272. </div>
  273. <script type="text/javascript">
  274. var main_chart;
  275. jQuery(function(){
  276. var chart_data = jQuery.parseJSON( '<?php echo $chart_data; ?>' );
  277. var drawGraph = function( highlight ) {
  278. var series = [
  279. {
  280. label: "<?php echo esc_js( __( 'Customer Orders', 'woocommerce' ) ) ?>",
  281. data: chart_data.customer_orders,
  282. color: '<?php echo $this->chart_colours['customers']; ?>',
  283. bars: { fillColor: '<?php echo $this->chart_colours['customers']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
  284. shadowSize: 0,
  285. enable_tooltip: true,
  286. append_tooltip: "<?php echo ' ' . __( 'customer orders', 'woocommerce' ); ?>",
  287. stack: true,
  288. },
  289. {
  290. label: "<?php echo esc_js( __( 'Guest Orders', 'woocommerce' ) ) ?>",
  291. data: chart_data.guest_orders,
  292. color: '<?php echo $this->chart_colours['guests']; ?>',
  293. bars: { fillColor: '<?php echo $this->chart_colours['guests']; ?>', fill: true, show: true, lineWidth: 0, barWidth: <?php echo $this->barwidth; ?> * 0.5, align: 'center' },
  294. shadowSize: 0,
  295. enable_tooltip: true,
  296. append_tooltip: "<?php echo ' ' . __( 'guest orders', 'woocommerce' ); ?>",
  297. stack: true,
  298. },
  299. {
  300. label: "<?php echo esc_js( __( 'Signups', 'woocommerce' ) ) ?>",
  301. data: chart_data.signups,
  302. color: '<?php echo $this->chart_colours['signups']; ?>',
  303. points: { show: true, radius: 5, lineWidth: 3, fillColor: '#fff', fill: true },
  304. lines: { show: true, lineWidth: 4, fill: false },
  305. shadowSize: 0,
  306. enable_tooltip: true,
  307. append_tooltip: "<?php echo ' ' . __( 'new users', 'woocommerce' ); ?>",
  308. stack: false
  309. },
  310. ];
  311. if ( highlight !== 'undefined' && series[ highlight ] ) {
  312. highlight_series = series[ highlight ];
  313. highlight_series.color = '#9c5d90';
  314. if ( highlight_series.bars )
  315. highlight_series.bars.fillColor = '#9c5d90';
  316. if ( highlight_series.lines ) {
  317. highlight_series.lines.lineWidth = 5;
  318. }
  319. }
  320. main_chart = jQuery.plot(
  321. jQuery('.chart-placeholder.main'),
  322. series,
  323. {
  324. legend: {
  325. show: false
  326. },
  327. grid: {
  328. color: '#aaa',
  329. borderColor: 'transparent',
  330. borderWidth: 0,
  331. hoverable: true
  332. },
  333. xaxes: [ {
  334. color: '#aaa',
  335. position: "bottom",
  336. tickColor: 'transparent',
  337. mode: "time",
  338. timeformat: "<?php if ( $this->chart_groupby == 'day' ) echo '%d %b'; else echo '%b'; ?>",
  339. monthNames: <?php echo json_encode( array_values( $wp_locale->month_abbrev ) ) ?>,
  340. tickLength: 1,
  341. minTickSize: [1, "<?php echo $this->chart_groupby; ?>"],
  342. tickSize: [1, "<?php echo $this->chart_groupby; ?>"],
  343. font: {
  344. color: "#aaa"
  345. }
  346. } ],
  347. yaxes: [
  348. {
  349. min: 0,
  350. minTickSize: 1,
  351. tickDecimals: 0,
  352. color: '#ecf0f1',
  353. font: { color: "#aaa" }
  354. }
  355. ],
  356. }
  357. );
  358. jQuery('.chart-placeholder').resize();
  359. }
  360. drawGraph();
  361. jQuery('.highlight_series').hover(
  362. function() {
  363. drawGraph( jQuery(this).data('series') );
  364. },
  365. function() {
  366. drawGraph();
  367. }
  368. );
  369. });
  370. </script>
  371. <?php
  372. }
  373. }