PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.26
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.26
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.26, at admin/views/sp-framework/classes/metabox.class.php

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