| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | /** |
| 4 | 4 | * LP_Meta_Box_Duration_Attribute |
| 5 | 5 | * |
| 6 | 6 | * @author Nhamdv |
| 7 | - * @version 1.0.0 | |
| 7 | + * @version 1.0.2 | |
| 8 | 8 | * @since 4.0.0 |
| 9 | 9 | */ |
| 10 | 10 | class LP_Meta_Box_Select_Field extends LP_Meta_Box_Field { |
| 11 | 11 | |
| @@ -11,24 +11,24 @@ | ||
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Constructor. |
| 14 | 14 | * |
| 15 | - * @param string $id | |
| 16 | 15 | * @param string $label |
| 17 | 16 | * @param string $description |
| 18 | - * @param mixed $default | |
| 19 | - * @param array $extra | |
| 17 | + * @param mixed $default | |
| 18 | + * @param array $extra | |
| 20 | 19 | */ |
| 21 | 20 | public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 22 | 21 | parent::__construct( $label, $description, $default, $extra ); |
| 23 | 22 | } |
| 24 | 23 | |
| 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 ); | |
| 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 | 28 | } |
| 29 | 29 | |
| 30 | - public function output( $thepostid ) { | |
| 30 | + public function output( $post_id ) { | |
| 31 | 31 | if ( empty( $this->id ) ) { |
| 32 | 32 | return; |
| 33 | 33 | } |
| 34 | 34 | |
| @@ -37,11 +37,16 @@ | ||
| 37 | 37 | $field['default'] = $this->default; |
| 38 | 38 | $field['description'] = $this->description; |
| 39 | 39 | $field['label'] = $this->label; |
| 40 | 40 | |
| 41 | - $field['multil_meta'] = isset( $field['multil_meta'] ) ? $field['multil_meta'] : false; | |
| 42 | - $meta = $this->meta_value( $thepostid ); | |
| 41 | + $field['multil_meta'] = $field['multil_meta'] ?? false; | |
| 42 | + $meta = $this->meta_value( $post_id ); | |
| 43 | 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 | + | |
| 44 | 49 | $default = ( ! $meta && isset( $field['default'] ) ) ? $field['default'] : $meta; |
| 45 | 50 | |
| 46 | 51 | $field = wp_parse_args( |
| 47 | 52 | $field, |
| @@ -48,36 +53,68 @@ | ||
| 48 | 53 | array( |
| 49 | 54 | 'class' => 'select', |
| 50 | 55 | 'style' => '', |
| 51 | 56 | 'wrapper_class' => '', // Use "lp-select-2" for select2. |
| 52 | - 'value' => isset( $field['value'] ) ? $field['value'] : $default, | |
| 57 | + 'value' => $field['value'] ?? $default, | |
| 53 | 58 | 'name' => $field['id'], |
| 54 | 59 | 'desc_tip' => false, |
| 55 | 60 | 'multiple' => false, |
| 56 | 61 | 'custom_attributes' => array(), |
| 62 | + 'tom_select' => false, | |
| 63 | + 'wrapper_attr' => [], | |
| 64 | + 'options' => array(), | |
| 57 | 65 | ) |
| 58 | 66 | ); |
| 59 | 67 | |
| 60 | - $label_attributes = array( | |
| 61 | - 'for' => $field['id'], | |
| 62 | - ); | |
| 68 | + if ( isset( $field['options'] ) && ! is_array( $field['options'] ) ) { | |
| 69 | + $field['options'] = (array) $field['options']; | |
| 70 | + } | |
| 63 | 71 | |
| 64 | 72 | $field_attributes = (array) $field['custom_attributes']; |
| 65 | 73 | $field_attributes['style'] = $field['style']; |
| 66 | 74 | $field_attributes['id'] = $field['id']; |
| 67 | 75 | $field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name']; |
| 76 | + if ( isset( $field['name_no_bracket'] ) ) { | |
| 77 | + $field_attributes['name'] = $field['name']; | |
| 78 | + } | |
| 68 | 79 | $field_attributes['class'] = $field['class']; |
| 69 | 80 | |
| 70 | 81 | if ( $field['multiple'] ) { |
| 71 | - $field['wrapper_class'] = 'lp-select-2'; | |
| 72 | 82 | $field_attributes['multiple'] = true; |
| 73 | 83 | } |
| 74 | 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 | + | |
| 75 | 102 | $tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : ''; |
| 76 | 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 | + } | |
| 77 | 113 | ?> |
| 78 | 114 | |
| 79 | 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'] ) ); ?> | |
| 80 | 117 | <?php learn_press_echo_vuejs_write_on_php( $this->condition ? $this->condition : '' ); ?>> |
| 81 | 118 | <label for="<?php echo esc_attr( $field['id'] ); ?>"> |
| 82 | 119 | <?php echo wp_kses_post( $field['label'] ); ?> |
| 83 | 120 | </label> |
| @@ -84,18 +121,28 @@ | ||
| 84 | 121 | <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> |
| 85 | 122 | <option value="" hidden style="display: none"></option> |
| 86 | 123 | <?php |
| 87 | 124 | 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>'; | |
| 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 | + ); | |
| 89 | 136 | } |
| 90 | 137 | ?> |
| 91 | 138 | </select> |
| 92 | 139 | <?php |
| 93 | 140 | if ( ! empty( $field['description'] ) ) { |
| 94 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 141 | + echo '<span class="description">' . wp_kses_post( $description ) . '</span>'; | |
| 95 | 142 | |
| 96 | 143 | if ( ! empty( $field['desc_tip'] ) ) { |
| 97 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 144 | + learn_press_quick_tip( $tooltip ); | |
| 98 | 145 | } |
| 99 | 146 | } |
| 100 | 147 | ?> |
| 101 | 148 | </p> |
| @@ -104,23 +151,21 @@ | ||
| 104 | 151 | |
| 105 | 152 | public function save( $post_id ) { |
| 106 | 153 | $multiple_meta = $this->extra['multil_meta'] ?? false; |
| 107 | 154 | |
| 108 | - if ( ! isset( $_POST[ $this->id ] ) ) { | |
| 109 | - return; | |
| 110 | - } | |
| 155 | + if ( ! empty( $this->extra['custom_save'] ) ) { | |
| 156 | + do_action( 'learnpress/admin/metabox/select/save', $this->id, $_POST[ $this->id ] ?? '', $post_id ); | |
| 111 | 157 | |
| 112 | - if ( ! empty( $this->extra['custom_save'] ) ) { | |
| 113 | - do_action( 'learnpress/admin/metabox/select/save', $this->id, $_POST[ $this->id ], $post_id ); | |
| 114 | - return; | |
| 158 | + return ''; | |
| 115 | 159 | } |
| 116 | 160 | |
| 117 | 161 | if ( $multiple_meta ) { |
| 118 | - $get_values = get_post_meta( $post_id, $this->id ) ?? array(); | |
| 119 | - $new_values = LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ?? [] ); | |
| 162 | + $data = LP_Request::get_param( $this->id, $this->default ?? [] ); | |
| 163 | + $get_values = get_post_meta( $post_id, $this->id ) ?? []; | |
| 164 | + $new_values = $data; | |
| 120 | 165 | |
| 121 | - $array_get_values = ! empty( $get_values ) ? array_values( $get_values ) : array(); | |
| 122 | - $array_new_values = ! empty( $new_values ) ? array_values( $new_values ) : array(); | |
| 166 | + $array_get_values = ! empty( $get_values ) ? array_values( $get_values ) : []; | |
| 167 | + $array_new_values = ! empty( $new_values ) ? array_values( $new_values ) : []; | |
| 123 | 168 | |
| 124 | 169 | $del_val = array_diff( $array_get_values, $array_new_values ); |
| 125 | 170 | $new_val = array_diff( $array_new_values, $array_get_values ); |
| 126 | 171 | |
| @@ -130,27 +175,36 @@ | ||
| 130 | 175 | |
| 131 | 176 | foreach ( $new_val as $level_id ) { |
| 132 | 177 | add_post_meta( $post_id, $this->id, $level_id, false ); |
| 133 | 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; | |
| 134 | 186 | } else { |
| 135 | 187 | $multiple = ! empty( $this->extra['multiple'] ); |
| 136 | - | |
| 137 | 188 | if ( $multiple ) { |
| 138 | - $value_tmp = ! empty( $_POST[ $this->id ] ) ? LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ) : array(); | |
| 189 | + $data = LP_Request::get_param( $this->id, $this->default ?? [] ); | |
| 139 | 190 | // Clear item has value empty. |
| 140 | 191 | $value = []; |
| 141 | 192 | array_map( |
| 142 | - function( $item ) use ( &$value ) { | |
| 193 | + function ( $item ) use ( &$value ) { | |
| 143 | 194 | if ( $item !== '' ) { |
| 144 | 195 | $value[] = $item; |
| 145 | 196 | } |
| 146 | 197 | }, |
| 147 | - $value_tmp | |
| 198 | + $data | |
| 148 | 199 | ); |
| 149 | 200 | } else { |
| 150 | - $value = ! empty( $_POST[ $this->id ] ) ? sanitize_text_field( wp_unslash( $_POST[ $this->id ] ) ) : ''; | |
| 201 | + $data = LP_Request::get_param( $this->id, $this->default ?? '' ); | |
| 202 | + $value = $data; | |
| 151 | 203 | } |
| 152 | 204 | |
| 153 | 205 | update_post_meta( $post_id, $this->id, $value ); |
| 206 | + | |
| 207 | + return $value; | |
| 154 | 208 | } |
| 155 | 209 | } |
| 156 | 210 | } |