/var/www/html/wp-content/plugins/woocommerce/includes/admin/wc-meta-box-functions.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
<?php
/**
 * WooCommerce Meta Box Functions
 *
 * @author      WooThemes
 * @category    Core
 * @package     WooCommerce\Admin\Functions
 * @version     2.3.0
 */

use Automattic\WooCommerce\Utilities\OrderUtil;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly
}

/**
 * Output a text input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_text_input$field, ?WC_Data $data null ) {
    global 
$post;

    
$field['placeholder']   = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
    
$field['class']         = isset( $field['class'] ) ? $field['class'] : 'short';
    
$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    
$field['value']         = $field['value'] ?? OrderUtil::get_post_or_object_meta$post$data$field['id'], true );
    
$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    
$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
    
$field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
    
$data_type              = empty( $field['data_type'] ) ? '' $field['data_type'];

    switch ( 
$data_type ) {
        case 
'price':
            
$field['class'] .= ' wc_input_price';
            
$field['value']  = wc_format_localized_price$field['value'] );
            break;
        case 
'decimal':
            
$field['class'] .= ' wc_input_decimal';
            
$field['value']  = wc_format_localized_decimal$field['value'] );
            break;
        case 
'stock':
            
$field['class'] .= ' wc_input_stock';
            
$field['value']  = wc_stock_amount$field['value'] );
            break;
        case 
'url':
            
$field['class'] .= ' wc_input_url';
            
$field['value']  = esc_url$field['value'] );
            break;

        default:
            break;
    }

    
// Custom attribute handling
    
$custom_attributes = array();

    if ( ! empty( 
$field['custom_attributes'] ) && is_array$field['custom_attributes'] ) ) {

        foreach ( 
$field['custom_attributes'] as $attribute => $value ) {
            
$custom_attributes[] = esc_attr$attribute ) . '="' esc_attr$value ) . '"';
        }
    }

    echo 
'<p class="form-field ' esc_attr$field['id'] ) . '_field ' esc_attr$field['wrapper_class'] ) . '">
        <label for="' 
esc_attr$field['id'] ) . '">' wp_kses_post$field['label'] ) . '</label>';

    if ( ! empty( 
$field['description'] ) && false !== $field['desc_tip'] ) {
        echo 
wc_help_tip$field['description'] );
    }

    echo 
'<input type="' esc_attr$field['type'] ) . '" class="' esc_attr$field['class'] ) . '" style="' esc_attr$field['style'] ) . '" name="' esc_attr$field['name'] ) . '" id="' esc_attr$field['id'] ) . '" value="' esc_attr$field['value'] ) . '" placeholder="' esc_attr$field['placeholder'] ) . '" ' implode' '$custom_attributes ) . ' /> ';

    if ( ! empty( 
$field['description'] ) && false === $field['desc_tip'] ) {
        echo 
'<span class="description">' wp_kses_post$field['description'] ) . '</span>';
    }

    echo 
'</p>';
}

/**
 * Output a hidden input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_hidden_input$field, ?WC_Data $data null ) {
    global 
$post;

    
$field['value'] = isset( $field['value'] ) ? $field['value'] : OrderUtil::get_post_or_object_meta$post$data$field['id'], true );
    
$field['class'] = isset( $field['class'] ) ? $field['class'] : '';

    echo 
'<input type="hidden" class="' esc_attr$field['class'] ) . '" name="' esc_attr$field['id'] ) . '" id="' esc_attr$field['id'] ) . '" value="' esc_attr$field['value'] ) . '" /> ';
}

/**
 * Output a textarea input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_textarea_input$field, ?WC_Data $data null ) {
    global 
$post;

    
$field['placeholder']   = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
    
$field['class']         = isset( $field['class'] ) ? $field['class'] : 'short';
    
$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    
$field['value']         = $field['value'] ?? OrderUtil::get_post_or_object_meta$post$data$field['id'], true );
    
$field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
    
$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    
$field['rows']          = isset( $field['rows'] ) ? $field['rows'] : 2;
    
$field['cols']          = isset( $field['cols'] ) ? $field['cols'] : 20;

    
// Custom attribute handling
    
$custom_attributes = array();

    if ( ! empty( 
$field['custom_attributes'] ) && is_array$field['custom_attributes'] ) ) {

        foreach ( 
$field['custom_attributes'] as $attribute => $value ) {
            
$custom_attributes[] = esc_attr$attribute ) . '="' esc_attr$value ) . '"';
        }
    }

    echo 
'<p class="form-field ' esc_attr$field['id'] ) . '_field ' esc_attr$field['wrapper_class'] ) . '">
        <label for="' 
esc_attr$field['id'] ) . '">' wp_kses_post$field['label'] ) . '</label>';

    if ( ! empty( 
$field['description'] ) && false !== $field['desc_tip'] ) {
        echo 
wc_help_tip$field['description'] );
    }

    echo 
'<textarea class="' esc_attr$field['class'] ) . '" style="' esc_attr$field['style'] ) . '"  name="' esc_attr$field['name'] ) . '" id="' esc_attr$field['id'] ) . '" placeholder="' esc_attr$field['placeholder'] ) . '" rows="' esc_attr$field['rows'] ) . '" cols="' esc_attr$field['cols'] ) . '" ' implode' '$custom_attributes ) . '>' esc_textarea$field['value'] ) . '</textarea> ';

    if ( ! empty( 
$field['description'] ) && false === $field['desc_tip'] ) {
        echo 
'<span class="description">' wp_kses_post$field['description'] ) . '</span>';
    }

    echo 
'</p>';
}

/**
 * Output a checkbox input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_checkbox$field, ?WC_Data $data null ) {
    global 
$post;

    
$field['class']         = isset( $field['class'] ) ? $field['class'] : 'checkbox';
    
$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    
$field['value']         = $field['value'] ?? OrderUtil::get_post_or_object_meta$post$data$field['id'], true );
    
$field['cbvalue']       = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
    
$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    
$field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;

    
/**
     * These values are what get passed vis $_POST depending on if the field is checked or not. If no unchecked_value is
     * provided, the $_POST will not be set. This maintains backwards compatibility where consumers would use `isset`.
     */
    
$field['checked_value']   = isset( $field['checked_value'] ) ? $field['checked_value'] : $field['cbvalue'];
    
$field['unchecked_value'] = isset( $field['unchecked_value'] ) ? $field['unchecked_value'] : null;

    
// Custom attribute handling.
    
$custom_attributes = array();

    if ( ! empty( 
$field['custom_attributes'] ) && is_array$field['custom_attributes'] ) ) {

        foreach ( 
$field['custom_attributes'] as $attribute => $value ) {
            
$custom_attributes[] = esc_attr$attribute ) . '="' esc_attr$value ) . '"';
        }
    }

    if ( ! empty( 
$field['style'] ) ) {
        
$custom_attributes[] = 'style="' esc_attr$field['style'] ) . '"';
    }

    if ( ! empty( 
$field['class'] ) ) {
        
$custom_attributes[] = 'class="' esc_attr$field['class'] ) . '"';
    }

    echo 
'<p class="form-field ' esc_attr$field['id'] ) . '_field ' esc_attr$field['wrapper_class'] ) . '">
        <label for="' 
esc_attr$field['id'] ) . '">' wp_kses_post$field['label'] ) . '</label>';

    if ( ! empty( 
$field['description'] ) && false !== $field['desc_tip'] ) {
        echo 
wc_help_tip$field['description'] );
    }

    
// Output a hidden field so a value is POSTed if the box is not checked.
    
if ( ! is_null$field['unchecked_value'] ) ) {
        
printf'<input type="hidden" name="%1$s" value="%2$s" />'esc_attr$field['name'] ), esc_attr$field['unchecked_value'] ) );
    }

    
printf(
        
'<input type="checkbox" name="%1$s" id="%2$s" value="%3$s" %4$s %5$s />',
        
esc_attr$field['name'] ),
        
esc_attr$field['id'] ),
        
esc_attr$field['checked_value'] ),
        
checked$field['value'], $field['checked_value'], false ),
        
implode' '$custom_attributes // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    
);

    if ( ! empty( 
$field['description'] ) && false === $field['desc_tip'] ) {
        echo 
'<span class="description">' wp_kses_post$field['description'] ) . '</span>';
    }

    echo 
'</p>';
}

/**
 * Output a select input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_select$field, ?WC_Data $data null ) {
    global 
$post;

    
$field wp_parse_args(
        
$field,
        array(
            
'class'             => 'select short',
            
'style'             => '',
            
'wrapper_class'     => '',
            
'value'             => OrderUtil::get_post_or_object_meta$post$data$field['id'], true ),
            
'name'              => $field['id'],
            
'desc_tip'          => false,
            
'custom_attributes' => array(),
        )
    );

    
$wrapper_attributes = array(
        
'class' => $field['wrapper_class'] . " form-field {$field['id']}_field",
    );

    
$label_attributes = array(
        
'for' => $field['id'],
    );

    
$field_attributes          = (array) $field['custom_attributes'];
    
$field_attributes['style'] = $field['style'];
    
$field_attributes['id']    = $field['id'];
    
$field_attributes['name']  = $field['name'];
    
$field_attributes['class'] = $field['class'];

    
$tooltip     = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : '';
    
$description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : '';
    
?>
    <p <?php echo wc_implode_html_attributes$wrapper_attributes ); // WPCS: XSS ok. ?>>
        <label <?php echo wc_implode_html_attributes$label_attributes ); // WPCS: XSS ok. ?>><?php echo wp_kses_post$field['label'] ); ?></label>
        <?php if ( $tooltip ) : ?>
            <?php echo wc_help_tip$tooltip ); // WPCS: XSS ok. ?>
        <?php endif; ?>
        <select <?php echo wc_implode_html_attributes$field_attributes ); // WPCS: XSS ok. ?>>
            <?php
            
foreach ( $field['options'] as $key => $value ) {
                echo 
'<option value="' esc_attr$key ) . '"' wc_selected$key$field['value'] ) . '>' esc_html$value ) . '</option>';
            }
            
?>
        </select>
        <?php if ( $description ) : ?>
            <span class="description"><?php echo wp_kses_post$description ); ?></span>
        <?php endif; ?>
    </p>
    <?php
}

/**
 * Output a radio input box.
 *
 * @param array        $field Field data.
 * @param WC_Data|null $data  WC_Data object, will be preferred over post object when passed.
 */
function woocommerce_wp_radio$field, ?WC_Data $data null ) {
    global 
$post;

    
$field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
    
$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    
$field['value']         = $field['value'] ?? OrderUtil::get_post_or_object_meta$post$data$field['id'], true );
    
$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    
$field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;

    echo 
'<fieldset class="form-field ' esc_attr$field['id'] ) . '_field ' esc_attr$field['wrapper_class'] ) . '"><legend>' wp_kses_post$field['label'] ) . '</legend>';

    if ( ! empty( 
$field['description'] ) && false !== $field['desc_tip'] ) {
        echo 
wc_help_tip$field['description'] );
    }

    echo 
'<ul class="wc-radios">';

    foreach ( 
$field['options'] as $key => $value ) {

        echo 
'<li><label><input
                name="' 
esc_attr$field['name'] ) . '"
                value="' 
esc_attr$key ) . '"
                type="radio"
                class="' 
esc_attr$field['class'] ) . '"
                style="' 
esc_attr$field['style'] ) . '"
                ' 
checkedesc_attr$field['value'] ), esc_attr$key ), false ) . '
                /> ' 
esc_html$value ) . '</label>
        </li>'
;
    }
    echo 
'</ul>';

    if ( ! empty( 
$field['description'] ) && false === $field['desc_tip'] ) {
        echo 
'<span class="description">' wp_kses_post$field['description'] ) . '</span>';
    }

    echo 
'</fieldset>';
}

/**
 * Output a note.
 *
 * @param array $field Field data.
 */
function woocommerce_wp_note$field ) {
    
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';

    echo 
'<p class="form-field ' esc_attr$field['wrapper_class'] ) . '">';
    echo 
'<label for="' esc_attr$field['id'] ) . '" ';

    if ( ! empty( 
$field['label-aria-label'] ) ) {
        echo 
'aria-label="' esc_attr$field['label-aria-label'] ) . '"';
    }

    echo 
'>' esc_attr$field['label'] ) . '</label>';
    echo 
'<output name="' esc_attr$field['id'] ) . '" id="' esc_attr$field['id'] ) . '" aria-live="off">' wp_kses_post$field['message'] ) . '</output>';
    echo 
'</p>';
}