PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.1.3
Adminify – White Label, Admin Menu Editor, Login Customizer v4.1.3
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 / Libs / adminify-framework / classes / metabox-options.class.php

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

412 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( 'ADMINIFY_Metabox' ) ) {
11 class ADMINIFY_Metabox extends ADMINIFY_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( "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 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[] = 'adminify-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[] = 'adminify-post-format-'. $format;
88 }
89
90 if ( ! in_array( $saved_post_format, $this->post_formats ) ) {
91 $classes[] = 'adminify-metabox-hide';
92 } else {
93 $classes[] = 'adminify-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[] = 'adminify-page-templates';
103
104 foreach ( $this->page_templates as $template ) {
105 $classes[] = 'adminify-page-'. preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) );
106 }
107
108 if ( ! in_array( $saved_template, $this->page_templates ) ) {
109 $classes[] = 'adminify-metabox-hide';
110 } else {
111 $classes[] = 'adminify-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 ) ? ' adminify-show-all' : '';
176 $post_type = ( is_object ( $post ) ) ? $post->post_type : '';
177 $errors = ( is_object ( $post ) ) ? get_post_meta( $post->ID, '_adminify_errors_'. $this->unique, true ) : array();
178 $errors = ( ! empty( $errors ) ) ? $errors : array();
179 $theme = ( $this->args['theme'] ) ? ' adminify-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, '_adminify_errors_'. $this->unique );
184 }
185
186 wp_nonce_field( 'adminify_metabox_nonce', 'adminify_metabox_nonce'. $this->unique );
187
188 echo '<div class="adminify adminify-metabox'. esc_attr( $theme ) .'">';
189
190 echo '<div class="adminify-wrapper'. esc_attr( $show_all ) .'">';
191
192 if ( $has_nav ) {
193
194 echo '<div class="adminify-nav adminify-nav-'. esc_attr( $nav_type ) .' adminify-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="adminify-label-error adminify-error">!</i>' : '';
207 $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
208
209 echo '<li><a href="#">'. $tab_icon . $section['title'] . $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="adminify-content">';
222
223 echo '<div class="adminify-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 ) ? ' adminify-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="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
237
238 echo '<div class="adminify-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
239
240 echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
241 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. $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 ADMINIFY::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' );
256
257 }
258
259 } else {
260
261 echo '<div class="adminify-no-option">'. esc_html__( 'No data available.', 'adminify' ) .'</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="adminify-sections-reset">';
276 echo '<label>';
277 echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
278 echo '<span class="button adminify-button-reset">'. esc_html__( 'Reset', 'adminify' ) .'</span>';
279 echo '<span class="button adminify-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'adminify' ), esc_html__( 'Cancel', 'adminify' ) ) .'</span>';
280 echo '</label>';
281 echo '</div>';
282
283 }
284
285 echo '</div>';
286
287 echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="adminify-nav-background"></div>' : '';
288
289 echo '<div class="clear"></div>';
290
291 echo '</div>';
292
293 echo '</div>';
294
295 }
296
297 // save metabox
298 public function save_meta_box( $post_id ) {
299
300 $count = 1;
301 $data = array();
302 $errors = array();
303 $noncekey = 'adminify_metabox_nonce'. $this->unique;
304 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
305
306 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_metabox_nonce' ) ) {
307 return $post_id;
308 }
309
310 // XSS ok.
311 // No worries, This "POST" requests is sanitizing in the below foreach.
312 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
313
314 if ( ! empty( $request ) ) {
315
316 foreach ( $this->sections as $section ) {
317
318 if ( ! empty( $section['fields'] ) ) {
319
320 foreach ( $section['fields'] as $field ) {
321
322 if ( ! empty( $field['id'] ) ) {
323
324 $field_id = $field['id'];
325 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
326
327 // Sanitize "post" request of field.
328 if ( ! isset( $field['sanitize'] ) ) {
329
330 if( is_array( $field_value ) ) {
331 $data[$field_id] = wp_kses_post_deep( $field_value );
332 } else {
333 $data[$field_id] = wp_kses_post( $field_value );
334 }
335
336 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
337
338 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
339
340 } else {
341
342 $data[$field_id] = $field_value;
343
344 }
345
346 // Validate "post" request of field.
347 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
348
349 $has_validated = call_user_func( $field['validate'], $field_value );
350
351 if ( ! empty( $has_validated ) ) {
352
353 $errors['sections'][$count] = true;
354 $errors['fields'][$field_id] = $has_validated;
355 $data[$field_id] = $this->get_meta_value( $field );
356
357 }
358
359 }
360
361 }
362
363 }
364
365 }
366
367 $count++;
368
369 }
370
371 }
372
373 $data = apply_filters( "adminify_{$this->unique}_save", $data, $post_id, $this );
374
375 do_action( "adminify_{$this->unique}_save_before", $data, $post_id, $this );
376
377 if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
378
379 if ( $this->args['data_type'] !== 'serialize' ) {
380 foreach ( $this->pre_fields as $field ) {
381 if ( ! empty( $field['id'] ) ) {
382 delete_post_meta( $post_id, $field['id'] );
383 }
384 }
385 } else {
386 delete_post_meta( $post_id, $this->unique );
387 }
388
389 } else {
390
391 if ( $this->args['data_type'] !== 'serialize' ) {
392 foreach ( $data as $key => $value ) {
393 update_post_meta( $post_id, $key, $value );
394 }
395 } else {
396 update_post_meta( $post_id, $this->unique, $data );
397 }
398
399 if ( ! empty( $errors ) ) {
400 update_post_meta( $post_id, '_adminify_errors_'. $this->unique, $errors );
401 }
402
403 }
404
405 do_action( "adminify_{$this->unique}_saved", $data, $post_id, $this );
406
407 do_action( "adminify_{$this->unique}_save_after", $data, $post_id, $this );
408
409 }
410 }
411 }
412