| 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 |
$name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); |
| 40 |
$name = 'name="' . $name . '"'; |
| 41 |
|
| 42 |
$value_db = $this->meta_value( $thepostid ); |
| 43 |
|
| 44 |
if ( ! $value_db && $field['default'] !== '' ) { |
| 45 |
$value_db = $field['default']; |
| 46 |
} |
| 47 |
|
| 48 |
$checked = ''; |
| 49 |
if ( 'yes' === $value_db ) { |
| 50 |
$checked = 'checked="checked"'; |
| 51 |
} |
| 52 |
|
| 53 |
if ( isset( $field['value'] ) && $field['value'] === 'yes' ) { |
| 54 |
$checked = 'checked="checked"'; |
| 55 |
} |
| 56 |
|
| 57 |
// Custom attribute handling |
| 58 |
$custom_attributes = array(); |
| 59 |
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { |
| 60 |
foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { |
| 61 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 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>'; |
| 67 |
|
| 68 |
echo '<input type="checkbox" ' . $class . ' ' . $style . ' ' . $name . ' ' . $checked . ' id="' . esc_attr( $field['id'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> '; |
| 69 |
|
| 70 |
if ( ! empty( $field['description'] ) ) { |
| 71 |
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| 72 |
|
| 73 |
if ( ! empty( $field['desc_tip'] ) ) { |
| 74 |
learn_press_quick_tip( $field['desc_tip'] ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
echo '</div>'; |
| 79 |
} |
| 80 |
|
| 81 |
public function save( $post_id ) { |
| 82 |
$value = isset( $_POST[ $this->id ] ) ? 'yes' : 'no'; |
| 83 |
|
| 84 |
update_post_meta( $post_id, $this->id, $value ); |
| 85 |
} |
| 86 |
} |
| 87 |
|