| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* LP_Meta_Box_Repeater |
| 5 |
* |
| 6 |
* @author Nhamdv |
| 7 |
* @version 1.0.0 |
| 8 |
* @since 4.1.4 |
| 9 |
*/ |
| 10 |
class LP_Meta_Box_Repeater_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 : fields for repeater, add_text for Add more text, title_text for Title |
| 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 |
$repeaters = $this->meta_value( $thepostid ); |
| 27 |
?> |
| 28 |
|
| 29 |
<div class="form-field lp_repeater_meta_box"> |
| 30 |
<label for="_lp_key_features"> |
| 31 |
<?php echo wp_kses_post( $this->label ); ?> |
| 32 |
</label> |
| 33 |
<div class="lp_repeater_meta_box__wrapper"> |
| 34 |
<div class="lp_repeater_meta_box__fields"> |
| 35 |
<?php if ( ! empty( $repeaters ) ) : ?> |
| 36 |
<?php foreach ( $repeaters as $key => $repeater ) : ?> |
| 37 |
<?php $this->repeater_html( $thepostid, $repeater, false, $key ); ?> |
| 38 |
<?php endforeach; ?> |
| 39 |
<?php endif; ?> |
| 40 |
</div> |
| 41 |
|
| 42 |
<a href="#" class="button button-primary lp_repeater_meta_box__add" |
| 43 |
data-add=" |
| 44 |
<?php |
| 45 |
ob_start(); |
| 46 |
$this->repeater_html( $thepostid, false, true ); |
| 47 |
echo esc_attr( ob_get_clean() ); |
| 48 |
?> |
| 49 |
"> |
| 50 |
<?php echo wp_kses_post( $this->extra['add_text'] ?? esc_html__( '+ Add more', 'learnpress' ) ); ?> |
| 51 |
</a> |
| 52 |
</div> |
| 53 |
</div> |
| 54 |
|
| 55 |
<?php |
| 56 |
} |
| 57 |
|
| 58 |
public function save( $post_id ) { |
| 59 |
$data = LP_Request::get_param( $this->id, $this->default ?? [], 'html' ); |
| 60 |
$output = array(); |
| 61 |
|
| 62 |
if ( ! empty( $data ) && is_array( $data ) ) { |
| 63 |
foreach ( $data as $key => $val ) { |
| 64 |
foreach ( $this->extra['fields'] as $field_key => $field ) { |
| 65 |
if ( get_class( $field ) === 'LP_Meta_Box_Checkbox_Field' ) { |
| 66 |
$val[ $field_key ] = isset( $val[ $field_key ] ) ? 'yes' : 'no'; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$sort_key = $val['sort']; |
| 71 |
unset( $val['sort'] ); |
| 72 |
|
| 73 |
$output[ $sort_key ] = $val; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
update_post_meta( $post_id, $this->id, $output ); |
| 78 |
|
| 79 |
return $output; |
| 80 |
} |
| 81 |
|
| 82 |
public function repeater_html( $thepostid, $repeater, $is_attr = false, $key = 'lp_metabox_repeater_key' ) { |
| 83 |
?> |
| 84 |
<div class="lp_repeater_meta_box__field <?php echo esc_attr( $is_attr ? 'lp_repeater_meta_box__field_active' : '' ); ?>"> |
| 85 |
<input class="lp_repeater_meta_box__field__count" type="hidden" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $this->id ) . '[' . $key . ']' . '[sort]'; ?>"> |
| 86 |
<div class="lp_repeater_meta_box__title"> |
| 87 |
<span class="lp_repeater_meta_box__title__sort"></span> |
| 88 |
<span class="lp_repeater_meta_box__title__title"> |
| 89 |
<?php echo wp_kses_post( $this->extra['title_text'] ?? esc_html__( 'Repeater', 'learnpress' ) ); ?> |
| 90 |
<span><?php echo esc_html( absint( $key ) + 1 ); ?></span> |
| 91 |
</span> |
| 92 |
<a href="#" class="lp_repeater_meta_box__title__delete"></a> |
| 93 |
<a href="#" class="lp_repeater_meta_box__title__toggle"></a> |
| 94 |
</div> |
| 95 |
<div class="lp_repeater_meta_box__content"> |
| 96 |
<?php |
| 97 |
if ( isset( $this->extra['fields'] ) ) { |
| 98 |
foreach ( $this->extra['fields'] as $field_key => $field ) { |
| 99 |
$field->id = $this->id . '[' . $key . '][' . $field_key . ']'; |
| 100 |
$field->extra['value'] = $is_attr ? '' : ( $repeater[ $field_key ] ?? '' ); |
| 101 |
learn_press_echo_vuejs_write_on_php( $field->output( $thepostid ) ); |
| 102 |
} |
| 103 |
} |
| 104 |
?> |
| 105 |
</div> |
| 106 |
</div> |
| 107 |
<?php |
| 108 |
} |
| 109 |
} |
| 110 |
|