PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.1.6
Adminify – White Label, Admin Menu Editor, Login Customizer v3.1.6
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 3.1.6, at lib/adminify-framework/classes/metabox-options.class.php

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