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 / select.php

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

157 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * LP_Meta_Box_Duration_Attribute
5 *
6 * @author Nhamdv
7 * @version 1.0.0
8 * @since 4.0.0
9 */
10 class LP_Meta_Box_Select_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 meta_value( $thepostid ) {
26 $multil_meta = isset( $this->extra['multil_meta'] ) ? $this->extra['multil_meta'] : false;
27 return $multil_meta ? get_post_meta( $thepostid, $this->id, false ) : get_post_meta( $thepostid, $this->id, true );
28 }
29
30 public function output( $thepostid ) {
31 if ( empty( $this->id ) ) {
32 return;
33 }
34
35 $field = $this->extra;
36 $field['id'] = $this->id;
37 $field['default'] = $this->default;
38 $field['description'] = $this->description;
39 $field['label'] = $this->label;
40
41 $field['multil_meta'] = isset( $field['multil_meta'] ) ? $field['multil_meta'] : false;
42 $meta = $this->meta_value( $thepostid );
43
44 $default = ( ! $meta && isset( $field['default'] ) ) ? $field['default'] : $meta;
45
46 $field = wp_parse_args(
47 $field,
48 array(
49 'class' => 'select',
50 'style' => '',
51 'wrapper_class' => '', // Use "lp-select-2" for select2.
52 'value' => isset( $field['value'] ) ? $field['value'] : $default,
53 'name' => $field['id'],
54 'desc_tip' => false,
55 'multiple' => false,
56 'custom_attributes' => array(),
57 )
58 );
59
60 $label_attributes = array(
61 'for' => $field['id'],
62 );
63
64 $field_attributes = (array) $field['custom_attributes'];
65 $field_attributes['style'] = $field['style'];
66 $field_attributes['id'] = $field['id'];
67 $field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name'];
68 $field_attributes['class'] = $field['class'];
69
70 if ( $field['multiple'] ) {
71 $field['wrapper_class'] = 'lp-select-2';
72 $field_attributes['multiple'] = true;
73 }
74
75 $tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : '';
76 $description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : '';
77 ?>
78
79 <p class="form-field <?php echo esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ); ?>"
80 <?php learn_press_echo_vuejs_write_on_php( $this->condition ? $this->condition : '' ); ?>>
81 <label for="<?php echo esc_attr( $field['id'] ); ?>">
82 <?php echo wp_kses_post( $field['label'] ); ?>
83 </label>
84 <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>>
85 <option value="" hidden style="display: none"></option>
86 <?php
87 foreach ( $field['options'] as $key => $value ) {
88 echo '<option value="' . esc_attr( $key ) . '"' . ( is_array( $field['value'] ) ? selected( in_array( (string) $key, $field['value'], true ), true ) : selected( $key, $field['value'], false ) ) . '>' . esc_html( $value ) . '</option>';
89 }
90 ?>
91 </select>
92 <?php
93 if ( ! empty( $field['description'] ) ) {
94 echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
95
96 if ( ! empty( $field['desc_tip'] ) ) {
97 learn_press_quick_tip( $field['desc_tip'] );
98 }
99 }
100 ?>
101 </p>
102 <?php
103 }
104
105 public function save( $post_id ) {
106 $multiple_meta = $this->extra['multil_meta'] ?? false;
107
108 if ( ! isset( $_POST[ $this->id ] ) ) {
109 return;
110 }
111
112 if ( ! empty( $this->extra['custom_save'] ) ) {
113 do_action( 'learnpress/admin/metabox/select/save', $this->id, $_POST[ $this->id ], $post_id );
114 return;
115 }
116
117 if ( $multiple_meta ) {
118 $get_values = get_post_meta( $post_id, $this->id ) ?? array();
119 $new_values = LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ?? [] );
120
121 $array_get_values = ! empty( $get_values ) ? array_values( $get_values ) : array();
122 $array_new_values = ! empty( $new_values ) ? array_values( $new_values ) : array();
123
124 $del_val = array_diff( $array_get_values, $array_new_values );
125 $new_val = array_diff( $array_new_values, $array_get_values );
126
127 foreach ( $del_val as $level_id ) {
128 delete_post_meta( $post_id, $this->id, $level_id );
129 }
130
131 foreach ( $new_val as $level_id ) {
132 add_post_meta( $post_id, $this->id, $level_id, false );
133 }
134 } else {
135 $multiple = ! empty( $this->extra['multiple'] );
136
137 if ( $multiple ) {
138 $value_tmp = ! empty( $_POST[ $this->id ] ) ? LP_Helper::sanitize_params_submitted( $_POST[ $this->id ] ) : array();
139 // Clear item has value empty.
140 $value = [];
141 array_map(
142 function( $item ) use ( &$value ) {
143 if ( $item !== '' ) {
144 $value[] = $item;
145 }
146 },
147 $value_tmp
148 );
149 } else {
150 $value = ! empty( $_POST[ $this->id ] ) ? sanitize_text_field( wp_unslash( $_POST[ $this->id ] ) ) : '';
151 }
152
153 update_post_meta( $post_id, $this->id, $value );
154 }
155 }
156 }
157