| 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 $id |
| 16 |
* @param string $label |
| 17 |
* @param string $description |
| 18 |
* @param mixed $default |
| 19 |
* @param array $extra |
| 20 |
*/ |
| 21 |
public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 22 |
parent::__construct( $label, $description, $default, $extra ); |
| 23 |
} |
| 24 |
|
| 25 |
public function output( $thepostid ) { |
| 26 |
if ( empty( $this->id ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$extra = $this->extra; |
| 31 |
$placeholder = $extra['placeholder'] ?? ''; |
| 32 |
$class = ! empty( $extra['class'] ) ? 'class="' . esc_attr( $extra['class'] ) . '"' : ''; |
| 33 |
$style = ! empty( $extra['style'] ) ? 'style="' . esc_attr( $extra['style'] ) . '"' : ''; |
| 34 |
$wrapper_class = ! empty( $extra['wrapper_class'] ) ? esc_attr( $extra['wrapper_class'] ) : ''; |
| 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 |
echo '<div class="form-field ' . esc_attr( $this->id . '_field ' . $wrapper_class ) . '"> |
| 52 |
<label for="' . esc_attr( $this->id ) . '">' . wp_kses_post( $this->label ) . '</label>'; |
| 53 |
|
| 54 |
echo '<input type="' . esc_attr( $type_input ) . '" ' . $class . ' ' . $style . ' name="' . $this->id . '" id="' . $this->id . '" value="' . $value . '" placeholder="' . esc_attr( $placeholder ) . '" ' . implode( |
| 55 |
' ', |
| 56 |
$custom_attributes |
| 57 |
) . ' /> '; |
| 58 |
|
| 59 |
if ( ! empty( $this->description ) ) { |
| 60 |
echo '<p class="description">'; |
| 61 |
echo '<span>' . wp_kses_post( $this->description ) . '</span>'; |
| 62 |
|
| 63 |
if ( ! empty( $desc_tip ) ) { |
| 64 |
learn_press_quick_tip( $desc_tip ); |
| 65 |
} |
| 66 |
echo '</p>'; |
| 67 |
} |
| 68 |
echo '</div>'; |
| 69 |
} |
| 70 |
|
| 71 |
public function save( $post_id ) { |
| 72 |
$type_input = $this->extra['type_input'] ?? 'text'; |
| 73 |
$meta_value = isset( $_POST[ $this->id ] ) ? wp_unslash( $_POST[ $this->id ] ) : $this->default; |
| 74 |
|
| 75 |
if ( $meta_value !== '' && $type_input === 'number' ) { |
| 76 |
$step = isset( $this->extra['custom_attributes']['step'] ) ? $this->extra['custom_attributes']['step'] : ''; |
| 77 |
|
| 78 |
if ( floatval( $step ) !== 1 ) { |
| 79 |
$meta_value = floatval( $meta_value ); |
| 80 |
} else { |
| 81 |
$meta_value = absint( $meta_value ); |
| 82 |
} |
| 83 |
} else { |
| 84 |
$meta_value = LP_Helper::sanitize_params_submitted( $meta_value ); |
| 85 |
} |
| 86 |
|
| 87 |
update_post_meta( $post_id, $this->id, $meta_value ); |
| 88 |
} |
| 89 |
} |
| 90 |
|