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

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