| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\MetaBox; |
| 4 |
use LearnPress\Helpers\Template; |
| 5 |
|
| 6 |
/** |
| 7 |
* LP_Meta_Box_Field |
| 8 |
* |
| 9 |
* @version 1.0.0 |
| 10 |
* @since 4.2.3.1 |
| 11 |
*/ |
| 12 |
class LPMetaBoxField { |
| 13 |
const TEXT = 'text'; |
| 14 |
const NUMBER = 'number'; |
| 15 |
const CHECKBOX = 'checkbox'; |
| 16 |
const SELECT = 'select'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Extra options of field. |
| 20 |
* |
| 21 |
* @var string $class |
| 22 |
*/ |
| 23 |
public $extra = array(); |
| 24 |
|
| 25 |
public static function render( string $type, string $name, array $extra = [], array $el_wrapper = [] ) { |
| 26 |
$content = ''; |
| 27 |
$value = $extra['value'] ?? ( $extra['default'] ?? '' ); |
| 28 |
|
| 29 |
switch ( $type ) { |
| 30 |
case self::TEXT: |
| 31 |
case self::NUMBER: |
| 32 |
$content = sprintf( |
| 33 |
'<input type="%s" name="%s" id="%s" value="%s" placeholder="%s" />', |
| 34 |
esc_attr( $type ), |
| 35 |
esc_attr( $name ), |
| 36 |
esc_attr( $extra['id'] ?? '' ), |
| 37 |
esc_attr( $value ), |
| 38 |
esc_attr( $extra['placeholder'] ?? '' ) |
| 39 |
); |
| 40 |
break; |
| 41 |
case self::CHECKBOX: |
| 42 |
$content = sprintf( |
| 43 |
'<input type="checkbox" name="%s" id="%s" value="1" %s />', |
| 44 |
esc_attr( $name ), |
| 45 |
esc_attr( $extra['id'] ?? '' ), |
| 46 |
checked( $value, 1, false ) ? 'checked' : '' |
| 47 |
); |
| 48 |
break; |
| 49 |
case self::SELECT: |
| 50 |
$select = [ |
| 51 |
sprintf( |
| 52 |
'<select name="%s" id="%s">', |
| 53 |
esc_attr( $name ), |
| 54 |
esc_attr( $extra['id'] ?? '' ) |
| 55 |
) => '</select>', |
| 56 |
]; |
| 57 |
|
| 58 |
$options = ''; |
| 59 |
foreach ( $extra['options'] ?? [] as $key => $value_option ) { |
| 60 |
if ( $value === $key ) { |
| 61 |
$options .= sprintf( '<option value="%s" selected>%s</option>', esc_attr( $key ), wp_kses_post( $value_option ) ); |
| 62 |
continue; |
| 63 |
} |
| 64 |
$options .= sprintf( '<option value="%s">%s</option>', esc_attr( $key ), wp_kses_post( $value_option ) ); |
| 65 |
} |
| 66 |
|
| 67 |
$content = Template::instance()->nest_elements( $select, $options ); |
| 68 |
break; |
| 69 |
case apply_filters( 'learn-press/meta-box-field-type', 'custom' ): |
| 70 |
$content = apply_filters( 'learn-press/meta-box-field-content', '', $type, $name, $extra ); |
| 71 |
break; |
| 72 |
default: |
| 73 |
echo 'Not support type'; |
| 74 |
break; |
| 75 |
} |
| 76 |
|
| 77 |
echo Template::instance()->nest_elements( $el_wrapper, $content ); |
| 78 |
} |
| 79 |
} |
| 80 |
|