class-wc-admin-setup-wizard-tracking.php
5 years ago
class-wc-coupon-tracking.php
6 years ago
class-wc-coupons-tracking.php
6 years ago
class-wc-extensions-tracking.php
6 years ago
class-wc-importer-tracking.php
6 years ago
class-wc-order-tracking.php
6 years ago
class-wc-orders-tracking.php
5 years ago
class-wc-products-tracking.php
5 years ago
class-wc-settings-tracking.php
5 years ago
class-wc-status-tracking.php
5 years ago
class-wc-settings-tracking.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Settings Tracking |
| 4 | * |
| 5 | * @package WooCommerce\Tracks |
| 6 | */ |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * This class adds actions to track usage of WooCommerce Settings. |
| 12 | */ |
| 13 | class WC_Settings_Tracking { |
| 14 | |
| 15 | /** |
| 16 | * List of allowed WooCommerce settings to potentially track updates for. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | protected $allowed_options = array(); |
| 21 | |
| 22 | /** |
| 23 | * WooCommerce settings that have been updated (and will be tracked). |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $updated_options = array(); |
| 28 | |
| 29 | /** |
| 30 | * Init tracking. |
| 31 | */ |
| 32 | public function init() { |
| 33 | add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) ); |
| 34 | add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) ); |
| 35 | add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Add a WooCommerce option name to our allowed options list and attach |
| 40 | * the `update_option` hook. Rather than inspecting every updated |
| 41 | * option and pattern matching for "woocommerce", just build a dynamic |
| 42 | * list for WooCommerce options that might get updated. |
| 43 | * |
| 44 | * See `woocommerce_update_option` hook. |
| 45 | * |
| 46 | * @param array $option WooCommerce option (config) that might get updated. |
| 47 | */ |
| 48 | public function add_option_to_list( $option ) { |
| 49 | $this->allowed_options[] = $option['id']; |
| 50 | |
| 51 | // Delay attaching this action since it could get fired a lot. |
| 52 | if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) { |
| 53 | add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Add WooCommerce option to a list of updated options. |
| 59 | * |
| 60 | * @param string $option_name Option being updated. |
| 61 | * @param mixed $old_value Old value of option. |
| 62 | * @param mixed $new_value New value of option. |
| 63 | */ |
| 64 | public function track_setting_change( $option_name, $old_value, $new_value ) { |
| 65 | // Make sure this is a WooCommerce option. |
| 66 | if ( ! in_array( $option_name, $this->allowed_options, true ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Check to make sure the new value is truly different. |
| 71 | // `woocommerce_price_num_decimals` tends to trigger this |
| 72 | // because form values aren't coerced (e.g. '2' vs. 2). |
| 73 | if ( |
| 74 | is_scalar( $old_value ) && |
| 75 | is_scalar( $new_value ) && |
| 76 | (string) $old_value === (string) $new_value |
| 77 | ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $this->updated_options[] = $option_name; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Send a Tracks event for WooCommerce options that changed values. |
| 86 | */ |
| 87 | public function send_settings_change_event() { |
| 88 | global $current_tab; |
| 89 | |
| 90 | if ( empty( $this->updated_options ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $properties = array( |
| 95 | 'settings' => implode( ',', $this->updated_options ), |
| 96 | ); |
| 97 | |
| 98 | if ( isset( $current_tab ) ) { |
| 99 | $properties['tab'] = $current_tab; |
| 100 | } |
| 101 | |
| 102 | WC_Tracks::record_event( 'settings_change', $properties ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Send a Tracks event for WooCommerce settings page views. |
| 107 | */ |
| 108 | public function track_settings_page_view() { |
| 109 | global $current_tab, $current_section; |
| 110 | |
| 111 | $properties = array( |
| 112 | 'tab' => $current_tab, |
| 113 | 'section' => empty( $current_section ) ? null : $current_section, |
| 114 | ); |
| 115 | |
| 116 | WC_Tracks::record_event( 'settings_view', $properties ); |
| 117 | } |
| 118 | } |
| 119 |