Passed
Push — trunk ( 2608e2...26bef8 )
by Justin
02:23
created

includes/CMB2_hookup.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Handles hooking CMB2 forms/metaboxes into the post/attachement/user screens
4
 * and handles hooking in and saving those fields.
5
 *
6
 * @since  2.0.0
7
 *
8
 * @category  WordPress_Plugin
9
 * @package   CMB2
10
 * @author    CMB2 team
11
 * @license   GPL-2.0+
12
 * @link      https://cmb2.io
13
 */
14
class CMB2_hookup extends CMB2_Hookup_Base {
15
16
	/**
17
	 * Only allow JS registration once
18
	 *
19
	 * @var   bool
20
	 * @since 2.0.7
21
	 */
22
	protected static $js_registration_done = false;
23
24
	/**
25
	 * Only allow CSS registration once
26
	 *
27
	 * @var   bool
28
	 * @since 2.0.7
29
	 */
30
	protected static $css_registration_done = false;
31
32
	/**
33
	 * CMB taxonomies array for term meta
34
	 *
35
	 * @var   array
36
	 * @since 2.2.0
37
	 */
38
	protected $taxonomies = array();
39
40
	/**
41
	 * Custom field columns.
42
	 *
43
	 * @var   array
44
	 * @since 2.2.2
45
	 */
46
	protected $columns = array();
47
48
	/**
49
	 * Array of CMB2_Options_Hookup instances if options page metabox.
50
	 *
51
	 * @var   CMB2_Options_Hookup[]|null
52
	 * @since 2.2.5
53
	 */
54
	protected $options_hookup = null;
55
56
	/**
57
	 * A functionalized constructor, used for the hookup action callbacks.
58
	 *
59
	 * @since  2.2.6
60
	 *
61
	 * @param  CMB2 $cmb The CMB2 object to hookup.
62
	 *
63
	 * @return CMB2_Hookup_Base $hookup The hookup object.
64
	 */
65
	public static function maybe_init_and_hookup( CMB2 $cmb ) {
66
		if ( $cmb->prop( 'hookup' ) ) {
67
68
			$hookup = new self( $cmb );
69
70
			// Hook in the hookup... how meta.
71
			return $hookup->universal_hooks();
72
		}
73
74
		return false;
75
	}
76
77
	public function universal_hooks() {
78
		foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) {
79
			add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 );
80
		}
81
82
		if ( is_admin() ) {
83
			// Register our scripts and styles for cmb.
84
			$this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 );
85
			$this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
86
87
			$this->maybe_enqueue_column_display_styles();
88
89
			switch ( $this->object_type ) {
90
				case 'post':
91
					return $this->post_hooks();
92
				case 'comment':
93
					return $this->comment_hooks();
94
				case 'user':
95
					return $this->user_hooks();
96
				case 'term':
97
					return $this->term_hooks();
98
				case 'options-page':
99
					return $this->options_page_hooks();
100
			}
101
		}
102
103
		return $this;
104
	}
105
106
	public function post_hooks() {
107
108
		// Fetch the context we set in our call.
109
		$context = $this->cmb->prop( 'context' ) ? $this->cmb->prop( 'context' ) : 'normal';
110
111
		// Call the proper hook based on the context provided.
112
		switch ( $context ) {
113
114
			case 'form_top':
115
				add_action( 'edit_form_top', array( $this, 'add_context_metaboxes' ) );
116
				break;
117
118
			case 'before_permalink':
119
				add_action( 'edit_form_before_permalink', array( $this, 'add_context_metaboxes' ) );
120
				break;
121
122
			case 'after_title':
123
				add_action( 'edit_form_after_title', array( $this, 'add_context_metaboxes' ) );
124
				break;
125
126
			case 'after_editor':
127
				add_action( 'edit_form_after_editor', array( $this, 'add_context_metaboxes' ) );
128
				break;
129
130
			default:
131
				add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
132
		}
133
134
		add_action( 'add_attachment', array( $this, 'save_post' ) );
135
		add_action( 'edit_attachment', array( $this, 'save_post' ) );
136
		add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
137
138
		if ( $this->cmb->has_columns ) {
139
			foreach ( $this->cmb->box_types() as $post_type ) {
140
				add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) );
141
				add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 );
142
			}
143
		}
144
145
		return $this;
146
	}
147
148
	public function comment_hooks() {
149
		add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) );
150
		add_action( 'edit_comment', array( $this, 'save_comment' ) );
151
152
		if ( $this->cmb->has_columns ) {
153
			add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) );
154
			add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 );
155
		}
156
157
		return $this;
158
	}
159
160
	public function user_hooks() {
161
		$priority = $this->get_priority();
162
163
		add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
164
		add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
165
		add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority );
166
167
		add_action( 'personal_options_update', array( $this, 'save_user' ) );
168
		add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
169
		add_action( 'user_register', array( $this, 'save_user' ) );
170
171
		if ( $this->cmb->has_columns ) {
172
			add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) );
173
			add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 );
174
		}
175
176
		return $this;
177
	}
178
179
	public function term_hooks() {
180
		if ( ! function_exists( 'get_term_meta' ) ) {
181
			wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) );
182
		}
183
184
		if ( ! $this->cmb->prop( 'taxonomies' ) ) {
185
			wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) );
186
		}
187
188
		$this->taxonomies = (array) $this->cmb->prop( 'taxonomies' );
189
		$show_on_term_add = $this->cmb->prop( 'new_term_section' );
190
		$priority         = $this->get_priority( 8 );
191
192
		foreach ( $this->taxonomies as $taxonomy ) {
193
			// Display our form data.
194
			add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 );
195
196
			$show_on_add = is_array( $show_on_term_add )
197
				? in_array( $taxonomy, $show_on_term_add )
198
				: (bool) $show_on_term_add;
199
200
			/**
201
			 * Filter to determine if the term's fields should show in the "Add term" section.
202
			 *
203
			 * The dynamic portion of the hook name, $cmb_id, is the metabox id.
204
			 *
205
			 * @param bool   $show_on_add Default is the value of the new_term_section cmb parameter.
206
			 * @param object $cmb         The CMB2 instance
207
			 */
208
			$show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb );
209
210
			// Display form in add-new section (unless specified not to).
211
			if ( $show_on_add ) {
212
				add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 );
213
			}
214
215
			if ( $this->cmb->has_columns ) {
216
				add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) );
217
				add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 );
218
			}
219
		}
220
221
		add_action( 'created_term', array( $this, 'save_term' ), 10, 3 );
222
		add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 );
223
		add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 );
224
225
		return $this;
226
	}
227
228
	public function options_page_hooks() {
229
		$option_keys = $this->cmb->options_page_keys();
230
231
		if ( ! empty( $option_keys ) ) {
232
			foreach ( $option_keys as $option_key ) {
233
				$this->options_hookup[ $option_key ] = new CMB2_Options_Hookup( $this->cmb, $option_key );
234
				$this->options_hookup[ $option_key ]->hooks();
235
			}
236
		}
237
238
		return $this;
239
	}
240
241
	/**
242
	 * Registers styles for CMB2
243
	 *
244
	 * @since 2.0.7
245
	 */
246 1
	protected static function register_styles() {
247 1
		if ( self::$css_registration_done ) {
248
			return;
249
		}
250
251
		// Only use minified files if SCRIPT_DEBUG is off.
252 1
		$min   = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
253 1
		$front = is_admin() ? '' : '-front';
254 1
		$rtl   = is_rtl() ? '-rtl' : '';
255
256
		/**
257
		 * Filters the registered style dependencies for the cmb2 stylesheet.
258
		 *
259
		 * @param array $dependencies The registered style dependencies for the cmb2 stylesheet.
260
		 */
261 1
		$dependencies = apply_filters( 'cmb2_style_dependencies', array() );
262 1
		wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies );
263 1
		wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies );
264
265 1
		self::$css_registration_done = true;
266 1
	}
267
268
	/**
269
	 * Registers scripts for CMB2
270
	 *
271
	 * @since  2.0.7
272
	 */
273 1
	protected static function register_js() {
274 1
		if ( self::$js_registration_done ) {
275
			return;
276
		}
277
278 1
		$hook = is_admin() ? 'admin_footer' : 'wp_footer';
279 1
		add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 );
280
281 1
		self::$js_registration_done = true;
282 1
	}
283
284
	/**
285
	 * Registers scripts and styles for CMB2
286
	 *
287
	 * @since  1.0.0
288
	 */
289
	public static function register_scripts() {
290
		self::register_styles();
291
		self::register_js();
292
	}
293
294
	/**
295
	 * Enqueues scripts and styles for CMB2 in admin_head.
296
	 *
297
	 * @since 1.0.0
298
	 *
299
	 * @param string $hook Current hook for the admin page.
300
	 */
301
	public function do_scripts( $hook ) {
302
		$hooks = array(
303
			'post.php',
304
			'post-new.php',
305
			'page-new.php',
306
			'page.php',
307
			'comment.php',
308
			'edit-tags.php',
309
			'term.php',
310
			'user-new.php',
311
			'profile.php',
312
			'user-edit.php',
313
		);
314
		// only pre-enqueue our scripts/styles on the proper pages
315
		// show_form_for_type will have us covered if we miss something here.
316
		if ( in_array( $hook, $hooks, true ) ) {
317
			if ( $this->cmb->prop( 'cmb_styles' ) ) {
318
				self::enqueue_cmb_css();
319
			}
320
			if ( $this->cmb->prop( 'enqueue_js' ) ) {
321
				self::enqueue_cmb_js();
322
			}
323
		}
324
	}
325
326
	/**
327
	 * Register the CMB2 field column headers.
328
	 *
329
	 * @since 2.2.2
330
	 *
331
	 * @param array $columns Array of columns available for the admin page.
332
	 */
333
	public function register_column_headers( $columns ) {
334
		$fields = $this->cmb->prop( 'fields' );
335
336
		foreach ( $fields as $key => $field ) {
337
			if ( ! isset( $field['column'] ) ) {
338
				continue;
339
			}
340
341
			$column = $field['column'];
342
343
			if ( false === $column['position'] ) {
344
345
				$columns[ $field['id'] ] = $column['name'];
346
347
			} else {
348
349
				$before = array_slice( $columns, 0, absint( $column['position'] ) );
350
				$before[ $field['id'] ] = $column['name'];
351
				$columns = $before + $columns;
352
			}
353
354
			$column['field'] = $field;
355
			$this->columns[ $field['id'] ] = $column;
356
		}
357
358
		return $columns;
359
	}
360
361
	/**
362
	 * The CMB2 field column display output.
363
	 *
364
	 * @since 2.2.2
365
	 *
366
	 * @param string $column_name Current column name.
367
	 * @param mixed  $object_id Current object ID.
368
	 */
369
	public function column_display( $column_name, $object_id ) {
370
		if ( isset( $this->columns[ $column_name ] ) ) {
371
				$field = new CMB2_Field( array(
372
					'field_args'  => $this->columns[ $column_name ]['field'],
373
					'object_type' => $this->object_type,
374
					'object_id'   => $this->cmb->object_id( $object_id ),
375
					'cmb_id'      => $this->cmb->cmb_id,
376
				) );
377
378
				$this->cmb->get_field( $field )->render_column();
379
		}
380
	}
381
382
	/**
383
	 * Returns the column display.
384
	 *
385
	 * @since 2.2.2
386
	 */
387
	public function return_column_display( $empty, $custom_column, $object_id ) {
388
		ob_start();
389
		$this->column_display( $custom_column, $object_id );
390
		$column = ob_get_clean();
391
392
		return $column ? $column : $empty;
393
	}
394
395
	/**
396
	 * Output the CMB2 box/fields in an alternate context (not in a standard metabox area).
397
	 *
398
	 * @since 2.2.4
399
	 */
400
	public function add_context_metaboxes() {
401
402
		if ( ! $this->show_on() ) {
403
			return;
404
		}
405
406
		$page = get_current_screen()->id;
407
408
		foreach ( $this->cmb->box_types() as $object_type ) {
409
			$screen = convert_to_screen( $object_type );
410
411
			// If we're on the right post-type/object...
412
			if ( isset( $screen->id ) && $screen->id === $page ) {
413
414
				// Show the box.
415
				$this->output_context_metabox();
416
			}
417
		}
418
	}
419
420
	/**
421
	 * Output the CMB2 box/fields in an alternate context (not in a standard metabox area).
422
	 *
423
	 * @since 2.2.4
424
	 */
425
	public function output_context_metabox() {
426
		$title = $this->cmb->prop( 'title' );
427
428
		/*
429
		 * To keep from outputting the open/close markup, do not include
430
		 * a 'title' property in your metabox registration array.
431
		 *
432
		 * To output the fields 'naked' (without a postbox wrapper/style), then
433
		 * add a `'remove_box_wrap' => true` to your metabox registration array.
434
		 */
435
		$add_wrap = ! empty( $title ) || ! $this->cmb->prop( 'remove_box_wrap' );
436
		$add_handle = $add_wrap && ! empty( $title );
437
438
		// Open the context-box wrap.
439
		$this->context_box_title_markup_open( $add_handle );
440
441
		// Show the form fields.
442
		$this->cmb->show_form();
443
444
		// Close the context-box wrap.
445
		$this->context_box_title_markup_close( $add_handle );
446
	}
447
448
	/**
449
	 * Output the opening markup for a context box.
450
	 *
451
	 * @since 2.2.4
452
	 * @param bool $add_handle Whether to add the metabox handle and opening div for .inside.
453
	 */
454
	public function context_box_title_markup_open( $add_handle = true ) {
455
		$title = $this->cmb->prop( 'title' );
456
457
		$page = get_current_screen()->id;
458
		add_filter( "postbox_classes_{$page}_{$this->cmb->cmb_id}", array( $this, 'postbox_classes' ) );
459
460
		echo '<div id="' . $this->cmb->cmb_id . '" class="' . postbox_classes( $this->cmb->cmb_id, $page ) . '">' . "\n";
461
462
		if ( $add_handle ) {
463
464
			echo '<button type="button" class="handlediv button-link" aria-expanded="true">';
465
				echo '<span class="screen-reader-text">' . sprintf( __( 'Toggle panel: %s' ), $title ) . '</span>';
466
				echo '<span class="toggle-indicator" aria-hidden="true"></span>';
467
			echo '</button>';
468
469
			echo '<h2 class="hndle"><span>' . esc_attr( $title ) . '</span></h2>' . "\n";
470
			echo '<div class="inside">' . "\n";
471
		}
472
	}
473
474
	/**
475
	 * Output the closing markup for a context box.
476
	 *
477
	 * @since 2.2.4
478
	 * @param bool $add_inside_close Whether to add closing div for .inside.
479
	 */
480
	public function context_box_title_markup_close( $add_inside_close = true ) {
481
482
		// Load the closing divs for a title box.
483
		if ( $add_inside_close ) {
484
			echo '</div>' . "\n"; // .inside
485
		}
486
487
		echo '</div>' . "\n"; // .context-box
488
	}
489
490
	/**
491
	 * Add metaboxes (to 'post' or 'comment' object types)
492
	 *
493
	 * @since 1.0.0
494
	 */
495
	public function add_metaboxes() {
496
497
		if ( ! $this->show_on() ) {
498
			return;
499
		}
500
501
		/*
502
		 * To keep from registering an actual post-screen metabox,
503
		 * omit the 'title' property from the metabox registration array.
504
		 *
505
		 * (WordPress will not display metaboxes without titles anyway)
506
		 *
507
		 * This is a good solution if you want to handle outputting your
508
		 * metaboxes/fields elsewhere in the post-screen.
509
		 */
510
		if ( ! $this->cmb->prop( 'title' ) ) {
511
			return;
512
		}
513
514
		$page = get_current_screen()->id;
515
		add_filter( "postbox_classes_{$page}_{$this->cmb->cmb_id}", array( $this, 'postbox_classes' ) );
516
517
		foreach ( $this->cmb->box_types() as $object_type ) {
518
			if ( count( $this->cmb->tax_metaboxes_to_remove ) ) {
519
				$this->remove_default_tax_metaboxes( $object_type );
520
			}
521
522
			add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $object_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
0 ignored issues
show
The function add_meta_box was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

522
			/** @scrutinizer ignore-call */ 
523
   add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $object_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
Loading history...
523
		}
524
	}
525
526
	/**
527
	 * Remove the specified default taxonomy metaboxes for a post-type.
528
	 *
529
	 * @since 2.2.3
530
	 * @param string $post_type Post type to remove the metabox for.
531
	 */
532
	protected function remove_default_tax_metaboxes( $post_type ) {
533
		foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) {
534
			if ( ! taxonomy_exists( $taxonomy ) ) {
535
				continue;
536
			}
537
538
			$mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}";
539
			remove_meta_box( $mb_id, $post_type, 'side' );
540
		}
541
	}
542
543
	/**
544
	 * Modify metabox postbox classes.
545
	 *
546
	 * @since 2.2.4
547
	 * @param  array $classes Array of classes.
548
	 * @return array           Modified array of classes
549
	 */
550
	public function postbox_classes( $classes ) {
551
		if ( $this->cmb->prop( 'closed' ) && ! in_array( 'closed', $classes ) ) {
552
			$classes[] = 'closed';
553
		}
554
555
		if ( $this->cmb->is_alternate_context_box() ) {
556
			$classes = $this->alternate_context_postbox_classes( $classes );
557
		} else {
558
			$classes[] = 'cmb2-postbox';
559
		}
560
561
		return $classes;
562
	}
563
564
	/**
565
	 * Modify metabox altnernate context postbox classes.
566
	 *
567
	 * @since 2.2.4
568
	 * @param  array $classes Array of classes.
569
	 * @return array           Modified array of classes
570
	 */
571
	protected function alternate_context_postbox_classes( $classes ) {
572
		$classes[] = 'context-box';
573
		$classes[] = 'context-' . $this->cmb->prop( 'context' ) . '-box';
574
575
		if ( in_array( $this->cmb->cmb_id, get_hidden_meta_boxes( get_current_screen() ) ) ) {
576
			$classes[] = 'hide-if-js';
577
		}
578
579
		$add_wrap = $this->cmb->prop( 'title' ) || ! $this->cmb->prop( 'remove_box_wrap' );
580
581
		if ( $add_wrap ) {
582
			$classes[] = 'cmb2-postbox postbox';
583
		} else {
584
			$classes[] = 'cmb2-no-box-wrap';
585
		}
586
587
		return $classes;
588
	}
589
590
	/**
591
	 * Display metaboxes for a post or comment object.
592
	 *
593
	 * @since  1.0.0
594
	 */
595
	public function metabox_callback() {
596
		$object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID();
597
		$this->cmb->show_form( $object_id, $this->object_type );
598
	}
599
600
	/**
601
	 * Display metaboxes for new user page.
602
	 *
603
	 * @since 1.0.0
604
	 *
605
	 * @param mixed $section User section metabox.
606
	 */
607
	public function user_new_metabox( $section ) {
608
		if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
609
			$object_id = $this->cmb->object_id();
610
			$this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
611
			$this->user_metabox();
612
		}
613
	}
614
615
	/**
616
	 * Display metaboxes for a user object.
617
	 *
618
	 * @since  1.0.0
619
	 */
620
	public function user_metabox() {
621
		$this->show_form_for_type( 'user' );
622
	}
623
624
	/**
625
	 * Display metaboxes for a taxonomy term object.
626
	 *
627
	 * @since  2.2.0
628
	 */
629
	public function term_metabox() {
630
		$this->show_form_for_type( 'term' );
631
	}
632
633
	/**
634
	 * Display metaboxes for an object type.
635
	 *
636
	 * @since 2.2.0
637
	 * @param  string $type Object type.
638
	 * @return void
639
	 */
640
	public function show_form_for_type( $type ) {
641
		if ( $type != $this->object_type ) {
642
			return;
643
		}
644
645
		if ( ! $this->show_on() ) {
646
			return;
647
		}
648
649
		if ( $this->cmb->prop( 'cmb_styles' ) ) {
650
			self::enqueue_cmb_css();
651
		}
652
		if ( $this->cmb->prop( 'enqueue_js' ) ) {
653
			self::enqueue_cmb_js();
654
		}
655
656
		$this->cmb->show_form( 0, $type );
657
	}
658
659
	/**
660
	 * Determines if metabox should be shown in current context.
661
	 *
662
	 * @since 2.0.0
663
	 * @return bool Whether metabox should be added/shown.
664
	 */
665
	public function show_on() {
666
		// If metabox is requesting to be conditionally shown.
667
		$show = $this->cmb->should_show();
668
669
		/**
670
		 * Filter to determine if metabox should show. Default is true.
671
		 *
672
		 * @param array  $show          Default is true, show the metabox.
673
		 * @param mixed  $meta_box_args Array of the metabox arguments.
674
		 * @param mixed  $cmb           The CMB2 instance.
675
		 */
676
		$show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
677
678
		return $show;
679
	}
680
681
	/**
682
	 * Get the CMB priority property set to numeric hook priority.
683
	 *
684
	 * @since 2.2.0
685
	 *
686
	 * @param integer $default Default display hook priority.
687
	 * @return integer          Hook priority.
688
	 */
689
	public function get_priority( $default = 10 ) {
690
		$priority = $this->cmb->prop( 'priority' );
691
692
		if ( ! is_numeric( $priority ) ) {
693
			switch ( $priority ) {
694
695
				case 'high':
696
					$priority = 5;
697
					break;
698
699
				case 'low':
700
					$priority = 20;
701
					break;
702
703
				default:
704
					$priority = $default;
705
					break;
706
			}
707
		}
708
709
		return $priority;
710
	}
711
712
	/**
713
	 * Save data from post metabox
714
	 *
715
	 * @since 1.0.0
716
	 * @param  int   $post_id Post ID.
717
	 * @param  mixed $post    Post object.
718
	 * @return void
719
	 */
720
	public function save_post( $post_id, $post = false ) {
721
722
		$post_type = $post ? $post->post_type : get_post_type( $post_id );
723
724
		$do_not_pass_go = (
725
			! $this->can_save( $post_type )
726
			// Check user editing permissions.
727
			|| ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
728
			|| ! current_user_can( 'edit_post', $post_id )
729
		);
730
731
		if ( $do_not_pass_go ) {
732
			return;
733
		}
734
735
		$this->cmb->save_fields( $post_id, 'post', $_POST );
736
	}
737
738
	/**
739
	 * Save data from comment metabox.
740
	 *
741
	 * @since 2.0.9
742
	 * @param  int $comment_id Comment ID.
743
	 * @return void
744
	 */
745
	public function save_comment( $comment_id ) {
746
747
		$can_edit = current_user_can( 'moderate_comments', $comment_id );
748
749
		if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) {
750
			$this->cmb->save_fields( $comment_id, 'comment', $_POST );
751
		}
752
	}
753
754
	/**
755
	 * Save data from user fields.
756
	 *
757
	 * @since 1.0.x
758
	 * @param  int $user_id User ID.
759
	 * @return void
760
	 */
761
	public function save_user( $user_id ) {
762
		// check permissions.
763
		if ( $this->can_save( 'user' ) ) {
764
			$this->cmb->save_fields( $user_id, 'user', $_POST );
765
		}
766
	}
767
768
	/**
769
	 * Save data from term fields
770
	 *
771
	 * @since 2.2.0
772
	 * @param int    $term_id  Term ID.
773
	 * @param int    $tt_id    Term Taxonomy ID.
774
	 * @param string $taxonomy Taxonomy.
775
	 * @return void
776
	 */
777
	public function save_term( $term_id, $tt_id, $taxonomy = '' ) {
778
		$taxonomy = $taxonomy ? $taxonomy : $tt_id;
779
780
		// check permissions.
781
		if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) {
782
			$this->cmb->save_fields( $term_id, 'term', $_POST );
783
		}
784
	}
785
786
	/**
787
	 * Delete term meta when a term is deleted.
788
	 *
789
	 * @since 2.2.0
790
	 * @param  int    $term_id  Term ID.
791
	 * @param  int    $tt_id    Term Taxonomy ID.
792
	 * @param  string $taxonomy Taxonomy.
793
	 * @return void
794
	 */
795
	public function delete_term( $term_id, $tt_id, $taxonomy = '' ) {
796
		if ( $this->taxonomy_can_save( $taxonomy ) ) {
797
798
			$data_to_delete = array();
799
			foreach ( $this->cmb->prop( 'fields' ) as $field ) {
800
				$data_to_delete[ $field['id'] ] = '';
801
			}
802
803
			$this->cmb->save_fields( $term_id, 'term', $data_to_delete );
804
		}
805
	}
806
807
	/**
808
	 * Determines if the current object is able to be saved.
809
	 *
810
	 * @since  2.0.9
811
	 * @param  string $type Current object type.
812
	 * @return bool         Whether object can be saved.
813
	 */
814
	public function can_save( $type = '' ) {
815
816
		$can_save = (
817
			$this->cmb->prop( 'save_fields' )
818
			// check nonce.
819
			&& isset( $_POST[ $this->cmb->nonce() ] )
820
			&& wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
821
			// check if autosave.
822
			&& ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
823
			// get the metabox types & compare it to this type.
824
			&& ( $type && in_array( $type, $this->cmb->box_types() ) )
825
			// Don't do updates during a switch-to-blog instance.
826
			&& ! ( is_multisite() && ms_is_switched() )
827
		);
828
829
		/**
830
		 * Filter to determine if metabox is allowed to save.
831
		 *
832
		 * @param bool   $can_save Whether the current metabox can save.
833
		 * @param object $cmb      The CMB2 instance.
834
		 */
835
		return apply_filters( 'cmb2_can_save', $can_save, $this->cmb );
836
	}
837
838
	/**
839
	 * Determine if taxonomy of term being modified is cmb2-editable.
840
	 *
841
	 * @since 2.2.0
842
	 *
843
	 * @param string $taxonomy Taxonomy of term being modified.
844
	 * @return bool             Whether taxonomy is editable.
845
	 */
846
	public function taxonomy_can_save( $taxonomy ) {
847
		if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) {
848
			return false;
849
		}
850
851
		$taxonomy_object = get_taxonomy( $taxonomy );
852
		// Can the user edit this term?
853
		if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
854
			return false;
855
		}
856
857
		return true;
858
	}
859
860
	/**
861
	 * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc).
862
	 *
863
	 * @since  2.2.2.1
864
	 */
865
	protected function maybe_enqueue_column_display_styles() {
866
		global $pagenow;
867
		if (
868
			$pagenow
869
			&& $this->cmb->has_columns
870
			&& $this->cmb->prop( 'cmb_styles' )
871
			&& in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 )
872
			) {
873 1
			self::enqueue_cmb_css( 'cmb2-display-styles' );
874
		}
875
	}
876
877
	/**
878
	 * Includes CMB2 styles.
879
	 *
880 1
	 * @since 2.0.0
881
	 *
882
	 * @param string $handle CSS handle.
883
	 * @return mixed
884 1
	 */
885
	public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) {
886
887
		/**
888
		 * Filter to determine if CMB2'S css should be enqueued.
889
		 *
890 1
		 * @param bool $enqueue_css Default is true.
891
		 */
892
		if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
893
			return false;
894
		}
895
896
		self::register_styles();
897
898 1
		/*
899
		 * White list the options as this method can be used as a hook callback
900
		 * and have a different argument passed.
901
		 */
902
		return wp_enqueue_style( 'cmb2-display-styles' === $handle ? $handle : 'cmb2-styles' );
903
	}
904
905 1
	/**
906
	 * Includes CMB2 JS.
907
	 *
908
	 * @since  2.0.0
909 1
	 */
910 1
	public static function enqueue_cmb_js() {
911
912
		/**
913
		 * Filter to determine if CMB2'S JS should be enqueued.
914
		 *
915
		 * @param bool $enqueue_js Default is true.
916
		 */
917
		if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
918
			return false;
919
		}
920
921
		self::register_js();
922
		return true;
923
	}
924
925
}
926