PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 / meta-box / class-lp-meta-box-helper.php

class-lp-meta-box-helper.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.0, at inc/admin/meta-box/class-lp-meta-box-helper.php

330 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! class_exists( 'LP_Meta_Box_Helper' ) ) {
3 /**
4 * Class LP_Meta_Box_Helper
5 */
6 class LP_Meta_Box_Helper {
7 /**
8 * @var array
9 */
10 protected static $types = array();
11
12 /**
13 * @var array
14 */
15 protected static $conditional_logic = array();
16
17 /**
18 * @var null
19 */
20 protected static $_screen = null;
21
22 public static function output_fields( $options ) {
23 foreach ( $options as $value ) {
24 if ( ! isset( $value['type'] ) ) {
25 continue;
26 }
27 if ( ! isset( $value['id'] ) ) {
28 $value['id'] = '';
29 }
30 if ( ! isset( $value['title'] ) ) {
31 $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
32 }
33 if ( ! isset( $value['class'] ) ) {
34 $value['class'] = '';
35 }
36 if ( ! isset( $value['css'] ) ) {
37 $value['css'] = '';
38 }
39 if ( ! isset( $value['default'] ) ) {
40 $value['default'] = '';
41 }
42 if ( ! isset( $value['desc'] ) ) {
43 $value['desc'] = '';
44 }
45 if ( ! isset( $value['desc_tip'] ) ) {
46 $value['desc_tip'] = false;
47 }
48 if ( ! isset( $value['placeholder'] ) ) {
49 $value['placeholder'] = '';
50 }
51 if ( ! isset( $value['suffix'] ) ) {
52 $value['suffix'] = '';
53 }
54 if ( ! isset( $value['value'] ) ) {
55 $value['value'] = self::get_option( $value['id'], $value['default'] );
56 }
57
58 $custom_attributes = array();
59
60 if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) {
61 foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) {
62 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
63 }
64 }
65
66 $field_description = self::get_field_description( $value );
67 $description = $field_description['description'];
68 $tooltip_html = $field_description['tooltip_html'];
69
70 switch ( $value['type'] ) {
71 case 'text':
72 case 'password':
73 case 'datetime':
74 case 'datetime-local':
75 case 'date':
76 case 'month':
77 case 'time':
78 case 'week':
79 case 'number':
80 case 'email':
81 case 'url':
82 case 'tel':
83 include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/text.php';
84 break;
85 case 'select':
86 case 'multiselect':
87 include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/select.php';
88 break;
89 case 'image_advanced':
90 include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/image-advanced.php';
91 break;
92 case 'checkbox':
93 case 'yes-no':
94 include LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/checkbox.php';
95 break;
96 default:
97 $file_meta_box_custom = LP_PLUGIN_PATH . 'inc/admin/meta-box/fields/' . $value['type'] . '.php';
98 $file_meta_box_custom = apply_filters( 'learnpress/meta-box/field-custom', $file_meta_box_custom );
99
100 $pattern_find_match = '/\/admin\/meta-box\/fields\//';
101 preg_match( $pattern_find_match, $file_meta_box_custom, $match );
102
103 if ( empty( $match ) ) {
104 echo sprintf( '<p class="lp-error">Path file "%s" not valid. Format must is /admin/meta-box/fields</p>', $value['type'] );
105 }
106
107 if ( file_exists( $file_meta_box_custom ) ) {
108 include $file_meta_box_custom;
109 } else {
110 echo sprintf( '<p class="lp-error">File meta box "%s" not exists</p>', $value['type'] );
111 }
112
113 break;
114 }
115 }
116 }
117
118 public static function save_fields( $options, $data = null ) {
119 if ( is_null( $data ) ) {
120 $data = LP_Helper::sanitize_params_submitted( $_POST, 'html' );
121 }
122
123 if ( empty( $data ) ) {
124 return false;
125 }
126
127 $update_options = array();
128 $autoload_options = array();
129
130 foreach ( $options as $option ) {
131 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) || ( isset( $option['is_option'] ) && false === $option['is_option'] ) ) {
132 continue;
133 }
134
135 if ( strstr( $option['id'], '[' ) ) {
136 parse_str( $option['id'], $option_name_array );
137 $option_name = current( array_keys( $option_name_array ) );
138 $setting_name = key( $option_name_array[ $option_name ] );
139 $raw_value = isset( $data[ $option_name ][ $setting_name ] ) ? wp_unslash( $data[ $option_name ][ $setting_name ] ) : null;
140 } else {
141 $option_name = $option['id'];
142 $setting_name = '';
143 $raw_value = isset( $data[ $option['id'] ] ) ? wp_unslash( $data[ $option['id'] ] ) : null;
144 }
145
146 switch ( $option['type'] ) {
147 case 'checkbox':
148 case 'yes-no':
149 $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no';
150 break;
151 case 'textarea':
152 $value = trim( $raw_value );
153 break;
154 case 'multiselect':
155 case 'multi_select_countries':
156 $value = array_filter( array_map( 'learnpress_clean', (array) $raw_value ) );
157 break;
158 case 'image-dimensions':
159 $value = array();
160 if ( isset( $raw_value['width'] ) ) {
161 $value['width'] = learnpress_clean( $raw_value['width'] );
162 $value['height'] = learnpress_clean( $raw_value['height'] );
163 $value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0;
164 } else {
165 $value['width'] = $option['default'][0];
166 $value['height'] = $option['default'][1];
167 $value['crop'] = $option['default'][2];
168 }
169 break;
170 case 'select':
171 $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) );
172 if ( empty( $option['default'] ) && empty( $allowed_values ) ) {
173 $value = null;
174 break;
175 }
176 $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] );
177 $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default;
178 break;
179 case 'custom-fields':
180 $value = array();
181
182 if ( is_array( $raw_value ) && ! empty( $raw_value ) ) {
183 foreach ( $raw_value as $feilds ) {
184 foreach ( $option['options'] as $cfkey => $cfoption ) {
185 if ( $cfoption['type'] === 'checkbox' ) {
186 $feilds[ $cfkey ] = ( isset( $feilds[ $cfkey ] ) && ( $feilds[ $cfkey ] === '1' || $feilds[ $cfkey ] === 'yes' ) ) ? 'yes' : 'no';
187 }
188 }
189
190 $cfsort = $feilds['sort'];
191 unset( $feilds['sort'] );
192 $value[ $cfsort ] = $feilds;
193 }
194 }
195
196 //$value = LP_Helper::sanitize_params_submitted( $value );
197 break;
198
199 case 'image_advanced':
200 $value = ! empty( $raw_value ) ? array_filter( explode( ',', learnpress_clean( $raw_value ) ) ) : array();
201 break;
202 case 'image':
203 $value = ! empty( $raw_value ) ? absint( learnpress_clean( $raw_value ) ) : '';
204 break;
205 case 'email-content':
206 $value = ! empty( $raw_value ) ? $raw_value : array();
207 //$value = LP_Helper::sanitize_params_submitted( $value, 'html' );
208 break;
209 case 'url':
210 $value = ! empty( $raw_value ) ? esc_url_raw( $raw_value ) : '';
211 break;
212 default:
213 $value = $raw_value;
214 break;
215 }
216
217 /**
218 * Sanitize the value of an option.
219 *
220 * @since 4.0.0
221 */
222 $value = apply_filters( 'learnpress_metabox_settings_sanitize_option', $value, $option, $raw_value );
223
224 /**
225 * Sanitize the value of an option by option name.
226 *
227 * @since 4.0.0
228 */
229 $value = apply_filters( "learnpress_metabox_settings_sanitize_option_$option_name", $value, $option, $raw_value );
230
231 if ( is_null( $value ) ) {
232 continue;
233 }
234
235 if ( $option_name && $setting_name ) {
236 if ( ! isset( $update_options[ $option_name ] ) ) {
237 $update_options[ $option_name ] = get_option( $option_name, array() );
238 }
239 if ( ! is_array( $update_options[ $option_name ] ) ) {
240 $update_options[ $option_name ] = array();
241 }
242 $update_options[ $option_name ][ $setting_name ] = $value;
243 } else {
244 $update_options[ $option_name ] = $value;
245 }
246
247 $autoload_options[ $option_name ] = isset( $option['autoload'] ) ? (bool) $option['autoload'] : true;
248
249 do_action( 'learnpress_update_metabox_option', $option );
250
251 // Save all options in our array.
252 foreach ( $update_options as $name => $value ) {
253 update_option( $name, $value, $autoload_options[ $name ] ? 'yes' : 'no' );
254 }
255 }
256 }
257
258 public static function get_field_description( $value ) {
259 $description = '';
260 $tooltip_html = '';
261
262 if ( true === $value['desc_tip'] ) {
263 $tooltip_html = $value['desc'];
264 } elseif ( ! empty( $value['desc_tip'] ) ) {
265 $description = $value['desc'];
266 $tooltip_html = $value['desc_tip'];
267 } elseif ( ! empty( $value['desc'] ) ) {
268 $description = $value['desc'];
269 }
270
271 if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ), true ) ) {
272 $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
273 } elseif ( $description && in_array( $value['type'], array( 'checkbox' ), true ) ) {
274 $description = wp_kses_post( $description );
275 } elseif ( $description ) {
276 $description = '<p class="description">' . wp_kses_post( $description ) . '</p>';
277 }
278
279 if ( $tooltip_html && in_array( $value['type'], array( 'checkbox' ), true ) ) {
280 $tooltip_html = '<p class="description">' . $tooltip_html . '</p>';
281 } elseif ( $tooltip_html ) {
282 $tooltip_html = learn_press_quick_tip( $tooltip_html, false );
283 }
284
285 return array(
286 'description' => $description,
287 'tooltip_html' => $tooltip_html,
288 );
289 }
290
291 public static function get_option( $option_name, $default = '' ) {
292 if ( strstr( $option_name, '[' ) ) {
293 parse_str( $option_name, $option_array );
294
295 // Option name is first key
296 $option_name = current( array_keys( $option_array ) );
297
298 // Get value
299 $option_values = get_option( $option_name, '' );
300
301 $key = key( $option_array[ $option_name ] );
302
303 if ( is_array( $option_array[ $option_name ][ $key ] ) ) {
304 $key_child = key( $option_array[ $option_name ][ $key ] );
305 }
306
307 if ( isset( $option_values[ $key ] ) ) {
308 $option_value = $option_values[ $key ];
309
310 if ( is_array( $option_value ) && isset( $key_child )
311 && array_key_exists( $key_child, $option_value ) ) {
312 $option_value = $option_value[ $key_child ];
313 }
314 } else {
315 $option_value = null;
316 }
317 } else {
318 // Single value
319 $option_value = LP_Settings::instance()->get( preg_replace( '!^learn_press_!', '', $option_name ), null );
320 }
321
322 if ( ! is_array( $option_value ) && ! is_null( $option_value ) ) {
323 $option_value = stripslashes( $option_value );
324 }
325
326 return $option_value === null ? $default : $option_value;
327 }
328 }
329 }
330