class-wc-admin-setup-wizard-tracking.php
5 years ago
class-wc-coupon-tracking.php
6 years ago
class-wc-coupons-tracking.php
7 months ago
class-wc-extensions-tracking.php
1 year ago
class-wc-importer-tracking.php
3 years ago
class-wc-order-tracking.php
6 years ago
class-wc-orders-tracking.php
1 year ago
class-wc-product-collection-block-tracking.php
7 months ago
class-wc-products-tracking.php
1 month ago
class-wc-settings-tracking.php
11 months ago
class-wc-status-tracking.php
7 months ago
class-wc-theme-tracking.php
1 year ago
class-wc-orders-tracking.php
228 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Orders Tracking |
| 4 | * |
| 5 | * @package WooCommerce\Tracks |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 9 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 10 | use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * This class adds actions to track usage of WooCommerce Orders. |
| 16 | */ |
| 17 | class WC_Orders_Tracking { |
| 18 | /** |
| 19 | * Init tracking. |
| 20 | */ |
| 21 | public function init() { |
| 22 | add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_change' ), 10, 3 ); |
| 23 | // WC_Meta_Box_Order_Actions::save() hooks in at priority 50. |
| 24 | add_action( 'woocommerce_process_shop_order_meta', array( $this, 'track_order_action' ), 51 ); |
| 25 | |
| 26 | add_action( 'load-edit.php', array( $this, 'track_orders_view' ), 10 ); |
| 27 | add_action( 'load-woocommerce_page_wc-orders', array( $this, 'track_orders_view' ), 999 ); // HPOS. |
| 28 | |
| 29 | add_action( 'load-post-new.php', array( $this, 'track_add_order_from_edit' ), 10 ); |
| 30 | add_action( 'load-woocommerce_page_wc-orders', array( $this, 'track_add_order_from_edit' ), 999 ); // HPOS. |
| 31 | |
| 32 | add_action( 'woocommerce_process_shop_order_meta', array( $this, 'track_created_date_change' ), 10 ); |
| 33 | |
| 34 | add_action( 'load-edit.php', array( $this, 'track_search_in_orders_list' ) ); |
| 35 | add_action( 'load-woocommerce_page_wc-orders', array( $this, 'track_search_in_orders_list' ), 999 ); // HPOS. |
| 36 | |
| 37 | add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_order_tracking_scripts' ) ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Send a track event when on the Order Listing page, and search results are being displayed. |
| 42 | * |
| 43 | * @deprecated 8.6.0 |
| 44 | * |
| 45 | * @param array $order_ids Array of order_ids that are matches for the search. |
| 46 | * @param string $term The string that was used in the search. |
| 47 | * @param array $search_fields Fields that were used in the original search. |
| 48 | */ |
| 49 | public function track_order_search( $order_ids, $term, $search_fields ) { |
| 50 | wc_deprecated_function( __METHOD__, '8.6.0', 'WC_Orders_Tracking::track_search_in_orders_list' ); |
| 51 | |
| 52 | // Since `woocommerce_shop_order_search_results` can run in the front-end context, exit if get_current_screen isn't defined. |
| 53 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 54 | return $order_ids; |
| 55 | } |
| 56 | |
| 57 | $screen = get_current_screen(); |
| 58 | |
| 59 | // We only want to record this track when the filter is executed on the order listing page. |
| 60 | if ( 'edit-shop_order' === $screen->id ) { |
| 61 | // we are on the order listing page, and query results are being shown. |
| 62 | WC_Tracks::record_event( 'orders_view_search' ); |
| 63 | } |
| 64 | |
| 65 | return $order_ids; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Send a track event when on the Order Listing page, and search results are being displayed. |
| 70 | * |
| 71 | * @since 8.6.0 |
| 72 | */ |
| 73 | public function track_search_in_orders_list() { |
| 74 | if ( ! OrderUtil::is_order_list_table_screen() || empty( $_REQUEST['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | WC_Tracks::record_event( 'orders_view_search' ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Send a Tracks event when the Orders page is viewed. |
| 83 | */ |
| 84 | public function track_orders_view() { |
| 85 | if ( ! OrderUtil::is_order_list_table_screen() ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // phpcs:disable WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput |
| 90 | $properties = array( |
| 91 | 'status' => sanitize_text_field( $_GET['post_status'] ?? ( $_GET['status'] ?? 'all' ) ), |
| 92 | ); |
| 93 | // phpcs:enable |
| 94 | |
| 95 | WC_Tracks::record_event( 'orders_view', $properties ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Send a Tracks event when an order status is changed. |
| 100 | * |
| 101 | * @param int $id Order id. |
| 102 | * @param string $previous_status the old WooCommerce order status. |
| 103 | * @param string $next_status the new WooCommerce order status. |
| 104 | */ |
| 105 | public function track_order_status_change( $id, $previous_status, $next_status ) { |
| 106 | $order = wc_get_order( $id ); |
| 107 | |
| 108 | $properties = array( |
| 109 | 'order_id' => $id, |
| 110 | 'next_status' => $next_status, |
| 111 | 'previous_status' => $previous_status, |
| 112 | 'date_created' => $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d' ) : '', |
| 113 | 'payment_method' => $order->get_payment_method(), |
| 114 | 'order_total' => $order->get_total(), |
| 115 | ); |
| 116 | |
| 117 | WC_Tracks::record_event( 'orders_edit_status_change', $properties ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Send a Tracks event when an order date is changed. |
| 122 | * |
| 123 | * @param int $id Order id. |
| 124 | */ |
| 125 | public function track_created_date_change( $id ) { |
| 126 | if ( ! OrderUtil::is_order( $id ) ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $order = wc_get_order( $id ); |
| 131 | if ( ! $order || OrderStatus::AUTO_DRAFT === $order->get_status() ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | $date_created = $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d H:i:s' ) : ''; |
| 136 | // phpcs:disable WordPress.Security.NonceVerification |
| 137 | $new_date = sprintf( |
| 138 | '%s %2d:%02d:%02d', |
| 139 | isset( $_POST['order_date'] ) ? wc_clean( wp_unslash( $_POST['order_date'] ) ) : '', |
| 140 | isset( $_POST['order_date_hour'] ) ? wc_clean( wp_unslash( $_POST['order_date_hour'] ) ) : '', |
| 141 | isset( $_POST['order_date_minute'] ) ? wc_clean( wp_unslash( $_POST['order_date_minute'] ) ) : '', |
| 142 | isset( $_POST['order_date_second'] ) ? wc_clean( wp_unslash( $_POST['order_date_second'] ) ) : '' |
| 143 | ); |
| 144 | // phpcs:enable |
| 145 | |
| 146 | if ( $new_date !== $date_created ) { |
| 147 | $properties = array( |
| 148 | 'order_id' => $id, |
| 149 | 'status' => $order->get_status(), |
| 150 | ); |
| 151 | |
| 152 | WC_Tracks::record_event( 'order_edit_date_created', $properties ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Track order actions taken. |
| 158 | * |
| 159 | * @param int $order_id Order ID. |
| 160 | */ |
| 161 | public function track_order_action( $order_id ) { |
| 162 | // phpcs:disable WordPress.Security.NonceVerification |
| 163 | if ( ! empty( $_POST['wc_order_action'] ) ) { |
| 164 | $order = wc_get_order( $order_id ); |
| 165 | $action = wc_clean( wp_unslash( $_POST['wc_order_action'] ) ); |
| 166 | $properties = array( |
| 167 | 'order_id' => $order_id, |
| 168 | 'status' => $order->get_status(), |
| 169 | 'action' => $action, |
| 170 | ); |
| 171 | |
| 172 | WC_Tracks::record_event( 'order_edit_order_action', $properties ); |
| 173 | } |
| 174 | // phpcs:enable |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Track "add order" button on the Edit Order screen. |
| 179 | */ |
| 180 | public function track_add_order_from_edit() { |
| 181 | if ( ! OrderUtil::is_new_order_screen() ) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | $referer = wp_get_referer(); |
| 186 | if ( ! $referer ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $referring_page = wp_parse_url( $referer ); |
| 191 | |
| 192 | if ( empty( $referring_page['query'] ) ) { |
| 193 | // Edit Order screen has query args. |
| 194 | return; |
| 195 | } |
| 196 | parse_str( $referring_page['query'], $referring_args ); |
| 197 | |
| 198 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 199 | $post_edit_page = admin_url( 'admin.php?page=wc-orders' ); |
| 200 | $order_id = $referring_args['id'] ?? 0; |
| 201 | } else { |
| 202 | $post_edit_page = admin_url( 'post.php' ); |
| 203 | $order_id = $referring_args['post'] ?? 0; |
| 204 | } |
| 205 | $post_edit_page = wp_parse_url( $post_edit_page ); |
| 206 | |
| 207 | if ( |
| 208 | ( $post_edit_page['path'] === $referring_page['path'] ) && |
| 209 | ( ! isset( $post_edit_page['query'] ) || false !== strpos( $referring_page['query'], $post_edit_page['query'] ) ) && |
| 210 | ( isset( $referring_args['action'] ) && 'edit' === $referring_args['action'] ) && |
| 211 | 'shop_order' === OrderUtil::get_order_type( $order_id ) |
| 212 | ) { |
| 213 | WC_Tracks::record_event( 'order_edit_add_order' ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Adds the tracking scripts for product setting pages. |
| 219 | */ |
| 220 | public function possibly_add_order_tracking_scripts() { |
| 221 | if ( ! OrderUtil::is_new_order_screen() && ! OrderUtil::is_order_edit_screen() && ! OrderUtil::is_order_list_table_screen() ) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | WCAdminAssets::register_script( 'wp-admin-scripts', 'order-tracking', false ); |
| 226 | } |
| 227 | } |
| 228 |