← All changes
|
inc/admin/views/meta-boxes/fields/autocomplete.php
+125
-125
4.3.8
→
4.4.8
View file →
| @@ -1,125 +1,125 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * | |
| 5 | - * @author nhamdv | |
| 6 | - * @version 1.0.0 | |
| 7 | - * @since 4.1.7 | |
| 8 | - */ | |
| 9 | -class LP_Meta_Box_Autocomplete_Field extends LP_Meta_Box_Field { | |
| 10 | - | |
| 11 | - /** | |
| 12 | - * @param array $field | |
| 13 | - * @param array $extra = array( 'placeholder' => 'Select an item', 'action': rest_url( 'wp/v2/users' ), rest_url( 'wp/v2/post' ), rest_url( 'wp/v2/page' ), 'data': users, page, post, course, lesson ) | |
| 14 | - * | |
| 15 | - * @return string | |
| 16 | - */ | |
| 17 | - public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { | |
| 18 | - parent::__construct( $label, $description, $default, $extra ); | |
| 19 | - } | |
| 20 | - | |
| 21 | - public function output( $thepostid ) { | |
| 22 | - // Enqueue scripts here for future use everywhere called. | |
| 23 | - wp_enqueue_script( 'lp-admin-learnpress' ); | |
| 24 | - | |
| 25 | - if ( empty( $this->id ) ) { | |
| 26 | - return; | |
| 27 | - } | |
| 28 | - | |
| 29 | - $field = $this->extra; | |
| 30 | - $field['id'] = $this->id; | |
| 31 | - $field['default'] = $this->default; | |
| 32 | - $field['description'] = $this->description; | |
| 33 | - $field['label'] = $this->label; | |
| 34 | - | |
| 35 | - $meta = $this->meta_value( $thepostid ); | |
| 36 | - | |
| 37 | - $default = ( ! $meta && isset( $field['default'] ) ) ? (array) $field['default'] : $meta; | |
| 38 | - | |
| 39 | - $field = wp_parse_args( | |
| 40 | - $field, | |
| 41 | - array( | |
| 42 | - 'class' => 'select', | |
| 43 | - 'style' => '', | |
| 44 | - 'wrapper_class' => '', | |
| 45 | - 'value' => isset( $field['value'] ) ? $field['value'] : $default, | |
| 46 | - 'name' => $field['id'], | |
| 47 | - 'desc_tip' => false, | |
| 48 | - 'custom_attributes' => array(), | |
| 49 | - ) | |
| 50 | - ); | |
| 51 | - | |
| 52 | - $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : ''; | |
| 53 | - | |
| 54 | - $field_attributes = (array) $field['custom_attributes']; | |
| 55 | - $field_attributes['style'] = 'width: 300px;' . $field['style']; | |
| 56 | - $field_attributes['id'] = $field['id']; | |
| 57 | - $field_attributes['name'] = $field['name'] . '[]'; | |
| 58 | - $field_attributes['class'] = $field['class']; | |
| 59 | - $field_attributes['multiple'] = true; | |
| 60 | - | |
| 61 | - $data_atts = array( | |
| 62 | - 'placeholder' => $field['placeholder'] ?? esc_html__( 'Select', 'learnpress' ), | |
| 63 | - 'action' => $field['action'] ?? '', | |
| 64 | - 'data' => $field['data'] ?? '', // users, pages, posts, lp_course, lp_lesson... | |
| 65 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 66 | - 'rest_url' => rest_url(), | |
| 67 | - ); | |
| 68 | - ?> | |
| 69 | - <p class="form-field lp_autocomplete_metabox_field <?php echo esc_attr( $field['id'] . '_field ' . $wrapper_class ); ?>" | |
| 70 | - data-atts="<?php echo esc_attr( wp_json_encode( $data_atts ) ); ?>"> | |
| 71 | - <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 72 | - <?php echo wp_kses_post( $field['label'] ); ?> | |
| 73 | - </label> | |
| 74 | - | |
| 75 | - <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> | |
| 76 | - <?php | |
| 77 | - if ( ! empty( $field['value'] ) ) { | |
| 78 | - foreach ( $field['value'] as $value ) { | |
| 79 | - if ( ! empty( $field['data'] ) ) { | |
| 80 | - if ( $field['data'] === 'users' ) { | |
| 81 | - $user = get_user_by( 'id', $value ); | |
| 82 | - | |
| 83 | - if ( ! $user ) { | |
| 84 | - continue; | |
| 85 | - } | |
| 86 | - | |
| 87 | - echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $user->display_name ) . '</option>'; | |
| 88 | - } else { | |
| 89 | - $post = get_post( $value ); | |
| 90 | - if ( ! $post ) { | |
| 91 | - continue; | |
| 92 | - } | |
| 93 | - echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $post->post_title ) . '</option>'; | |
| 94 | - } | |
| 95 | - } else { | |
| 96 | - echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $value ) . '</option>'; | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - do_action( 'learn-press/admin/metabox/autocomplete/' . $field['id'] . '/option', $field['value'], $field, $thepostid ); | |
| 101 | - } | |
| 102 | - ?> | |
| 103 | - </select> | |
| 104 | - | |
| 105 | - <?php | |
| 106 | - if ( ! empty( $field['description'] ) ) { | |
| 107 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 108 | - | |
| 109 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 110 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 111 | - } | |
| 112 | - } | |
| 113 | - ?> | |
| 114 | - </p> | |
| 115 | - <?php | |
| 116 | - } | |
| 117 | - | |
| 118 | - public function save( $post_id ) { | |
| 119 | - $value = LP_Request::get_param( $this->id, $this->default ?? [] ); | |
| 120 | - | |
| 121 | - update_post_meta( $post_id, $this->id, $value ); | |
| 122 | - | |
| 123 | - return $value; | |
| 124 | - } | |
| 125 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * | |
| 5 | + * @author nhamdv | |
| 6 | + * @version 1.0.0 | |
| 7 | + * @since 4.1.7 | |
| 8 | + */ | |
| 9 | +class LP_Meta_Box_Autocomplete_Field extends LP_Meta_Box_Field { | |
| 10 | + | |
| 11 | + /** | |
| 12 | + * @param array $field | |
| 13 | + * @param array $extra = array( 'placeholder' => 'Select an item', 'action': rest_url( 'wp/v2/users' ), rest_url( 'wp/v2/post' ), rest_url( 'wp/v2/page' ), 'data': users, page, post, course, lesson ) | |
| 14 | + * | |
| 15 | + * @return string | |
| 16 | + */ | |
| 17 | + public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { | |
| 18 | + parent::__construct( $label, $description, $default, $extra ); | |
| 19 | + } | |
| 20 | + | |
| 21 | + public function output( $thepostid ) { | |
| 22 | + // Enqueue scripts here for future use everywhere called. | |
| 23 | + wp_enqueue_script( 'lp-admin-learnpress' ); | |
| 24 | + | |
| 25 | + if ( empty( $this->id ) ) { | |
| 26 | + return; | |
| 27 | + } | |
| 28 | + | |
| 29 | + $field = $this->extra; | |
| 30 | + $field['id'] = $this->id; | |
| 31 | + $field['default'] = $this->default; | |
| 32 | + $field['description'] = $this->description; | |
| 33 | + $field['label'] = $this->label; | |
| 34 | + | |
| 35 | + $meta = $this->meta_value( $thepostid ); | |
| 36 | + | |
| 37 | + $default = ( ! $meta && isset( $field['default'] ) ) ? (array) $field['default'] : $meta; | |
| 38 | + | |
| 39 | + $field = wp_parse_args( | |
| 40 | + $field, | |
| 41 | + array( | |
| 42 | + 'class' => 'select', | |
| 43 | + 'style' => '', | |
| 44 | + 'wrapper_class' => '', | |
| 45 | + 'value' => isset( $field['value'] ) ? $field['value'] : $default, | |
| 46 | + 'name' => $field['id'], | |
| 47 | + 'desc_tip' => false, | |
| 48 | + 'custom_attributes' => array(), | |
| 49 | + ) | |
| 50 | + ); | |
| 51 | + | |
| 52 | + $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : ''; | |
| 53 | + | |
| 54 | + $field_attributes = (array) $field['custom_attributes']; | |
| 55 | + $field_attributes['style'] = 'width: 300px;' . $field['style']; | |
| 56 | + $field_attributes['id'] = $field['id']; | |
| 57 | + $field_attributes['name'] = $field['name'] . '[]'; | |
| 58 | + $field_attributes['class'] = $field['class']; | |
| 59 | + $field_attributes['multiple'] = true; | |
| 60 | + | |
| 61 | + $data_atts = array( | |
| 62 | + 'placeholder' => $field['placeholder'] ?? esc_html__( 'Select', 'learnpress' ), | |
| 63 | + 'action' => $field['action'] ?? '', | |
| 64 | + 'data' => $field['data'] ?? '', // users, pages, posts, lp_course, lp_lesson... | |
| 65 | + 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 66 | + 'rest_url' => rest_url(), | |
| 67 | + ); | |
| 68 | + ?> | |
| 69 | + <p class="form-field lp_autocomplete_metabox_field <?php echo esc_attr( $field['id'] . '_field ' . $wrapper_class ); ?>" | |
| 70 | + data-atts="<?php echo esc_attr( wp_json_encode( $data_atts ) ); ?>"> | |
| 71 | + <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 72 | + <?php echo wp_kses_post( $field['label'] ); ?> | |
| 73 | + </label> | |
| 74 | + | |
| 75 | + <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> | |
| 76 | + <?php | |
| 77 | + if ( ! empty( $field['value'] ) ) { | |
| 78 | + foreach ( $field['value'] as $value ) { | |
| 79 | + if ( ! empty( $field['data'] ) ) { | |
| 80 | + if ( $field['data'] === 'users' ) { | |
| 81 | + $user = get_user_by( 'id', $value ); | |
| 82 | + | |
| 83 | + if ( ! $user ) { | |
| 84 | + continue; | |
| 85 | + } | |
| 86 | + | |
| 87 | + echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $user->display_name ) . '</option>'; | |
| 88 | + } else { | |
| 89 | + $post = get_post( $value ); | |
| 90 | + if ( ! $post ) { | |
| 91 | + continue; | |
| 92 | + } | |
| 93 | + echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $post->post_title ) . '</option>'; | |
| 94 | + } | |
| 95 | + } else { | |
| 96 | + echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $value ) . '</option>'; | |
| 97 | + } | |
| 98 | + } | |
| 99 | + | |
| 100 | + do_action( 'learn-press/admin/metabox/autocomplete/' . $field['id'] . '/option', $field['value'], $field, $thepostid ); | |
| 101 | + } | |
| 102 | + ?> | |
| 103 | + </select> | |
| 104 | + | |
| 105 | + <?php | |
| 106 | + if ( ! empty( $field['description'] ) ) { | |
| 107 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 108 | + | |
| 109 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 110 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + ?> | |
| 114 | + </p> | |
| 115 | + <?php | |
| 116 | + } | |
| 117 | + | |
| 118 | + public function save( $post_id ) { | |
| 119 | + $value = LP_Request::get_param( $this->id, $this->default ?? [] ); | |
| 120 | + | |
| 121 | + update_post_meta( $post_id, $this->id, $value ); | |
| 122 | + | |
| 123 | + return $value; | |
| 124 | + } | |
| 125 | +} | |