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

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