PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/woocommerce/includes/tracks/events/class-wc-orders-tracking.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 178 lines | 101 code | 24 blank | 53 comment | 14 complexity | bf59fcb82327c09bad43a7bc59c4f4f1 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Orders Tracking
  4. *
  5. * @package WooCommerce\Tracks
  6. */
  7. defined( 'ABSPATH' ) || exit;
  8. /**
  9. * This class adds actions to track usage of WooCommerce Orders.
  10. */
  11. class WC_Orders_Tracking {
  12. /**
  13. * Init tracking.
  14. */
  15. public function init() {
  16. add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_change' ), 10, 3 );
  17. add_action( 'load-edit.php', array( $this, 'track_orders_view' ), 10 );
  18. add_action( 'pre_post_update', array( $this, 'track_created_date_change' ), 10 );
  19. // WC_Meta_Box_Order_Actions::save() hooks in at priority 50.
  20. add_action( 'woocommerce_process_shop_order_meta', array( $this, 'track_order_action' ), 51 );
  21. add_action( 'load-post-new.php', array( $this, 'track_add_order_from_edit' ), 10 );
  22. add_filter( 'woocommerce_shop_order_search_results', array( $this, 'track_order_search' ), 10, 3 );
  23. }
  24. /**
  25. * Send a track event when on the Order Listing page, and search results are being displayed.
  26. *
  27. * @param array $order_ids Array of order_ids that are matches for the search.
  28. * @param string $term The string that was used in the search.
  29. * @param array $search_fields Fields that were used in the original search.
  30. */
  31. public function track_order_search( $order_ids, $term, $search_fields ) {
  32. // Since `woocommerce_shop_order_search_results` can run in the front-end context, exit if get_current_screen isn't defined.
  33. if ( ! function_exists( 'get_current_screen' ) ) {
  34. return $order_ids;
  35. }
  36. $screen = get_current_screen();
  37. // We only want to record this track when the filter is executed on the order listing page.
  38. if ( 'edit-shop_order' === $screen->id ) {
  39. // we are on the order listing page, and query results are being shown.
  40. WC_Tracks::record_event( 'orders_view_search' );
  41. }
  42. return $order_ids;
  43. }
  44. /**
  45. * Send a Tracks event when the Orders page is viewed.
  46. */
  47. public function track_orders_view() {
  48. if ( isset( $_GET['post_type'] ) && 'shop_order' === wp_unslash( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  49. // phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
  50. $properties = array(
  51. 'status' => isset( $_GET['post_status'] ) ? sanitize_text_field( $_GET['post_status'] ) : 'all',
  52. );
  53. // phpcs:enable
  54. WC_Tracks::record_event( 'orders_view', $properties );
  55. }
  56. }
  57. /**
  58. * Send a Tracks event when an order status is changed.
  59. *
  60. * @param int $id Order id.
  61. * @param string $previous_status the old WooCommerce order status.
  62. * @param string $next_status the new WooCommerce order status.
  63. */
  64. public function track_order_status_change( $id, $previous_status, $next_status ) {
  65. $order = wc_get_order( $id );
  66. $properties = array(
  67. 'order_id' => $id,
  68. 'next_status' => $next_status,
  69. 'previous_status' => $previous_status,
  70. 'date_created' => $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d' ) : '',
  71. 'payment_method' => $order->get_payment_method(),
  72. 'order_total' => $order->get_total(),
  73. );
  74. WC_Tracks::record_event( 'orders_edit_status_change', $properties );
  75. }
  76. /**
  77. * Send a Tracks event when an order date is changed.
  78. *
  79. * @param int $id Order id.
  80. */
  81. public function track_created_date_change( $id ) {
  82. $post_type = get_post_type( $id );
  83. if ( 'shop_order' !== $post_type ) {
  84. return;
  85. }
  86. if ( 'auto-draft' === get_post_status( $id ) ) {
  87. return;
  88. }
  89. $order = wc_get_order( $id );
  90. $date_created = $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d H:i:s' ) : '';
  91. // phpcs:disable WordPress.Security.NonceVerification
  92. $new_date = sprintf(
  93. '%s %2d:%2d:%2d',
  94. isset( $_POST['order_date'] ) ? wc_clean( wp_unslash( $_POST['order_date'] ) ) : '',
  95. isset( $_POST['order_date_hour'] ) ? wc_clean( wp_unslash( $_POST['order_date_hour'] ) ) : '',
  96. isset( $_POST['order_date_minute'] ) ? wc_clean( wp_unslash( $_POST['order_date_minute'] ) ) : '',
  97. isset( $_POST['order_date_second'] ) ? wc_clean( wp_unslash( $_POST['order_date_second'] ) ) : ''
  98. );
  99. // phpcs:enable
  100. if ( $new_date !== $date_created ) {
  101. $properties = array(
  102. 'order_id' => $id,
  103. 'status' => $order->get_status(),
  104. );
  105. WC_Tracks::record_event( 'order_edit_date_created', $properties );
  106. }
  107. }
  108. /**
  109. * Track order actions taken.
  110. *
  111. * @param int $order_id Order ID.
  112. */
  113. public function track_order_action( $order_id ) {
  114. // phpcs:disable WordPress.Security.NonceVerification
  115. if ( ! empty( $_POST['wc_order_action'] ) ) {
  116. $order = wc_get_order( $order_id );
  117. $action = wc_clean( wp_unslash( $_POST['wc_order_action'] ) );
  118. $properties = array(
  119. 'order_id' => $order_id,
  120. 'status' => $order->get_status(),
  121. 'action' => $action,
  122. );
  123. WC_Tracks::record_event( 'order_edit_order_action', $properties );
  124. }
  125. // phpcs:enable
  126. }
  127. /**
  128. * Track "add order" button on the Edit Order screen.
  129. */
  130. public function track_add_order_from_edit() {
  131. // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  132. if ( isset( $_GET['post_type'] ) && 'shop_order' === wp_unslash( $_GET['post_type'] ) ) {
  133. $referer = wp_get_referer();
  134. if ( $referer ) {
  135. $referring_page = wp_parse_url( $referer );
  136. $referring_args = array();
  137. $post_edit_page = wp_parse_url( admin_url( 'post.php' ) );
  138. if ( ! empty( $referring_page['query'] ) ) {
  139. parse_str( $referring_page['query'], $referring_args );
  140. }
  141. // Determine if we arrived from an Order Edit screen.
  142. if (
  143. $post_edit_page['path'] === $referring_page['path'] &&
  144. isset( $referring_args['action'] ) &&
  145. 'edit' === $referring_args['action'] &&
  146. isset( $referring_args['post'] ) &&
  147. 'shop_order' === get_post_type( $referring_args['post'] )
  148. ) {
  149. WC_Tracks::record_event( 'order_edit_add_order' );
  150. }
  151. }
  152. }
  153. }
  154. }