-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.php
144 lines (115 loc) · 3.92 KB
/
field.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
<?php
// Exits if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Sets field data.
$field_classes = [];
$field_additional_attrs = [];
$field_placeholder = $args[ 'placeholder' ] ?? null;
$field_value = ymfo_get_option( $args[ 'page_slug_tale' ], $args[ 'field_slug_tale' ] );
$field_print_mask = '<input type="%1$s" name="%2$s" id="%3$s" class="%4$s" %5$s placeholder="%6$s" value="%7$s" %8$s>';
// Changes field data by type.
switch ( $args[ 'field_type' ] ) {
// Text.
case 'text':
array_push( $field_classes, 'regular-text' );
break;
// Textarea.
case 'textarea':
$field_additional_attrs[ 'rows' ] = $args[ 'rows' ] ?? 2;
$field_print_mask = '<textarea name="%2$s" id="%3$s" class="%4$s" %5$s placeholder="%6$s" %8$s>%7$s</textarea>';
array_push( $field_classes, 'regular-text' );
break;
// Number.
case 'number':
if ( isset( $args[ 'min' ] ) ) {
$field_additional_attrs[ 'min' ] = $args[ 'min' ];
}
if ( isset( $args[ 'max' ] ) ) {
$field_additional_attrs[ 'max' ] = $args[ 'max' ];
}
if ( isset( $args[ 'step' ] ) ) {
$field_additional_attrs[ 'step' ] = $args[ 'step' ];
}
break;
// Select.
case 'select':
$field_print_mask = '<select name="%2$s" id="%3$s" %5$s %8$s>';
foreach ( $args[ 'options' ] as $option ) {
$value = $option[ 'value' ];
$label = $option[ 'label' ];
$field_print_mask .= sprintf( '<option value="%s" %s>%s</option>',
esc_attr( $value ),
selected( $field_value, $value, false ),
esc_html( $label ),
);
}
$field_print_mask .= '</select>';
break;
// Checkbox.
case 'checkbox':
if ( checked( $field_value, true, false ) ) {
$field_additional_attrs[ 'checked' ] = 'checked';
}
$field_print_mask = '<label for="%3$s">';
$field_print_mask .= '<input type="%1$s" name="%2$s" id="%3$s" %5$s value="1" %8$s> ' . $args[ 'description' ] ?? '';
$field_print_mask .= '</label>';
break;
// Radio.
case 'radio':
$field_print_mask = '<fieldset>';
$field_print_mask .= '<legend class="screen-reader-text"><span>' . $args[ 'field_title' ] . '</span></legend>';
foreach ( $args[ 'options' ] as $option ) {
$value = $option[ 'value' ];
$label = $option[ 'label' ];
$field_print_mask .= "<label><input type=\"%1$s\" name=\"%2$s\" value=\"$value\"" . checked( $field_value, $value, false ) . "><span>$label</span></label><br>";
}
$field_print_mask .= '</fieldset>';
break;
// Tel.
case 'tel':
array_push( $field_classes, 'regular-text' );
break;
// Email.
case 'email':
array_push( $field_classes, 'regular-text' );
break;
// URL.
case 'url':
array_push( $field_classes, 'regular-text' );
array_push( $field_classes, 'code' );
break;
// Image.
case 'image':
$field_print_mask = '';
include 'field-image.php';
break;
}
// Formats additional arguments.
$field_additional_attrs_output = [];
foreach ( $field_additional_attrs as $attr => $value ) {
$field_additional_attrs_output[] = $attr . ( ! is_null( $value ) ? esc_attr( "=$value" ) : '' );
}
/** Print field */
printf( $field_print_mask,
esc_attr( $args[ 'field_type' ] ), // 1. Type.
esc_attr( $args[ 'field_name' ] ), // 2. Name.
esc_attr( $args[ 'field_id' ] ), // 3. ID.
esc_attr( implode( ' ', $field_classes ) ), // 4. Class.
implode( ' ', $field_additional_attrs_output ), // 5. Additional attributes.
esc_attr( $field_placeholder ), // 6. Placeholder.
esc_attr( $field_value ), // 7. Value.
esc_attr( $args[ 'is_field_required' ] ? 'required' : '' ), // 8. Required.
);
// Prints description.
$print_description = true;
if ( ! isset( $args[ 'description' ] ) ) {
$print_description = false;
}
if ( $args[ 'field_type' ] == 'checkbox' ) {
$print_description = false;
}
// Prints field.
printf( '<p class="description"><code class="ymfo-copyable">%s</code>%s</p>',
esc_html( $args[ 'field_slug_tale' ] ),
wp_kses_post( $print_description ? " – {$args[ 'description' ]}" : '' ),
);