| 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_Radio_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 |
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select'; |
| 37 |
$field['style'] = isset( $field['style'] ) ? $field['style'] : ''; |
| 38 |
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; |
| 39 |
$wrapper_attr = $field['wrapper_attr'] ?? []; |
| 40 |
$field['default'] = ( ! $this->meta_value( $thepostid ) && isset( $field['default'] ) ) ? $field['default'] : $this->meta_value( $thepostid ); |
| 41 |
$field['value'] = isset( $field['value'] ) ? $field['value'] : $field['default']; |
| 42 |
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; |
| 43 |
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false; |
| 44 |
|
| 45 |
$dependency_check = $this->extra['dependency'] ?? []; |
| 46 |
if ( ! empty( $dependency_check ) ) { |
| 47 |
if ( $dependency_check['is_disable'] ) { |
| 48 |
$field['wrapper_class'] .= ' lp-option-disabled'; |
| 49 |
} |
| 50 |
|
| 51 |
$wrapper_attr[] = 'data-dependency=' . $dependency_check['name']; |
| 52 |
} |
| 53 |
|
| 54 |
printf( |
| 55 |
'<fieldset class="form-field %s" %s><label for="%s">%s</label>', |
| 56 |
esc_attr( $this->id . '_field ' . $field['wrapper_class'] ), |
| 57 |
implode( ' ', $wrapper_attr ), |
| 58 |
esc_attr( $this->id ), |
| 59 |
wp_kses_post( $this->label ) |
| 60 |
); |
| 61 |
|
| 62 |
if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { |
| 63 |
learn_press_quick_tip( $field['description'] ); |
| 64 |
} |
| 65 |
|
| 66 |
echo '<ul class="lp-radios-field-meta-box">'; |
| 67 |
|
| 68 |
foreach ( $field['options'] as $key => $value ) { |
| 69 |
echo '<li><label><input |
| 70 |
name="' . esc_attr( $field['name'] ) . '" |
| 71 |
value="' . esc_attr( $key ) . '" |
| 72 |
type="radio" |
| 73 |
class="' . esc_attr( $field['class'] ) . '" |
| 74 |
style="' . esc_attr( $field['style'] ) . '" |
| 75 |
' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' |
| 76 |
/> ' . ( $value ) . '</label> |
| 77 |
</li>'; |
| 78 |
} |
| 79 |
echo '</ul>'; |
| 80 |
|
| 81 |
if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { |
| 82 |
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| 83 |
} |
| 84 |
|
| 85 |
echo '</fieldset>'; |
| 86 |
} |
| 87 |
|
| 88 |
public function save( $post_id ) { |
| 89 |
$value = LP_Request::get_param( $this->id, $this->default ?? '' ); |
| 90 |
update_post_meta( $post_id, $this->id, $value ); |
| 91 |
|
| 92 |
return $value; |
| 93 |
} |
| 94 |
} |
| 95 |
|