PluginProbe
Easy Hotel – Powerful Hotel Booking / 2.0.2
Easy Hotel – Powerful Hotel Booking v2.0.2
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / framework / classes / metabox-options.class.php

metabox-options.class.php in Easy Hotel – Powerful Hotel Booking 2.0.2, at admin/includes/framework/classes/metabox-options.class.php

417 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Metabox Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ESHB_Metabox' ) ) {
11 class ESHB_Metabox extends ESHB_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'metabox';
16 public $sections = array();
17 public $pre_fields = array();
18 public $post_type = array();
19 public $args = array(
20 'title' => '',
21 'post_type' => 'post',
22 'data_type' => 'serialize',
23 'context' => 'advanced',
24 'priority' => 'default',
25 'exclude_post_types' => array(),
26 'page_templates' => '',
27 'post_formats' => '',
28 'show_reset' => false,
29 'show_restore' => false,
30 'enqueue_webfont' => true,
31 'async_webfont' => false,
32 'output_css' => true,
33 'nav' => 'normal',
34 'theme' => 'dark',
35 'class' => '',
36 'defaults' => array(),
37 );
38
39 // run metabox construct
40 public function __construct( $key, $params = array() ) {
41
42 $this->unique = $key;
43 $this->args = apply_filters( "eshb_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
44 $this->sections = apply_filters( "eshb_{$this->unique}_sections", $params['sections'], $this );
45 $this->post_type = ( is_array( $this->args['post_type'] ) ) ? $this->args['post_type'] : array_filter( (array) $this->args['post_type'] );
46 $this->post_formats = ( is_array( $this->args['post_formats'] ) ) ? $this->args['post_formats'] : array_filter( (array) $this->args['post_formats'] );
47 $this->page_templates = ( is_array( $this->args['page_templates'] ) ) ? $this->args['page_templates'] : array_filter( (array) $this->args['page_templates'] );
48 $this->pre_fields = $this->pre_fields( $this->sections );
49
50 add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
51 add_action( 'save_post', array( $this, 'save_meta_box' ) );
52 add_action( 'edit_attachment', array( $this, 'save_meta_box' ) );
53
54 if ( ! empty( $this->page_templates ) || ! empty( $this->post_formats ) || ! empty( $this->args['class'] ) ) {
55 foreach ( $this->post_type as $post_type ) {
56 add_filter( 'postbox_classes_'. $post_type .'_'. $this->unique, array( $this, 'add_metabox_classes' ) );
57 }
58 }
59
60 // wp enqeueu for typography and output css
61 parent::__construct();
62
63 }
64
65 // instance
66 public static function instance( $key, $params = array() ) {
67 return new self( $key, $params );
68 }
69
70 public function add_metabox_classes( $classes ) {
71
72 global $post;
73
74 if ( ! empty( $this->post_formats ) ) {
75
76 $saved_post_format = ( is_object( $post ) ) ? get_post_format( $post ) : false;
77 $saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default';
78
79 $classes[] = 'csf-post-formats';
80
81 // Sanitize post format for standard to default
82 if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) {
83 $this->post_formats[$key] = 'default';
84 }
85
86 foreach ( $this->post_formats as $format ) {
87 $classes[] = 'csf-post-format-'. $format;
88 }
89
90 if ( ! in_array( $saved_post_format, $this->post_formats ) ) {
91 $classes[] = 'csf-metabox-hide';
92 } else {
93 $classes[] = 'csf-metabox-show';
94 }
95
96 }
97
98 if ( ! empty( $this->page_templates ) ) {
99
100 $saved_template = ( is_object( $post ) && ! empty( $post->page_template ) ) ? $post->page_template : 'default';
101
102 $classes[] = 'csf-page-templates';
103
104 foreach ( $this->page_templates as $template ) {
105 $classes[] = 'csf-page-'. preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) );
106 }
107
108 if ( ! in_array( $saved_template, $this->page_templates ) ) {
109 $classes[] = 'csf-metabox-hide';
110 } else {
111 $classes[] = 'csf-metabox-show';
112 }
113
114 }
115
116 if ( ! empty( $this->args['class'] ) ) {
117 $classes[] = $this->args['class'];
118 }
119
120 return $classes;
121
122 }
123
124 // add metabox
125 public function add_meta_box( $post_type ) {
126
127 if ( ! in_array( $post_type, $this->args['exclude_post_types'] ) ) {
128 add_meta_box( $this->unique, $this->args['title'], array( $this, 'add_meta_box_content' ), $this->post_type, $this->args['context'], $this->args['priority'], $this->args );
129 }
130
131 }
132
133 // get default value
134 public function get_default( $field ) {
135
136 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
137 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
138
139 return $default;
140
141 }
142
143 // get meta value
144 public function get_meta_value( $field ) {
145
146 global $post;
147
148 $value = null;
149
150 if ( is_object( $post ) && ! empty( $field['id'] ) ) {
151
152 if ( $this->args['data_type'] !== 'serialize' ) {
153 $meta = get_post_meta( $post->ID, $field['id'] );
154 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
155 } else {
156 $meta = get_post_meta( $post->ID, $this->unique, true );
157 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
158 }
159
160 }
161
162 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
163 $value = ( isset( $value ) ) ? $value : $default;
164
165 return $value;
166
167 }
168
169 // add metabox content
170 public function add_meta_box_content( $post, $callback ) {
171
172 global $post;
173
174 $has_nav = ( count( $this->sections ) > 1 && $this->args['context'] !== 'side' ) ? true : false;
175 $show_all = ( ! $has_nav ) ? ' csf-show-all' : '';
176 $post_type = ( is_object ( $post ) ) ? $post->post_type : '';
177 $errors = ( is_object ( $post ) ) ? get_post_meta( $post->ID, '_eshb_errors_'. $this->unique, true ) : array();
178 $errors = ( ! empty( $errors ) ) ? $errors : array();
179 $theme = ( $this->args['theme'] ) ? ' csf-theme-'. $this->args['theme'] : '';
180 $nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
181
182 if ( is_object ( $post ) && ! empty( $errors ) ) {
183 delete_post_meta( $post->ID, '_eshb_errors_'. $this->unique );
184 }
185
186 wp_nonce_field( 'eshb_metabox_nonce', 'eshb_metabox_nonce'. $this->unique );
187
188 echo '<div class="csf csf-metabox'. esc_attr( $theme ) .'">';
189
190 echo '<div class="csf-wrapper'. esc_attr( $show_all ) .'">';
191
192 if ( $has_nav ) {
193
194 echo '<div class="csf-nav csf-nav-'. esc_attr( $nav_type ) .' csf-nav-metabox">';
195
196 echo '<ul>';
197
198 $tab_key = 0;
199
200 foreach ( $this->sections as $section ) {
201
202 if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
203 continue;
204 }
205
206 $tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="csf-label-error csf-error">!</i>' : '';
207 $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
208
209 echo '<li><a href="#">' . wp_kses_post( $tab_icon ) . esc_html( $section['title'] ) . esc_html( $tab_error ) . '</a></li>';
210
211 $tab_key++;
212
213 }
214
215 echo '</ul>';
216
217 echo '</div>';
218
219 }
220
221 echo '<div class="csf-content">';
222
223 echo '<div class="csf-sections">';
224
225 $section_key = 0;
226
227 foreach ( $this->sections as $section ) {
228
229 if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
230 continue;
231 }
232
233 $section_onload = ( ! $has_nav ) ? ' csf-onload' : '';
234 $section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
235 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
236 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
237
238 echo '<div class="csf-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
239
240 echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>' . wp_kses_post( $section_icon ) . esc_html( $section_title ) . '</h3></div>' : '';
241 echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">' . esc_html( $section['description'] ) . '</div>' : '';
242
243 if ( ! empty( $section['fields'] ) ) {
244
245 foreach ( $section['fields'] as $field ) {
246
247 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
248 $field['_error'] = $errors['fields'][$field['id']];
249 }
250
251 if ( ! empty( $field['id'] ) ) {
252 $field['default'] = $this->get_default( $field );
253 }
254
255 ESHB::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' );
256
257 }
258
259 } else {
260
261 echo '<div class="csf-no-option">'. esc_html__( 'No data available.', 'easy-hotel' ) .'</div>';
262
263 }
264
265 echo '</div>';
266
267 $section_key++;
268
269 }
270
271 echo '</div>';
272
273 if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) {
274
275 echo '<div class="csf-sections-reset">';
276 echo '<label>';
277 echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
278 echo '<span class="button csf-button-reset">'. esc_html__( 'Reset', 'easy-hotel' ) .'</span>';
279 echo '<span class="button csf-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'easy-hotel' ), esc_html__( 'Cancel', 'easy-hotel' ) ) .'</span>';
280 echo '</label>';
281 echo '</div>';
282
283 }
284
285 echo '</div>';
286
287 echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="csf-nav-background"></div>' : '';
288
289 echo '<div class="clear"></div>';
290
291 echo '</div>';
292
293 echo '</div>';
294
295 }
296
297
298 // save metabox
299 public function save_meta_box( $post_id ) {
300
301 $count = 1;
302 $data = array();
303 $errors = array();
304 $noncekey = 'eshb_metabox_nonce'. $this->unique;
305 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
306
307 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'eshb_metabox_nonce' ) ) {
308 return $post_id;
309 }
310
311 // XSS ok.
312 // No worries, This "POST" requests is sanitizing in the below foreach.
313 //$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
314
315 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? map_deep( wp_unslash( $_POST[ $this->unique ] ), 'sanitize_text_field' ) : array();
316
317
318 if ( ! empty( $request ) ) {
319
320 foreach ( $this->sections as $section ) {
321
322 if ( ! empty( $section['fields'] ) ) {
323
324 foreach ( $section['fields'] as $field ) {
325
326 if ( ! empty( $field['id'] ) ) {
327
328 $field_id = $field['id'];
329 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
330
331 // Sanitize "post" request of field.
332 if ( ! isset( $field['sanitize'] ) ) {
333
334 if( is_array( $field_value ) ) {
335 $data[$field_id] = wp_kses_post_deep( $field_value );
336 } else {
337 $data[$field_id] = wp_kses_post( $field_value );
338 }
339
340 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
341
342 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
343
344 } else {
345
346 $data[$field_id] = $field_value;
347
348 }
349
350 // Validate "post" request of field.
351 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
352
353 $has_validated = call_user_func( $field['validate'], $field_value );
354
355 if ( ! empty( $has_validated ) ) {
356
357 $errors['sections'][$count] = true;
358 $errors['fields'][$field_id] = $has_validated;
359 $data[$field_id] = $this->get_meta_value( $field );
360
361 }
362
363 }
364
365 }
366
367 }
368
369 }
370
371 $count++;
372
373 }
374
375 }
376
377 $data = apply_filters( "eshb_{$this->unique}_save", $data, $post_id, $this );
378
379 do_action( "eshb_{$this->unique}_save_before", $data, $post_id, $this );
380
381 if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
382
383 if ( $this->args['data_type'] !== 'serialize' ) {
384 foreach ( $this->pre_fields as $field ) {
385 if ( ! empty( $field['id'] ) ) {
386 delete_post_meta( $post_id, $field['id'] );
387 }
388 }
389 } else {
390 delete_post_meta( $post_id, $this->unique );
391 }
392
393 } else {
394
395 if ( $this->args['data_type'] !== 'serialize' ) {
396 foreach ( $data as $key => $value ) {
397 update_post_meta( $post_id, $key, $value );
398 }
399 } else {
400 update_post_meta( $post_id, $this->unique, $data );
401 }
402
403 if ( ! empty( $errors ) ) {
404 update_post_meta( $post_id, '_eshb_errors_'. $this->unique, $errors );
405 }
406
407 }
408
409 do_action( "eshb_{$this->unique}_saved", $data, $post_id, $this );
410
411 do_action( "eshb_{$this->unique}_save_after", $data, $post_id, $this );
412
413
414 }
415 }
416 }
417