PluginProbe
Float menu – awesome floating side menu / 7.1
Float menu – awesome floating side menu v7.1
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / classes / Admin / CreateFields.php

CreateFields.php in Float menu – awesome floating side menu 7.1, at classes/Admin/CreateFields.php

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