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-settings-tracking.php
240 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Settings Tracking |
| 4 | * |
| 5 | * @package WooCommerce\Tracks |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * This class adds actions to track usage of WooCommerce Settings. |
| 14 | */ |
| 15 | class WC_Settings_Tracking { |
| 16 | |
| 17 | /** |
| 18 | * List of allowed WooCommerce settings to potentially track updates for. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $allowed_options = array(); |
| 23 | |
| 24 | /** |
| 25 | * WooCommerce settings that have been updated (and will be tracked). |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $updated_options = array(); |
| 30 | |
| 31 | /** |
| 32 | * List of option names that are dropdown menus. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $dropdown_menu_options = array(); |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * List of options that have been modified. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $modified_options = array(); |
| 45 | |
| 46 | /** |
| 47 | * List of options that have been deleted. |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
| 51 | protected $deleted_options = array(); |
| 52 | |
| 53 | /** |
| 54 | * List of options that have been added. |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | protected $added_options = array(); |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Toggled options. |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | protected $toggled_options = array( |
| 67 | 'enabled' => array(), |
| 68 | 'disabled' => array(), |
| 69 | ); |
| 70 | |
| 71 | /** |
| 72 | * Init tracking. |
| 73 | */ |
| 74 | public function init() { |
| 75 | add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) ); |
| 76 | add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) ); |
| 77 | add_action( 'woocommerce_update_non_option_setting', array( $this, 'add_option_to_list_and_track_setting_change' ) ); |
| 78 | add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) ); |
| 79 | add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_settings_tracking_scripts' ) ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Adds the option to the allowed and updated options directly. |
| 84 | * Currently used for settings that don't use update_option. |
| 85 | * |
| 86 | * @param array $option WooCommerce option that should be updated. |
| 87 | */ |
| 88 | public function add_option_to_list_and_track_setting_change( $option ) { |
| 89 | if ( ! in_array( $option['id'], $this->allowed_options, true ) ) { |
| 90 | $this->allowed_options[] = $option['id']; |
| 91 | } |
| 92 | if ( isset( $option['action'] ) ) { |
| 93 | if ( 'add' === $option['action'] ) { |
| 94 | $this->added_options[] = $option['id']; |
| 95 | } elseif ( 'delete' === $option['action'] ) { |
| 96 | $this->deleted_options[] = $option['id']; |
| 97 | } elseif ( ! in_array( $option['id'], $this->updated_options, true ) ) { |
| 98 | $this->updated_options[] = $option['id']; |
| 99 | } |
| 100 | } elseif ( isset( $option['value'] ) ) { |
| 101 | if ( 'select' === $option['type'] ) { |
| 102 | $this->modified_options[ $option['id'] ] = $option['value']; |
| 103 | |
| 104 | } elseif ( 'checkbox' === $option['type'] ) { |
| 105 | $option_state = 'yes' === $option['value'] ? 'enabled' : 'disabled'; |
| 106 | $this->toggled_options[ $option_state ][] = $option['id']; |
| 107 | } |
| 108 | $this->updated_options[] = $option['id']; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Add a WooCommerce option name to our allowed options list and attach |
| 115 | * the `update_option` hook. Rather than inspecting every updated |
| 116 | * option and pattern matching for "woocommerce", just build a dynamic |
| 117 | * list for WooCommerce options that might get updated. |
| 118 | * |
| 119 | * See `woocommerce_update_option` hook. |
| 120 | * |
| 121 | * @param array $option WooCommerce option (config) that might get updated. |
| 122 | */ |
| 123 | public function add_option_to_list( $option ) { |
| 124 | $this->allowed_options[] = $option['id']; |
| 125 | |
| 126 | if ( isset( $option['options'] ) ) { |
| 127 | $this->dropdown_menu_options[] = $option['id']; |
| 128 | } |
| 129 | |
| 130 | // Delay attaching this action since it could get fired a lot. |
| 131 | if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) { |
| 132 | add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Add WooCommerce option to a list of updated options. |
| 138 | * |
| 139 | * @param string $option_name Option being updated. |
| 140 | * @param mixed $old_value Old value of option. |
| 141 | * @param mixed $new_value New value of option. |
| 142 | */ |
| 143 | public function track_setting_change( $option_name, $old_value, $new_value ) { |
| 144 | // Make sure this is a WooCommerce option. |
| 145 | if ( ! in_array( $option_name, $this->allowed_options, true ) ) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // Check to make sure the new value is truly different. |
| 150 | // `woocommerce_price_num_decimals` tends to trigger this |
| 151 | // because form values aren't coerced (e.g. '2' vs. 2). |
| 152 | if ( |
| 153 | is_scalar( $old_value ) && |
| 154 | is_scalar( $new_value ) && |
| 155 | (string) $old_value === (string) $new_value |
| 156 | ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if ( in_array( $option_name, $this->dropdown_menu_options, true ) ) { |
| 161 | $this->modified_options[ $option_name ] = $new_value; |
| 162 | } elseif ( in_array( $new_value, array( 'yes', 'no' ), true ) && in_array( $old_value, array( 'yes', 'no' ), true ) ) { |
| 163 | // Save toggled options. |
| 164 | $option_state = 'yes' === $new_value ? 'enabled' : 'disabled'; |
| 165 | $this->toggled_options[ $option_state ][] = $option_name; |
| 166 | } |
| 167 | |
| 168 | $this->updated_options[] = $option_name; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Send a Tracks event for WooCommerce options that changed values. |
| 173 | */ |
| 174 | public function send_settings_change_event() { |
| 175 | global $current_tab, $current_section; |
| 176 | |
| 177 | if ( empty( $this->updated_options ) && empty( $this->deleted_options ) && empty( $this->added_options ) ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | $properties = array(); |
| 182 | |
| 183 | if ( ! empty( $this->updated_options ) ) { |
| 184 | $properties['settings'] = implode( ',', $this->updated_options ); |
| 185 | } |
| 186 | |
| 187 | if ( ! empty( $this->deleted_options ) ) { |
| 188 | $properties['deleted'] = implode( ',', $this->deleted_options ); |
| 189 | } |
| 190 | |
| 191 | if ( ! empty( $this->added_options ) ) { |
| 192 | $properties['added'] = implode( ',', $this->added_options ); |
| 193 | } |
| 194 | |
| 195 | foreach ( $this->toggled_options as $state => $options ) { |
| 196 | if ( ! empty( $options ) ) { |
| 197 | $properties[ $state ] = implode( ',', $options ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if ( ! empty( $this->modified_options ) ) { |
| 202 | foreach ( $this->modified_options as $option_name => $selected_option ) { |
| 203 | $properties[ $option_name ] = $selected_option ?? ''; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | $properties['tab'] = $current_tab ?? ''; |
| 208 | $properties['section'] = $current_section ?? ''; |
| 209 | |
| 210 | WC_Tracks::record_event( 'settings_change', $properties ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Send a Tracks event for WooCommerce settings page views. |
| 215 | */ |
| 216 | public function track_settings_page_view() { |
| 217 | global $current_tab, $current_section; |
| 218 | |
| 219 | $properties = array( |
| 220 | 'tab' => $current_tab, |
| 221 | 'section' => empty( $current_section ) ? null : $current_section, |
| 222 | ); |
| 223 | |
| 224 | WC_Tracks::record_event( 'settings_view', $properties ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adds the tracking scripts for product setting pages. |
| 229 | * |
| 230 | * @param string $hook Page hook. |
| 231 | */ |
| 232 | public function possibly_add_settings_tracking_scripts( $hook ) { |
| 233 | if ( ! is_wc_admin_settings_page() ) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | WCAdminAssets::register_script( 'wp-admin-scripts', 'settings-tracking', false ); |
| 238 | } |
| 239 | } |
| 240 |