2019-03-04 22:37:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Settings Tracking
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tracks
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class adds actions to track usage of WooCommerce Settings.
|
|
|
|
*/
|
|
|
|
class WC_Settings_Tracking {
|
2020-08-25 21:03:03 +00:00
|
|
|
|
2019-03-04 22:37:59 +00:00
|
|
|
/**
|
2020-08-25 21:03:03 +00:00
|
|
|
* List of allowed WooCommerce settings to potentially track updates for.
|
2019-03-04 22:37:59 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-08-25 21:03:03 +00:00
|
|
|
protected $allowed_options = array();
|
2019-03-04 22:37:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce settings that have been updated (and will be tracked).
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $updated_options = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init tracking.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function init() {
|
|
|
|
add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) );
|
2020-08-25 21:03:03 +00:00
|
|
|
add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) );
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) );
|
2019-03-04 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-25 21:03:03 +00:00
|
|
|
* Add a WooCommerce option name to our allowed options list and attach
|
2019-03-04 22:37:59 +00:00
|
|
|
* the `update_option` hook. Rather than inspecting every updated
|
|
|
|
* option and pattern matching for "woocommerce", just build a dynamic
|
2020-08-25 21:03:03 +00:00
|
|
|
* list for WooCommerce options that might get updated.
|
2019-03-04 22:37:59 +00:00
|
|
|
*
|
|
|
|
* See `woocommerce_update_option` hook.
|
|
|
|
*
|
|
|
|
* @param array $option WooCommerce option (config) that might get updated.
|
|
|
|
*/
|
2020-08-25 21:03:03 +00:00
|
|
|
public function add_option_to_list( $option ) {
|
|
|
|
$this->allowed_options[] = $option['id'];
|
2019-03-04 22:37:59 +00:00
|
|
|
|
|
|
|
// Delay attaching this action since it could get fired a lot.
|
2019-03-05 00:30:01 +00:00
|
|
|
if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) {
|
|
|
|
add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 );
|
2019-03-04 22:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add WooCommerce option to a list of updated options.
|
|
|
|
*
|
|
|
|
* @param string $option_name Option being updated.
|
|
|
|
* @param mixed $old_value Old value of option.
|
|
|
|
* @param mixed $new_value New value of option.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_setting_change( $option_name, $old_value, $new_value ) {
|
2019-03-04 22:37:59 +00:00
|
|
|
// Make sure this is a WooCommerce option.
|
2020-08-25 21:03:03 +00:00
|
|
|
if ( ! in_array( $option_name, $this->allowed_options, true ) ) {
|
2019-03-04 22:37:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure the new value is truly different.
|
|
|
|
// `woocommerce_price_num_decimals` tends to trigger this
|
|
|
|
// because form values aren't coerced (e.g. '2' vs. 2).
|
|
|
|
if (
|
|
|
|
is_scalar( $old_value ) &&
|
|
|
|
is_scalar( $new_value ) &&
|
|
|
|
(string) $old_value === (string) $new_value
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
$this->updated_options[] = $option_name;
|
2019-03-04 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Tracks event for WooCommerce options that changed values.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function send_settings_change_event() {
|
2019-03-04 22:37:59 +00:00
|
|
|
global $current_tab;
|
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
if ( empty( $this->updated_options ) ) {
|
2019-03-04 22:37:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array(
|
2019-08-13 19:03:09 +00:00
|
|
|
'settings' => implode( ',', $this->updated_options ),
|
2019-03-04 22:37:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $current_tab ) ) {
|
|
|
|
$properties['tab'] = $current_tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'settings_change', $properties );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a Tracks event for WooCommerce settings page views.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_settings_page_view() {
|
2019-03-04 22:37:59 +00:00
|
|
|
global $current_tab, $current_section;
|
|
|
|
|
|
|
|
$properties = array(
|
|
|
|
'tab' => $current_tab,
|
|
|
|
'section' => empty( $current_section ) ? null : $current_section,
|
|
|
|
);
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'settings_view', $properties );
|
|
|
|
}
|
|
|
|
}
|