PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.1
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / admin / views / meta-boxes / fields / autocomplete.php

autocomplete.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.1, at inc/admin/views/meta-boxes/fields/autocomplete.php

124 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( empty( $this->id ) ) {
23 return;
24 }
25
26 $field = $this->extra;
27 $field['id'] = $this->id;
28 $field['default'] = $this->default;
29 $field['description'] = $this->description;
30 $field['label'] = $this->label;
31
32 $meta = $this->meta_value( $thepostid );
33
34 $default = ( ! $meta && isset( $field['default'] ) ) ? (array) $field['default'] : $meta;
35
36 $field = wp_parse_args(
37 $field,
38 array(
39 'class' => 'select',
40 'style' => '',
41 'wrapper_class' => '',
42 'value' => isset( $field['value'] ) ? $field['value'] : $default,
43 'name' => $field['id'],
44 'desc_tip' => false,
45 'custom_attributes' => array(),
46 )
47 );
48
49 $wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : '';
50
51 $field_attributes = (array) $field['custom_attributes'];
52 $field_attributes['style'] = 'width: 300px;' . $field['style'];
53 $field_attributes['id'] = $field['id'];
54 $field_attributes['name'] = $field['name'] . '[]';
55 $field_attributes['class'] = $field['class'];
56 $field_attributes['multiple'] = true;
57
58 $data_atts = array(
59 'placeholder' => $field['placeholder'] ?? esc_html__( 'Select', 'learnpress' ),
60 'action' => $field['action'] ?? '',
61 'data' => $field['data'] ?? '', // users, pages, posts, lp_course, lp_lesson...
62 'nonce' => wp_create_nonce( 'wp_rest' ),
63 'rest_url' => rest_url(),
64 );
65 ?>
66 <p class="form-field lp_autocomplete_metabox_field <?php echo esc_attr( $field['id'] . '_field ' . $wrapper_class ); ?>" data-atts="<?php echo esc_attr( wp_json_encode( $data_atts ) ); ?>">
67 <label for="<?php echo esc_attr( $field['id'] ); ?>">
68 <?php echo wp_kses_post( $field['label'] ); ?>
69 </label>
70
71 <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>>
72 <?php
73 if ( ! empty( $field['value'] ) ) {
74 foreach ( $field['value'] as $value ) {
75 if ( ! empty( $field['data'] ) ) {
76 if ( $field['data'] === 'users' ) {
77 $user = get_user_by( 'id', $value );
78
79 if ( ! $user ) {
80 continue;
81 }
82
83 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $user->display_name ) . '</option>';
84 } else {
85 $post = get_post( $value );
86 if ( ! $post ) {
87 continue;
88 }
89 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $post->post_title ) . '</option>';
90 }
91 } else {
92 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $value ) . '</option>';
93 }
94 }
95
96 do_action( 'learn-press/admin/metabox/autocomplete/' . $field['id'] . '/option', $field['value'], $field, $thepostid );
97 }
98 ?>
99 </select>
100
101 <?php
102 if ( ! empty( $field['description'] ) ) {
103 echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
104
105 if ( ! empty( $field['desc_tip'] ) ) {
106 learn_press_quick_tip( $field['desc_tip'] );
107 }
108 }
109 ?>
110 </p>
111 <?php
112 }
113
114 public function save( $post_id ) {
115 $raw_value = isset( $_POST[ $this->id ] ) ? (array) wp_unslash( $_POST[ $this->id ] ) : array();
116 $value = array_map( 'absint', $raw_value );
117 $value = apply_filters( 'learn-press/admin/metabox/autocomplete/' . $this->id . '/save', $value, $raw_value, $post_id );
118
119 update_post_meta( $post_id, $this->id, $value );
120
121 do_action( 'lp/metabox/field/autocomplete/save/after', $post_id, $this->id, $value );
122 }
123 }
124