Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: Allow dropdown options recording for WooCommerce Settings #38035

Merged
merged 3 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Modify 'WC_Settings_Tracking' to allow dropdown options recording for WooCommerce Settings
Expand Up @@ -28,6 +28,21 @@ class WC_Settings_Tracking {
*/
protected $updated_options = array();

/**
* List of option names that are dropdown menus.
*
* @var array
*/
protected $dropdown_menu_options = array();


/**
* List of options that have been modified.
*
* @var array
*/
protected $modified_options = array();

/**
* Toggled options.
*
Expand Down Expand Up @@ -61,6 +76,10 @@ public function init() {
public function add_option_to_list( $option ) {
$this->allowed_options[] = $option['id'];

if ( isset( $option['options'] ) ) {
$this->dropdown_menu_options[] = $option['id'];
}

// Delay attaching this action since it could get fired a lot.
if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) {
add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 );
Expand Down Expand Up @@ -91,8 +110,10 @@ public function track_setting_change( $option_name, $old_value, $new_value ) {
return;
}

// Check and save toggled options.
if ( in_array( $new_value, array( 'yes', 'no' ), true ) && in_array( $old_value, array( 'yes', 'no' ), true ) ) {
if ( in_array( $option_name, $this->dropdown_menu_options, true ) ) {
$this->modified_options[ $option_name ] = $new_value;
} elseif ( in_array( $new_value, array( 'yes', 'no' ), true ) && in_array( $old_value, array( 'yes', 'no' ), true ) ) {
// Save toggled options.
$option_state = 'yes' === $new_value ? 'enabled' : 'disabled';
$this->toggled_options[ $option_state ][] = $option_name;
}
Expand Down Expand Up @@ -120,6 +141,12 @@ public function send_settings_change_event() {
}
}

if ( ! empty( $this->modified_options ) ) {
foreach ( $this->modified_options as $option_name => $selected_option ) {
$properties[ $option_name ] = $selected_option ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned that we aren't checking if the $option_name conflicts with any built-in/other Tracks event property names. But, we are already not doing this for $toggled_options $state above, and I suppose our options are all going to start with woocommerce_ or wc_, so we are probably okay for now. Something to keep in mind in the future for tracking... we should ideally namespace/prefix these sort of things when we log them to avoid possible collisions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a little concerned that we don't do anything to make sure $option_name is a valid Tracks event property name (ensuring use of _ and not -, etc.).

I'll open up a separate issue for us to make this a bit more robust, since this isn't an immediate concern.

}
}

$properties['tab'] = $current_tab ?? '';
$properties['section'] = $current_section ?? '';

Expand Down