| @@ -1,105 +1,105 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * LP_Meta_Box_Duration_Attribute | |
| 5 | - * | |
| 6 | - * @author tungnx | |
| 7 | - * @version 1.0.0 | |
| 8 | - * @since 4.0.0 | |
| 9 | - */ | |
| 10 | -class LP_Meta_Box_Checkbox_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 | - $field = $this->extra; | |
| 31 | - $field['id'] = $this->id; | |
| 32 | - $field['default'] = $this->default; | |
| 33 | - $field['description'] = $this->description; | |
| 34 | - $field['label'] = $this->label; | |
| 35 | - | |
| 36 | - $class = ! empty( $field['class'] ) ? 'class="' . esc_attr( $field['class'] ) . '"' : ''; | |
| 37 | - $style = ! empty( $field['style'] ) ? 'style="' . esc_attr( $field['style'] ) . '"' : ''; | |
| 38 | - $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : ''; | |
| 39 | - $wrapper_attr = $this->extra['wrapper_attr'] ?? []; | |
| 40 | - $name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); | |
| 41 | - $name = 'name="' . $name . '"'; | |
| 42 | - | |
| 43 | - // Check if meta key exists in database | |
| 44 | - $meta_exists = metadata_exists( 'post', $thepostid, $this->id ); | |
| 45 | - $value_db = $this->meta_value( $thepostid ); | |
| 46 | - | |
| 47 | - // Only use default if meta key doesn't exist in database | |
| 48 | - if ( ! $meta_exists && $field['default'] !== '' ) { | |
| 49 | - $value_db = $field['default']; | |
| 50 | - } | |
| 51 | - | |
| 52 | - $checked = ''; | |
| 53 | - if ( 'yes' === $value_db ) { | |
| 54 | - $checked = 'checked="checked"'; | |
| 55 | - } | |
| 56 | - | |
| 57 | - if ( isset( $field['value'] ) && $field['value'] === 'yes' ) { | |
| 58 | - $checked = 'checked="checked"'; | |
| 59 | - } | |
| 60 | - | |
| 61 | - // Custom attribute handling | |
| 62 | - $custom_attributes = array(); | |
| 63 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 64 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 65 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 66 | - } | |
| 67 | - } | |
| 68 | - | |
| 69 | - $dependency_check = $this->extra['dependency'] ?? []; | |
| 70 | - if ( ! empty( $dependency_check ) ) { | |
| 71 | - if ( $dependency_check['is_disable'] ) { | |
| 72 | - $wrapper_class .= ' lp-option-disabled'; | |
| 73 | - } | |
| 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 | - | |
| 86 | - echo '<input type="checkbox" ' . $class . ' ' . $style . ' ' . $name . ' ' . $checked . ' id="' . esc_attr( $field['id'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> '; | |
| 87 | - | |
| 88 | - if ( ! empty( $field['description'] ) ) { | |
| 89 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 90 | - | |
| 91 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 92 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - echo '</div>'; | |
| 97 | - } | |
| 98 | - | |
| 99 | - public function save( $post_id ) { | |
| 100 | - $value = isset( $_POST[ $this->id ] ) ? 'yes' : 'no'; | |
| 101 | - update_post_meta( $post_id, $this->id, $value ); | |
| 102 | - | |
| 103 | - return $value; | |
| 104 | - } | |
| 105 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * LP_Meta_Box_Duration_Attribute | |
| 5 | + * | |
| 6 | + * @author tungnx | |
| 7 | + * @version 1.0.0 | |
| 8 | + * @since 4.0.0 | |
| 9 | + */ | |
| 10 | +class LP_Meta_Box_Checkbox_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 | + $field = $this->extra; | |
| 31 | + $field['id'] = $this->id; | |
| 32 | + $field['default'] = $this->default; | |
| 33 | + $field['description'] = $this->description; | |
| 34 | + $field['label'] = $this->label; | |
| 35 | + | |
| 36 | + $class = ! empty( $field['class'] ) ? 'class="' . esc_attr( $field['class'] ) . '"' : ''; | |
| 37 | + $style = ! empty( $field['style'] ) ? 'style="' . esc_attr( $field['style'] ) . '"' : ''; | |
| 38 | + $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : ''; | |
| 39 | + $wrapper_attr = $this->extra['wrapper_attr'] ?? []; | |
| 40 | + $name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); | |
| 41 | + $name = 'name="' . $name . '"'; | |
| 42 | + | |
| 43 | + // Check if meta key exists in database | |
| 44 | + $meta_exists = metadata_exists( 'post', $thepostid, $this->id ); | |
| 45 | + $value_db = $this->meta_value( $thepostid ); | |
| 46 | + | |
| 47 | + // Only use default if meta key doesn't exist in database | |
| 48 | + if ( ! $meta_exists && $field['default'] !== '' ) { | |
| 49 | + $value_db = $field['default']; | |
| 50 | + } | |
| 51 | + | |
| 52 | + $checked = ''; | |
| 53 | + if ( 'yes' === $value_db ) { | |
| 54 | + $checked = 'checked="checked"'; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if ( isset( $field['value'] ) && $field['value'] === 'yes' ) { | |
| 58 | + $checked = 'checked="checked"'; | |
| 59 | + } | |
| 60 | + | |
| 61 | + // Custom attribute handling | |
| 62 | + $custom_attributes = array(); | |
| 63 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 64 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 65 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + $dependency_check = $this->extra['dependency'] ?? []; | |
| 70 | + if ( ! empty( $dependency_check ) ) { | |
| 71 | + if ( $dependency_check['is_disable'] ) { | |
| 72 | + $wrapper_class .= ' lp-option-disabled'; | |
| 73 | + } | |
| 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 | + | |
| 86 | + echo '<input type="checkbox" ' . $class . ' ' . $style . ' ' . $name . ' ' . $checked . ' id="' . esc_attr( $field['id'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> '; | |
| 87 | + | |
| 88 | + if ( ! empty( $field['description'] ) ) { | |
| 89 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 90 | + | |
| 91 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 92 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + echo '</div>'; | |
| 97 | + } | |
| 98 | + | |
| 99 | + public function save( $post_id ) { | |
| 100 | + $value = isset( $_POST[ $this->id ] ) ? 'yes' : 'no'; | |
| 101 | + update_post_meta( $post_id, $this->id, $value ); | |
| 102 | + | |
| 103 | + return $value; | |
| 104 | + } | |
| 105 | +} | |