| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Meta_Box_Text_Field |
| 5 |
* |
| 6 |
* @author nhamdv |
| 7 |
* @version 1.0.0 |
| 8 |
* @since 4.0.0 |
| 9 |
*/ |
| 10 |
class LP_Meta_Box_Text_Field extends LP_Meta_Box_Field { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor. |
| 14 |
* |
| 15 |
* @param string $label |
| 16 |
* @param string $description |
| 17 |
* @param mixed $default |
| 18 |
* @param array $extra |
| 19 |
*/ |
| 20 |
public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 21 |
parent::__construct( $label, $description, $default, $extra ); |
| 22 |
} |
| 23 |
|
| 24 |
public function output( $thepostid ) { |
| 25 |
if ( empty( $this->id ) ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$extra = $this->extra; |
| 30 |
$placeholder = $extra['placeholder'] ?? ''; |
| 31 |
$class = ! empty( $extra['class'] ) ? 'class="' . esc_attr( $extra['class'] ) . '"' : ''; |
| 32 |
$style = ! empty( $extra['style'] ) ? 'style="' . esc_attr( $extra['style'] ) . '"' : ''; |
| 33 |
$wrapper_class = ! empty( $extra['wrapper_class'] ) ? esc_attr( $extra['wrapper_class'] ) : ''; |
| 34 |
$wrapper_attr = $extra['wrapper_attr'] ?? []; |
| 35 |
|
| 36 |
$meta_exists = LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $this->id ); |
| 37 |
$meta = get_post_meta( $thepostid, $this->id, true ); |
| 38 |
$value = $meta_exists ? $meta : ( $this->default ?? '' ); |
| 39 |
$value = esc_attr( $extra['value'] ?? $value ); |
| 40 |
$type_input = $extra['type_input'] ?? 'text'; |
| 41 |
$desc_tip = $extra['desc_tip'] ?? ''; |
| 42 |
|
| 43 |
// Custom attribute handling |
| 44 |
$custom_attributes = array(); |
| 45 |
if ( ! empty( $extra['custom_attributes'] ) && is_array( $extra['custom_attributes'] ) ) { |
| 46 |
foreach ( $extra['custom_attributes'] as $attribute => $custom_attribute ) { |
| 47 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$dependency_check = $extra['dependency'] ?? []; |
| 52 |
if ( ! empty( $dependency_check ) ) { |
| 53 |
if ( $dependency_check['is_disable'] ) { |
| 54 |
$wrapper_class .= ' lp-option-disabled'; |
| 55 |
} |
| 56 |
|
| 57 |
$wrapper_attr[] = 'data-dependency=' . $dependency_check['name']; |
| 58 |
} |
| 59 |
|
| 60 |
printf( |
| 61 |
'<div class="form-field %s" %s><label for="%s">%s</label>', |
| 62 |
esc_attr( $this->id . '_field ' . $wrapper_class ), |
| 63 |
esc_attr( implode( ' ', $wrapper_attr ) ), |
| 64 |
esc_attr( $this->id ), |
| 65 |
wp_kses_post( $this->label ) |
| 66 |
); |
| 67 |
|
| 68 |
printf( |
| 69 |
'<input type="%s" %s %s name="%s" id="%s" value="%s" placeholder="%s" %s />', |
| 70 |
esc_attr( $type_input ), |
| 71 |
$class, |
| 72 |
$style, |
| 73 |
$this->id, |
| 74 |
$this->id, |
| 75 |
$value, |
| 76 |
esc_attr( $placeholder ), |
| 77 |
implode( ' ', $custom_attributes ) |
| 78 |
); |
| 79 |
|
| 80 |
if ( ! empty( $this->description ) ) { |
| 81 |
echo '<p class="description">'; |
| 82 |
echo '<span>' . wp_kses_post( $this->description ) . '</span>'; |
| 83 |
|
| 84 |
if ( ! empty( $desc_tip ) ) { |
| 85 |
learn_press_quick_tip( $desc_tip ); |
| 86 |
} |
| 87 |
echo '</p>'; |
| 88 |
} |
| 89 |
echo '</div>'; |
| 90 |
} |
| 91 |
|
| 92 |
public function save( $post_id ) { |
| 93 |
$type_input = $this->extra['type_input'] ?? 'text'; |
| 94 |
$meta_value = LP_Request::get_param( $this->id, $this->default ?? '', 'text', 'post' ); |
| 95 |
|
| 96 |
if ( $meta_value !== '' && $type_input === 'number' ) { |
| 97 |
$meta_value = (float) $meta_value; |
| 98 |
if ( isset( $this->extra['custom_attributes']['max'] ) ) { |
| 99 |
$value_max = (float) $this->extra['custom_attributes']['max']; |
| 100 |
if ( $meta_value > $value_max ) { |
| 101 |
$meta_value = $value_max; |
| 102 |
} |
| 103 |
} elseif ( isset( $this->extra['custom_attributes']['min'] ) ) { |
| 104 |
$value_min = (float) $this->extra['custom_attributes']['min']; |
| 105 |
if ( $meta_value < $value_min ) { |
| 106 |
$meta_value = $value_min; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
update_post_meta( $post_id, $this->id, $meta_value ); |
| 112 |
|
| 113 |
return $meta_value; |
| 114 |
} |
| 115 |
} |
| 116 |
|