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