Add `wcadmin_settings_change` tracks event to shipping fields (#38305)

* Call update_option and update_options in shipping_zone_add_method

* Add new hook 'woocommerce_update_shipping_setting' to settings tracking

* Add tracking for shipping_zone_add_method

* Add tracking to shipping_zone_methods_save_changes

* Add changelog

* Update hook name and track modified option

* Prevent options from being added twice to array
start fixing php sniff issues

* Add docblocks

* Remove empty space

* Add docblock for parameter

* Fix lint issue

* Fix code introduced by mistake
This commit is contained in:
Nathan Silveira 2023-05-19 16:05:07 -03:00 committed by GitHub
parent ef0a32ff48
commit 5723f436cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Add tracks events to shipping settings

View File

@ -2994,9 +2994,28 @@ class WC_AJAX {
$zone = new WC_Shipping_Zone( $zone_data['zone_id'] );
if ( isset( $zone_data['zone_order'] ) ) {
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action(
'woocommerce_update_non_option_setting',
array(
'id' => 'zone_order',
)
);
$zone->set_zone_order( $zone_data['zone_order'] );
}
global $current_tab;
$current_tab = 'shipping';
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_options' );
$zone->save();
}
}
@ -3028,10 +3047,30 @@ class WC_AJAX {
wp_die();
}
$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
$zone = new WC_Shipping_Zone( $zone_id );
$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
$zone = new WC_Shipping_Zone( $zone_id );
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action(
'woocommerce_update_non_option_setting',
array(
'id' => 'zone_method',
)
);
$instance_id = $zone->add_shipping_method( wc_clean( wp_unslash( $_POST['method_id'] ) ) );
global $current_tab;
$current_tab = 'shipping';
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_options' );
wp_send_json_success(
array(
'instance_id' => $instance_id,
@ -3068,10 +3107,22 @@ class WC_AJAX {
$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $changes['zone_name'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_name' ) );
$zone->set_zone_name( wc_clean( $changes['zone_name'] ) );
}
if ( isset( $changes['zone_locations'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_locations' ) );
$zone->clear_locations( array( 'state', 'country', 'continent' ) );
$locations = array_filter( array_map( 'wc_clean', (array) $changes['zone_locations'] ) );
foreach ( $locations as $location ) {
@ -3092,6 +3143,12 @@ class WC_AJAX {
}
if ( isset( $changes['zone_postcodes'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_postcodes' ) );
$zone->clear_locations( 'postcode' );
$postcodes = array_filter( array_map( 'strtoupper', array_map( 'wc_clean', explode( "\n", $changes['zone_postcodes'] ) ) ) );
foreach ( $postcodes as $postcode ) {
@ -3100,6 +3157,12 @@ class WC_AJAX {
}
if ( isset( $changes['methods'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_methods' ) );
foreach ( $changes['methods'] as $instance_id => $data ) {
$method_id = $wpdb->get_var( $wpdb->prepare( "SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d", $instance_id ) );
@ -3122,10 +3185,22 @@ class WC_AJAX {
);
if ( isset( $method_data['method_order'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_methods_order' ) );
$wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $method_data['method_order'] ) ), array( 'instance_id' => absint( $instance_id ) ) );
}
if ( isset( $method_data['enabled'] ) ) {
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_methods_enabled' ) );
$is_enabled = absint( 'yes' === $method_data['enabled'] );
if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'is_enabled' => $is_enabled ), array( 'instance_id' => absint( $instance_id ) ) ) ) {
do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method_id, $zone_id, $is_enabled );
@ -3136,6 +3211,15 @@ class WC_AJAX {
$zone->save();
global $current_tab;
$current_tab = 'shipping';
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_options' );
wp_send_json_success(
array(
'zone_id' => $zone->get_id(),
@ -3167,7 +3251,22 @@ class WC_AJAX {
$instance_id = absint( $_POST['instance_id'] );
$zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id );
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_method_settings' ) );
$shipping_method->set_post_data( wp_unslash( $_POST['data'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
global $current_tab;
$current_tab = 'shipping';
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_options' );
$shipping_method->process_admin_options();
WC_Cache_Helper::get_transient_version( 'shipping', true );
@ -3219,14 +3318,32 @@ class WC_AJAX {
$update_args = array();
if ( isset( $data['name'] ) ) {
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_name' ) );
$update_args['name'] = wc_clean( $data['name'] );
}
if ( isset( $data['slug'] ) ) {
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_slug' ) );
$update_args['slug'] = wc_clean( $data['slug'] );
}
if ( isset( $data['description'] ) ) {
/**
* Notify that a non-option setting has been updated.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_description' ) );
$update_args['description'] = wc_clean( $data['description'] );
}
@ -3244,6 +3361,15 @@ class WC_AJAX {
do_action( 'woocommerce_shipping_classes_save_class', $term_id, $data );
}
global $current_tab, $current_section;
$current_tab = 'shipping';
$current_section = 'classes';
/**
* Completes the saving process for options.
*
* @since 7.8.0
*/
do_action( 'woocommerce_update_options' );
$wc_shipping = WC_Shipping::instance();
wp_send_json_success(
@ -3296,7 +3422,6 @@ class WC_AJAX {
// Disable the gateway.
$gateway->update_option( 'enabled', 'no' );
}
do_action( 'woocommerce_update_options' );
wp_send_json_success( ! wc_string_to_bool( $enabled ) );
wp_die();

View File

@ -59,10 +59,26 @@ class WC_Settings_Tracking {
public function init() {
add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) );
add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) );
add_action( 'woocommerce_update_non_option_setting', array( $this, 'add_option_to_list_and_track_setting_change' ) );
add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_settings_tracking_scripts' ) );
}
/**
* Adds the option to the allowed and updated options directly.
* Currently used for settings that don't use update_option.
*
* @param array $option WooCommerce option that should be updated.
*/
public function add_option_to_list_and_track_setting_change( $option ) {
if ( ! in_array( $option['id'], $this->allowed_options, true ) ) {
$this->allowed_options[] = $option['id'];
}
if ( ! in_array( $option['id'], $this->updated_options, true ) ) {
$this->updated_options[] = $option['id'];
}
}
/**
* Add a WooCommerce option name to our allowed options list and attach
* the `update_option` hook. Rather than inspecting every updated