| 1 |
<?php |
| 2 |
namespace FakerPress; |
| 3 |
use FakerPress\Provider\HTML; |
| 4 |
use FakerPress\Utils; |
| 5 |
|
| 6 |
class Field { |
| 7 |
|
| 8 |
const plugin = 'fakerpress'; |
| 9 |
|
| 10 |
public $type = 'raw'; |
| 11 |
|
| 12 |
public $id; |
| 13 |
|
| 14 |
public $field; |
| 15 |
|
| 16 |
public $container; |
| 17 |
|
| 18 |
public $heads; |
| 19 |
|
| 20 |
public $has_container = true; |
| 21 |
|
| 22 |
public $has_wrap = true; |
| 23 |
|
| 24 |
public $has_label = false; |
| 25 |
|
| 26 |
public static $default_container = [ |
| 27 |
'label' => '', |
| 28 |
'description' => '', |
| 29 |
'attributes' => [], |
| 30 |
'actions' => [], |
| 31 |
'heads' => [], |
| 32 |
'class' => [], |
| 33 |
'wrap' => [ |
| 34 |
'class' => [] |
| 35 |
], |
| 36 |
'blocks' => [ 'label', 'fields', 'description', 'actions' ], |
| 37 |
]; |
| 38 |
|
| 39 |
public static $valid_types = [ |
| 40 |
'heading', |
| 41 |
'input', |
| 42 |
'text', |
| 43 |
'dropdown', |
| 44 |
'range', |
| 45 |
'interval', |
| 46 |
'number', |
| 47 |
'hidden', |
| 48 |
'meta', |
| 49 |
'taxonomy', |
| 50 |
'radio', |
| 51 |
'checkbox', |
| 52 |
'raw', |
| 53 |
]; |
| 54 |
|
| 55 |
/** |
| 56 |
* Stores the Error structure |
| 57 |
* |
| 58 |
* @since 0.6.1 |
| 59 |
* |
| 60 |
* @var mixed |
| 61 |
*/ |
| 62 |
public $error; |
| 63 |
|
| 64 |
/** |
| 65 |
* |
| 66 |
* |
| 67 |
* @since 0.6.1 |
| 68 |
* |
| 69 |
* @var mixed |
| 70 |
*/ |
| 71 |
public $callback; |
| 72 |
|
| 73 |
/** |
| 74 |
* |
| 75 |
* |
| 76 |
* @since 0.6.1 |
| 77 |
* |
| 78 |
* @var mixed |
| 79 |
*/ |
| 80 |
public $conditional; |
| 81 |
|
| 82 |
/** |
| 83 |
* |
| 84 |
* |
| 85 |
* @since 0.6.1 |
| 86 |
* |
| 87 |
* @var mixed |
| 88 |
*/ |
| 89 |
public $label; |
| 90 |
|
| 91 |
/** |
| 92 |
* |
| 93 |
* |
| 94 |
* @since 0.6.1 |
| 95 |
* |
| 96 |
* @var mixed |
| 97 |
*/ |
| 98 |
public $description; |
| 99 |
|
| 100 |
/** |
| 101 |
* |
| 102 |
* |
| 103 |
* @since 0.6.1 |
| 104 |
* |
| 105 |
* @var mixed |
| 106 |
*/ |
| 107 |
public $attributes; |
| 108 |
|
| 109 |
/** |
| 110 |
* |
| 111 |
* |
| 112 |
* @since 0.6.1 |
| 113 |
* |
| 114 |
* @var mixed |
| 115 |
*/ |
| 116 |
public $actions; |
| 117 |
|
| 118 |
/** |
| 119 |
* |
| 120 |
* |
| 121 |
* @since 0.6.1 |
| 122 |
* |
| 123 |
* @var mixed |
| 124 |
*/ |
| 125 |
public $blocks; |
| 126 |
|
| 127 |
/** |
| 128 |
* |
| 129 |
* |
| 130 |
* @since 0.6.1 |
| 131 |
* |
| 132 |
* @var mixed |
| 133 |
*/ |
| 134 |
public $class; |
| 135 |
|
| 136 |
/** |
| 137 |
* |
| 138 |
* |
| 139 |
* @since 0.6.1 |
| 140 |
* |
| 141 |
* @var mixed |
| 142 |
*/ |
| 143 |
public $wrap; |
| 144 |
|
| 145 |
public function __construct( $type, $field, $container = [] ) { |
| 146 |
// Default Error Structure |
| 147 |
$this->error = false; |
| 148 |
|
| 149 |
// Non Valid types are just set to Raw |
| 150 |
if ( ! self::is_valid_type( $type ) ) { |
| 151 |
$type = 'raw'; |
| 152 |
} |
| 153 |
|
| 154 |
if ( is_string( $field ) ) { |
| 155 |
$this->field = (object) [ |
| 156 |
'id' => $field, |
| 157 |
]; |
| 158 |
} else { |
| 159 |
// Setup the Container if required |
| 160 |
$this->field = (object) $field; |
| 161 |
} |
| 162 |
|
| 163 |
$container = (object) wp_parse_args( $container, self::$default_container ); |
| 164 |
|
| 165 |
// set the ID |
| 166 |
$this->type = $type; |
| 167 |
if ( ! isset( $this->field->id ) ) { |
| 168 |
$this->id = (array) Utils::abbr( uniqid( '', true ) ); |
| 169 |
} else { |
| 170 |
$this->id = (array) $this->field->id; |
| 171 |
} |
| 172 |
|
| 173 |
$this->callback = null; |
| 174 |
$this->conditional = true; |
| 175 |
|
| 176 |
$this->label = $container->label; |
| 177 |
$this->description = $container->description; |
| 178 |
$this->attributes = $container->attributes; |
| 179 |
$this->actions = $container->actions; |
| 180 |
$this->blocks = $container->blocks; |
| 181 |
$this->class = $container->class; |
| 182 |
$this->wrap = $container->wrap; |
| 183 |
} |
| 184 |
|
| 185 |
public function output( $print = false ) { |
| 186 |
if ( ! $this->conditional ) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
if ( $this->callback && is_callable( $this->callback ) ) { |
| 191 |
// if there's a callback, run it |
| 192 |
call_user_func( $this->callback ); |
| 193 |
} elseif ( in_array( $this->type, self::$valid_types ) ) { |
| 194 |
// the specified type exists, run the appropriate method |
| 195 |
$field = call_user_func_array( [ __CLASS__, 'type_' . $this->type ], [ $this->field, $this, 'string', [] ] ); |
| 196 |
|
| 197 |
// filter the output |
| 198 |
$field = apply_filters( self::plugin . '/fields/field-output-' . $this->type, $field, $this ); |
| 199 |
|
| 200 |
if ( $print ) { |
| 201 |
echo balanceTags( $field ); |
| 202 |
} else { |
| 203 |
return $field; |
| 204 |
} |
| 205 |
} else { |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
public function build( $content, $output = 'string', $html = [] ) { |
| 211 |
$content = (array) $content; |
| 212 |
$key = array_search( 'fields', $this->blocks ); |
| 213 |
$is_multiple = is_array( reset( $content ) ); |
| 214 |
if ( ! $is_multiple ) { |
| 215 |
$content = [ $content ]; |
| 216 |
} |
| 217 |
|
| 218 |
$before = array_filter( array_slice( $this->blocks, 0, $key ), 'is_array' ); |
| 219 |
$before_content = []; |
| 220 |
foreach ( $before as $i => $block ) { |
| 221 |
$_html = ''; |
| 222 |
if ( ! empty( $block['html'] ) ) { |
| 223 |
$_html = $block['html']; |
| 224 |
unset( $block['html'] ); |
| 225 |
} |
| 226 |
$before_content[] = '<td' . Utils::attr( $block ) . '>' . $_html . '</td>'; |
| 227 |
} |
| 228 |
|
| 229 |
$after = array_filter( array_slice( $this->blocks, $key + 1, count( $this->blocks ) - ( $key + 1 ) ), 'is_array' ); |
| 230 |
$after_content = []; |
| 231 |
foreach ( $after as $i => $block ) { |
| 232 |
$_html = ''; |
| 233 |
if ( ! empty( $block['html'] ) ) { |
| 234 |
$_html = $block['html']; |
| 235 |
unset( $block['html'] ); |
| 236 |
} |
| 237 |
$after_content[] = '<td' . Utils::attr( $block ) . '>' . $_html . '</td>'; |
| 238 |
} |
| 239 |
|
| 240 |
if ( in_array( 'heading', $this->blocks ) ) { |
| 241 |
$html[] = self::type_heading( [ |
| 242 |
'type' => 'heading', |
| 243 |
'title' => $this->label, |
| 244 |
'description' => $this->description, |
| 245 |
], null, 'string' ); |
| 246 |
} |
| 247 |
|
| 248 |
foreach ( $content as $blocks ) { |
| 249 |
if ( in_array( 'table', $this->blocks ) ) { |
| 250 |
$html[] = self::start_table( $this ); |
| 251 |
} |
| 252 |
|
| 253 |
$html[] = self::start_container( $this ); |
| 254 |
$html[] = implode( "\r\n", $before_content ); |
| 255 |
|
| 256 |
if ( in_array( 'label', $this->blocks ) ) { |
| 257 |
$html[] = self::label( $this ); |
| 258 |
} |
| 259 |
|
| 260 |
$html[] = self::start_wrap( $this ); |
| 261 |
$html[] = implode( "\r\n", $blocks ); |
| 262 |
$html[] = self::end_wrap( $this ); |
| 263 |
|
| 264 |
$html[] = implode( "\r\n", $after_content ); |
| 265 |
|
| 266 |
$html[] = self::end_container( $this ); |
| 267 |
|
| 268 |
if ( in_array( 'table', $this->blocks ) ) { |
| 269 |
$html[] = self::end_table( $this ); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if ( 'string' === $output ) { |
| 274 |
return implode( "\r\n", $html ); |
| 275 |
} else { |
| 276 |
return $html; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
public static function start_table( $container, $output = 'string', $html = [] ) { |
| 281 |
$html[] = '<table class="' . Utils::abbr( 'table-' . implode( '-', $container->id ) ) . '">'; |
| 282 |
if ( ! empty( $container->heads ) ) { |
| 283 |
$html[] = '<thead>'; |
| 284 |
foreach ( $container->heads as $head ) { |
| 285 |
$_html = ''; |
| 286 |
if ( ! empty( $head['html'] ) ) { |
| 287 |
$_html = $head['html']; |
| 288 |
unset( $head['html'] ); |
| 289 |
} |
| 290 |
$html[] = '<th' . Utils::attr( $head ) . '>' . $_html . '</th>'; |
| 291 |
} |
| 292 |
$html[] = '</thead>'; |
| 293 |
} |
| 294 |
$html[] = '<tbody>'; |
| 295 |
|
| 296 |
$html = apply_filters( self::plugin . '/fields/field-start_table', $html, $container ); |
| 297 |
if ( 'string' === $output ) { |
| 298 |
return implode( "\r\n", $html ); |
| 299 |
} else { |
| 300 |
return $html; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
public static function end_table( $container, $output = 'string', $html = [] ) { |
| 305 |
$html[] = '</tbody>'; |
| 306 |
$html[] = '</table>'; |
| 307 |
|
| 308 |
$html = apply_filters( self::plugin . '/fields/field-end_table', $html, $container ); |
| 309 |
if ( 'string' === $output ) { |
| 310 |
return implode( "\r\n", $html ); |
| 311 |
} else { |
| 312 |
return $html; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
public static function start_container( $container, $output = 'string', $html = [] ) { |
| 317 |
if ( ! is_array( $container->class ) ) { |
| 318 |
$container->class = (array) $container->class; |
| 319 |
} |
| 320 |
$container->class[] = 'field-container'; |
| 321 |
$container->class[] = 'type-' . $container->type . '-container'; |
| 322 |
|
| 323 |
if ( is_wp_error( $container->error ) ) { |
| 324 |
$container->class[] = 'error'; |
| 325 |
} |
| 326 |
|
| 327 |
$container->class = array_map( [ Utils::class, 'abbr' ], $container->class ); |
| 328 |
|
| 329 |
if ( ! in_array( 'table', $container->blocks ) ) { |
| 330 |
$html[] = '<tr id="' . self::id( $container->id, true ) . '" class="' . implode( ' ', $container->class ) . '" ' . Utils::attr( $container->attributes ) . '>'; |
| 331 |
} |
| 332 |
|
| 333 |
$html = apply_filters( self::plugin . '/fields/field-start_container', $html, $container ); |
| 334 |
if ( 'string' === $output ) { |
| 335 |
return implode( "\r\n", $html ); |
| 336 |
} else { |
| 337 |
return $html; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
public static function end_container( $container, $output = 'string', $html = [] ) { |
| 342 |
if ( ! in_array( 'table', $container->blocks ) ) { |
| 343 |
$html[] = '</tr>'; |
| 344 |
} |
| 345 |
|
| 346 |
$html = apply_filters( self::plugin . '/fields/field-end_container', $html, $container ); |
| 347 |
if ( 'string' === $output ) { |
| 348 |
return implode( "\r\n", $html ); |
| 349 |
} else { |
| 350 |
return $html; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
public static function wrapper( $content = [], $field = [], $output = 'string' ) { |
| 355 |
$attributes = (object) []; |
| 356 |
$attributes->class[] = 'field-wrap'; |
| 357 |
$attributes->class[] = 'type-' . $field->type . '-wrap'; |
| 358 |
|
| 359 |
$html = []; |
| 360 |
if ( ! empty( $content ) ) { |
| 361 |
$html[] = '<fieldset' . Utils::attr( $attributes ) . '>'; |
| 362 |
$html[] = implode( "\r\n", (array) $content ); |
| 363 |
if ( ! empty( $field->label ) ) { |
| 364 |
$html[] = '<label class="' . Utils::abbr( 'internal-label' ) . '">' . $field->label . '</label>'; |
| 365 |
} |
| 366 |
$html[] = '</fieldset>'; |
| 367 |
} |
| 368 |
|
| 369 |
if ( 'string' === $output ) { |
| 370 |
return implode( "\r\n", $html ); |
| 371 |
} else { |
| 372 |
return $html; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
public static function start_wrap( $container, $output = 'string', $html = [] ) { |
| 377 |
$container->wrap['class'][] = 'field-wrap'; |
| 378 |
$container->wrap['class'][] = 'type-' . $container->type . '-wrap'; |
| 379 |
if ( in_array( 'fields', $container->blocks ) ) { |
| 380 |
$html[] = '<td colspan="1">'; |
| 381 |
$html[] = '<fieldset' . Utils::attr( $container->wrap ) . '>'; |
| 382 |
} elseif ( ! in_array( 'table', $container->blocks ) ) { |
| 383 |
$container->wrap['colspan'] = 2; |
| 384 |
$html[] = '<td' . Utils::attr( $container->wrap ) . '>'; |
| 385 |
} |
| 386 |
|
| 387 |
$html = apply_filters( self::plugin . '/fields/field-start_wrap', $html, $container ); |
| 388 |
if ( 'string' === $output ) { |
| 389 |
return implode( "\r\n", $html ); |
| 390 |
} else { |
| 391 |
return $html; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
public static function end_wrap( $container, $output = 'string', $html = [] ) { |
| 396 |
if ( in_array( 'actions', $container->blocks ) ) { |
| 397 |
$html[] = self::actions( $container ); |
| 398 |
} |
| 399 |
|
| 400 |
if ( in_array( 'fields', $container->blocks ) && ! in_array( 'table', $container->blocks ) ) { |
| 401 |
$html[] = '</fieldset>'; |
| 402 |
} |
| 403 |
|
| 404 |
if ( in_array( 'description', $container->blocks ) ) { |
| 405 |
$html[] = self::description( $container ); |
| 406 |
} |
| 407 |
if ( ! in_array( 'table', $container->blocks ) ) { |
| 408 |
$html[] = '</td>'; |
| 409 |
} |
| 410 |
|
| 411 |
$html = apply_filters( self::plugin . '/fields/field-end_wrap', $html, $container ); |
| 412 |
if ( 'string' === $output ) { |
| 413 |
return implode( "\r\n", $html ); |
| 414 |
} else { |
| 415 |
return $html; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
public static function label( $container, $output = 'string', $html = [] ) { |
| 420 |
$is_td = ( false !== strpos( $container->type, 'meta' ) ) || ( false !== strpos( $container->type, 'taxonomy' ) ); |
| 421 |
|
| 422 |
$html[] = '<' . ( $is_td ? 'td' : 'th' ) . ' scope="row" colspan="1">'; |
| 423 |
|
| 424 |
if ( isset( $container->label ) && false !== $container->label ) { |
| 425 |
$html[] = '<label class="' . Utils::abbr( 'field-label' ) . '" for="' . self::id( $container->id ) . '">' . $container->label . '</label>'; |
| 426 |
} |
| 427 |
|
| 428 |
$html[] = '</' . ( $is_td ? 'td' : 'th' ) . '>'; |
| 429 |
|
| 430 |
$html = apply_filters( self::plugin . '/fields/field-label', $html, $container ); |
| 431 |
if ( 'string' === $output ) { |
| 432 |
return implode( "\r\n", $html ); |
| 433 |
} else { |
| 434 |
return $html; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
public static function actions( $container, $output = 'string', $html = [] ) { |
| 439 |
if ( empty( $container->actions ) ) { |
| 440 |
return ( 'string' === $output ? '' : [] ); |
| 441 |
} |
| 442 |
|
| 443 |
$html[] = '<div class="' . Utils::abbr( 'actions' ) . '">'; |
| 444 |
foreach ( $container->actions as $action => $label ) { |
| 445 |
$html[] = get_submit_button( $label, 'primary', self::plugin . '[actions][' . $action . ']', false ); |
| 446 |
} |
| 447 |
$html[] = '</div>'; |
| 448 |
|
| 449 |
$html = apply_filters( self::plugin . '/fields/field-actions', $html, $container ); |
| 450 |
if ( 'string' === $output ) { |
| 451 |
return implode( "\r\n", $html ); |
| 452 |
} else { |
| 453 |
return $html; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
public static function description( $container, $output = 'string', $html = [] ) { |
| 458 |
if ( ! empty( $container->description ) ) { |
| 459 |
$html[] = '<p class="' . Utils::abbr( 'field-description' ) . '">' . $container->description . '</p>';; |
| 460 |
} |
| 461 |
|
| 462 |
$html = apply_filters( self::plugin . '/fields/field-description', $html, $container ); |
| 463 |
if ( 'string' === $output ) { |
| 464 |
return implode( "\r\n", $html ); |
| 465 |
} else { |
| 466 |
return $html; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
/****************** |
| 472 |
* Static methods * |
| 473 |
******************/ |
| 474 |
|
| 475 |
public static function is_valid_type( $type = false ) { |
| 476 |
// a list of valid field types, to prevent screwy behaviour |
| 477 |
return in_array( $type, apply_filters( self::plugin . '/fields/valid_types', self::$valid_types ) ); |
| 478 |
} |
| 479 |
|
| 480 |
public static function name( $indexes = [] ) { |
| 481 |
return self::plugin . '[' . implode( '][', (array) $indexes ) . ']'; |
| 482 |
} |
| 483 |
|
| 484 |
public static function id( $id = [], $container = false ) { |
| 485 |
if ( ! is_array( $id ) ) { |
| 486 |
$id = (array) $id; |
| 487 |
} |
| 488 |
if ( $container ) { |
| 489 |
$id[] = 'container'; |
| 490 |
} |
| 491 |
return self::plugin . '-field-' . implode( '-', (array) $id ); |
| 492 |
} |
| 493 |
|
| 494 |
public static function parse( $field, &$container = null ) { |
| 495 |
if ( is_scalar( $field ) ) { |
| 496 |
if ( ! is_string( $field ) ) { |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
$field = (object) [ |
| 501 |
'type' => $field, |
| 502 |
]; |
| 503 |
} elseif ( is_array( $field ) ) { |
| 504 |
$field = (object) $field; |
| 505 |
} |
| 506 |
|
| 507 |
if ( ! is_a( $container, __CLASS__ ) ) { |
| 508 |
$container = (object) wp_parse_args( $container, self::$default_container ); |
| 509 |
} |
| 510 |
if ( ! isset( $container->id ) ) { |
| 511 |
$container->id = (array) Utils::abbr( uniqid() ); |
| 512 |
} |
| 513 |
|
| 514 |
$field = (object) wp_parse_args( $field, ( ! empty( $container->field ) ? $container->field : [] ) ); |
| 515 |
|
| 516 |
// Setup Private Attributes (_*) |
| 517 |
if ( isset( $field->_id ) ) { |
| 518 |
|
| 519 |
} elseif ( empty( $field->id ) ) { |
| 520 |
$field->_id = (array) $container->id; |
| 521 |
} else { |
| 522 |
$field->_id = (array) $field->id; |
| 523 |
} |
| 524 |
|
| 525 |
if ( isset( $field->_name ) ) { |
| 526 |
|
| 527 |
} elseif ( ! isset( $field->name ) ) { |
| 528 |
$field->_name = (array) ( isset( $container->field->name ) ? $container->field->name : $field->_id ); |
| 529 |
} else { |
| 530 |
$field->_name = (array) $field->name; |
| 531 |
} |
| 532 |
|
| 533 |
// Setup Public Attributes |
| 534 |
if ( empty( $field->type ) ) { |
| 535 |
$field->type = $container->type; |
| 536 |
} |
| 537 |
$field->_type = $field->type; |
| 538 |
|
| 539 |
$field->id = self::id( $field->_id ); |
| 540 |
$field->name = self::name( $field->_name ); |
| 541 |
|
| 542 |
switch ( $field->type ) { |
| 543 |
case 'heading': |
| 544 |
if ( ! isset( $field->title ) ) { |
| 545 |
$field->title = ''; |
| 546 |
} |
| 547 |
|
| 548 |
if ( ! isset( $field->description ) ) { |
| 549 |
$field->description = ''; |
| 550 |
} |
| 551 |
|
| 552 |
$container->has_label = false; |
| 553 |
$container->blocks = [ 'actions' ]; |
| 554 |
break; |
| 555 |
case 'meta': |
| 556 |
case 'taxonomy': |
| 557 |
if ( ! isset( $container->label ) ) { |
| 558 |
$container->label = ''; |
| 559 |
} |
| 560 |
|
| 561 |
if ( ! isset( $field->config ) ) { |
| 562 |
$field->config = []; |
| 563 |
} |
| 564 |
|
| 565 |
if ( ! isset( $field->index ) ) { |
| 566 |
$field->index = 0; |
| 567 |
} |
| 568 |
|
| 569 |
$container->has_label = false; |
| 570 |
$container->blocks = [ 'actions' ]; |
| 571 |
break; |
| 572 |
case 'input': |
| 573 |
# code... |
| 574 |
break; |
| 575 |
case 'text': |
| 576 |
if ( empty( $field->size ) ) { |
| 577 |
$field->size = 'medium'; |
| 578 |
} |
| 579 |
break; |
| 580 |
case 'number': |
| 581 |
if ( empty( $field->size ) ) { |
| 582 |
$field->size = 'tiny'; |
| 583 |
} |
| 584 |
break; |
| 585 |
case 'radio': |
| 586 |
case 'checkbox': |
| 587 |
unset( $field->size ); |
| 588 |
|
| 589 |
if ( ! isset( $field->options ) ) { |
| 590 |
$field->options = []; |
| 591 |
} |
| 592 |
$field->options = (array) $field->options; |
| 593 |
|
| 594 |
break; |
| 595 |
case 'dropdown': |
| 596 |
if ( isset( $field->multiple ) && $field->multiple ) { |
| 597 |
$field->type = 'hidden'; |
| 598 |
|
| 599 |
if ( isset( $field->{'data-tags'} ) && true === $field->{'data-tags'} && empty( $field->{'data-separator'} ) ) { |
| 600 |
$field->{'data-separator'} = ','; |
| 601 |
} |
| 602 |
} else { |
| 603 |
if ( ! isset( $field->options ) ) { |
| 604 |
$field->options = []; |
| 605 |
} |
| 606 |
$field->options = (array) $field->options; |
| 607 |
} |
| 608 |
|
| 609 |
break; |
| 610 |
case 'interval': |
| 611 |
|
| 612 |
break; |
| 613 |
case 'date': |
| 614 |
$field->type = 'text'; |
| 615 |
$field->size = 'small'; |
| 616 |
break; |
| 617 |
} |
| 618 |
|
| 619 |
$field = apply_filters( self::plugin . '/fields/field', $field, $container ); |
| 620 |
$container = apply_filters( self::plugin . '/fields/container', $container, $field ); |
| 621 |
|
| 622 |
$field = apply_filters( self::plugin . '/fields/field-' . $field->_type, $field, $container ); |
| 623 |
$container = apply_filters( self::plugin . '/fields/container-' . $field->_type, $container, $field ); |
| 624 |
|
| 625 |
if ( ! empty( $field->class ) ) { |
| 626 |
$field->class = (array) $field->class; |
| 627 |
} |
| 628 |
$field->class[] = 'field'; |
| 629 |
$field->class[] = 'type-' . $field->_type; |
| 630 |
|
| 631 |
if ( ! empty( $field->size ) ) { |
| 632 |
$field->class[] = 'size-' . $field->size; |
| 633 |
} |
| 634 |
|
| 635 |
return $field; |
| 636 |
} |
| 637 |
|
| 638 |
/***************** |
| 639 |
* Field Methods * |
| 640 |
*****************/ |
| 641 |
|
| 642 |
public static function type_input( $field, $container = null, $output = 'string', $html = [] ) { |
| 643 |
$field = self::parse( $field, $container ); |
| 644 |
if ( is_scalar( $field ) ) { |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
$content[] = '<input' . Utils::attr( $field ) . '/>'; |
| 649 |
|
| 650 |
if ( is_a( $container, __CLASS__ ) ) { |
| 651 |
$html[] = $container->build( $content ); |
| 652 |
} else { |
| 653 |
$html = $content; |
| 654 |
} |
| 655 |
|
| 656 |
if ( 'string' === $output ) { |
| 657 |
return implode( "\r\n", $html ); |
| 658 |
} else { |
| 659 |
return $html; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
public static function type_button( $field, $container = null, $output = 'string', $html = [] ) { |
| 664 |
return self::type_input( $field, $container, $output, $html ); |
| 665 |
} |
| 666 |
|
| 667 |
public static function type_number( $field, $container = null, $output = 'string', $html = [] ) { |
| 668 |
return self::type_input( $field, $container, $output, $html ); |
| 669 |
} |
| 670 |
|
| 671 |
public static function type_text( $field, $container = null, $output = 'string', $html = [] ) { |
| 672 |
return self::type_input( $field, $container, $output, $html ); |
| 673 |
} |
| 674 |
|
| 675 |
public static function type_hidden( $field, $container = null, $output = 'string', $html = [] ) { |
| 676 |
return self::type_input( $field, $container, $output, $html ); |
| 677 |
} |
| 678 |
|
| 679 |
public static function type_date( $field, $container = null, $output = 'string', $html = [] ) { |
| 680 |
return self::type_input( $field, $container, $output, $html ); |
| 681 |
} |
| 682 |
|
| 683 |
public static function type_heading( $field, $container = null, $output = 'string', $html = [] ) { |
| 684 |
$field = self::parse( $field, $container ); |
| 685 |
if ( is_scalar( $field ) ) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
$content[] = '<h3>' . $field->title . '</h3>'; |
| 690 |
|
| 691 |
if ( ! empty( $field->description ) ) { |
| 692 |
$content[] = '<div class="' . Utils::abbr( 'field-description' ) . '">' . $field->description . '</div>'; |
| 693 |
} |
| 694 |
|
| 695 |
if ( is_a( $container, __CLASS__ ) ) { |
| 696 |
$html[] = $container->build( $content ); |
| 697 |
} else { |
| 698 |
$html = $content; |
| 699 |
} |
| 700 |
|
| 701 |
if ( 'string' === $output ) { |
| 702 |
return implode( "\r\n", $html ); |
| 703 |
} else { |
| 704 |
return $html; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
public static function type_radio( $field, $container = null, $output = 'string', $html = [] ) { |
| 709 |
$field = self::parse( $field, $container ); |
| 710 |
if ( is_scalar( $field ) ) { |
| 711 |
return false; |
| 712 |
} |
| 713 |
|
| 714 |
foreach ( $field->options as $opt ) { |
| 715 |
$radio = clone $field; |
| 716 |
if ( isset( $opt['value'] ) ) { |
| 717 |
$radio->_id[] = sanitize_html_class( $opt['value'] ); |
| 718 |
$radio->value = $opt['value']; |
| 719 |
} |
| 720 |
|
| 721 |
if ( ! empty( $opt['class'] ) ) { |
| 722 |
$radio->class = array_merge( $radio->class, (array) $opt['class'] ); |
| 723 |
} |
| 724 |
|
| 725 |
if ( isset( $field->value ) && $field->value === $radio->value ) { |
| 726 |
$radio->checked = true; |
| 727 |
} |
| 728 |
|
| 729 |
$content[] = self::type_input( $radio, null, 'string', [] ); |
| 730 |
$content[] = '<label class="' . Utils::abbr( 'field-label' ) . '" for="' . self::id( $radio->_id ) . '">' . $opt['text'] . '</label><br />'; |
| 731 |
} |
| 732 |
|
| 733 |
if ( is_a( $container, __CLASS__ ) ) { |
| 734 |
$html[] = $container->build( $content ); |
| 735 |
} else { |
| 736 |
$html = $content; |
| 737 |
} |
| 738 |
|
| 739 |
if ( 'string' === $output ) { |
| 740 |
return implode( "\r\n", $html ); |
| 741 |
} else { |
| 742 |
return $html; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
public static function type_checkbox( $field, $container = null, $output = 'string', $html = [] ) { |
| 747 |
$field = self::parse( $field, $container ); |
| 748 |
if ( is_scalar( $field ) ) { |
| 749 |
return false; |
| 750 |
} |
| 751 |
|
| 752 |
foreach ( $field->options as $opt ) { |
| 753 |
$checkbox = clone $field; |
| 754 |
if ( isset( $opt['value'] ) ) { |
| 755 |
$checkbox->_id[] = sanitize_html_class( $opt['value'] ); |
| 756 |
$checkbox->value = $opt['value']; |
| 757 |
} |
| 758 |
|
| 759 |
if ( ! empty( $opt['class'] ) ) { |
| 760 |
$checkbox->class = array_merge( $checkbox->class, (array) $opt['class'] ); |
| 761 |
} |
| 762 |
|
| 763 |
if ( isset( $field->value ) && $field->value === $checkbox->value ) { |
| 764 |
$checkbox->checked = true; |
| 765 |
} |
| 766 |
|
| 767 |
$content[] = self::type_input( $checkbox, null, 'string', [] ); |
| 768 |
$content[] = '<label class="' . Utils::abbr( 'field-label' ) . '" for="' . self::id( $checkbox->_id ) . '">' . $opt['text'] . '</label><br />'; |
| 769 |
} |
| 770 |
|
| 771 |
if ( is_a( $container, __CLASS__ ) ) { |
| 772 |
$html[] = $container->build( $content ); |
| 773 |
} else { |
| 774 |
$html = $content; |
| 775 |
} |
| 776 |
|
| 777 |
if ( 'string' === $output ) { |
| 778 |
return implode( "\r\n", $html ); |
| 779 |
} else { |
| 780 |
return $html; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
public static function type_dropdown( $field, $container = null, $output = 'string', $html = [] ) { |
| 785 |
$field = self::parse( $field, $container ); |
| 786 |
if ( is_scalar( $field ) ) { |
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
if ( isset( $field->multiple ) && $field->multiple ) { |
| 791 |
$content[] = self::type_input( $field, null, 'string', [] ); |
| 792 |
} else { |
| 793 |
$content[] = '<select' . Utils::attr( $field ) . '>'; |
| 794 |
$content[] = '<option></option>'; |
| 795 |
foreach ( $field->options as $option ) { |
| 796 |
$option = (array) $option; |
| 797 |
|
| 798 |
if ( ! isset( $option['value'] ) && isset( $option['id'] ) ) { |
| 799 |
$option['value'] = $option['id']; |
| 800 |
} elseif ( ! isset( $option['value'] ) && isset( $option['ID'] ) ) { |
| 801 |
$option['value'] = $option['ID']; |
| 802 |
} elseif ( ! isset( $option['value'] ) ) { |
| 803 |
$option['value'] = false; |
| 804 |
} |
| 805 |
|
| 806 |
if ( isset( $field->value ) && $field->value === $option['value'] ) { |
| 807 |
$option['selected'] = true; |
| 808 |
} |
| 809 |
$content[] = '<option' . Utils::attr( $option ) . '>' . esc_attr( $option['text'] ) . '</option>'; |
| 810 |
} |
| 811 |
$content[] = '</select>'; |
| 812 |
} |
| 813 |
|
| 814 |
if ( is_a( $container, __CLASS__ ) ) { |
| 815 |
$html[] = $container->build( $content ); |
| 816 |
} else { |
| 817 |
$html = $content; |
| 818 |
} |
| 819 |
|
| 820 |
if ( 'string' === $output ) { |
| 821 |
return implode( "\r\n", $html ); |
| 822 |
} else { |
| 823 |
return $html; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
public static function type_range( $field, $container = null, $output = 'string', $html = [] ) { |
| 828 |
$field = self::parse( $field, $container ); |
| 829 |
if ( is_scalar( $field ) ) { |
| 830 |
return false; |
| 831 |
} |
| 832 |
|
| 833 |
$min = clone $field; |
| 834 |
$min->_id[] = 'min'; |
| 835 |
$min->_name[] = 'min'; |
| 836 |
$min->type = 'number'; |
| 837 |
$min->{'data-type'} = 'min'; |
| 838 |
$min->min = 0; |
| 839 |
|
| 840 |
if ( isset( $field->min ) && is_numeric( $field->min ) ) { |
| 841 |
$min->value = $field->min; |
| 842 |
} |
| 843 |
|
| 844 |
if ( isset( $field->_max ) && is_numeric( $field->_max ) ) { |
| 845 |
$min->max = $field->_max; |
| 846 |
} |
| 847 |
|
| 848 |
if ( isset( $field->_min ) && is_numeric( $field->_min ) ) { |
| 849 |
$min->min = $field->_min; |
| 850 |
} |
| 851 |
|
| 852 |
$min->class = []; |
| 853 |
|
| 854 |
$min->placeholder = esc_attr__( 'e.g.: 3', 'fakerpress' ); |
| 855 |
if ( ! empty( $min->_placeholder_min ) ) { |
| 856 |
$min->placeholder = $min->_placeholder_min; |
| 857 |
} |
| 858 |
|
| 859 |
$max = clone $field; |
| 860 |
$max->_id[] = 'max'; |
| 861 |
$max->_name[] = 'max'; |
| 862 |
$max->{'data-type'} = 'max'; |
| 863 |
$max->type = 'number'; |
| 864 |
$max->min = 0; |
| 865 |
|
| 866 |
if ( isset( $field->max ) && is_numeric( $field->max ) ) { |
| 867 |
$max->value = $field->max; |
| 868 |
} elseif ( ! isset( $max->_prevent_disable ) || ! $max->_prevent_disable ) { |
| 869 |
$max->disabled = true; |
| 870 |
} |
| 871 |
|
| 872 |
if ( isset( $field->_max ) && is_numeric( $field->_max ) ) { |
| 873 |
$max->max = $field->_max; |
| 874 |
} |
| 875 |
|
| 876 |
if ( isset( $field->_min ) && is_numeric( $field->_min ) ) { |
| 877 |
$max->min = $field->_min; |
| 878 |
} |
| 879 |
|
| 880 |
$max->class = []; |
| 881 |
|
| 882 |
$max->placeholder = esc_attr__( 'e.g.: 12', 'fakerpress' ); |
| 883 |
if ( ! empty( $max->_placeholder_max ) ) { |
| 884 |
$max->placeholder = $max->_placeholder_max; |
| 885 |
} |
| 886 |
|
| 887 |
$content[] = self::type_input( $min, null, 'string', [] ); |
| 888 |
$content[] = '<div title="' . esc_attr__( 'To', 'fakerpress' ) . '" class="dashicons dashicons-arrow-right-alt2 dashicon-date" style="display: inline-block;"></div>'; |
| 889 |
$content[] = self::type_input( $max, null, 'string', [] ); |
| 890 |
|
| 891 |
if ( is_a( $container, __CLASS__ ) ) { |
| 892 |
$html[] = $container->build( $content ); |
| 893 |
} else { |
| 894 |
$html = $content; |
| 895 |
} |
| 896 |
|
| 897 |
if ( 'string' === $output ) { |
| 898 |
return implode( "\r\n", $html ); |
| 899 |
} else { |
| 900 |
return $html; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
public static function type_taxonomy( $field, $container = null, $output = 'string', $html = [] ) { |
| 905 |
$field = self::parse( $field, $container ); |
| 906 |
if ( is_scalar( $field ) ) { |
| 907 |
return false; |
| 908 |
} |
| 909 |
|
| 910 |
$index = clone $field; |
| 911 |
$index->_id[] = 'index'; |
| 912 |
$index->_name[] = 'index'; |
| 913 |
$index->type = 'button'; |
| 914 |
$index->value = '1'; |
| 915 |
$index->disabled = true; |
| 916 |
$index->class = [ 'action-order' ]; |
| 917 |
|
| 918 |
$remove = clone $field; |
| 919 |
$remove->_id[] = 'remove'; |
| 920 |
$remove->_name[] = 'remove'; |
| 921 |
$remove->type = 'button'; |
| 922 |
$remove->value = '−'; |
| 923 |
$remove->class = [ 'action-remove' ]; |
| 924 |
|
| 925 |
$duplicate = clone $field; |
| 926 |
$duplicate->_id[] = 'duplicate'; |
| 927 |
$duplicate->_name[] = 'duplicate'; |
| 928 |
$duplicate->type = 'button'; |
| 929 |
$duplicate->deactive = true; |
| 930 |
$duplicate->value = '+'; |
| 931 |
$duplicate->class = [ 'action-duplicate' ]; |
| 932 |
|
| 933 |
$table = clone $container; |
| 934 |
$table->blocks = [ 'heading', 'table' ]; |
| 935 |
$table->heads = [ |
| 936 |
[ |
| 937 |
'class' => 'order-table', |
| 938 |
'html' => self::type_button( $index, null, 'string' ), |
| 939 |
], |
| 940 |
[ |
| 941 |
'class' => 'label-table', |
| 942 |
'html' => '', |
| 943 |
], |
| 944 |
[ |
| 945 |
'class' => 'fields-table', |
| 946 |
'html' => '', |
| 947 |
], |
| 948 |
[ |
| 949 |
'html' => self::type_button( $remove, null, 'string' ) . self::type_button( $duplicate, null, 'string' ), |
| 950 |
'class' => 'actions-table', |
| 951 |
], |
| 952 |
]; |
| 953 |
$blocks = [ |
| 954 |
[ |
| 955 |
'html' => '', |
| 956 |
'class' => 'order-table', |
| 957 |
], |
| 958 |
'label', |
| 959 |
'fields', |
| 960 |
'description', |
| 961 |
[ |
| 962 |
'html' => '', |
| 963 |
'class' => 'actions-table', |
| 964 |
], |
| 965 |
]; |
| 966 |
|
| 967 |
$tax_container = clone $container; |
| 968 |
$tax_container->id[] = 'taxonomies'; |
| 969 |
$tax_container->type .= '_taxonomies'; |
| 970 |
$tax_container->label = __( 'Taxonomies', 'fakerpress' ); |
| 971 |
$tax_container->description = ''; |
| 972 |
$tax_container->blocks = $blocks; |
| 973 |
|
| 974 |
$taxonomies = get_taxonomies( [ 'public' => true ], 'object' ); |
| 975 |
$_json_taxonomies_output = []; |
| 976 |
foreach ( $taxonomies as $key => $taxonomy ) { |
| 977 |
$_json_taxonomies_output[] = [ |
| 978 |
'id' => $taxonomy->name, |
| 979 |
'text' => $taxonomy->labels->name, |
| 980 |
]; |
| 981 |
} |
| 982 |
|
| 983 |
$tax_field = clone $field; |
| 984 |
$tax_field->_id[] = 'taxonomies'; |
| 985 |
$tax_field->_name[] = 'taxonomies'; |
| 986 |
$tax_field->type = 'dropdown'; |
| 987 |
$tax_field->multiple = true; |
| 988 |
$tax_field->{'data-options'} = $_json_taxonomies_output; |
| 989 |
$tax_field->value = 'post_tag, category'; |
| 990 |
$tax_field->class = [ 'taxonomies' ]; |
| 991 |
$tax_field->placeholder = esc_attr__( 'Select Which taxonomies', 'fakerpress' ); |
| 992 |
|
| 993 |
$content[] = $tax_container->build( self::type_dropdown( $tax_field, null, 'string' ) ); |
| 994 |
|
| 995 |
$terms_container = clone $container; |
| 996 |
$terms_container->id[] = 'terms'; |
| 997 |
$terms_container->type .= '_terms'; |
| 998 |
$terms_container->label = __( 'Terms', 'fakerpress' ); |
| 999 |
$terms_container->description = ''; |
| 1000 |
$terms_container->blocks = $blocks; |
| 1001 |
|
| 1002 |
$terms = clone $field; |
| 1003 |
$terms->_id[] = 'terms'; |
| 1004 |
$terms->_name[] = 'terms'; |
| 1005 |
$terms->type = 'dropdown'; |
| 1006 |
$terms->multiple = true; |
| 1007 |
$terms->description = esc_html__( 'If you do not select any, the plugin will choose from all the existing terms.', 'fakerpress' ); |
| 1008 |
$terms->{'data-source'} = 'search_terms'; |
| 1009 |
$terms->{'data-nonce'} = wp_create_nonce( Plugin::$slug . '-select2-search_terms' ); |
| 1010 |
|
| 1011 |
$terms->placeholder = esc_attr__( 'Which terms can be used', 'fakerpress' ); |
| 1012 |
|
| 1013 |
$content[] = $terms_container->build( self::type_dropdown( $terms, null, 'string' ) ); |
| 1014 |
|
| 1015 |
$rate_container = clone $container; |
| 1016 |
$rate_container->id[] = 'rate'; |
| 1017 |
$rate_container->type .= 'rate'; |
| 1018 |
$rate_container->label = esc_html__( 'Rate', 'fakerpress' ); |
| 1019 |
$rate_container->description = esc_html__( 'Percentage rate of posts that will have terms generated for the amount below', 'fakerpress' ); |
| 1020 |
$rate_container->blocks = $blocks; |
| 1021 |
|
| 1022 |
$rate = clone $field; |
| 1023 |
$rate->_id[] = 'rate'; |
| 1024 |
$rate->_name[] = 'rate'; |
| 1025 |
$rate->type = 'number'; |
| 1026 |
$rate->placeholder = esc_attr__( 'Rate', 'fakerpress' ); |
| 1027 |
$rate->min = 0; |
| 1028 |
$rate->max = 100; |
| 1029 |
$rate->value = 85; |
| 1030 |
|
| 1031 |
$content[] = $rate_container->build( self::type_text( $rate, null, 'string' ) ); |
| 1032 |
|
| 1033 |
$qty_container = clone $container; |
| 1034 |
$qty_container->id[] = 'qty'; |
| 1035 |
$qty_container->type .= '_qty'; |
| 1036 |
$qty_container->label = esc_html__( 'Quantity', 'fakerpress' ); |
| 1037 |
$qty_container->description = __( 'How many terms will be selected. <br> E.g.: From 1 to 4 or just fill the first field for an exact number', 'fakerpress' ); |
| 1038 |
$qty_container->blocks = $blocks; |
| 1039 |
|
| 1040 |
$qty = clone $field; |
| 1041 |
$qty->_id[] = 'qty'; |
| 1042 |
$qty->_name[] = 'qty'; |
| 1043 |
$qty->type = 'range'; |
| 1044 |
$qty->min = 1; |
| 1045 |
$qty->max = 4; |
| 1046 |
$qty->_max = 200; |
| 1047 |
$qty->class = [ 'qty' ]; |
| 1048 |
|
| 1049 |
$content[] = $qty_container->build( self::type_range( $qty, null, 'string' ) ); |
| 1050 |
|
| 1051 |
$content = $table->build( $content ); |
| 1052 |
|
| 1053 |
if ( is_a( $container, __CLASS__ ) ) { |
| 1054 |
$html[] = $container->build( $content ); |
| 1055 |
} else { |
| 1056 |
$html = $content; |
| 1057 |
} |
| 1058 |
|
| 1059 |
if ( 'string' === $output ) { |
| 1060 |
return implode( "\r\n", $html ); |
| 1061 |
} else { |
| 1062 |
return $html; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
public static function type_meta( $field, $container = null, $output = 'string', $html = [] ) { |
| 1067 |
$field = self::parse( $field, $container ); |
| 1068 |
if ( is_scalar( $field ) ) { |
| 1069 |
return false; |
| 1070 |
} |
| 1071 |
|
| 1072 |
$index = clone $field; |
| 1073 |
$index->_id[] = 'index'; |
| 1074 |
$index->_name[] = 'index'; |
| 1075 |
$index->type = 'button'; |
| 1076 |
$index->value = $field->index + 1; |
| 1077 |
$index->disabled = true; |
| 1078 |
$index->class = [ 'action-order' ]; |
| 1079 |
|
| 1080 |
$remove = clone $field; |
| 1081 |
$remove->_id[] = 'remove'; |
| 1082 |
$remove->_name[] = 'remove'; |
| 1083 |
$remove->type = 'button'; |
| 1084 |
$remove->value = '−'; |
| 1085 |
$remove->class = [ 'action-remove' ]; |
| 1086 |
|
| 1087 |
$duplicate = clone $field; |
| 1088 |
$duplicate->_id[] = 'duplicate'; |
| 1089 |
$duplicate->_name[] = 'duplicate'; |
| 1090 |
$duplicate->type = 'button'; |
| 1091 |
$duplicate->deactive = true; |
| 1092 |
$duplicate->value = '+'; |
| 1093 |
$duplicate->class = [ 'action-duplicate' ]; |
| 1094 |
|
| 1095 |
$table = clone $container; |
| 1096 |
$table->blocks = [ 'heading', 'table' ]; |
| 1097 |
$table->heads = [ |
| 1098 |
[ |
| 1099 |
'class' => 'order-table', |
| 1100 |
'html' => self::type_button( $index, null, 'string' ), |
| 1101 |
], |
| 1102 |
[ |
| 1103 |
'class' => 'label-table', |
| 1104 |
'html' => '', |
| 1105 |
], |
| 1106 |
[ |
| 1107 |
'class' => 'fields-table', |
| 1108 |
'html' => '', |
| 1109 |
], |
| 1110 |
[ |
| 1111 |
'html' => self::type_button( $remove, null, 'string' ) . self::type_button( $duplicate, null, 'string' ), |
| 1112 |
'class' => 'actions-table', |
| 1113 |
], |
| 1114 |
]; |
| 1115 |
|
| 1116 |
$container_blocks = [ |
| 1117 |
[ |
| 1118 |
'html' => '', |
| 1119 |
'class' => 'order-table', |
| 1120 |
], |
| 1121 |
'label', |
| 1122 |
'fields', |
| 1123 |
[ |
| 1124 |
'html' => '', |
| 1125 |
'class' => 'actions-table', |
| 1126 |
], |
| 1127 |
]; |
| 1128 |
|
| 1129 |
// Store the Configuration |
| 1130 |
$configuration = $field->config; |
| 1131 |
|
| 1132 |
// Prevents Unwanted Attributes |
| 1133 |
unset( $field->config ); |
| 1134 |
|
| 1135 |
// Makes Sure we at least have some configuration to avoid bugs |
| 1136 |
if ( empty( $configuration ) ) { |
| 1137 |
$configuration = [ true ]; |
| 1138 |
} |
| 1139 |
|
| 1140 |
foreach ( $configuration as $index => $config ) { |
| 1141 |
$type = clone $field; |
| 1142 |
$type->_id[] = 'type'; |
| 1143 |
$type->_name[] = 'type'; |
| 1144 |
$type->type = 'dropdown'; |
| 1145 |
$type->options = self::get_meta_types(); |
| 1146 |
$type->class = [ 'meta_type' ]; |
| 1147 |
$type->placeholder = esc_attr__( 'Select a Field type', 'fakerpress' ); |
| 1148 |
if ( isset( $config['type'] ) ) { |
| 1149 |
$type->value = $config['type']; |
| 1150 |
} |
| 1151 |
|
| 1152 |
$name = clone $field; |
| 1153 |
$name->_id[] = 'name'; |
| 1154 |
$name->_name[] = 'name'; |
| 1155 |
$name->type = 'text'; |
| 1156 |
$name->class = [ 'meta_name' ]; |
| 1157 |
$name->placeholder = esc_attr__( 'Newborn Meta needs a Name, E.g.: _new_image', 'fakerpress' ); |
| 1158 |
if ( isset( $config['name'] ) ) { |
| 1159 |
$name->value = $config['name']; |
| 1160 |
} |
| 1161 |
|
| 1162 |
$containers = (object) []; |
| 1163 |
|
| 1164 |
$containers->type = clone $container; |
| 1165 |
$containers->type->id[] = 'type'; |
| 1166 |
$containers->type->type .= '_type'; |
| 1167 |
$containers->type->label = __( 'Type', 'fakerpress' ); |
| 1168 |
$containers->type->description = __( 'Select a type of the Meta Field', 'fakerpress' ); |
| 1169 |
$containers->type->class = [ 'meta_type-container' ]; |
| 1170 |
$containers->type->blocks = $container_blocks; |
| 1171 |
|
| 1172 |
$containers->name = clone $container; |
| 1173 |
$containers->name->id[] = 'name'; |
| 1174 |
$containers->name->type .= '_name'; |
| 1175 |
$containers->name->label = __( 'Name', 'fakerpress' ); |
| 1176 |
$containers->name->description = __( 'Select the name for Meta Field', 'fakerpress' ); |
| 1177 |
$containers->name->class = [ 'meta_name-container' ]; |
| 1178 |
$containers->name->blocks = $container_blocks; |
| 1179 |
|
| 1180 |
$containers->conf = clone $container; |
| 1181 |
$containers->conf->id[] = 'conf'; |
| 1182 |
$containers->conf->type .= '_conf'; |
| 1183 |
$containers->conf->label = __( 'Configuration', 'fakerpress' ); |
| 1184 |
$containers->conf->description = __( '', 'fakerpress' ); |
| 1185 |
$containers->conf->class = [ 'meta_conf-container' ]; |
| 1186 |
$containers->conf->blocks = $container_blocks; |
| 1187 |
$containers->conf->attributes = [ |
| 1188 |
'data-config' => $config, |
| 1189 |
]; |
| 1190 |
|
| 1191 |
$content[] = [ |
| 1192 |
$containers->type->build( self::type_dropdown( $type, null, 'string' ) ), |
| 1193 |
$containers->name->build( self::type_text( $name, null, 'string' ) ), |
| 1194 |
$containers->conf->build( '' ), |
| 1195 |
]; |
| 1196 |
|
| 1197 |
} |
| 1198 |
|
| 1199 |
$content = $table->build( $content ); |
| 1200 |
|
| 1201 |
if ( is_a( $container, __CLASS__ ) ) { |
| 1202 |
$html[] = $container->build( $content ); |
| 1203 |
} else { |
| 1204 |
$html = $content; |
| 1205 |
} |
| 1206 |
|
| 1207 |
// Creates Templates for generating Meta via JavaScript |
| 1208 |
foreach ( $type->options as $key => $ftype ) { |
| 1209 |
$is_callable = ( isset( $ftype->template ) && is_callable( $ftype->template ) ); |
| 1210 |
$html[] = '<script type="text/html" data-rel="' . self::id( $container->id, true ) . '" class="' . Utils::abbr( 'template-' . $ftype->value ) . '"' . ( $is_callable ? ' data-callable' : '' ) . '>'; |
| 1211 |
if ( $is_callable ) { |
| 1212 |
$html[] = call_user_func_array( $ftype->template, [ $field, $ftype ] ); |
| 1213 |
} |
| 1214 |
$html[] = '</script>'; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if ( 'string' === $output ) { |
| 1218 |
return implode( "\r\n", $html ); |
| 1219 |
} else { |
| 1220 |
return $html; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
public static function get_meta_types() { |
| 1225 |
$types = (object) []; |
| 1226 |
|
| 1227 |
$default = (object) [ |
| 1228 |
'separator' => [ |
| 1229 |
'_id' => [ 'meta', 'separator' ], |
| 1230 |
'_name' => [ 'meta', 'separator' ], |
| 1231 |
'type' => 'text', |
| 1232 |
'size' => 'tiny', |
| 1233 |
'class' => [], |
| 1234 |
'value' => ',', |
| 1235 |
'placeholder' => __( 'E.g.: , ', 'fakerpress' ), |
| 1236 |
'label' => __( 'Separator', 'fakerpress' ), |
| 1237 |
], |
| 1238 |
'weight' => [ |
| 1239 |
'_id' => [ 'meta', 'weight' ], |
| 1240 |
'_name' => [ 'meta', 'weight' ], |
| 1241 |
'type' => 'number', |
| 1242 |
'class' => [], |
| 1243 |
'value' => 90, |
| 1244 |
'min' => 0, |
| 1245 |
'max' => 100, |
| 1246 |
'placeholder' => __( 'E.g.: 55', 'fakerpress' ), |
| 1247 |
'label' => __( 'Weight', 'fakerpress' ), |
| 1248 |
], |
| 1249 |
'qty' => [ |
| 1250 |
'_id' => [ 'meta', 'qty' ], |
| 1251 |
'_name' => [ 'meta', 'qty' ], |
| 1252 |
'type' => 'range', |
| 1253 |
'class' => [], |
| 1254 |
'label' => __( 'Quantity', 'fakerpress' ), |
| 1255 |
], |
| 1256 |
]; |
| 1257 |
foreach ( $default as $key => $field ) { |
| 1258 |
$default->{$key} = (object) $field; |
| 1259 |
} |
| 1260 |
|
| 1261 |
$types->numbers = [ |
| 1262 |
'value' => 'numbers', |
| 1263 |
'text' => __( 'Number', 'fakerpress' ), |
| 1264 |
'template' => function( $field, $type ) use ( $default ) { |
| 1265 |
$range = clone $field; |
| 1266 |
$range->_id = [ 'meta', 'number' ]; |
| 1267 |
$range->_name = [ 'meta', 'number' ]; |
| 1268 |
$range->type = 'range'; |
| 1269 |
$range->class = []; |
| 1270 |
$range->label = __( 'Range of possible numbers', 'fakerpress' ); |
| 1271 |
$range->_min = 0; |
| 1272 |
|
| 1273 |
$html[] = Field::wrapper( Field::type_range( $range, null, 'string' ), $range ); |
| 1274 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1275 |
|
| 1276 |
return implode( "\r\n", $html ); |
| 1277 |
}, |
| 1278 |
]; |
| 1279 |
|
| 1280 |
$types->wp_query = [ |
| 1281 |
'value' => 'wp_query', |
| 1282 |
'text' => __( 'WP_Query', 'fakerpress' ), |
| 1283 |
'template' => function( $field, $type ) use ( $default ) { |
| 1284 |
$query = clone $field; |
| 1285 |
$query->_id = [ 'meta', 'query' ]; |
| 1286 |
$query->_name = [ 'meta', 'query' ]; |
| 1287 |
$query->type = 'text'; |
| 1288 |
$query->class = []; |
| 1289 |
$query->size = 'large'; |
| 1290 |
$query->placeholder = __( 'category=2&posts_per_page=10', 'fakerpress' ); |
| 1291 |
$query->label = __( 'Uses <a href="http://codex.wordpress.org/Class_Reference/WP_Query" target="_blank">WP_Query</a>', 'fakerpress' ); |
| 1292 |
|
| 1293 |
$html[] = Field::wrapper( Field::type_text( $query, null, 'string' ), $query ); |
| 1294 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1295 |
|
| 1296 |
return implode( "\r\n", $html ); |
| 1297 |
}, |
| 1298 |
]; |
| 1299 |
|
| 1300 |
$types->attachment = [ |
| 1301 |
'value' => 'attachment', |
| 1302 |
'text' => __( 'Attachment', 'fakerpress' ), |
| 1303 |
'template' => function( $field, $type ) use ( $default ) { |
| 1304 |
$store = clone $field; |
| 1305 |
$store->_id = [ 'meta', 'store' ]; |
| 1306 |
$store->_name = [ 'meta', 'store' ]; |
| 1307 |
$store->type = 'dropdown'; |
| 1308 |
$store->value = 'id'; |
| 1309 |
$store->options = [ |
| 1310 |
[ |
| 1311 |
'id' => 'id', |
| 1312 |
'text' => esc_attr__( 'Attachment ID' ), |
| 1313 |
], |
| 1314 |
[ |
| 1315 |
'id' => 'url', |
| 1316 |
'text' => esc_attr__( 'Attachment URL' ), |
| 1317 |
], |
| 1318 |
]; |
| 1319 |
$store->class = []; |
| 1320 |
$store->placeholder = __( 'Which value should be saved on the Meta', 'fakerpress' ); |
| 1321 |
$store->label = __( 'Stored Data', 'fakerpress' ); |
| 1322 |
|
| 1323 |
$providers = clone $field; |
| 1324 |
$providers->_id = [ 'meta', 'provider' ]; |
| 1325 |
$providers->_name = [ 'meta', 'provider' ]; |
| 1326 |
$providers->type = 'dropdown'; |
| 1327 |
$providers->class = []; |
| 1328 |
$providers->multiple = true; |
| 1329 |
$providers->placeholder = __( 'Select at least one Provider', 'fakerpress' ); |
| 1330 |
$providers->label = __( 'Which image services will the generator use?', 'fakerpress' ); |
| 1331 |
$providers->value = implode( ',', wp_list_pluck( Module\Attachment::get_providers(), 'id' ) ); |
| 1332 |
$providers->{'data-options'} = Module\Attachment::get_providers(); |
| 1333 |
|
| 1334 |
$size_width = clone $field; |
| 1335 |
$size_width->_id = [ 'meta', 'width' ]; |
| 1336 |
$size_width->_name = [ 'meta', 'width' ]; |
| 1337 |
$size_width->type = 'range'; |
| 1338 |
$size_width->class = []; |
| 1339 |
$size_width->label = __( 'Range of possible width sizes for the image', 'fakerpress' ); |
| 1340 |
$size_width->_min = 0; |
| 1341 |
$size_width->_placeholder_min = esc_attr__( 'e.g.: 350', 'fakerpress' ); |
| 1342 |
$size_width->_placeholder_max = esc_attr__( 'e.g.: 900', 'fakerpress' ); |
| 1343 |
$size_width->_prevent_disable = true; |
| 1344 |
|
| 1345 |
$size_height = clone $field; |
| 1346 |
$size_height->_id = [ 'meta', 'height' ]; |
| 1347 |
$size_height->_name = [ 'meta', 'height' ]; |
| 1348 |
$size_height->type = 'range'; |
| 1349 |
$size_height->class = []; |
| 1350 |
$size_height->label = __( 'Range of possible height sizes for the image', 'fakerpress' ); |
| 1351 |
$size_height->_min = 0; |
| 1352 |
$size_height->_placeholder_min = esc_attr__( 'e.g.: 125', 'fakerpress' ); |
| 1353 |
$size_height->_placeholder_max = esc_attr__( 'e.g.: 650', 'fakerpress' ); |
| 1354 |
$size_height->_prevent_disable = true; |
| 1355 |
|
| 1356 |
$html[] = Field::wrapper( Field::type_dropdown( $store, null, 'string' ), $store ); |
| 1357 |
$html[] = Field::wrapper( Field::type_dropdown( $providers, null, 'string' ), $providers ); |
| 1358 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1359 |
|
| 1360 |
// Image Dimensions |
| 1361 |
$html[] = Field::wrapper( Field::type_range( $size_width, null, 'string' ), $size_width ); |
| 1362 |
$html[] = Field::wrapper( Field::type_range( $size_height, null, 'string' ), $size_height ); |
| 1363 |
|
| 1364 |
return implode( "\r\n", $html ); |
| 1365 |
}, |
| 1366 |
]; |
| 1367 |
|
| 1368 |
$types->elements = [ |
| 1369 |
'value' => 'elements', |
| 1370 |
'text' => __( 'Elements', 'fakerpress' ), |
| 1371 |
'template' => function( $field, $type ) use ( $default ) { |
| 1372 |
$tags = clone $field; |
| 1373 |
$tags->_id = [ 'meta', 'elements' ]; |
| 1374 |
$tags->_name = [ 'meta', 'elements' ]; |
| 1375 |
$tags->type = 'dropdown'; |
| 1376 |
$tags->multiple = true; |
| 1377 |
$tags->{'data-options'} = []; |
| 1378 |
$tags->{'data-tags'} = true; |
| 1379 |
$tags->class = []; |
| 1380 |
$tags->placeholder = __( 'Type all possible elements (Tab or Return)', 'fakerpress' ); |
| 1381 |
$tags->label = __( '', 'fakerpress' ); |
| 1382 |
|
| 1383 |
$html[] = Field::wrapper( Field::type_dropdown( $tags, null, 'string' ), $tags ); |
| 1384 |
$html[] = Field::wrapper( Field::type_range( $default->qty, null, 'string' ), $default->qty ); |
| 1385 |
$html[] = Field::wrapper( Field::type_text( $default->separator, null, 'string' ), $default->separator ); |
| 1386 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1387 |
|
| 1388 |
return implode( "\r\n", $html ); |
| 1389 |
}, |
| 1390 |
]; |
| 1391 |
|
| 1392 |
$types->letter = [ |
| 1393 |
'value' => 'letter', |
| 1394 |
'text' => __( 'Letter', 'fakerpress' ), |
| 1395 |
'template' => function( $field, $type ) use ( $default ) { |
| 1396 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1397 |
|
| 1398 |
return implode( "\r\n", $html ); |
| 1399 |
}, |
| 1400 |
]; |
| 1401 |
|
| 1402 |
$types->words = [ |
| 1403 |
'value' => 'words', |
| 1404 |
'text' => __( 'Words', 'fakerpress' ), |
| 1405 |
'template' => function( $field, $type ) use ( $default ) { |
| 1406 |
|
| 1407 |
$html[] = Field::wrapper( Field::type_range( $default->qty, null, 'string' ), $default->qty ); |
| 1408 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1409 |
|
| 1410 |
return implode( "\r\n", $html ); |
| 1411 |
}, |
| 1412 |
]; |
| 1413 |
|
| 1414 |
$types->text = [ |
| 1415 |
'value' => 'text', |
| 1416 |
'text' => __( 'Text', 'fakerpress' ), |
| 1417 |
'template' => function( $field, $type ) use ( $default ) { |
| 1418 |
$text = clone $field; |
| 1419 |
$text->_id = [ 'meta', 'text_type' ]; |
| 1420 |
$text->_name = [ 'meta', 'text_type' ]; |
| 1421 |
$text->type = 'dropdown'; |
| 1422 |
$text->options = [ |
| 1423 |
[ |
| 1424 |
'text' => __( 'Sentences', 'fakerpress' ), |
| 1425 |
'value' => 'sentences', |
| 1426 |
], |
| 1427 |
[ |
| 1428 |
'text' => __( 'Paragraphs', 'fakerpress' ), |
| 1429 |
'value' => 'paragraphs', |
| 1430 |
], |
| 1431 |
]; |
| 1432 |
$text->value = 'paragraphs'; |
| 1433 |
$text->class = []; |
| 1434 |
|
| 1435 |
$separator = clone $default->separator; |
| 1436 |
$separator->value = '\n'; |
| 1437 |
$separator->label = __( 'Separator <b space>—</b> New Line: "\n"', 'fakerpress' ); |
| 1438 |
|
| 1439 |
$html[] = Field::wrapper( Field::type_dropdown( $text, null, 'string' ), $text ); |
| 1440 |
$html[] = Field::wrapper( Field::type_range( $default->qty, null, 'string' ), $default->qty ); |
| 1441 |
$html[] = Field::wrapper( Field::type_text( $separator, null, 'string' ), $separator ); |
| 1442 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1443 |
|
| 1444 |
return implode( "\r\n", $html ); |
| 1445 |
}, |
| 1446 |
]; |
| 1447 |
|
| 1448 |
$types->html = [ |
| 1449 |
'value' => 'html', |
| 1450 |
'text' => __( 'HTML', 'fakerpress' ), |
| 1451 |
'template' => function( $field, $type ) use ( $default ) { |
| 1452 |
$elements = clone $field; |
| 1453 |
$elements->_id = [ 'meta', 'elements' ]; |
| 1454 |
$elements->_name = [ 'meta', 'elements' ]; |
| 1455 |
$elements->type = 'dropdown'; |
| 1456 |
$elements->multiple = true; |
| 1457 |
$elements->{'data-tags'} = true; |
| 1458 |
$elements->{'data-options'} = array_merge( HTML::$sets['header'], HTML::$sets['list'], HTML::$sets['block'], HTML::$sets['self_close'] ); |
| 1459 |
$elements->value = implode( ',', $elements->{'data-options'} ); |
| 1460 |
$elements->class = []; |
| 1461 |
$elements->label = __( 'HTML Tags, without < or >', 'fakerpress' ); |
| 1462 |
|
| 1463 |
$html[] = Field::wrapper( Field::type_dropdown( $elements, null, 'string' ), $elements ); |
| 1464 |
$html[] = Field::wrapper( Field::type_range( $default->qty, null, 'string' ), $default->qty ); |
| 1465 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1466 |
|
| 1467 |
return implode( "\r\n", $html ); |
| 1468 |
}, |
| 1469 |
]; |
| 1470 |
|
| 1471 |
$types->lexify = [ |
| 1472 |
'value' => 'lexify', |
| 1473 |
'text' => __( 'Lexify', 'fakerpress' ), |
| 1474 |
'template' => function( $field, $type ) use ( $default ) { |
| 1475 |
$template = clone $field; |
| 1476 |
$template->_id = [ 'meta', 'template' ]; |
| 1477 |
$template->_name = [ 'meta', 'template' ]; |
| 1478 |
$template->type = 'text'; |
| 1479 |
$template->class = []; |
| 1480 |
$template->placeholder = __( 'E.g.: John ##??', 'fakerpress' ); |
| 1481 |
$template->label = __( 'John ##?? <b spacer>»</b> John 29ze', 'fakerpress' ); |
| 1482 |
|
| 1483 |
$html[] = Field::wrapper( Field::type_text( $template, null, 'string' ), $template ); |
| 1484 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1485 |
|
| 1486 |
return implode( "\r\n", $html ); |
| 1487 |
}, |
| 1488 |
]; |
| 1489 |
|
| 1490 |
$types->asciify = [ |
| 1491 |
'value' => 'asciify', |
| 1492 |
'text' => __( 'Asciify', 'fakerpress' ), |
| 1493 |
'template' => function( $field, $type ) use ( $default ) { |
| 1494 |
$template = clone $field; |
| 1495 |
$template->_id = [ 'meta', 'template' ]; |
| 1496 |
$template->_name = [ 'meta', 'template' ]; |
| 1497 |
$template->type = 'text'; |
| 1498 |
$template->class = []; |
| 1499 |
$template->placeholder = __( 'E.g.: John ****', 'fakerpress' ); |
| 1500 |
$template->label = __( 'John **** <b spacer>»</b> John r9"+', 'fakerpress' ); |
| 1501 |
|
| 1502 |
$html[] = Field::wrapper( Field::type_text( $template, null, 'string' ), $template ); |
| 1503 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1504 |
|
| 1505 |
return implode( "\r\n", $html ); |
| 1506 |
}, |
| 1507 |
]; |
| 1508 |
|
| 1509 |
$types->regexify = [ |
| 1510 |
'value' => 'regexify', |
| 1511 |
'text' => __( 'Regexify', 'fakerpress' ), |
| 1512 |
'template' => function( $field, $type ) use ( $default ) { |
| 1513 |
$template = clone $field; |
| 1514 |
$template->_id = [ 'meta', 'template' ]; |
| 1515 |
$template->_name = [ 'meta', 'template' ]; |
| 1516 |
$template->type = 'text'; |
| 1517 |
$template->class = []; |
| 1518 |
$template->placeholder = __( 'E.g.: [A-Z0-9._%+-]+@[A-Z0-9.-]', 'fakerpress' ); |
| 1519 |
$template->label = __( '[A-Z0-9._%+-]+@[A-Z0-9.-] <b spacer>»</b> sm0@y8k96a', 'fakerpress' ); |
| 1520 |
|
| 1521 |
$html[] = Field::wrapper( Field::type_text( $template, null, 'string' ), $template ); |
| 1522 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1523 |
|
| 1524 |
return implode( "\r\n", $html ); |
| 1525 |
}, |
| 1526 |
]; |
| 1527 |
|
| 1528 |
$types->person = [ |
| 1529 |
'value' => 'person', |
| 1530 |
'text' => __( 'Person', 'fakerpress' ), |
| 1531 |
'template' => function( $field, $type ) use ( $default ) { |
| 1532 |
$template = clone $field; |
| 1533 |
$template->_id = [ 'meta', 'template' ]; |
| 1534 |
$template->_name = [ 'meta', 'template' ]; |
| 1535 |
$template->type = 'dropdown'; |
| 1536 |
$template->multiple = true; |
| 1537 |
$template->{'data-tags'} = true; |
| 1538 |
$template->{'data-options'} = [ |
| 1539 |
[ |
| 1540 |
'text' => __( 'Title', 'fakerpress' ), |
| 1541 |
'value' => '{% title %}', |
| 1542 |
], |
| 1543 |
[ |
| 1544 |
'text' => __( 'First Name', 'fakerpress' ), |
| 1545 |
'value' => '{% first_name %}', |
| 1546 |
], |
| 1547 |
[ |
| 1548 |
'text' => __( 'Last Name', 'fakerpress' ), |
| 1549 |
'value' => '{% last_name %}', |
| 1550 |
], |
| 1551 |
[ |
| 1552 |
'text' => __( 'Suffix', 'fakerpress' ), |
| 1553 |
'value' => '{% suffix %}', |
| 1554 |
], |
| 1555 |
]; |
| 1556 |
$template->value = 'title,first_name,last_name,suffix'; |
| 1557 |
$template->value = '{% title %}|{% first_name %}|{% last_name %}|{% suffix %}'; |
| 1558 |
$template->{'data-separator'} = '|'; |
| 1559 |
$template->class = []; |
| 1560 |
$template->label = __( 'Name Template', 'fakerpress' ); |
| 1561 |
|
| 1562 |
$gender = clone $field; |
| 1563 |
$gender->_id = [ 'meta', 'gender' ]; |
| 1564 |
$gender->_name = [ 'meta', 'gender' ]; |
| 1565 |
$gender->type = 'radio'; |
| 1566 |
$gender->options = [ |
| 1567 |
[ |
| 1568 |
'text' => __( 'Male', 'fakerpress' ), |
| 1569 |
'value' => 'male', |
| 1570 |
], |
| 1571 |
[ |
| 1572 |
'text' => __( 'Female', 'fakerpress' ), |
| 1573 |
'value' => 'female', |
| 1574 |
], |
| 1575 |
]; |
| 1576 |
$gender->value = 'female'; |
| 1577 |
$gender->class = []; |
| 1578 |
|
| 1579 |
$html[] = Field::wrapper( Field::type_dropdown( $template, null, 'string' ), $template ); |
| 1580 |
$html[] = Field::wrapper( Field::type_radio( $gender, null, 'string' ), $gender ); |
| 1581 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1582 |
|
| 1583 |
return implode( "\r\n", $html ); |
| 1584 |
}, |
| 1585 |
]; |
| 1586 |
|
| 1587 |
$types->geo = [ |
| 1588 |
'value' => 'geo', |
| 1589 |
'text' => __( 'Geo Information', 'fakerpress' ), |
| 1590 |
'template' => function( $field, $type ) use ( $default ) { |
| 1591 |
$template = clone $field; |
| 1592 |
$template->_id = [ 'meta', 'template' ]; |
| 1593 |
$template->_name = [ 'meta', 'template' ]; |
| 1594 |
$template->type = 'dropdown'; |
| 1595 |
$template->multiple = true; |
| 1596 |
$template->{'data-tags'} = true; |
| 1597 |
$template->{'data-options'} = [ |
| 1598 |
[ |
| 1599 |
'text' => __( 'Country', 'fakerpress' ), |
| 1600 |
'value' => '{% country %}', |
| 1601 |
], |
| 1602 |
[ |
| 1603 |
'text' => __( 'Country Code (e.g.: US)', 'fakerpress' ), |
| 1604 |
'value' => '{% country_code %}', |
| 1605 |
], |
| 1606 |
[ |
| 1607 |
'text' => __( 'Country ABBR (e.g.: USA', 'fakerpress' ), |
| 1608 |
'value' => '{% country_abbr %}', |
| 1609 |
], |
| 1610 |
[ |
| 1611 |
'text' => __( 'City Prefix', 'fakerpress' ), |
| 1612 |
'value' => '{% city_prefix %}', |
| 1613 |
], |
| 1614 |
[ |
| 1615 |
'text' => __( 'City Suffix', 'fakerpress' ), |
| 1616 |
'value' => '{% city_suffix %}', |
| 1617 |
], |
| 1618 |
[ |
| 1619 |
'text' => __( 'City', 'fakerpress' ), |
| 1620 |
'value' => '{% city %}', |
| 1621 |
], |
| 1622 |
[ |
| 1623 |
'text' => __( 'State', 'fakerpress' ), |
| 1624 |
'value' => '{% state %}', |
| 1625 |
], |
| 1626 |
[ |
| 1627 |
'text' => __( 'State Abbr', 'fakerpress' ), |
| 1628 |
'value' => '{% state_abbr %}', |
| 1629 |
], |
| 1630 |
[ |
| 1631 |
'text' => __( 'Address', 'fakerpress' ), |
| 1632 |
'value' => '{% address %}', |
| 1633 |
], |
| 1634 |
[ |
| 1635 |
'text' => __( 'Secondary Address', 'fakerpress' ), |
| 1636 |
'value' => '{% secondary_address %}', |
| 1637 |
], |
| 1638 |
[ |
| 1639 |
'text' => __( 'Building Number', 'fakerpress' ), |
| 1640 |
'value' => '{% building_number %}', |
| 1641 |
], |
| 1642 |
[ |
| 1643 |
'text' => __( 'Street Name', 'fakerpress' ), |
| 1644 |
'value' => '{% street_name %}', |
| 1645 |
], |
| 1646 |
[ |
| 1647 |
'text' => __( 'Street Address', 'fakerpress' ), |
| 1648 |
'value' => '{% street_address %}', |
| 1649 |
], |
| 1650 |
[ |
| 1651 |
'text' => __( 'Postal Code', 'fakerpress' ), |
| 1652 |
'value' => '{% postalcode %}', |
| 1653 |
], |
| 1654 |
[ |
| 1655 |
'text' => __( 'Latitude', 'fakerpress' ), |
| 1656 |
'value' => '{% latitude %}', |
| 1657 |
], |
| 1658 |
[ |
| 1659 |
'text' => __( 'Longitude', 'fakerpress' ), |
| 1660 |
'value' => '{% longitude %}', |
| 1661 |
], |
| 1662 |
]; |
| 1663 |
$template->value = '{% latitude %}|,|{% longitude %}'; |
| 1664 |
$template->{'data-separator'} = '|'; |
| 1665 |
$template->class = []; |
| 1666 |
$template->label = __( 'Address Format', 'fakerpress' ); |
| 1667 |
|
| 1668 |
$html[] = Field::wrapper( Field::type_dropdown( $template, null, 'string' ), $template ); |
| 1669 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1670 |
|
| 1671 |
return implode( "\r\n", $html ); |
| 1672 |
}, |
| 1673 |
]; |
| 1674 |
|
| 1675 |
$types->phone = [ |
| 1676 |
'value' => 'company', |
| 1677 |
'text' => __( 'Company', 'fakerpress' ), |
| 1678 |
'template' => function( $field, $type ) use ( $default ) { |
| 1679 |
$template = clone $field; |
| 1680 |
$template->_id = [ 'meta', 'template' ]; |
| 1681 |
$template->_name = [ 'meta', 'template' ]; |
| 1682 |
$template->type = 'dropdown'; |
| 1683 |
$template->multiple = true; |
| 1684 |
$template->{'data-tags'} = true; |
| 1685 |
$template->{'data-options'} = [ |
| 1686 |
[ |
| 1687 |
'text' => __( 'Catch Phrase', 'fakerpress' ), |
| 1688 |
'value' => '{% catch_phrase %}', |
| 1689 |
], |
| 1690 |
[ |
| 1691 |
'text' => __( 'BS', 'fakerpress' ), |
| 1692 |
'value' => '{% bs %}', |
| 1693 |
], |
| 1694 |
[ |
| 1695 |
'text' => __( 'Company', 'fakerpress' ), |
| 1696 |
'value' => '{% company %}', |
| 1697 |
], |
| 1698 |
[ |
| 1699 |
'text' => __( 'Suffix', 'fakerpress' ), |
| 1700 |
'value' => '{% suffix %}', |
| 1701 |
], |
| 1702 |
]; |
| 1703 |
$template->value = '{% company %}| |{% suffix %}'; |
| 1704 |
$template->{'data-separator'} = '|'; |
| 1705 |
$template->class = []; |
| 1706 |
$template->label = __( 'Company Format', 'fakerpress' ); |
| 1707 |
|
| 1708 |
$html[] = Field::wrapper( Field::type_dropdown( $template, null, 'string' ), $template ); |
| 1709 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1710 |
|
| 1711 |
return implode( "\r\n", $html ); |
| 1712 |
}, |
| 1713 |
]; |
| 1714 |
|
| 1715 |
$types->date = [ |
| 1716 |
'value' => 'date', |
| 1717 |
'text' => __( 'Date', 'fakerpress' ), |
| 1718 |
'template' => function( $field, $type ) use ( $default ) { |
| 1719 |
$template = clone $field; |
| 1720 |
$template->_id = [ 'meta', 'interval' ]; |
| 1721 |
$template->_name = [ 'meta', 'interval' ]; |
| 1722 |
$template->type = 'interval'; |
| 1723 |
$template->class = []; |
| 1724 |
|
| 1725 |
$format = clone $field; |
| 1726 |
$format->_id = [ 'meta', 'format' ]; |
| 1727 |
$format->_name = [ 'meta', 'format' ]; |
| 1728 |
$format->type = 'text'; |
| 1729 |
$format->class = []; |
| 1730 |
$format->value = 'Y-m-d H:i:s'; |
| 1731 |
$format->label = __( 'Date Format <b space>—</b> See <a href="http://php.net/manual/function.date.php" target="_blank">PHP Date</a>', 'fakerpress' ); |
| 1732 |
|
| 1733 |
$html[] = Field::wrapper( Field::type_interval( $template, null, 'string' ), $template ); |
| 1734 |
$html[] = Field::wrapper( Field::type_text( $format, null, 'string' ), $format ); |
| 1735 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1736 |
|
| 1737 |
return implode( "\r\n", $html ); |
| 1738 |
}, |
| 1739 |
]; |
| 1740 |
|
| 1741 |
$types->timezone = [ |
| 1742 |
'value' => 'timezone', |
| 1743 |
'text' => __( 'TimeZone', 'fakerpress' ), |
| 1744 |
'template' => function( $field, $type ) use ( $default ) { |
| 1745 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1746 |
|
| 1747 |
return implode( "\r\n", $html ); |
| 1748 |
}, |
| 1749 |
]; |
| 1750 |
|
| 1751 |
$types->email = [ |
| 1752 |
'value' => 'email', |
| 1753 |
'text' => __( 'Email', 'fakerpress' ), |
| 1754 |
'template' => function( $field, $type ) use ( $default ) { |
| 1755 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1756 |
|
| 1757 |
return implode( "\r\n", $html ); |
| 1758 |
}, |
| 1759 |
]; |
| 1760 |
|
| 1761 |
$types->domain = [ |
| 1762 |
'value' => 'domain', |
| 1763 |
'text' => __( 'Domain', 'fakerpress' ), |
| 1764 |
'template' => function( $field, $type ) use ( $default ) { |
| 1765 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1766 |
|
| 1767 |
return implode( "\r\n", $html ); |
| 1768 |
}, |
| 1769 |
]; |
| 1770 |
|
| 1771 |
$types->ip = [ |
| 1772 |
'value' => 'ip', |
| 1773 |
'text' => __( 'IP', 'fakerpress' ), |
| 1774 |
'template' => function( $field, $type ) use ( $default ) { |
| 1775 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1776 |
|
| 1777 |
return implode( "\r\n", $html ); |
| 1778 |
}, |
| 1779 |
]; |
| 1780 |
|
| 1781 |
$types->user_agent = [ |
| 1782 |
'value' => 'user_agent', |
| 1783 |
'text' => __( 'Browser User Agent', 'fakerpress' ), |
| 1784 |
'template' => function( $field, $type ) use ( $default ) { |
| 1785 |
$html[] = Field::wrapper( Field::type_number( $default->weight, null, 'string' ), $default->weight ); |
| 1786 |
|
| 1787 |
return implode( "\r\n", $html ); |
| 1788 |
}, |
| 1789 |
]; |
| 1790 |
|
| 1791 |
foreach ( $types as $key => $type ) { |
| 1792 |
$types->{$key} = (object) $type; |
| 1793 |
} |
| 1794 |
|
| 1795 |
return apply_filters( self::plugin . '/fields/meta_types', $types ); |
| 1796 |
} |
| 1797 |
|
| 1798 |
public static function type_interval( $field, $container = null, $output = 'string', $html = [] ) { |
| 1799 |
$field = self::parse( $field, $container ); |
| 1800 |
if ( is_scalar( $field ) ) { |
| 1801 |
return false; |
| 1802 |
} |
| 1803 |
|
| 1804 |
$min = clone $field; |
| 1805 |
$min->_id[] = 'min'; |
| 1806 |
$min->_name[] = 'min'; |
| 1807 |
$min->type = 'date'; |
| 1808 |
$min->{'data-type'} = 'min'; |
| 1809 |
$min->value = ''; |
| 1810 |
$min->class = []; |
| 1811 |
$min->placeholder = esc_attr__( 'yyyy-mm-dd', 'fakerpress' ); |
| 1812 |
|
| 1813 |
$max = clone $field; |
| 1814 |
$max->_id[] = 'max'; |
| 1815 |
$max->_name[] = 'max'; |
| 1816 |
$max->type = 'date'; |
| 1817 |
$max->{'data-type'} = 'max'; |
| 1818 |
$max->class = []; |
| 1819 |
$max->value = ''; |
| 1820 |
$max->placeholder = esc_attr__( 'yyyy-mm-dd', 'fakerpress' ); |
| 1821 |
|
| 1822 |
$interval = clone $field; |
| 1823 |
$interval->_id[] = 'name'; |
| 1824 |
$interval->_name[] = 'name'; |
| 1825 |
$interval->type = 'dropdown'; |
| 1826 |
$interval->class = []; |
| 1827 |
$interval->{'data-placeholder'} = esc_attr__( 'Select an Interval', 'fakerpress' ); |
| 1828 |
$interval->options = Dates::get_intervals(); |
| 1829 |
|
| 1830 |
$content[] = self::type_dropdown( $interval, null, 'string' ); |
| 1831 |
$content[] = self::type_date( $min, null, 'string' ); |
| 1832 |
$content[] = '<div class="dashicons dashicons-arrow-right-alt2 dashicon-date" style="display: inline-block;"></div>'; |
| 1833 |
$content[] = self::type_date( $max, null, 'string' ); |
| 1834 |
|
| 1835 |
if ( is_a( $container, __CLASS__ ) ) { |
| 1836 |
$html[] = $container->build( $content ); |
| 1837 |
} else { |
| 1838 |
$html = $content; |
| 1839 |
} |
| 1840 |
|
| 1841 |
if ( 'string' === $output ) { |
| 1842 |
return implode( "\r\n", $html ); |
| 1843 |
} else { |
| 1844 |
return $html; |
| 1845 |
} |
| 1846 |
} |
| 1847 |
|
| 1848 |
public static function type_raw( $field, $container = null, $output = 'string', $html = [] ) { |
| 1849 |
$field = self::parse( $field, $container ); |
| 1850 |
if ( is_scalar( $field ) ) { |
| 1851 |
return false; |
| 1852 |
} |
| 1853 |
|
| 1854 |
if ( ! empty( $field->html ) ) { |
| 1855 |
$content[] = $field->html; |
| 1856 |
} else { |
| 1857 |
$content = ''; |
| 1858 |
} |
| 1859 |
|
| 1860 |
if ( is_a( $container, __CLASS__ ) ) { |
| 1861 |
$html[] = $container->build( $content ); |
| 1862 |
} else { |
| 1863 |
$html = $content; |
| 1864 |
} |
| 1865 |
|
| 1866 |
if ( 'string' === $output ) { |
| 1867 |
return implode( "\r\n", $html ); |
| 1868 |
} else { |
| 1869 |
return $html; |
| 1870 |
} |
| 1871 |
} |
| 1872 |
} |
| 1873 |
|