/var/www/html/wp-content/plugins/woocommerce/includes/tracks/events/class-wc-settings-tracking.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
 * WooCommerce Settings Tracking
 *
 * @package WooCommerce\Tracks
 */

use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;

defined'ABSPATH' ) || exit;

/**
 * This class adds actions to track usage of WooCommerce Settings.
 */
class WC_Settings_Tracking {

    
/**
     * List of allowed WooCommerce settings to potentially track updates for.
     *
     * @var array
     */
    
protected $allowed_options = array();

    
/**
     * WooCommerce settings that have been updated (and will be tracked).
     *
     * @var array
     */
    
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();

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

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


    
/**
     * Toggled options.
     *
     * @var array
     */
    
protected $toggled_options = array(
        
'enabled'  => array(),
        
'disabled' => array(),
    );

    
/**
     * Init 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_optionstrue ) ) {
            
$this->allowed_options[] = $option['id'];
        }
        if ( isset( 
$option['action'] ) ) {
            if ( 
'add' === $option['action'] ) {
                
$this->added_options[] = $option['id'];
            } elseif ( 
'delete' === $option['action'] ) {
                
$this->deleted_options[] = $option['id'];
            } elseif ( ! 
in_array$option['id'], $this->updated_optionstrue ) ) {
                
$this->updated_options[] = $option['id'];
            }
        } elseif ( isset( 
$option['value'] ) ) {
            if ( 
'select' === $option['type'] ) {
                
$this->modified_options$option['id'] ] = $option['value'];

            } elseif ( 
'checkbox' === $option['type'] ) {
                
$option_state                             'yes' === $option['value'] ? 'enabled' 'disabled';
                
$this->toggled_options$option_state ][] = $option['id'];
            }
            
$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
     * option and pattern matching for "woocommerce", just build a dynamic
     * list for WooCommerce options that might get updated.
     *
     * See `woocommerce_update_option` hook.
     *
     * @param array $option WooCommerce option (config) that might get updated.
     */
    
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);
        }
    }

    
/**
     * 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.
     */
    
public function track_setting_change$option_name$old_value$new_value ) {
        
// Make sure this is a WooCommerce option.
        
if ( ! in_array$option_name$this->allowed_optionstrue ) ) {
            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;
        }

        if ( 
in_array$option_name$this->dropdown_menu_optionstrue ) ) {
            
$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;
        }

        
$this->updated_options[] = $option_name;
    }

    
/**
     * Send a Tracks event for WooCommerce options that changed values.
     */
    
public function send_settings_change_event() {
        global 
$current_tab$current_section;

        if ( empty( 
$this->updated_options ) && empty( $this->deleted_options ) && empty( $this->added_options ) ) {
            return;
        }

        
$properties = array();

        if ( ! empty( 
$this->updated_options ) ) {
            
$properties['settings'] = implode','$this->updated_options );
        }

        if ( ! empty( 
$this->deleted_options ) ) {
            
$properties['deleted'] = implode','$this->deleted_options );
        }

        if ( ! empty( 
$this->added_options ) ) {
            
$properties['added'] = implode','$this->added_options );
        }

        foreach ( 
$this->toggled_options as $state => $options ) {
            if ( ! empty( 
$options ) ) {
                
$properties$state ] = implode','$options );
            }
        }

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

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

        
WC_Tracks::record_event'settings_change'$properties );
    }

    
/**
     * Send a Tracks event for WooCommerce settings page views.
     */
    
public function track_settings_page_view() {
        global 
$current_tab$current_section;

        
$properties = array(
            
'tab'     => $current_tab,
            
'section' => empty( $current_section ) ? null $current_section,
        );

        
WC_Tracks::record_event'settings_view'$properties );
    }

    
/**
     * Adds the tracking scripts for product setting pages.
     *
     * @param string $hook Page hook.
     */
    
public function possibly_add_settings_tracking_scripts$hook ) {
        
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification
        
if (
            ! isset( 
$_GET['page'] ) ||
            
'wc-settings' !== wp_unslash$_GET['page'] )
        ) {
            return;
        }
        
// phpcs:enable

        
WCAdminAssets::register_script'wp-admin-scripts''settings-tracking'false );
    }
}