PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.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.3.1, at inc/admin/views/meta-boxes/fields/autocomplete.php

127 lines 4.1 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 // 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 ); ?>" data-atts="<?php echo esc_attr( wp_json_encode( $data_atts ) ); ?>">
70 <label for="<?php echo esc_attr( $field['id'] ); ?>">
71 <?php echo wp_kses_post( $field['label'] ); ?>
72 </label>
73
74 <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>>
75 <?php
76 if ( ! empty( $field['value'] ) ) {
77 foreach ( $field['value'] as $value ) {
78 if ( ! empty( $field['data'] ) ) {
79 if ( $field['data'] === 'users' ) {
80 $user = get_user_by( 'id', $value );
81
82 if ( ! $user ) {
83 continue;
84 }
85
86 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $user->display_name ) . '</option>';
87 } else {
88 $post = get_post( $value );
89 if ( ! $post ) {
90 continue;
91 }
92 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $post->post_title ) . '</option>';
93 }
94 } else {
95 echo '<option value="' . esc_attr( $value ) . '" selected>' . esc_html( $value ) . '</option>';
96 }
97 }
98
99 do_action( 'learn-press/admin/metabox/autocomplete/' . $field['id'] . '/option', $field['value'], $field, $thepostid );
100 }
101 ?>
102 </select>
103
104 <?php
105 if ( ! empty( $field['description'] ) ) {
106 echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
107
108 if ( ! empty( $field['desc_tip'] ) ) {
109 learn_press_quick_tip( $field['desc_tip'] );
110 }
111 }
112 ?>
113 </p>
114 <?php
115 }
116
117 public function save( $post_id ) {
118 $raw_value = isset( $_POST[ $this->id ] ) ? (array) wp_unslash( $_POST[ $this->id ] ) : array();
119 $value = array_map( 'absint', $raw_value );
120 $value = apply_filters( 'learn-press/admin/metabox/autocomplete/' . $this->id . '/save', $value, $raw_value, $post_id );
121
122 update_post_meta( $post_id, $this->id, $value );
123
124 do_action( 'lp/metabox/field/autocomplete/save/after', $post_id, $this->id, $value );
125 }
126 }
127