Completed
Push — master ( aa706f...7c0d02 )
by Gerhard
06:53
created

WC_Orders_Tracking::track_order_action()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 12
cp 0
crap 6
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce Orders Tracking
4
 *
5
 * @package WooCommerce\Tracks
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * This class adds actions to track usage of WooCommerce Orders.
12
 */
13
class WC_Orders_Tracking {
14
	/**
15
	 * Init tracking.
16
	 */
17
	public function init() {
18
		add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_change' ), 10, 3 );
19
		add_action( 'load-edit.php', array( $this, 'track_orders_view' ), 10 );
20
		add_action( 'pre_post_update', array( $this, 'track_created_date_change' ), 10 );
21
		// WC_Meta_Box_Order_Actions::save() hooks in at priority 50.
22
		add_action( 'woocommerce_process_shop_order_meta', array( $this, 'track_order_action' ), 51 );
23
		add_action( 'load-post-new.php', array( $this, 'track_add_order_from_edit' ), 10 );
24
	}
25
26
	/**
27
	 * Send a Tracks event when the Orders page is viewed.
28
	 */
29
	public function track_orders_view() {
30
		if ( isset( $_GET['post_type'] ) && 'shop_order' === wp_unslash( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
31
32
			// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput
33
			$properties = array(
34
				'status' => isset( $_GET['post_status'] ) ? sanitize_text_field( $_GET['post_status'] ) : 'all',
35
			);
36
			// phpcs:enable
37
38
			WC_Tracks::record_event( 'orders_view', $properties );
39
		}
40
	}
41
42
	/**
43
	 * Send a Tracks event when an order status is changed.
44
	 *
45
	 * @param int    $id Order id.
46
	 * @param string $previous_status the old WooCommerce order status.
47
	 * @param string $next_status the new WooCommerce order status.
48
	 */
49
	public function track_order_status_change( $id, $previous_status, $next_status ) {
50
		$order = wc_get_order( $id );
51
		$date  = $order->get_date_created();
52
53
		$properties = array(
54
			'order_id'        => $id,
55
			'next_status'     => $next_status,
56
			'previous_status' => $previous_status,
57
			'date_created'    => $date->date( 'Y-m-d' ),
58
			'payment_method'  => $order->get_payment_method(),
59
		);
60
61
		WC_Tracks::record_event( 'orders_edit_status_change', $properties );
62
	}
63
64
	/**
65
	 * Send a Tracks event when an order date is changed.
66
	 *
67
	 * @param int $id Order id.
68
	 */
69
	public function track_created_date_change( $id ) {
70
		$post_type = get_post_type( $id );
71
72
		if ( 'shop_order' !== $post_type ) {
73
			return;
74
		}
75
76
		$order        = wc_get_order( $id );
77
		$date_created = $order->get_date_created()->date( 'Y-m-d H:i:s' );
78
		// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
79
		$new_date     = sprintf(
80
			'%s %2d:%2d:%2d',
81
			isset( $_POST['order_date'] ) ? wc_clean( wp_unslash( $_POST['order_date'] ) ) : '',
82
			isset( $_POST['order_date_hour'] ) ? wc_clean( wp_unslash( $_POST['order_date_hour'] ) ) : '',
83
			isset( $_POST['order_date_minute'] ) ? wc_clean( wp_unslash( $_POST['order_date_minute'] ) ) : '',
84
			isset( $_POST['order_date_second'] ) ? wc_clean( wp_unslash( $_POST['order_date_second'] ) ) : ''
85
		);
86
		// phpcs:enable
87
88
		if ( $new_date !== $date_created ) {
89
			$properties = array(
90
				'order_id'   => $id,
91
				'status'     => $order->get_status(),
92
			);
93
94
			WC_Tracks::record_event( 'order_edit_date_created', $properties );
95
		}
96
	}
97
98
	/**
99
	 * Track order actions taken.
100
	 *
101
	 * @param int $order_id Order ID.
102
	 */
103
	public function track_order_action( $order_id ) {
104
		// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
105
		if ( ! empty( $_POST['wc_order_action'] ) ) {
106
			$order      = wc_get_order( $order_id );
107
			$action     = wc_clean( wp_unslash( $_POST['wc_order_action'] ) );
108
			$properties = array(
109
				'order_id' => $order_id,
110
				'status'   => $order->get_status(),
111
				'action'   => $action,
112
			);
113
114
			WC_Tracks::record_event( 'order_edit_order_action', $properties );
115
		}
116
		// phpcs:enable
117
	}
118
119
	/**
120
	 * Track "add order" button on the Edit Order screen.
121
	 */
122
	public function track_add_order_from_edit() {
123
		// phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
124
		if ( isset( $_GET['post_type'] ) && 'shop_order' === wp_unslash( $_GET['post_type'] ) ) {
125
			$referer = wp_get_referer();
126
127
			if ( $referer ) {
128
				$referring_page = parse_url( $referer );
129
				$referring_args = array();
130
				$post_edit_page = parse_url( admin_url( 'post.php' ) );
131
132
				if ( ! empty( $referring_page['query'] ) ) {
133
					parse_str( $referring_page['query'], $referring_args );
134
				}
135
136
				// Determine if we arrived from an Order Edit screen.
137
				if (
138
					$post_edit_page['path'] === $referring_page['path'] &&
139
					isset( $referring_args['action'] ) &&
140
					'edit' === $referring_args['action'] &&
141
					isset( $referring_args['post'] ) &&
142
					'shop_order' === get_post_type( $referring_args['post'] )
143
				) {
144
					WC_Tracks::record_event( 'order_edit_add_order' );
145
				}
146
			}
147
		}
148
	}
149
}
150