PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.5.0
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.5.0
2.1.3 2.1.2 2.1.1 2.1.0 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 trunk 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 All 57 releases
darkify / src / Admin / Framework / Classes / DarkifyMetabox.php

DarkifyMetabox.php in Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) 1.5.0, at src/Admin/Framework/Classes/DarkifyMetabox.php

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