PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.0
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.0
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / lib / adminify-framework / classes / metabox-options.class.php

metabox-options.class.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.0, at lib/adminify-framework/classes/metabox-options.class.php

426 lines 14.0 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( 'ADMINIFY_Metabox' ) ) {
11 class ADMINIFY_Metabox extends ADMINIFY_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'metabox';
16 public $pre_fields = array();
17 public $sections = 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( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
44 $this->sections = apply_filters( "adminify_{$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 pre_fields( $sections ) {
71
72 $result = array();
73
74 foreach ( $sections as $key => $section ) {
75 if ( ! empty( $section['fields'] ) ) {
76 foreach ( $section['fields'] as $field ) {
77 $result[] = $field;
78 }
79 }
80 }
81
82 return $result;
83
84 }
85
86 public function add_metabox_classes( $classes ) {
87
88 global $post;
89
90 if ( ! empty( $this->post_formats ) ) {
91
92 $saved_post_format = ( is_object( $post ) ) ? get_post_format( $post ) : false;
93 $saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default';
94
95 $classes[] = 'adminify-post-formats';
96
97 // Sanitize post format for standard to default
98 if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) {
99 $this->post_formats[$key] = 'default';
100 }
101
102 foreach ( $this->post_formats as $format ) {
103 $classes[] = 'adminify-post-format-'. $format;
104 }
105
106 if ( ! in_array( $saved_post_format, $this->post_formats ) ) {
107 $classes[] = 'adminify-metabox-hide';
108 } else {
109 $classes[] = 'adminify-metabox-show';
110 }
111
112 }
113
114 if ( ! empty( $this->page_templates ) ) {
115
116 $saved_template = ( is_object( $post ) && ! empty( $post->page_template ) ) ? $post->page_template : 'default';
117
118 $classes[] = 'adminify-page-templates';
119
120 foreach ( $this->page_templates as $template ) {
121 $classes[] = 'adminify-page-'. preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) );
122 }
123
124 if ( ! in_array( $saved_template, $this->page_templates ) ) {
125 $classes[] = 'adminify-metabox-hide';
126 } else {
127 $classes[] = 'adminify-metabox-show';
128 }
129
130 }
131
132 if ( ! empty( $this->args['class'] ) ) {
133 $classes[] = $this->args['class'];
134 }
135
136 return $classes;
137
138 }
139
140 // add metabox
141 public function add_meta_box( $post_type ) {
142
143 if ( ! in_array( $post_type, $this->args['exclude_post_types'] ) ) {
144 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 );
145 }
146
147 }
148
149 // get default value
150 public function get_default( $field ) {
151
152 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
153 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
154
155 return $default;
156
157 }
158
159 // get meta value
160 public function get_meta_value( $field ) {
161
162 global $post;
163
164 $value = null;
165
166 if ( is_object( $post ) && ! empty( $field['id'] ) ) {
167
168 if ( $this->args['data_type'] !== 'serialize' ) {
169 $meta = get_post_meta( $post->ID, $field['id'] );
170 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
171 } else {
172 $meta = get_post_meta( $post->ID, $this->unique, true );
173 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
174 }
175
176 }
177
178 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
179 $value = ( isset( $value ) ) ? $value : $default;
180
181 return $value;
182
183 }
184
185 // add metabox content
186 public function add_meta_box_content( $post, $callback ) {
187
188 global $post;
189
190 $has_nav = ( count( $this->sections ) > 1 && $this->args['context'] !== 'side' ) ? true : false;
191 $show_all = ( ! $has_nav ) ? ' adminify-show-all' : '';
192 $post_type = ( is_object ( $post ) ) ? $post->post_type : '';
193 $errors = ( is_object ( $post ) ) ? get_post_meta( $post->ID, '_adminify_errors_'. $this->unique, true ) : array();
194 $errors = ( ! empty( $errors ) ) ? $errors : array();
195 $theme = ( $this->args['theme'] ) ? ' adminify-theme-'. $this->args['theme'] : '';
196 $nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
197
198 if ( is_object ( $post ) && ! empty( $errors ) ) {
199 delete_post_meta( $post->ID, '_adminify_errors_'. $this->unique );
200 }
201
202 wp_nonce_field( 'adminify_metabox_nonce', 'adminify_metabox_nonce'. $this->unique );
203
204 echo '<div class="adminify adminify-metabox'. esc_attr( $theme ) .'">';
205
206 echo '<div class="adminify-wrapper'. esc_attr( $show_all ) .'">';
207
208 if ( $has_nav ) {
209
210 echo '<div class="adminify-nav adminify-nav-'. esc_attr( $nav_type ) .' adminify-nav-metabox">';
211
212 echo '<ul>';
213
214 $tab_key = 0;
215
216 foreach ( $this->sections as $section ) {
217
218 if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
219 continue;
220 }
221
222 $tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="adminify-label-error adminify-error">!</i>' : '';
223 $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
224
225 echo '<li><a href="#">'. $tab_icon . $section['title'] . $tab_error .'</a></li>';
226
227 $tab_key++;
228
229 }
230
231 echo '</ul>';
232
233 echo '</div>';
234
235 }
236
237 echo '<div class="adminify-content">';
238
239 echo '<div class="adminify-sections">';
240
241 $section_key = 0;
242
243 foreach ( $this->sections as $section ) {
244
245 if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
246 continue;
247 }
248
249 $section_onload = ( ! $has_nav ) ? ' adminify-onload' : '';
250 $section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
251 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
252 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
253
254 echo '<div class="adminify-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
255
256 echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
257 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. $section['description'] .'</div>' : '';
258
259 if ( ! empty( $section['fields'] ) ) {
260
261 foreach ( $section['fields'] as $field ) {
262
263 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
264 $field['_error'] = $errors['fields'][$field['id']];
265 }
266
267 if ( ! empty( $field['id'] ) ) {
268 $field['default'] = $this->get_default( $field );
269 }
270
271 ADMINIFY::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' );
272
273 }
274
275 } else {
276
277 echo '<div class="adminify-no-option">'. esc_html__( 'No data available.', 'adminify' ) .'</div>';
278
279 }
280
281 echo '</div>';
282
283 $section_key++;
284
285 }
286
287 echo '</div>';
288
289 if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) {
290
291 echo '<div class="adminify-sections-reset">';
292 echo '<label>';
293 echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
294 echo '<span class="button adminify-button-reset">'. esc_html__( 'Reset', 'adminify' ) .'</span>';
295 echo '<span class="button adminify-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'adminify' ), esc_html__( 'Cancel', 'adminify' ) ) .'</span>';
296 echo '</label>';
297 echo '</div>';
298
299 }
300
301 echo '</div>';
302
303 echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="adminify-nav-background"></div>' : '';
304
305 echo '<div class="clear"></div>';
306
307 echo '</div>';
308
309 echo '</div>';
310
311 }
312
313 // save metabox
314 public function save_meta_box( $post_id ) {
315
316 $count = 1;
317 $data = array();
318 $errors = array();
319 $noncekey = 'adminify_metabox_nonce'. $this->unique;
320 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
321
322 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_metabox_nonce' ) ) {
323 return $post_id;
324 }
325
326 // XSS ok.
327 // No worries, This "POST" requests is sanitizing in the below foreach.
328 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
329
330 if ( ! empty( $request ) ) {
331
332 foreach ( $this->sections as $section ) {
333
334 if ( ! empty( $section['fields'] ) ) {
335
336 foreach ( $section['fields'] as $field ) {
337
338 if ( ! empty( $field['id'] ) ) {
339
340 $field_id = $field['id'];
341 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
342
343 // Sanitize "post" request of field.
344 if ( ! isset( $field['sanitize'] ) ) {
345
346 if( is_array( $field_value ) ) {
347 $data[$field_id] = wp_kses_post_deep( $field_value );
348 } else {
349 $data[$field_id] = wp_kses_post( $field_value );
350 }
351
352 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
353
354 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
355
356 } else {
357
358 $data[$field_id] = $field_value;
359
360 }
361
362 // Validate "post" request of field.
363 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
364
365 $has_validated = call_user_func( $field['validate'], $field_value );
366
367 if ( ! empty( $has_validated ) ) {
368
369 $errors['sections'][$count] = true;
370 $errors['fields'][$field_id] = $has_validated;
371 $data[$field_id] = $this->get_meta_value( $field );
372
373 }
374
375 }
376
377 }
378
379 }
380
381 }
382
383 $count++;
384
385 }
386
387 }
388
389 $data = apply_filters( "adminify_{$this->unique}_save", $data, $post_id, $this );
390
391 do_action( "adminify_{$this->unique}_save_before", $data, $post_id, $this );
392
393 if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
394
395 if ( $this->args['data_type'] !== 'serialize' ) {
396 foreach ( $data as $key => $value ) {
397 delete_post_meta( $post_id, $key );
398 }
399 } else {
400 delete_post_meta( $post_id, $this->unique );
401 }
402
403 } else {
404
405 if ( $this->args['data_type'] !== 'serialize' ) {
406 foreach ( $data as $key => $value ) {
407 update_post_meta( $post_id, $key, $value );
408 }
409 } else {
410 update_post_meta( $post_id, $this->unique, $data );
411 }
412
413 if ( ! empty( $errors ) ) {
414 update_post_meta( $post_id, '_adminify_errors_'. $this->unique, $errors );
415 }
416
417 }
418
419 do_action( "adminify_{$this->unique}_saved", $data, $post_id, $this );
420
421 do_action( "adminify_{$this->unique}_save_after", $data, $post_id, $this );
422
423 }
424 }
425 }
426