| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Meta_Box_Duration_Attribute |
| 5 |
* |
| 6 |
* @author Nhamdv |
| 7 |
* @version 1.0.2 |
| 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 $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 meta_value( $post_id ) { |
| 25 |
$multil_meta = $this->extra['multil_meta'] ?? false; |
| 26 |
|
| 27 |
return $multil_meta ? get_post_meta( $post_id, $this->id, false ) : get_post_meta( $post_id, $this->id, true ); |
| 28 |
} |
| 29 |
|
| 30 |
public function output( $post_id ) { |
| 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'] = $field['multil_meta'] ?? false; |
| 42 |
$meta = $this->meta_value( $post_id ); |
| 43 |
|
| 44 |
// Fix case select multiple but meta is array with single empty value. |
| 45 |
if ( is_array($meta) && count($meta) === 1 && empty( $meta[0] ) ) { |
| 46 |
$meta = []; |
| 47 |
} |
| 48 |
|
| 49 |
$default = ( ! $meta && isset( $field['default'] ) ) ? $field['default'] : $meta; |
| 50 |
|
| 51 |
$field = wp_parse_args( |
| 52 |
$field, |
| 53 |
array( |
| 54 |
'class' => 'select', |
| 55 |
'style' => '', |
| 56 |
'wrapper_class' => '', // Use "lp-select-2" for select2. |
| 57 |
'value' => $field['value'] ?? $default, |
| 58 |
'name' => $field['id'], |
| 59 |
'desc_tip' => false, |
| 60 |
'multiple' => false, |
| 61 |
'custom_attributes' => array(), |
| 62 |
'tom_select' => false, |
| 63 |
'wrapper_attr' => [], |
| 64 |
'options' => array(), |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
if ( isset( $field['options'] ) && ! is_array( $field['options'] ) ) { |
| 69 |
$field['options'] = (array) $field['options']; |
| 70 |
} |
| 71 |
|
| 72 |
$field_attributes = (array) $field['custom_attributes']; |
| 73 |
$field_attributes['style'] = $field['style']; |
| 74 |
$field_attributes['id'] = $field['id']; |
| 75 |
$field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name']; |
| 76 |
if ( isset( $field['name_no_bracket'] ) ) { |
| 77 |
$field_attributes['name'] = $field['name']; |
| 78 |
} |
| 79 |
$field_attributes['class'] = $field['class']; |
| 80 |
|
| 81 |
if ( $field['multiple'] ) { |
| 82 |
$field_attributes['multiple'] = true; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $field['tom_select'] ) { |
| 86 |
$field_attributes['class'] .= ' lp-tom-select'; |
| 87 |
if ( ! empty( $field['ts-remove-button'] ) ) { |
| 88 |
$field_attributes['data-ts-remove-button'] = $field['ts-remove-button']; |
| 89 |
} |
| 90 |
} elseif ( $field['multiple'] ) { |
| 91 |
$field['wrapper_class'] = 'lp-select-2'; |
| 92 |
} |
| 93 |
|
| 94 |
if ( isset( $field['data-saved'] ) ) { |
| 95 |
$field_attributes['data-saved'] = htmlentities2( json_encode( $field['data-saved'] ) ); |
| 96 |
} elseif ( ! empty( $meta ) ) { |
| 97 |
$field_attributes['data-saved'] = htmlentities2( json_encode( $meta ) ); |
| 98 |
} else { |
| 99 |
$field_attributes['data-saved'] = htmlentities2( json_encode( $default ) ); |
| 100 |
} |
| 101 |
|
| 102 |
$tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : ''; |
| 103 |
$description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : ''; |
| 104 |
|
| 105 |
$dependency_check = $field['dependency'] ?? []; |
| 106 |
if ( ! empty( $dependency_check ) ) { |
| 107 |
if ( $dependency_check['is_disable'] ) { |
| 108 |
$field['wrapper_class'] .= ' lp-option-disabled'; |
| 109 |
} |
| 110 |
|
| 111 |
$field['wrapper_attr'][] = 'data-dependency=' . $dependency_check['name']; |
| 112 |
} |
| 113 |
?> |
| 114 |
|
| 115 |
<p class="form-field <?php echo esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ); ?>" |
| 116 |
<?php echo esc_attr( implode( ' ', $field['wrapper_attr'] ) ); ?> |
| 117 |
<?php learn_press_echo_vuejs_write_on_php( $this->condition ? $this->condition : '' ); ?>> |
| 118 |
<label for="<?php echo esc_attr( $field['id'] ); ?>"> |
| 119 |
<?php echo wp_kses_post( $field['label'] ); ?> |
| 120 |
</label> |
| 121 |
<select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> |
| 122 |
<option value="" hidden style="display: none"></option> |
| 123 |
<?php |
| 124 |
foreach ( $field['options'] as $key => $value ) { |
| 125 |
if ( is_array( $field['value'] ) ) { |
| 126 |
$selected = in_array( $key, $field['value'] ) ? 'selected="selected"' : ''; |
| 127 |
} else { |
| 128 |
$selected = selected( $key, $field['value'], false ); |
| 129 |
} |
| 130 |
printf( |
| 131 |
'<option value="%s" %s>%s</option>', |
| 132 |
esc_attr( $key ), |
| 133 |
esc_attr( $selected ), |
| 134 |
esc_html( $value ) |
| 135 |
); |
| 136 |
} |
| 137 |
?> |
| 138 |
</select> |
| 139 |
<?php |
| 140 |
if ( ! empty( $field['description'] ) ) { |
| 141 |
echo '<span class="description">' . wp_kses_post( $description ) . '</span>'; |
| 142 |
|
| 143 |
if ( ! empty( $field['desc_tip'] ) ) { |
| 144 |
learn_press_quick_tip( $tooltip ); |
| 145 |
} |
| 146 |
} |
| 147 |
?> |
| 148 |
</p> |
| 149 |
<?php |
| 150 |
} |
| 151 |
|
| 152 |
public function save( $post_id ) { |
| 153 |
$multiple_meta = $this->extra['multil_meta'] ?? false; |
| 154 |
|
| 155 |
if ( ! empty( $this->extra['custom_save'] ) ) { |
| 156 |
do_action( 'learnpress/admin/metabox/select/save', $this->id, $_POST[ $this->id ] ?? '', $post_id ); |
| 157 |
|
| 158 |
return ''; |
| 159 |
} |
| 160 |
|
| 161 |
if ( $multiple_meta ) { |
| 162 |
$data = LP_Request::get_param( $this->id, $this->default ?? [] ); |
| 163 |
$get_values = get_post_meta( $post_id, $this->id ) ?? []; |
| 164 |
$new_values = $data; |
| 165 |
|
| 166 |
$array_get_values = ! empty( $get_values ) ? array_values( $get_values ) : []; |
| 167 |
$array_new_values = ! empty( $new_values ) ? array_values( $new_values ) : []; |
| 168 |
|
| 169 |
$del_val = array_diff( $array_get_values, $array_new_values ); |
| 170 |
$new_val = array_diff( $array_new_values, $array_get_values ); |
| 171 |
|
| 172 |
foreach ( $del_val as $level_id ) { |
| 173 |
delete_post_meta( $post_id, $this->id, $level_id ); |
| 174 |
} |
| 175 |
|
| 176 |
foreach ( $new_val as $level_id ) { |
| 177 |
add_post_meta( $post_id, $this->id, $level_id, false ); |
| 178 |
} |
| 179 |
|
| 180 |
// Fix case select multiple but meta is array with single empty value. |
| 181 |
if ( empty( $data ) ) { |
| 182 |
delete_post_meta( $post_id, $this->id ); |
| 183 |
} |
| 184 |
|
| 185 |
return $data; |
| 186 |
} else { |
| 187 |
$multiple = ! empty( $this->extra['multiple'] ); |
| 188 |
if ( $multiple ) { |
| 189 |
$data = LP_Request::get_param( $this->id, $this->default ?? [] ); |
| 190 |
// Clear item has value empty. |
| 191 |
$value = []; |
| 192 |
array_map( |
| 193 |
function ( $item ) use ( &$value ) { |
| 194 |
if ( $item !== '' ) { |
| 195 |
$value[] = $item; |
| 196 |
} |
| 197 |
}, |
| 198 |
$data |
| 199 |
); |
| 200 |
} else { |
| 201 |
$data = LP_Request::get_param( $this->id, $this->default ?? '' ); |
| 202 |
$value = $data; |
| 203 |
} |
| 204 |
|
| 205 |
update_post_meta( $post_id, $this->id, $value ); |
| 206 |
|
| 207 |
return $value; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|