PluginProbe
Float menu – awesome floating side menu / 7.0.4
Float menu – awesome floating side menu v7.0.4
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.0.4, at classes/Admin/CreateFields.php

447 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 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
129 );
130 $allowed_post_tags[] = $allowed_html;
131 $merged_allowed_tags = array_merge( $allowed_post_tags, $allowed_html );
132
133 echo wp_kses( $content, $merged_allowed_tags );
134 }
135
136 private function get_template( $args ): string {
137 if ( $args['type'] === 'select' ) {
138 return $this->select_template();
139 }
140
141 if ( $args['type'] === 'checkbox' ) {
142 return $this->checkbox_template();
143 }
144
145 if ( $args['type'] === 'textarea' ) {
146 return $this->textarea_template();
147 }
148
149 if ( $args['type'] === 'editor' ) {
150 return $this->editor_template();
151 }
152
153 return $this->text_template();
154 }
155
156 private function get_title( $args ) {
157 if ( empty( $args['title'] ) ) {
158 return '';
159 }
160
161 if ( is_string( $args['title'] ) ) {
162 $title = '<div class="wpie-field__title">';
163 $title .= esc_html( $args['title'] );
164 if ( isset( $args['tooltip'] ) ) {
165 $title .= '<sup class="has-tooltip wpie-color-dark" data-tooltip="' . esc_attr( $args['tooltip'] ) . '">ℹ</sup>';
166 }
167 $title .= '</div>';
168
169 return $title;
170 }
171
172 if ( is_array( $args['title'] ) ) {
173 return $this->get_checkbox_title( $args['title'] );
174 }
175
176 return '';
177 }
178
179 private function get_checkbox_title( $args ) {
180 $key = $this->get_key( $args['name'] );
181 $data_field = $args['name'] ?? 'wpie-field';
182
183 $opt_val = $args['val'] ?? $args['value'] ?? '';
184 $default = $opt_val;
185
186 if (empty($this->options['tool_id'])) {
187 $default = $opt_val;
188 }
189
190 // $value = $this->options[ $key ] ?? $default;
191 $value = $this->get_the_value( $args['name'], $default );
192 $label_class = isset( $args['label'] ) ? '' : 'screen-reader-text';
193 $label = ! empty( $args['label'] ) ? $args['label'] : '';
194 $checked = checked( "1", $value, false );
195 $toogle = isset( $args['toggle'] ) ? ' has-checked' : '';
196 $tooltip = isset( $args['tooltip'] ) ? '<sup class="has-tooltip wpie-color-dark" data-tooltip="' . esc_attr( $args['tooltip'] ) . '">ℹ</sup>' : '';
197 $template = '<div class="wpie-field__title' . esc_attr( $toogle ) . '">
198 <label class="wpie-field__title-label">
199 <input type="checkbox" data-field="{{data_field}}" {{checked}}>
200 <input type="hidden" name="{{name}}" value="' . esc_attr( $value ) . '">
201 <span class="{{label_class}}">{{label}} {{tooltip}}</span></label></div>';
202
203 return str_replace( [
204 '{{name}}',
205 '{{checked}}',
206 '{{label_class}}',
207 '{{label}}',
208 '{{data_field}}',
209 '{{tooltip}}',
210
211 ], [
212 esc_attr( $key ),
213 $checked,
214 esc_attr( $label_class ),
215 esc_html( $label ),
216 $data_field,
217 $tooltip
218
219 ], $template );
220 }
221
222
223 private function get_addon( $args ) {
224 if ( empty( $args['addon'] ) ) {
225 return '';
226 }
227
228 if ( is_string( $args['addon'] ) ) {
229 return '<span class="wpie-field__label is-addon">' . esc_html( $args['addon'] ) . '</span>';
230 }
231
232 if ( is_array( $args['addon'] ) ) {
233 return $this->get_addon_select( $args['addon'] );
234 }
235
236 return '';
237 }
238
239 private function get_addon_select( $args ) {
240 $label_class = isset( $args['label'] ) ? '' : 'screen-reader-text';
241 $label = ! empty( $args['label'] ) ? $args['label'] : '';
242 $key = $this->get_key( $args['name'] );
243 $data_field = $args['name'] ?? 'wpie-field';
244 $default = $args['val'] ?? '';
245 $value = $this->get_the_value( $args['name'], $default );
246 $atts = $this->get_attributes( $args, $value );
247 $template = '<label class="wpie-field__label">
248 <span class="{{label_class}}">{{label}}</span>
249 <select name="{{name}}" data-field="{{data_field}}">
250 {{atts}}
251 </select>
252 </label>';
253
254 return str_replace( [
255 '{{name}}',
256 '{{atts}}',
257 '{{label_class}}',
258 '{{label}}',
259 '{{data_field}}',
260
261 ], [
262 esc_attr( $key ),
263 $atts,
264 esc_attr( $label_class ),
265 esc_html( $label ),
266 $data_field
267
268 ], $template );
269 }
270
271
272 private function get_attributes( $args, $value ) {
273 $arr = $args['atts'] ?? $args['options'] ?? '';
274
275 if (empty($arr) || !is_array($arr)) {
276 return false;
277 }
278 $atts = '';
279
280 foreach ($arr as $key => $val) {
281 if ( $args['type'] === 'select' ) {
282 if ( strrpos( $key, '_start' ) ) {
283 $atts .= '<optgroup label="' . esc_attr( $val ) . '">';
284 } elseif ( strrpos( $key, '_end' ) ) {
285 $atts .= '</optgroup>';
286 } else {
287 $atts .= '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key,false ) . '>' . esc_html( $val ) . '</option>';
288 }
289 } else {
290 $atts .= ' ' . esc_attr( $key ) . '="' . esc_attr( $val ) . '"';
291 }
292 }
293
294
295 return $atts;
296 }
297
298 private function get_the_value( $name, $default = '' ) {
299
300 if ( empty( $this->order ) || ! is_array( $this->order ) ) {
301 return $this->options[ $name ] ?? $default;
302 }
303
304 if ( strpos( $name, '-' ) !== false ) {
305 $parts = explode( '-', $name );
306 if ( empty( $this->options[ $parts[0] ][ $parts[1] ] ) ) {
307 $value = [];
308 } else {
309 $value = $this->options[ $parts[0] ][ $parts[1] ];
310 }
311 } else {
312 if ( empty( $this->options[ $name ] ) ) {
313 $value = [];
314 } else {
315 $value = $this->options[ $name ];
316 }
317 }
318
319 foreach ( $this->order as $order ) {
320 if ( is_numeric( $order ) ) {
321 if ( isset( $value[ $order ] ) && is_array( $value ) ) {
322 $value = &$value[ $order ];
323 } else {
324 return $default;
325 }
326 }
327 }
328
329 return $value ?? $default;
330 }
331
332 private function get_opt_key( $name ) {
333 if ( empty( $this->order ) ) {
334 return $name;
335 }
336
337 if ( ! is_array( $this->order ) ) {
338 return $name;
339 }
340
341 $key = $name;
342
343 foreach ( $this->order as $order ) {
344 if ( is_numeric( $order ) && $order >= 0 ) {
345 $key .= '[' . $order . ']';
346 } elseif ( is_string( $order ) ) {
347 $key .= '[' . $order . ']';
348 } else {
349 $key .= '[]';
350 }
351 }
352
353 return $key;
354 }
355
356 private function get_key( $name ) {
357
358 if ( strpos( $name, '-' ) !== false ) {
359 $parts = explode( '-', $name );
360 $arr_name = '';
361 foreach ( $parts as $partkey => $part ) {
362 if ( $partkey === 0 ) {
363 $arr_name .= 'param[' . $part . ']';
364 continue;
365 }
366 $arr_name .= '[' . esc_attr( $part ) . ']';
367 }
368 $name = $arr_name;
369 }
370
371
372 if ( empty( $this->order ) ) {
373 if ( strpos( $name, 'param[' ) !== false ) {
374 return $name;
375 }
376
377 return 'param[' . $name . ']';
378 }
379
380 if ( ! is_array( $this->order ) ) {
381 if ( strpos( $name, 'param[' ) !== false ) {
382 return $name;
383 }
384
385 return 'param[' . $name . ']';
386 }
387
388 $key = ( strpos( $name, 'param[' ) !== false ) ? $name : 'param[' . $name . ']';
389
390 foreach ( $this->order as $order ) {
391 if ( is_string( $order ) ) {
392 $key .= '[' . esc_attr( $order ) . ']';
393 } else {
394 $key .= '[]';
395 }
396 }
397
398 return $key;
399 }
400
401 private function text_template(): string {
402 return '
403 <label class="wpie-field__label">
404 <input type="{{type}}" name="{{name}}" value="{{value}}" data-field="{{data_field}}" {{atts}}>
405 <span class="{{label_class}}">{{label}}</span>
406 </label>
407 {{addon}}
408 ';
409 }
410
411 private function select_template(): string {
412 return '
413 <label class="wpie-field__label">
414 <span class="{{label_class}}">{{label}}</span>
415 <select name="{{name}}" data-field="{{data_field}}">
416 {{atts}}
417 </select>
418 </label>
419 ';
420 }
421
422 private function checkbox_template(): string {
423 return '<label class="wpie-field__label">
424 <input type="checkbox" data-field="{{data_field}}" {{checked}}>
425 <input type="hidden" name="{{name}}" value="{{value}}">
426 <span class="{{label_class}}">{{label}}</span>
427 </label>';
428 }
429
430 private function textarea_template(): string {
431 return '
432 <label class="wpie-field__label">
433 <textarea name="{{name}}" data-field="{{data_field}}" {{atts}}>{{value}}</textarea>
434 <span class="{{label_class}}">{{label}}</span>
435 </label>
436 ';
437 }
438
439 private function editor_template(): string {
440 return '
441 <div class="wpie-field__label">
442 <textarea name="{{name}}" data-field="{{data_field}}" {{atts}}>{{value}}</textarea>
443 <span class="{{label_class}}">{{label}}</span>
444 </div>
445 ';
446 }
447 }