| 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_Duration_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['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; |
| 37 |
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; |
| 38 |
$field['style'] = isset( $field['style'] ) ? $field['style'] : ''; |
| 39 |
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; |
| 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 |
$data_type = empty( $field['data_type'] ) ? '' : $field['data_type']; |
| 44 |
$duration = learn_press_get_course_duration_support(); |
| 45 |
|
| 46 |
$duration_keys = array_keys( $duration ); |
| 47 |
$default_time = ! empty( $field['default_time'] ) ? $field['default_time'] : end( $duration_keys ); |
| 48 |
|
| 49 |
if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $field['value'], $matches ) ) { |
| 50 |
$a1 = $matches[1][0]; |
| 51 |
$a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : $default_time; |
| 52 |
} else { |
| 53 |
$a1 = absint( $field['value'] ); |
| 54 |
$a2 = $default_time; |
| 55 |
} |
| 56 |
|
| 57 |
// Custom attribute handling |
| 58 |
$custom_attributes = array(); |
| 59 |
|
| 60 |
if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { |
| 61 |
foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { |
| 62 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$html_option = ''; |
| 67 |
foreach ( $duration as $k => $v ) { |
| 68 |
$html_option .= sprintf( '<option value="%s" %s>%s</option>', $k, selected( $k, $a2, false ), $v ); |
| 69 |
} |
| 70 |
|
| 71 |
echo '<p class="lp-meta-box__duration form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"> |
| 72 |
<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; |
| 73 |
|
| 74 |
echo '<input type="number" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '[]" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $a1 ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( ' ', $custom_attributes ) . ' /> '; |
| 75 |
|
| 76 |
echo '<select name="' . esc_attr( $field['name'] ) . '[]" class="lp-meta-box__duration-select">' . $html_option . '</select>'; |
| 77 |
|
| 78 |
if ( ! empty( $field['description'] ) ) { |
| 79 |
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; |
| 80 |
|
| 81 |
if ( ! empty( $field['desc_tip'] ) ) { |
| 82 |
learn_press_quick_tip( $field['desc_tip'] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
echo '</p>'; |
| 87 |
} |
| 88 |
|
| 89 |
public function save( $post_id ) { |
| 90 |
$duration = learn_press_get_course_duration_support(); |
| 91 |
$duration_keys = array_keys( $duration ); |
| 92 |
$default_time = ! empty( $this->extra['default_time'] ) ? $this->extra['default_time'] : end( $duration_keys ); |
| 93 |
$data = LP_Request::get_param( $this->id, [] ); |
| 94 |
$duration = isset( $data[0] ) && $data[0] !== '' ? absint( $data[0] ) . ' ' . trim( $data[1] ) : absint( $this->default ) . ' ' . trim( $default_time ); |
| 95 |
|
| 96 |
update_post_meta( $post_id, $this->id, $duration ); |
| 97 |
|
| 98 |
return $duration; |
| 99 |
} |
| 100 |
} |
| 101 |
|