PluginProbe
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress / trunk
Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress vtrunk
2.0.14 trunk 1.0 1.2 1.2.1 1.2.2 1.2.3 1.2.4 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
counter-box / classes / Admin / CreateFields.php

CreateFields.php in Counter Box – Add Countdowns, Timers & Dynamic Counters to WordPress trunk, at classes/Admin/CreateFields.php

452 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CounterBox\Admin;
4
5 class CreateFields {
6 /**
7 * @var mixed|string
8 */
9 private $order;
10 /**
11 * @var mixed
12 */
13 private $options;
14 /**
15 * @var mixed
16 */
17 private $page_options;
18
19 public function __construct( $options, $page_options ) {
20 $this->options = $options;
21 $this->page_options = $page_options;
22 }
23
24 public function create( $name, ...$order ): bool {
25 $this->order = $order;
26 if ( ! isset( $this->page_options[ $name ] ) ) {
27 return false;
28 }
29 $args = $this->page_options[ $name ];
30 $key = $this->get_key( $name );
31 $data_field = $name;
32 $default = $args['val'] ?? '';
33 if ( empty( $this->options['tool_id'] ) ) {
34 $default = $args['val'] ?? '';
35 }
36 $opt_key = $this->get_opt_key( $name );
37 $value = $this->get_the_value( $name, $default );
38 $class = ! empty( $args['class'] ) ? ' ' . $args['class'] : '';
39 $type = $args['type'] ?? 'text';
40 $label_class = isset( $args['label'] ) ? '' : 'screen-reader-text';
41 $label = ! empty( $args['label'] ) ? $args['label'] : '';
42 $atts = $this->get_attributes( $args, $value );
43 $addon = $this->get_addon( $args );
44 $checked = checked( "1", $value, false );
45
46 $template = '<div class="wpie-field{{class}}" data-field-box="' . esc_attr( $name ) . '">';
47 $template .= $this->get_title( $args );
48 if ( ! empty( $addon ) ) {
49 $template .= '<div class="wpie-field__group">';
50 $template .= $this->get_template( $args );
51 $template .= '</div>';
52 } else {
53 $template .= $this->get_template( $args );
54 }
55
56 $template .= '</div>';
57
58 $content = str_replace( [
59 '{{class}}',
60 '{{type}}',
61 '{{name}}',
62 '{{value}}',
63 '{{atts}}',
64 '{{label_class}}',
65 '{{label}}',
66 '{{addon}}',
67 '{{checked}}',
68 '{{data_field}}',
69
70 ], [
71 $class,
72 esc_attr( $type ),
73 esc_attr( $key ),
74 esc_attr( $value ),
75 $atts,
76 esc_attr( $label_class ),
77 esc_html( $label ),
78 $addon,
79 $checked,
80 $data_field
81
82 ], $template );
83
84 $this->output( $content );
85
86 return true;
87 }
88
89 private function output( $content ): void {
90 $allowed_html = array(
91 'label' => array(
92 'class' => [],
93 ),
94 'span' => array(
95 'class' => [],
96 ),
97 'sup' => [
98 'class' => [],
99 'data-tooltip' => [],
100 ],
101 'input' => array(
102 'type' => [],
103 'data-field' => [],
104 'name' => [],
105 'value' => [],
106 'placeholder' => [],
107 'class' => [],
108 'checked' => [],
109 'readonly' => [],
110 'disabled' => [],
111 'min' => [],
112 'max' => [],
113 'step' => [],
114 'data-alpha-enabled' => [],
115 ),
116 'textarea' => [
117 'name' => [],
118 'value' => [],
119 'placeholder' => [],
120 'data-field' => [],
121 'class' => [],
122 ],
123 'div' => [
124 'data-field-box' => [],
125 'class' => [],
126 ],
127 'select' => [
128 'name' => [],
129 'data-field' => [],
130
131 ],
132 'option' => [
133 'value' => [],
134 'selected' => [],
135 ],
136 'optgroup' => [
137 'label' => [],
138 ],
139
140 );
141
142 echo wp_kses( $content, $allowed_html );
143 }
144
145 private function get_template( $args ): string {
146 if ( $args['type'] === 'select' ) {
147 return $this->select_template();
148 }
149
150 if ( $args['type'] === 'checkbox' ) {
151 return $this->checkbox_template();
152 }
153
154 if ( $args['type'] === 'textarea' ) {
155 return $this->textarea_template();
156 }
157
158 if ( $args['type'] === 'editor' ) {
159 return $this->editor_template();
160 }
161
162 return $this->text_template();
163 }
164
165 private function get_title( $args ) {
166 if ( empty( $args['title'] ) ) {
167 return '';
168 }
169
170 if ( is_string( $args['title'] ) ) {
171 $title = '<div class="wpie-field__title">';
172 $title .= esc_html( $args['title'] );
173 if ( isset( $args['tooltip'] ) ) {
174 $title .= '<sup class="has-tooltip wpie-color-dark" data-tooltip="' . esc_attr( $args['tooltip'] ) . '">ℹ</sup>';
175 }
176 $title .= '</div>';
177
178 return $title;
179 }
180
181 if ( is_array( $args['title'] ) ) {
182 return $this->get_checkbox_title( $args['title'] );
183 }
184
185 return '';
186 }
187
188 private function get_checkbox_title( $args ) {
189 $key = $this->get_key( $args['name'] );
190 $data_field = $args['name'] ?? 'wpie-field';
191
192 $default = $args['val'] ?? '';
193 if ( empty( $this->options['id'] ) ) {
194 $default = $args['val'] ?? '';
195 }
196 // $value = $this->options[ $key ] ?? $default;
197 $value = $this->get_the_value( $args['name'], $default );
198 $label_class = isset( $args['label'] ) ? '' : 'screen-reader-text';
199 $label = ! empty( $args['label'] ) ? $args['label'] : '';
200 $checked = checked( "1", $value, false );
201 $toogle = isset( $args['toggle'] ) ? ' has-checked' : '';
202 $tooltip = isset($args['tooltip']) ? '<sup class="has-tooltip wpie-color-dark" data-tooltip="' . esc_attr( $args['tooltip'] ) . '">ℹ</sup>' : '';
203 $template = '<div class="wpie-field__title' . esc_attr( $toogle ) . '">
204 <label class="wpie-field__title-label">
205 <input type="checkbox" data-field="{{data_field}}" {{checked}}>
206 <input type="hidden" name="{{name}}" value="' . esc_attr( $value ) . '">
207 <span class="{{label_class}}">{{label}} {{tooltip}}</span></label></div>';
208
209 return str_replace( [
210 '{{name}}',
211 '{{checked}}',
212 '{{label_class}}',
213 '{{label}}',
214 '{{data_field}}',
215 '{{tooltip}}',
216
217 ], [
218 esc_attr( $key ),
219 $checked,
220 esc_attr( $label_class ),
221 esc_html( $label ),
222 $data_field,
223 $tooltip
224
225 ], $template );
226 }
227
228
229 private function get_addon( $args ) {
230 if ( empty( $args['addon'] ) ) {
231 return '';
232 }
233
234 if ( is_string( $args['addon'] ) ) {
235 return '<span class="wpie-field__label is-addon">' . esc_html( $args['addon'] ) . '</span>';
236 }
237
238 if ( is_array( $args['addon'] ) ) {
239 return $this->get_addon_select( $args['addon'] );
240 }
241
242 return '';
243 }
244
245 private function get_addon_select( $args ) {
246 $label_class = isset( $args['label'] ) ? '' : 'screen-reader-text';
247 $label = ! empty( $args['label'] ) ? $args['label'] : '';
248 $key = $this->get_key( $args['name'] );
249 $data_field = $args['name'] ?? 'wpie-field';
250 $default = $args['val'] ?? '';
251 $value = $this->get_the_value( $args['name'], $default );
252 $atts = $this->get_attributes( $args, $value );
253 $template = '<label class="wpie-field__label">
254 <span class="{{label_class}}">{{label}}</span>
255 <select name="{{name}}" data-field="{{data_field}}">
256 {{atts}}
257 </select>
258 </label>';
259
260 return str_replace( [
261 '{{name}}',
262 '{{atts}}',
263 '{{label_class}}',
264 '{{label}}',
265 '{{data_field}}',
266
267 ], [
268 esc_attr( $key ),
269 $atts,
270 esc_attr( $label_class ),
271 esc_html( $label ),
272 $data_field
273
274 ], $template );
275 }
276
277
278 private function get_attributes( $args, $value ) {
279 if ( empty( $args['atts'] ) || ! is_array( $args['atts'] ) ) {
280 return false;
281 }
282 $atts = '';
283
284 foreach ( $args['atts'] as $key => $val ) {
285 if ( $args['type'] === 'select' ) {
286 if ( strrpos( $key, '_start' ) ) {
287 $atts .= '<optgroup label="' . esc_attr( $val ) . '">';
288 } elseif ( strrpos( $key, '_end' ) ) {
289 $atts .= '</optgroup>';
290 } else {
291 $atts .= '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key,
292 false ) . '>' . esc_html( $val ) . '</option>';
293 }
294 } else {
295 $atts .= ' ' . esc_attr( $key ) . '="' . esc_attr( $val ) . '"';
296 }
297 }
298
299
300 return $atts;
301 }
302
303 private function get_the_value( $name, $default = '' ) {
304
305 if ( empty( $this->order ) || ! is_array( $this->order ) ) {
306 return $this->options[ $name ] ?? $default;
307 }
308
309 if ( strpos( $name, '-' ) !== false ) {
310 $parts = explode( '-', $name );
311 if ( empty( $this->options[ $parts[0] ][ $parts[1] ] ) ) {
312 $value = [];
313 } else {
314 $value = $this->options[ $parts[0] ][ $parts[1] ];
315 }
316 } else {
317 if ( empty( $this->options[ $name ] ) ) {
318 $value = [];
319 } else {
320 $value = $this->options[ $name ];
321 }
322 }
323
324 foreach ( $this->order as $order ) {
325 if ( is_numeric( $order ) ) {
326 if ( isset( $value[ $order ] ) && is_array( $value ) ) {
327 $value = &$value[ $order ];
328 } else {
329 return $default;
330 }
331 }
332 }
333
334 return $value ?? $default;
335 }
336
337 private function get_opt_key( $name ) {
338 if ( empty( $this->order ) ) {
339 return $name;
340 }
341
342 if ( ! is_array( $this->order ) ) {
343 return $name;
344 }
345
346 $key = $name;
347
348 foreach ( $this->order as $order ) {
349 if ( is_numeric( $order ) && $order >= 0 ) {
350 $key .= '[' . $order . ']';
351 } elseif ( is_string( $order ) ) {
352 $key .= '[' . $order . ']';
353 } else {
354 $key .= '[]';
355 }
356 }
357
358 return $key;
359 }
360
361 private function get_key( $name ) {
362
363 if ( strpos( $name, '-' ) !== false ) {
364 $parts = explode( '-', $name );
365 $arr_name = '';
366 foreach ( $parts as $partkey => $part ) {
367 if ( $partkey === 0 ) {
368 $arr_name .= 'param[' . $part . ']';
369 continue;
370 }
371 $arr_name .= '[' . esc_attr( $part ) . ']';
372 }
373 $name = $arr_name;
374 }
375
376
377 if ( empty( $this->order ) ) {
378 if ( strpos( $name, 'param[' ) !== false ) {
379 return $name;
380 }
381
382 return 'param[' . $name . ']';
383 }
384
385 if ( ! is_array( $this->order ) ) {
386 if ( strpos( $name, 'param[' ) !== false ) {
387 return $name;
388 }
389
390 return 'param[' . $name . ']';
391 }
392
393 $key = ( strpos( $name, 'param[' ) !== false ) ? $name : 'param[' . $name . ']';
394
395 foreach ( $this->order as $order ) {
396 if ( is_string( $order ) ) {
397 $key .= '[' . esc_attr( $order ) . ']';
398 } else {
399 $key .= '[]';
400 }
401 }
402
403 return $key;
404 }
405
406 private function text_template(): string {
407 return '
408 <label class="wpie-field__label">
409 <input type="{{type}}" name="{{name}}" value="{{value}}" data-field="{{data_field}}" {{atts}}>
410 <span class="{{label_class}}">{{label}}</span>
411 </label>
412 {{addon}}
413 ';
414 }
415
416 private function select_template(): string {
417 return '
418 <label class="wpie-field__label">
419 <span class="{{label_class}}">{{label}}</span>
420 <select name="{{name}}" data-field="{{data_field}}">
421 {{atts}}
422 </select>
423 </label>
424 ';
425 }
426
427 private function checkbox_template(): string {
428 return '<label class="wpie-field__label">
429 <input type="checkbox" data-field="{{data_field}}" {{checked}}>
430 <input type="hidden" name="{{name}}" value="{{value}}">
431 <span class="{{label_class}}">{{label}}</span>
432 </label>';
433 }
434
435 private function textarea_template(): string {
436 return '
437 <label class="wpie-field__label">
438 <textarea name="{{name}}" data-field="{{data_field}}" {{atts}}>{{value}}</textarea>
439 <span class="{{label_class}}">{{label}}</span>
440 </label>
441 ';
442 }
443
444 private function editor_template() {
445 return '
446 <div class="wpie-field__label">
447 <textarea name="{{name}}" data-field="{{data_field}}" {{atts}}>{{value}}</textarea>
448 <span class="{{label_class}}">{{label}}</span>
449 </div>
450 ';
451 }
452 }