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

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