/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/Features/Settings/Transformer.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
 * WooCommerce Settings Data Transformer.
 */

declare( strict_types );

namespace 
Automattic\WooCommerce\Admin\Features\Settings;

/**
 * Transforms WooCommerce settings data into a structured format with logical groupings.
 */
class Transformer {
    
/**
     * Current group being processed.
     *
     * @var array|null
     */
    
private ?array $current_group null;

    
/**
     * Current checkbox group being processed.
     *
     * @var array|null
     */
    
private ?array $current_checkbox_group null;

    
/**
     * Transform settings data.
     *
     * @param array $raw_settings Raw settings data.
     *
     * @return array Transformed settings data.
     */
    
public function transform( array $raw_settings ): array {
        
$transformed = array();

        foreach ( 
$raw_settings as $tab_id => $tab ) {
            
// If the tab doesn't have sections, or the sections aren't an array, skip it.
            
if ( ! isset( $tab['sections'] ) || ! is_array$tab['sections'] ) ) {
                
$transformed$tab_id ] = $tab;
                continue;
            }

            
$transformed$tab_id ]             = $tab;
            
$transformed$tab_id ]['sections'] = $this->transform_sections$tab['sections'] );
        }

        return 
$transformed;
    }

    
/**
     * Transform sections within a tab.
     *
     * @param array $sections Sections to transform.
     *
     * @return array Transformed sections.
     */
    
private function transform_sections( array $sections ): array {
        
$transformed_sections = array();

        foreach ( 
$sections as $section_id => $section ) {
            
// If the section doesn't have settings, or the settings aren't an array, skip it.
            
if ( ! isset( $section['settings'] ) || ! is_array$section['settings'] ) ) {
                
$transformed_sections$section_id ] = $section;
                continue;
            }

            
$transformed_sections$section_id ]             = $section;
            
$transformed_sections$section_id ]['settings'] = $this->transform_section_settings$section['settings'] );
        }

        return 
$transformed_sections;
    }

    
/**
     * Transform settings within a section.
     *
     * @param array $settings Settings to transform.
     *
     * @return array Transformed settings.
     */
    
private function transform_section_settings( array $settings ): array {
        
$this->reset_state();
        
$transformed_settings = array();

        foreach ( 
$settings as $setting ) {
            
$this->process_setting$setting$transformed_settings );
        }
        
$this->finalize_transformation$transformed_settings );

        return 
$transformed_settings;
    }

    
/**
     * Process individual setting.
     *
     * @param array $setting Setting to process.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function process_setting( array $setting, array &$transformed_settings ): void {
        
$type $setting['type'] ?? '';

        if ( 
$this->current_checkbox_group && 'checkbox' !== $type ) {
            
// It's expected that a checkbox group will always be closed before a non-checkbox setting.
            // If not, it's likely a checkbox group was not closed properly so we flush the current checkbox group and add the setting as-is.
            
$this->flush_current_checkbox_group();
        }

        switch ( 
$type ) {
            case 
'title':
                
$this->handle_group_start$setting$transformed_settings );
                break;

            case 
'sectionend':
                
$this->handle_group_end$setting$transformed_settings );
                break;

            case 
'checkbox':
                
$this->handle_checkbox_setting$setting$transformed_settings );
                break;

            default:
                
$this->add_setting$setting$transformed_settings );
                break;
        }
    }

    
/**
     * Handle the start of a new group.
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function handle_group_start( array $setting, array &$transformed_settings ): void {
        
// If we already have a group, flush it to settings before starting a new one.
        
if ( $this->current_group ) {
            
$this->flush_current_group$transformed_settings );
        }

        
$this->current_group = array( $setting );
    }

    
/**
     * Handle the end of a group.
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function handle_group_end( array $setting, array &$transformed_settings ): void {
        
$ids_match $this->current_group &&
            isset( 
$this->current_group[0]['id'] ) &&
            isset( 
$setting['id'] ) &&
            
$this->current_group[0]['id'] === $setting['id'];

        
$ids_match_undefined $this->current_group &&
            ! isset( 
$this->current_group[0]['id'] ) &&
            ! isset( 
$setting['id'] );

        
// If IDs match, add the group and close it.
        
if ( $ids_match || $ids_match_undefined ) {
            
// Compose the group setting.
            
$title_setting          array_shift$this->current_group );
            
$transformed_settings[] = array_merge(
                
$title_setting,
                array(
                    
'type'     => 'group',
                    
'settings' => $this->current_group,
                )
            );
            
$this->current_group    null;
            return;
        }

        
// If IDs don't match, we don't need to transform anything so flush the current group.
        
$this->flush_current_group$transformed_settings );
        
$this->add_setting$setting$transformed_settings );
    }

    
/**
     * Flush current group to transformed settings.
     *
     * @param array $transformed_settings Transformed settings array.
     */
    
private function flush_current_group( array &$transformed_settings ): void {
        if ( 
is_array$this->current_group ) ) {
            
$transformed_settings array_merge$transformed_settings$this->current_group );
            
$this->current_group  null;
        }
    }

    
/**
     * Handle checkbox setting and grouping.
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function handle_checkbox_setting( array $setting, array &$transformed_settings ): void {
        
$checkboxgroup $setting['checkboxgroup'] ?? '';

        switch ( 
$checkboxgroup ) {
            case 
'start':
                
$this->start_checkbox_group$setting );
                break;

            case 
'end':
                
$this->end_checkbox_group$setting$transformed_settings );
                break;

            default:
                
$this->handle_checkbox_group_item$setting$transformed_settings );
                break;
        }
    }

    
/**
     * Start a new checkbox group.
     *
     * @param array $setting Setting to add.
     */
    
private function start_checkbox_group( array $setting ): void {
        
// If we already have an open checkbox group, flush it to settings before starting a new one.
        
if ( is_array$this->current_checkbox_group ) ) {
            
$this->flush_current_checkbox_group();
        }

        
$this->current_checkbox_group = array( $setting );
    }

    
/**
     * End current checkbox group.
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function end_checkbox_group( array $setting, array &$transformed_settings ): void {
        if ( empty( 
$this->current_checkbox_group ) ) {
            
// If we don't have an open checkbox group, add the setting as-is.
            
$this->add_setting$setting$transformed_settings );
            return;
        }

        
$this->current_checkbox_group[] = $setting;
        
$first_setting                  $this->current_checkbox_group[0];

        
$checkbox_group_setting = array(
            
'type'     => 'checkboxgroup',
            
'title'    => $first_setting['title'] ?? '',
            
'settings' => $this->current_checkbox_group,
        );

        
$this->add_setting$checkbox_group_setting$transformed_settings );
        
$this->current_checkbox_group null;
    }

    
/**
     * Handle checkbox within a group.
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function handle_checkbox_group_item( array $setting, array &$transformed_settings ): void {
        if ( 
is_array$this->current_checkbox_group ) ) {
            
$this->current_checkbox_group[] = $setting;
            return;
        }

        
// If we don't have an open checkbox group, add the setting as-is.
        
$this->add_setting$setting$transformed_settings );
    }

    
/**
     * Flush current checkbox group to transformed settings.
     */
    
private function flush_current_checkbox_group(): void {
        if ( 
is_array$this->current_checkbox_group ) ) {
            if ( 
is_array$this->current_group ) ) {
                
$this->current_group array_merge$this->current_group$this->current_checkbox_group );
            } else {
                
$this->current_group $this->current_checkbox_group;
            }

            
$this->current_checkbox_group null;
        }
    }

    
/**
     * Add setting to current context (group or root).
     *
     * @param array $setting Setting to add.
     * @param array $transformed_settings Transformed settings array.
     */
    
private function add_setting( array $setting, array &$transformed_settings ): void {
        if ( 
is_array$this->current_group ) ) {
            
$this->current_group[] = $setting;
            return;
        }

        
$transformed_settings[] = $setting;
    }

    
/**
     * Finalize the transformation process.
     *
     * @param array &$transformed_settings Transformed settings array.
     */
    
private function finalize_transformation( array &$transformed_settings ): void {
        
$this->flush_current_checkbox_group();
        
$this->flush_current_group$transformed_settings );
    }

    
/**
     * Reset the state to its initial values.
     */
    
public function reset_state(): void {
        
$this->current_group          null;
        
$this->current_checkbox_group null;
    }
}