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
|
<?php // phpcs:ignoreFile
namespace AutomateWoo\Fields;
use AutomateWoo\Clean;
/** * @class Field */ abstract class Field {
/** @var string - deprecated, use $title */ protected $default_title;
/** @var string - deprecated, use $name */ protected $default_name;
/** @var string */ protected $title;
/** @var string */ protected $name;
/** @var string */ protected $type;
/** @var string */ protected $description;
/** @var string trigger or action */ protected $name_base;
/** @var bool */ protected $required = false;
/** @var array */ protected $classes = [];
/** * Extra attributes that will appended to the HTML field element. * * @var array */ protected $extra_attrs = [];
/** @var string */ protected $placeholder = '';
/** * Field meta data. * * This prop can be used when misc data needs to be added to the field. * Not to be confused with $this->extra_attrs. * * @since 4.6.0 * * @var array */ public $meta = [];
/** * Output the field HTML. * * @param mixed $value */ abstract function render( $value );
/** * Field constructor. */ function __construct() { $this->classes[] = 'automatewoo-field'; $this->classes[] = 'automatewoo-field--type-' . $this->type; }
/** * @param $name * @return $this */ function set_name( $name ) { $this->name = $name; return $this; }
/** * @param $title * @return $this */ function set_title( $title ) { $this->title = $title; return $this; }
/** * @return string */ function get_title() { return $this->title ? $this->title :$this->default_title; }
/** * @return string */ function get_name() { return $this->name ? $this->name :$this->default_name; }
/** * @return string */ function get_type() { return $this->type; }
/** * @param $description * @return $this */ function set_description( $description ) { $this->description = $description; return $this; }
/** * @return string */ function get_description() { return $this->description; }
/** * @param $placeholder string * @return $this */ function set_placeholder( $placeholder ) { $this->placeholder = $placeholder; return $this; }
/** * @return string */ function get_placeholder() { return $this->placeholder; }
/** * @param $classes string * @return $this */ function add_classes( $classes ) { $this->classes = array_merge( $this->classes, explode( ' ', $classes ) ); return $this; }
/** * @param bool $implode * @return array|string */ function get_classes( $implode = true ) { if ( $implode ) { return implode( ' ', $this->classes ); } return $this->classes; }
/** * @param $name * @param $value * @return $this */ function add_extra_attr( $name, $value = null ) { $this->extra_attrs[$name] = $value; return $this; }
/** * @param $name * @return bool */ function has_data_attr( $name ) { return isset( $this->extra_attrs[ 'data-' . $name ] ); }
/** * @param $name * @param $value * @return $this */ function add_data_attr( $name, $value = null ) { $this->add_extra_attr( 'data-' . $name, $value ); return $this; }
/** * Outputs the extra field attrs in HTML attribute format. */ function output_extra_attrs() { $string = '';
foreach ( $this->extra_attrs as $name => $value ) { if ( is_null( $value ) ) { $string .= esc_attr( $name ) . ' '; } else { $string .= esc_attr( $name ) . '="' . esc_attr( $value ) . '" '; } }
echo $string; }
/** * @param bool $required * @return $this */ function set_required( $required = true ) { $this->required = $required; return $this; }
/** * @return bool */ function get_required() { return $this->required; }
/** * @return $this */ function set_disabled() { $this->add_extra_attr( 'disabled', 'true' ); return $this; }
/** * @param $name_base * @return $this */ function set_name_base( $name_base ) { $this->name_base = $name_base; return $this; }
/** * @return bool */ function get_name_base() { return $this->name_base; }
/** * @return string */ function get_full_name() { return ( $this->get_name_base() ? $this->get_name_base() . '[' . $this->get_name() . ']' : $this->get_name() ); }
/** * @param string $options * @return $this */ function set_variable_validation( $options = '' ) { $this->set_validation( 'variables ' . $options ); return $this; }
/** * If $options is left blank then the field not support variables * * @param string $options * @return $this */ function set_validation( $options = '' ) { $this->add_data_attr( 'automatewoo-validate', $options ); return $this; }
/** * Sanitizes the value of the field. * * This method runs before WRITING a value to the DB but doesn't run before READING. * * Defaults to sanitize as a single line string. Override this method for fields that should be sanitized differently. * * @since 4.4.0 * * @param string $value * * @return string */ function sanitize_value( $value ) { return Clean::string( $value ); }
}
|