| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Meta_Box_Duration_Attribute |
| 5 |
* |
| 6 |
* @author Nhamdv |
| 7 |
* @version 1.0.0 |
| 8 |
* @since 4.0.0 |
| 9 |
*/ |
| 10 |
class LP_Meta_Box_Select_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 meta_value( $thepostid ) { |
| 26 |
$multil_meta = isset( $this->extra['multil_meta'] ) ? $this->extra['multil_meta'] : false; |
| 27 |
return $multil_meta ? get_post_meta( $thepostid, $this->id, false ) : get_post_meta( $thepostid, $this->id, true ); |
| 28 |
} |
| 29 |
|
| 30 |
public function output( $thepostid ) { |
| 31 |
if ( empty( $this->id ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$field = $this->extra; |
| 36 |
$field['id'] = $this->id; |
| 37 |
$field['default'] = $this->default; |
| 38 |
$field['description'] = $this->description; |
| 39 |
$field['label'] = $this->label; |
| 40 |
|
| 41 |
$field['multil_meta'] = isset( $field['multil_meta'] ) ? $field['multil_meta'] : false; |
| 42 |
$meta = $this->meta_value( $thepostid ); |
| 43 |
|
| 44 |
$default = ( ! $meta && isset( $field['default'] ) ) ? $field['default'] : $meta; |
| 45 |
|
| 46 |
$field = wp_parse_args( |
| 47 |
$field, |
| 48 |
array( |
| 49 |
'class' => 'select', |
| 50 |
'style' => '', |
| 51 |
'wrapper_class' => '', // Use "lp-select-2" for select2. |
| 52 |
'value' => isset( $field['value'] ) ? $field['value'] : $default, |
| 53 |
'name' => $field['id'], |
| 54 |
'desc_tip' => false, |
| 55 |
'multiple' => false, |
| 56 |
'custom_attributes' => array(), |
| 57 |
) |
| 58 |
); |
| 59 |
|
| 60 |
$label_attributes = array( |
| 61 |
'for' => $field['id'], |
| 62 |
); |
| 63 |
|
| 64 |
$field_attributes = (array) $field['custom_attributes']; |
| 65 |
$field_attributes['style'] = $field['style']; |
| 66 |
$field_attributes['id'] = $field['id']; |
| 67 |
$field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name']; |
| 68 |
$field_attributes['class'] = $field['class']; |
| 69 |
|
| 70 |
if ( $field['multiple'] ) { |
| 71 |
$field['wrapper_class'] = 'lp-select-2'; |
| 72 |
$field_attributes['multiple'] = true; |
| 73 |
} |
| 74 |
|
| 75 |
$tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : ''; |
| 76 |
$description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : ''; |
| 77 |
?> |
| 78 |
|
| 79 |
<p class="form-field <?php echo esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ); ?>" |
| 80 |
<?php learn_press_echo_vuejs_write_on_php( $this->condition ? $this->condition : '' ); ?>> |
| 81 |
<label for="<?php echo esc_attr( $field['id'] ); ?>"> |
| 82 |
<?php echo wp_kses_post( $field['label'] ); ?> |
| 83 |
</label> |
| 84 |
<select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> |
| 85 |
<option value="" hidden style="display: none"></option> |
| 86 |
<?php |
| 87 |
foreach ( $field['options'] as $key => $value ) { |
| 88 |
echo '<option value="' . esc_attr( $key ) . '"' . ( is_array( $field['value'] ) ? selected( in_array( (string) $key, $field['value'], true ), true ) : selected( $key, $field['value'], false ) ) . '>' . esc_html( $value ) . '</option>'; |
| 89 |
} |
| 90 |
?> |
| 91 |
</select> |
| 92 |
<?php |
| 93 |
if ( ! empty( $field['description'] ) ) { |
| 94 |
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| 95 |
|
| 96 |
if ( ! empty( $field['desc_tip'] ) ) { |
| 97 |
learn_press_quick_tip( $field['desc_tip'] ); |
| 98 |
} |
| 99 |
} |
| 100 |
?> |
| 101 |
</p> |
| 102 |
<?php |
| 103 |
} |
| 104 |
|
| 105 |
public function save( $post_id ) { |
| 106 |
$multiple_meta = $this->extra['multil_meta'] ?? false; |
| 107 |
|
| 108 |
if ( ! isset( $_POST[ $this->id ] ) ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( $multiple_meta ) { |
| 113 |
$get_values = get_post_meta( $post_id, $this->id ) ?? array(); |
| 114 |
$new_values = LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ?? [] ); |
| 115 |
|
| 116 |
$array_get_values = ! empty( $get_values ) ? array_values( $get_values ) : array(); |
| 117 |
$array_new_values = ! empty( $new_values ) ? array_values( $new_values ) : array(); |
| 118 |
|
| 119 |
$del_val = array_diff( $array_get_values, $array_new_values ); |
| 120 |
$new_val = array_diff( $array_new_values, $array_get_values ); |
| 121 |
|
| 122 |
foreach ( $del_val as $level_id ) { |
| 123 |
delete_post_meta( $post_id, $this->id, $level_id ); |
| 124 |
} |
| 125 |
|
| 126 |
foreach ( $new_val as $level_id ) { |
| 127 |
add_post_meta( $post_id, $this->id, $level_id, false ); |
| 128 |
} |
| 129 |
} else { |
| 130 |
$multiple = ! empty( $this->extra['multiple'] ); |
| 131 |
|
| 132 |
if ( $multiple ) { |
| 133 |
$value_tmp = ! empty( $_POST[ $this->id ] ) ? LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ) : array(); |
| 134 |
// Clear item has value empty. |
| 135 |
$value = []; |
| 136 |
array_map( |
| 137 |
function( $item ) use ( &$value ) { |
| 138 |
if ( $item !== '' ) { |
| 139 |
$value[] = $item; |
| 140 |
} |
| 141 |
}, |
| 142 |
$value_tmp |
| 143 |
); |
| 144 |
} else { |
| 145 |
$value = ! empty( $_POST[ $this->id ] ) ? sanitize_text_field( wp_unslash( $_POST[ $this->id ] ) ) : ''; |
| 146 |
} |
| 147 |
|
| 148 |
update_post_meta( $post_id, $this->id, $value ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|