PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.20
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.20
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / views / sp-framework / classes / metabox.class.php

metabox.class.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 2.4.20, at admin/views/sp-framework/classes/metabox.class.php

487 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The metabox class of the plugin.
4 *
5 * @package Smart_Post_Show
6 * @subpackage Smart_Post_Show/views/sp-framework/classes
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die;
11 } // Cannot access directly.
12
13 if ( ! class_exists( 'SP_PC_Metabox' ) ) {
14 /**
15 *
16 * Metabox Class
17 *
18 * @since 1.0.0
19 * @version 1.0.0
20 */
21 class SP_PC_Metabox extends SP_PC_Abstract {
22
23 /**
24 * Property.
25 *
26 * @var string
27 */
28 public $unique = '';
29 /**
30 * Properties for metabox.
31 *
32 * @var string
33 */
34 public $abstract = 'metabox';
35 /**
36 * Pre fields property.
37 *
38 * @var array
39 */
40 public $pre_fields = array();
41
42 /**
43 * Sections.
44 *
45 * @var array
46 */
47 public $sections = array();
48 /**
49 * Post Types.
50 *
51 * @var array
52 */
53 public $post_type = array();
54 /**
55 * Arguments.
56 *
57 * @var array
58 */
59 public $args = array(
60 'title' => '',
61 'post_type' => 'post',
62 'data_type' => 'serialize',
63 'context' => 'advanced',
64 'priority' => 'default',
65 'exclude_post_types' => array(),
66 'page_templates' => '',
67 'post_formats' => '',
68 'show_restore' => false,
69 'enqueue_webfont' => true,
70 'async_webfont' => false,
71 'output_css' => true,
72 'theme' => 'dark',
73 'class' => '',
74 'defaults' => array(),
75 );
76
77 /**
78 * Run metabox construct.
79 *
80 * @param mixed $key The metabox key.
81 * @param array $params The metabox parameters.
82 */
83 public function __construct( $key, $params = array() ) {
84
85 $this->unique = $key;
86 $this->args = apply_filters( "spf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
87 $this->sections = apply_filters( "spf_{$this->unique}_sections", $params['sections'], $this );
88 $this->post_type = ( is_array( $this->args['post_type'] ) ) ? $this->args['post_type'] : array_filter( (array) $this->args['post_type'] );
89 $this->post_formats = ( is_array( $this->args['post_formats'] ) ) ? $this->args['post_formats'] : array_filter( (array) $this->args['post_formats'] );
90 $this->page_templates = ( is_array( $this->args['page_templates'] ) ) ? $this->args['page_templates'] : array_filter( (array) $this->args['page_templates'] );
91 $this->pre_fields = $this->pre_fields( $this->sections );
92
93 add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) );
94 add_action( 'save_post', array( &$this, 'save_meta_box' ) );
95 add_action( 'edit_attachment', array( &$this, 'save_meta_box' ) );
96
97 if ( ! empty( $this->page_templates ) || ! empty( $this->post_formats ) || ! empty( $this->args['class'] ) ) {
98 foreach ( $this->post_type as $post_type ) {
99 add_filter( 'postbox_classes_' . $post_type . '_' . $this->unique, array( &$this, 'add_metabox_classes' ) );
100 }
101 }
102
103 // wp enqeueu for typography and output css.
104 parent::__construct();
105
106 }
107
108 /**
109 * Instance.
110 *
111 * @param string $key Key of the metabox.
112 * @param array $params Array of parameters.
113 * @return statement
114 */
115 public static function instance( $key, $params = array() ) {
116
117 return new self( $key, $params );
118 }
119
120 /**
121 * Pre fields
122 *
123 * @param array $sections The sections.
124 * @return statement
125 */
126 public function pre_fields( $sections ) {
127
128 $result = array();
129
130 foreach ( $sections as $key => $section ) {
131 if ( ! empty( $section['fields'] ) ) {
132 foreach ( $section['fields'] as $field ) {
133 $result[] = $field;
134 }
135 }
136 }
137
138 return $result;
139 }
140
141 /**
142 * Add metabox classes.
143 *
144 * @param array $classes The metabox classes.
145 */
146 public function add_metabox_classes( $classes ) {
147
148 global $post;
149
150 if ( ! empty( $this->post_formats ) ) {
151
152 $saved_post_format = ( is_object( $post ) ) ? get_post_format( $post ) : false;
153 $saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default';
154
155 $classes[] = 'spf-post-formats';
156
157 /**
158 * Sanitize post format for standard to default.
159 */
160 if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) {
161 $this->post_formats[ $key ] = 'default';
162 }
163
164 foreach ( $this->post_formats as $format ) {
165 $classes[] = 'spf-post-format-' . $format;
166 }
167
168 if ( ! in_array( $saved_post_format, $this->post_formats ) ) {
169 $classes[] = 'spf-hide';
170 } else {
171 $classes[] = 'spf-show';
172 }
173 }
174
175 if ( ! empty( $this->page_templates ) ) {
176
177 $saved_template = ( is_object( $post ) && ! empty( $post->page_template ) ) ? $post->page_template : 'default';
178
179 $classes[] = 'spf-page-templates';
180
181 foreach ( $this->page_templates as $template ) {
182 $classes[] = 'spf-page-' . preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) );
183 }
184
185 if ( ! in_array( $saved_template, $this->page_templates ) ) {
186 $classes[] = 'spf-hide';
187 } else {
188 $classes[] = 'spf-show';
189 }
190 }
191
192 if ( ! empty( $this->args['class'] ) ) {
193 $classes[] = $this->args['class'];
194 }
195
196 return $classes;
197
198 }
199
200 /**
201 * Add metabox
202 *
203 * @param array $post_type The post types.
204 */
205 public function add_meta_box( $post_type ) {
206
207 if ( ! in_array( $post_type, $this->args['exclude_post_types'] ) ) {
208 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 );
209 }
210
211 }
212
213 /**
214 * Get default value.
215 *
216 * @param array $field The field value.
217 * @return mixed
218 */
219 public function get_default( $field ) {
220
221 // $default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : '';
222 $default = ( isset( $field['id'] ) && isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : null;
223 $default = ( isset( $field['default'] ) ) ? $field['default'] : $default;
224
225 return $default;
226
227 }
228
229 /**
230 * Get meta value.
231 *
232 * @param object $field The field.
233 * @return statement
234 */
235 public function get_meta_value( $field ) {
236
237 global $post;
238
239 $value = null;
240
241 if ( is_object( $post ) && ! empty( $field['id'] ) ) {
242
243 if ( 'serialize' !== $this->args['data_type'] ) {
244 $meta = get_post_meta( $post->ID, $field['id'] );
245 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
246 } else {
247 $meta = get_post_meta( $post->ID, $this->unique, true );
248 $value = ( isset( $meta[ $field['id'] ] ) ) ? $meta[ $field['id'] ] : null;
249 }
250 }
251
252 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
253 $value = ( isset( $value ) ) ? $value : $default;
254
255 return $value;
256
257 }
258
259 /**
260 * Add metabox content
261 *
262 * @param object $post The post.
263 * @param array $callback The callback function.
264 * @return void
265 */
266 public function add_meta_box_content( $post, $callback ) {
267
268 global $post;
269
270 $has_nav = ( count( $this->sections ) > 1 && 'side' !== $this->args['context'] ) ? true : false;
271 $show_all = ( ! $has_nav ) ? ' spf-show-all' : '';
272 $errors = ( is_object( $post ) ) ? get_post_meta( $post->ID, '_spf_errors', true ) : array();
273 $errors = ( ! empty( $errors ) ) ? $errors : array();
274 $theme = ( $this->args['theme'] ) ? ' spf-theme-' . $this->args['theme'] : '';
275 // $class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : '';
276
277 if ( is_object( $post ) && ! empty( $errors ) ) {
278 delete_post_meta( $post->ID, '_spf_errors' );
279 }
280
281 wp_nonce_field( 'spf_pcp_metabox_nonce', 'spf_pcp_metabox_nonce' . $this->unique );
282
283 echo '<div class="spf spf-metabox' . esc_attr( $theme ) . '">';
284
285 echo '<div class="spf-wrapper' . esc_attr( $show_all ) . '">';
286
287 if ( $has_nav ) {
288
289 echo '<div class="spf-nav spf-nav-metabox" data-unique="' . esc_attr( $this->unique ) . '">';
290
291 echo '<ul>';
292 $tab_key = 1;
293 foreach ( $this->sections as $section ) {
294
295 $tab_error = ( ! empty( $errors['sections'][ $tab_key ] ) ) ? '<i class="spf-label-error spf-error">!</i>' : '';
296 $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="spf-icon ' . esc_attr( $section['icon'] ) . '"></i>' : '';
297 // Added li class to hide when needs, -ShapedPlugin.
298 echo '<li class="menu-item_' . esc_attr( $this->unique ) . '_' . esc_attr( $tab_key ) . '"><a href="#" data-section="' . esc_attr( $this->unique ) . '_' . esc_attr( $tab_key ) . '">' . wp_kses_post( $tab_icon ) . esc_html( $section['title'] ) . esc_html( $tab_error ) . '</a></li>';
299
300 $tab_key++;
301 }
302 echo '</ul>';
303
304 echo '</div>';
305
306 }
307
308 echo '<div class="spf-content">';
309
310 echo '<div class="spf-sections">';
311
312 $section_key = 1;
313
314 foreach ( $this->sections as $section ) {
315
316 $onload = ( ! $has_nav ) ? ' spf-onload' : '';
317
318 echo '<div id="spf-section-' . esc_attr( $this->unique ) . '_' . esc_attr( $section_key ) . '" class="spf-section' . esc_attr( $onload ) . '">';
319
320 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="spf-icon ' . $section['icon'] . '"></i>' : '';
321 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
322
323 echo ( $section_title || $section_icon ) ? '<div class="spf-section-title"><h3>' . esc_html( $section_icon ) . esc_html( $section_title ) . '</h3></div>' : '';
324
325 if ( ! empty( $section['fields'] ) ) {
326
327 foreach ( $section['fields'] as $field ) {
328
329 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][ $field['id'] ] ) ) {
330 $field['_error'] = $errors['fields'][ $field['id'] ];
331 }
332
333 SP_PC::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' );
334
335 }
336 } else {
337
338 echo '<div class="spf-no-option spf-text-muted">' . esc_html__( 'No option provided by developer.', 'post-carousel' ) . '</div>';
339
340 }
341
342 echo '</div>';
343
344 $section_key++;
345 }
346
347 echo '</div>';
348 echo '<a class="btn btn-success" id="spsp-show-preview" data-id="' . esc_attr( $post->ID ) . '"href=""><i class="fa fa-eye"></i> Show Preview</a>';
349 echo '<div class="clear"></div>';
350
351 if ( ! empty( $this->args['show_restore'] ) ) {
352
353 echo '<div class="spf-restore-wrapper">';
354 echo '<label>';
355 echo '<input type="checkbox" name="' . esc_attr( $this->unique ) . '[_restore]" />';
356 echo '<span class="button spf-button-restore">' . esc_html__( 'Restore', 'post-carousel' ) . '</span>';
357 echo '<span class="button spf-button-cancel">' . sprintf( '<small>( %s )</small> %s', esc_html__( 'update post for restore ', 'post-carousel' ), esc_html__( 'Cancel', 'post-carousel' ) ) . '</span>';
358 echo '</label>';
359 echo '</div>';
360
361 }
362
363 echo '</div>';
364
365 echo ( $has_nav ) ? '<div class="spf-nav-background"></div>' : '';
366
367 echo '<div class="clear"></div>';
368
369 echo '</div>';
370
371 echo '</div>';
372
373 }
374
375 /**
376 * Save metabox.
377 *
378 * @param array $post_id The post IDs.
379 * @return statement
380 */
381 public function save_meta_box( $post_id ) {
382
383 $count = 1;
384 $data = array();
385 $errors = array();
386 $noncekey = 'spf_pcp_metabox_nonce' . $this->unique;
387 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
388 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) {
389 return $post_id;
390 }
391
392 // XSS ok.
393 // No worries, This "POST" requests is sanitizing in the below foreach.
394 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array(); // phpcs:ignore
395
396 if ( ! empty( $request ) ) {
397
398 // ignore _nonce.
399 if ( isset( $request['_nonce'] ) ) {
400 unset( $request['_nonce'] );
401 }
402
403 // sanitize and validate.
404 $section_key = 1;
405 foreach ( $this->sections as $section ) {
406
407 if ( ! empty( $section['fields'] ) ) {
408
409 foreach ( $section['fields'] as $field ) {
410
411 if ( ! empty( $field['id'] ) ) {
412 $field_id = $field['id'];
413 $field_value = isset( $request[ $field_id ] ) ? $request[ $field_id ] : '';
414
415 // Sanitize "post" request of field.
416 if ( ! isset( $field['sanitize'] ) ) {
417
418 if ( is_array( $field_value ) ) {
419 $data[ $field_id ] = wp_kses_post_deep( $field_value );
420 } else {
421 $data[ $field_id ] = wp_kses_post( $field_value );
422 }
423 } elseif ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
424
425 $data[ $field_id ] = call_user_func( $field['sanitize'], $field_value );
426
427 } else {
428
429 $data[ $field_id ] = $field_value;
430
431 }
432
433 // Validate "post" request of field.
434 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
435
436 $has_validated = call_user_func( $field['validate'], $field_value );
437
438 if ( ! empty( $has_validated ) ) {
439
440 $errors['sections'][ $count ] = true;
441 $errors['fields'][ $field_id ] = $has_validated;
442 $data[ $field_id ] = $this->get_meta_value( $field );
443
444 }
445 }
446 }
447 }
448 }
449
450 $section_key++;
451 }
452 }
453 $data = apply_filters( "spf_{$this->unique}_save", $data, $post_id, $this );
454
455 do_action( "spf_{$this->unique}_save_before", $data, $post_id, $this );
456
457 if ( empty( $data ) || ! empty( $request['_restore'] ) ) {
458
459 if ( 'serialize' !== $this->args['data_type'] ) {
460 foreach ( $data as $key => $value ) {
461 delete_post_meta( $post_id, $key );
462 }
463 } else {
464 delete_post_meta( $post_id, $this->unique );
465 }
466 } else {
467
468 if ( 'serialize' !== $this->args['data_type'] ) {
469 foreach ( $data as $key => $value ) {
470 update_post_meta( $post_id, $key, $value );
471 }
472 } else {
473 update_post_meta( $post_id, $this->unique, $data );
474 }
475
476 if ( ! empty( $errors ) ) {
477 update_post_meta( $post_id, '_spf_errors', $errors );
478 }
479 }
480
481 do_action( "spf_{$this->unique}_saved", $data, $post_id, $this );
482
483 do_action( "spf_{$this->unique}_save_after", $data, $post_id, $this );
484 }
485 }
486 }
487