| @@ -14,10 +14,10 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * @param string $id |
| 16 | 16 | * @param string $label |
| 17 | 17 | * @param string $description |
| 18 | - * @param mixed $default | |
| 19 | - * @param array $extra | |
| 18 | + * @param mixed $default | |
| 19 | + * @param array $extra | |
| 20 | 20 | */ |
| 21 | 21 | public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 22 | 22 | parent::__construct( $label, $description, $default, $extra ); |
| 23 | 23 | } |
| @@ -35,14 +35,18 @@ | ||
| 35 | 35 | |
| 36 | 36 | $class = ! empty( $field['class'] ) ? 'class="' . esc_attr( $field['class'] ) . '"' : ''; |
| 37 | 37 | $style = ! empty( $field['style'] ) ? 'style="' . esc_attr( $field['style'] ) . '"' : ''; |
| 38 | 38 | $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : ''; |
| 39 | + $wrapper_attr = $this->extra['wrapper_attr'] ?? []; | |
| 39 | 40 | $name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); |
| 40 | 41 | $name = 'name="' . $name . '"'; |
| 41 | 42 | |
| 42 | - $value_db = $this->meta_value( $thepostid ); | |
| 43 | + // Check if meta key exists in database | |
| 44 | + $meta_exists = metadata_exists( 'post', $thepostid, $this->id ); | |
| 45 | + $value_db = $this->meta_value( $thepostid ); | |
| 43 | 46 | |
| 44 | - if ( ! $value_db && $field['default'] !== '' ) { | |
| 47 | + // Only use default if meta key doesn't exist in database | |
| 48 | + if ( ! $meta_exists && $field['default'] !== '' ) { | |
| 45 | 49 | $value_db = $field['default']; |
| 46 | 50 | } |
| 47 | 51 | |
| 48 | 52 | $checked = ''; |
| @@ -61,11 +65,25 @@ | ||
| 61 | 65 | $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; |
| 62 | 66 | } |
| 63 | 67 | } |
| 64 | 68 | |
| 65 | - echo '<div class="form-field ' . esc_attr( $field['id'] . '_field ' . $wrapper_class ) . '"> | |
| 66 | - <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; | |
| 69 | + $dependency_check = $this->extra['dependency'] ?? []; | |
| 70 | + if ( ! empty( $dependency_check ) ) { | |
| 71 | + if ( $dependency_check['is_disable'] ) { | |
| 72 | + $wrapper_class .= ' lp-option-disabled'; | |
| 73 | + } | |
| 67 | 74 | |
| 75 | + $wrapper_attr[] = 'data-dependency=' . $dependency_check['name']; | |
| 76 | + } | |
| 77 | + | |
| 78 | + printf( | |
| 79 | + '<div class="form-field %s" %s><label for="%s">%s</label>', | |
| 80 | + esc_attr( $this->id . '_field ' . $wrapper_class ), | |
| 81 | + implode( ' ', $wrapper_attr ), | |
| 82 | + esc_attr( $this->id ), | |
| 83 | + wp_kses_post( $this->label ) | |
| 84 | + ); | |
| 85 | + | |
| 68 | 86 | echo '<input type="checkbox" ' . $class . ' ' . $style . ' ' . $name . ' ' . $checked . ' id="' . esc_attr( $field['id'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> '; |
| 69 | 87 | |
| 70 | 88 | if ( ! empty( $field['description'] ) ) { |
| 71 | 89 | echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| @@ -79,8 +97,9 @@ | ||
| 79 | 97 | } |
| 80 | 98 | |
| 81 | 99 | public function save( $post_id ) { |
| 82 | 100 | $value = isset( $_POST[ $this->id ] ) ? 'yes' : 'no'; |
| 101 | + update_post_meta( $post_id, $this->id, $value ); | |
| 83 | 102 | |
| 84 | - update_post_meta( $post_id, $this->id, $value ); | |
| 103 | + return $value; | |
| 85 | 104 | } |
| 86 | 105 | } |