/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Traits/OrderAttributionMeta.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
declare( strict_types=);

namespace 
Automattic\WooCommerce\Internal\Traits;

use 
Automattic\WooCommerce\Vendor\Detection\MobileDetect;
use 
Exception;
use 
WC_Meta_Data;
use 
WC_Order;
use 
WP_Post;

/**
 * Trait OrderAttributionMeta
 *
 * @since 8.5.0
 *
 * phpcs:disable Generic.Commenting.DocComment.MissingShort
 */
trait OrderAttributionMeta {

    
/**
     * The default fields and their sourcebuster accessors,
     * to show in the source data metabox.
     *
     * @var string[]
     * */
    
private $default_fields = array(
        
// main fields.
        
'source_type'          => 'current.typ',
        
'referrer'             => 'current_add.rf',

        
// utm fields.
        
'utm_campaign'         => 'current.cmp',
        
'utm_source'           => 'current.src',
        
'utm_medium'           => 'current.mdm',
        
'utm_content'          => 'current.cnt',
        
'utm_id'               => 'current.id',
        
'utm_term'             => 'current.trm',
        
'utm_source_platform'  => 'current.plt',
        
'utm_creative_format'  => 'current.fmt',
        
'utm_marketing_tactic' => 'current.tct',

        
// additional fields.
        
'session_entry'        => 'current_add.ep',
        
'session_start_time'   => 'current_add.fd',
        
'session_pages'        => 'session.pgs',
        
'session_count'        => 'udata.vst',
        
'user_agent'           => 'udata.uag',
    );

    
/** @var array */
    
private $fields = array();

    
/**
     * Cached `array_keys( $fields )`.
     *
     * @var array
     * */
    
private $field_names = array();

    
/** @var string */
    
private $field_prefix '';

    
/**
     * Get the device type based on the other meta fields.
     *
     * @param array $values The meta values.
     *
     * @return string The device type.
     */
    
protected function get_device_type( array $values ): string {
        
$detector = new MobileDetect( array(), $values['user_agent'] );

        if ( 
$detector->isMobile() ) {
            return 
'Mobile';
        } elseif ( 
$detector->isTablet() ) {
            return 
'Tablet';
        } else {
            return 
'Desktop';
        }
    }

    
/**
     * Set the fields and the field prefix.
     *
     * @return void
     */
    
private function set_fields_and_prefix() {
        
/**
         * Filter the fields to show in the source data metabox.
         *
         * @since 8.5.0
         *
         * @param string[] $fields The fields to show.
         */
        
$this->fields      = (array) apply_filters'wc_order_attribution_tracking_fields'$this->default_fields );
        
$this->field_names array_keys$this->fields );
        
$this->set_field_prefix();
    }

    
/**
     * Set the meta prefix for our fields.
     *
     * @return void
     */
    
private function set_field_prefix(): void {
        
/**
         * Filter the prefix for the meta fields.
         *
         * @since 8.5.0
         *
         * @param string $prefix The prefix for the meta fields.
         */
        
$prefix = (string) apply_filters(
            
'wc_order_attribution_tracking_field_prefix',
            
'wc_order_attribution_'
        
);

        
// Remove leading and trailing underscores.
        
$prefix trim$prefix'_' );

        
// Ensure the prefix ends with _, and set the prefix.
        
$this->field_prefix "{$prefix}_";
    }

    
/**
     * Filter an order's meta data to only the keys that we care about.
     *
     * Sets the origin value based on the source type.
     *
     * @param WC_Meta_Data[] $meta The meta data.
     *
     * @return array
     */
    
private function filter_meta_data( array $meta ): array {
        
$return = array();
        
$prefix $this->get_meta_prefixed_field_name'' );

        foreach ( 
$meta as $item ) {
            if ( 
str_starts_with$item->key$prefix ) ) {
                
$return$this->unprefix_meta_field_name$item->key ) ] = $item->value;
            }
        }

        
// Determine the device type from the user agent.
        
if ( ! array_key_exists'device_type'$return ) && array_key_exists'user_agent'$return ) ) {
            
$return['device_type'] = $this->get_device_type$return );
        }

        
// Determine the origin based on source type and referrer.
        
$source_type      $return['source_type'] ?? '';
        
$source           $return['utm_source'] ?? '';
        
$return['origin'] = $this->get_origin_label$source_type$sourcetrue );

        return 
$return;
    }

    
/**
     * Get the field name with the appropriate prefix.
     *
     * @param string $name Field name.
     *
     * @return string The prefixed field name.
     */
    
private function get_prefixed_field_name$name ): string {
        return 
"{$this->field_prefix}{$name}";
    }

    
/**
     * Get the field name with the meta prefix.
     *
     * @param string $name The field name.
     *
     * @return string The prefixed field name.
     */
    
private function get_meta_prefixed_field_namestring $name ): string {
        return 
"_{$this->get_prefixed_field_name$name )}";
    }

    
/**
     * Remove the meta prefix from the field name.
     *
     * @param string $name The prefixed fieldname .
     *
     * @return string
     */
    
private function unprefix_meta_field_namestring $name ): string {
        return 
str_replace"_{$this->field_prefix}"''$name );
    }

    
/**
     * Get the order object with HPOS compatibility.
     *
     * @param WC_Order|WP_Post|int $post_or_order The post ID or object.
     *
     * @return WC_Order The order object
     * @throws Exception When the order isn't found.
     */
    
private function get_hpos_order_object$post_or_order ) {
        
// If we've already got an order object, just return it.
        
if ( $post_or_order instanceof WC_Order ) {
            return 
$post_or_order;
        }

        
// If we have a post ID, get the post object.
        
if ( is_numeric$post_or_order ) ) {
            
$post_or_order wc_get_order$post_or_order );
        }

        
// Throw an exception if we don't have an order object.
        
if ( ! $post_or_order instanceof WC_Order ) {
            throw new 
Exception__'Order not found.''woocommerce' ) );
        }

        return 
$post_or_order;
    }


    
/**
     * Map posted, prefixed values to field values.
     * Used for the classic forms.
     *
     * @param array $raw_values The raw values from the POST form.
     *
     * @return array
     */
    
private function get_unprefixed_field_values( array $raw_values = array() ): array {
        
$values = array();

        
// Look through each field in POST data.
        
foreach ( $this->field_names as $field_name ) {
            
$values$field_name ] = $raw_values$this->get_prefixed_field_name$field_name ) ] ?? '(none)';
        }

        return 
$values;
    }

    
/**
     * Map submitted values to meta values.
     *
     * @param array $raw_values The raw (unprefixed) values from the submitted data.
     *
     * @return array
     */
    
private function get_source_values( array $raw_values = array() ): array {
        
$values = array();

        
// Look through each field in given data.
        
foreach ( $this->field_names as $field_name ) {
            
$value sanitize_text_fieldwp_unslash$raw_values$field_name ] ) );
            if ( 
'(none)' === $value ) {
                continue;
            }

            
$values$field_name ] = $value;
        }

        
// Set the device type if possible using the user agent.
        
if ( array_key_exists'user_agent'$values ) && ! empty( $values['user_agent'] ) ) {
            
$values['device_type'] = $this->get_device_type$values );
        }

        return 
$values;
    }

    
/**
     * Get the label for the Order origin with placeholder where appropriate. Can be
     * translated (for DB / display) or untranslated (for Tracks).
     *
     * @param string $source_type The source type.
     * @param string $source      The source.
     * @param bool   $translated  Whether the label should be translated.
     *
     * @return string
     */
    
private function get_origin_labelstring $source_typestring $sourcebool $translated true ): string {
        
// Set up the label based on the source type.
        
switch ( $source_type ) {
            case 
'utm':
                
$label $translated ?
                    
/* translators: %s is the source value */
                    
__'Source: %s''woocommerce' )
                    : 
'Source: %s';
                break;
            case 
'organic':
                
$label $translated ?
                    
/* translators: %s is the source value */
                    
__'Organic: %s''woocommerce' )
                    : 
'Organic: %s';
                break;
            case 
'referral':
                
$label $translated ?
                    
/* translators: %s is the source value */
                    
__'Referral: %s''woocommerce' )
                    : 
'Referral: %s';
                break;
            case 
'typein':
                
$label  '';
                
$source $translated ?
                    
__'Direct''woocommerce' )
                    : 
'Direct';
                break;
            case 
'mobile_app':
                
$label  '';
                
$source $translated ?
                    
__'Mobile app''woocommerce' )
                    : 
'Mobile app';
                break;
            case 
'admin':
                
$label  '';
                
$source $translated ?
                    
__'Web admin''woocommerce' )
                    : 
'Web admin';
                break;

            default:
                
$label  '';
                
$source $translated ?
                    
__'Unknown''woocommerce' )
                    : 
'Unknown';
                break;
        }

        
/**
         * Filter the formatted source for the order origin.
         *
         * @since 8.5.0
         *
         * @param string $formatted_source The formatted source.
         * @param string $source           The source.
         */
        
$formatted_source apply_filters(
            
'wc_order_attribution_origin_formatted_source',
            
ucfirsttrim$source'()' ) ),
            
$source
        
);

        
/**
         * Filter the label for the order origin.
         *
         * This label should have a %s placeholder for the formatted source to be inserted
         * via sprintf().
         *
         * @since 8.5.0
         *
         * @param string $label            The label for the order origin.
         * @param string $source_type      The source type.
         * @param string $source           The source.
         * @param string $formatted_source The formatted source.
         */
        
$label = (string) apply_filters(
            
'wc_order_attribution_origin_label',
            
$label,
            
$source_type,
            
$source,
            
$formatted_source
        
);

        if ( 
false === strpos$label'%' ) ) {
            return 
$formatted_source;
        }

        return 
sprintf$label$formatted_source );
    }

    
/**
     * Get the description for the order attribution field.
     *
     * @param string $field_name The field name.
     *
     * @return string
     */
    
private function get_field_descriptionstring $field_name ): string {
        
/* translators: %s is the field name */
        
$description sprintf__'Order attribution field: %s''woocommerce' ), $field_name );

        
/**
         * Filter the description for the order attribution field.
         *
         * @since 8.5.0
         *
         * @param string $description The description for the order attribution field.
         * @param string $field_name  The field name.
         */
        
return (string) apply_filters'wc_order_attribution_field_description'$description$field_name );
    }
}