Completed
Pull Request — trunk (#838)
by Justin
09:58
created

CMB2_hookup::add_context_metaboxes()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 23.807

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 7
nop 0
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
ccs 3
cts 10
cp 0.3
crap 23.807
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    WebDevStudios
11
 * @license   GPL-2.0+
12
 * @link      http://webdevstudios.com
13
 */
14
class CMB2_hookup extends CMB2_Hookup_Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
16
	/**
17
	 * Only allow JS registration once
18
	 * @var   bool
19
	 * @since 2.0.7
20
	 */
21
	protected static $js_registration_done = false;
22
23
	/**
24
	 * Only allow CSS registration once
25
	 * @var   bool
26
	 * @since 2.0.7
27
	 */
28
	protected static $css_registration_done = false;
29
30
	/**
31
	 * CMB taxonomies array for term meta
32
	 * @var   array
33
	 * @since 2.2.0
34
	 */
35
	protected $taxonomies = array();
36
37
	/**
38
	 * Custom field columns.
39
	 * @var   array
40
	 * @since 2.2.2
41
	 */
42
	protected $columns = array();
43
44
	/**
45
	 * Constructor
46
	 * @since 2.0.0
47
	 * @param CMB2 $cmb The CMB2 object to hookup
48
	 */
49
	public function __construct( CMB2 $cmb ) {
50
		$this->cmb = $cmb;
51
		$this->object_type = $this->cmb->mb_object_type();
52
	}
53
54
	public function universal_hooks() {
55
		foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) {
56
			add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 );
57
		}
58
59
		if ( is_admin() ) {
60
			// register our scripts and styles for cmb
61
			$this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 );
62
			$this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
63
64
			$this->maybe_enqueue_column_display_styles();
65
66
			switch ( $this->object_type ) {
67
				case 'post':
68
					return $this->post_hooks();
69
				case 'comment':
70
					return $this->comment_hooks();
71
				case 'user':
72
					return $this->user_hooks();
73
				case 'term':
74
					return $this->term_hooks();
75
			}
76
		}
77
	}
78
79
	public function post_hooks() {
80
81
		// Fetch the context we set in our call.
82
		$context = ! empty( $this->cmb->prop( 'context' ) ) ? $this->cmb->prop( 'context' ) : 'normal';
83
84
		// Call the proper hook based on the context provided.
85
		switch ( $context ) {
86
87
			case 'form_top':
88
				add_action( 'edit_form_top', array( $this, 'add_context_metaboxes' ) );
89
				break;
90
91
			case 'before_permalink':
92
				add_action( 'edit_form_before_permalink', array( $this, 'add_context_metaboxes' ) );
93
				break;
94
95
			case 'after_title':
96
				add_action( 'edit_form_after_title', array( $this, 'add_context_metaboxes' ) );
97
				break;
98
99
			case 'after_editor':
100
				add_action( 'edit_form_after_editor', array( $this, 'add_context_metaboxes' ) );
101
				break;
102
103
			default:
104
				add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
105
		}
106
107
		add_action( 'add_attachment', array( $this, 'save_post' ) );
108
		add_action( 'edit_attachment', array( $this, 'save_post' ) );
109
		add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
110 1
111 1
		if ( $this->cmb->has_columns ) {
112 1
			foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
113
				add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) );
114
				add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 );
115
			}
116 1
		}
117
	}
118 1
119
	public function comment_hooks() {
120 1
		add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) );
121 1
		add_action( 'edit_comment', array( $this, 'save_comment' ) );
122 1
123 1
		if ( $this->cmb->has_columns ) {
124 1
			add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) );
125 1
			add_action( 'manage_comments_custom_column', array( $this, 'column_display'  ), 10, 3 );
126 1
		}
127 1
	}
128 1
129
	public function user_hooks() {
130 1
		$priority = $this->get_priority();
131
132
		add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
133 1
		add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
134
		add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority );
135 1
136
		add_action( 'personal_options_update', array( $this, 'save_user' ) );
137 1
		add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
138
		add_action( 'user_register', array( $this, 'save_user' ) );
139 1
140 1 View Code Duplication
		if ( $this->cmb->has_columns ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141 1
			add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) );
142 1
			add_filter( 'manage_users_custom_column', array( $this, 'return_column_display'  ), 10, 3 );
143 1
		}
144 1
	}
145
146 1
	public function term_hooks() {
147
		if ( ! function_exists( 'get_term_meta' ) ) {
148 1
			wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) );
149 1
		}
150 1
151 1
		if ( ! $this->cmb->prop( 'taxonomies' ) ) {
152 1
			wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) );
153 1
		}
154 1
155 1
		$this->taxonomies = (array) $this->cmb->prop( 'taxonomies' );
156 1
		$show_on_term_add = $this->cmb->prop( 'new_term_section' );
157 1
		$priority         = $this->get_priority( 8 );
158 1
159 1
		foreach ( $this->taxonomies as $taxonomy ) {
160 1
			// Display our form data
161 1
			add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 );
162
163 1
			$show_on_add = is_array( $show_on_term_add )
164 1
				? in_array( $taxonomy, $show_on_term_add )
165 1
				: (bool) $show_on_term_add;
166 1
167
			$show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb );
168 1
169 1
			// Display form in add-new section (unless specified not to)
170
			if ( $show_on_add ) {
171 1
				add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 );
172 1
			}
173 1
174 1 View Code Duplication
			if ( $this->cmb->has_columns ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175 1
				add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) );
176 1
				add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display'  ), 10, 3 );
177 1
			}
178 1
		}
179
180 1
		add_action( 'created_term', array( $this, 'save_term' ), 10, 3 );
181
		add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 );
182 1
		add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 );
183 1
184
	}
185
186
	/**
187
	 * Registers styles for CMB2
188
	 * @since 2.0.7
189
	 */
190
	protected static function register_styles() {
191
		if ( self::$css_registration_done ) {
192
			return;
193
		}
194
195
		// Only use minified files if SCRIPT_DEBUG is off
196
		$min   = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
197
		$front = is_admin() ? '' : '-front';
198
		$rtl   = is_rtl() ? '-rtl' : '';
199
200
		// Filter required styles and register stylesheet
201
		$dependencies = apply_filters( 'cmb2_style_dependencies', array() );
202
		wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies );
203
		wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies );
204
205
		self::$css_registration_done = true;
206
	}
207
208
	/**
209
	 * Registers scripts for CMB2
210
	 * @since  2.0.7
211
	 */
212
	protected static function register_js() {
213
		if ( self::$js_registration_done ) {
214
			return;
215
		}
216
217
		$hook = is_admin() ? 'admin_footer' : 'wp_footer';
218
		add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 );
219
220
		self::$js_registration_done = true;
221
	}
222
223
	/**
224
	 * Registers scripts and styles for CMB2
225
	 * @since  1.0.0
226
	 */
227
	public static function register_scripts() {
228
		self::register_styles();
229
		self::register_js();
230
	}
231
232
	/**
233
	 * Enqueues scripts and styles for CMB2 in admin_head.
234
	 * @since  1.0.0
235
	 */
236
	public function do_scripts( $hook ) {
237
		$hooks = array(
238
			'post.php',
239
			'post-new.php',
240
			'page-new.php',
241
			'page.php',
242
			'comment.php',
243
			'edit-tags.php',
244
			'term.php',
245
			'user-new.php',
246
			'profile.php',
247
			'user-edit.php',
248
		);
249
		// only pre-enqueue our scripts/styles on the proper pages
250
		// show_form_for_type will have us covered if we miss something here.
251
		if ( in_array( $hook, $hooks, true ) ) {
252
			if ( $this->cmb->prop( 'cmb_styles' ) ) {
253
				self::enqueue_cmb_css();
254
			}
255
			if ( $this->cmb->prop( 'enqueue_js' ) ) {
256
				self::enqueue_cmb_js();
257
			}
258
		}
259
	}
260
261
	/**
262
	 * Register the CMB2 field column headers.
263
	 * @since 2.2.2
264
	 */
265
	public function register_column_headers( $columns ) {
266
		$fields = $this->cmb->prop( 'fields' );
267
268
		foreach ( $fields as $key => $field ) {
269
			if ( ! isset( $field['column'] ) ) {
270
				continue;
271
			}
272
273
			$column = $field['column'];
274
275
			if ( false === $column['position'] ) {
276
277
				$columns[ $field['id'] ] = $column['name'];
278
279
			} else {
280
281
				$before = array_slice( $columns, 0, absint( $column['position'] ) );
282
				$before[ $field['id'] ] = $column['name'];
283
				$columns = $before + $columns;
284
			}
285
286
			$column['field'] = $field;
287
			$this->columns[ $field['id'] ] = $column;
288
		}
289
290
		return $columns;
291
	}
292
293
	/**
294
	 * The CMB2 field column display output.
295
	 * @since 2.2.2
296
	 */
297
	public function column_display( $column_name, $object_id ) {
298
		if ( isset( $this->columns[ $column_name ] ) ) {
299
 			$field = new CMB2_Field( array(
300
				'field_args'  => $this->columns[ $column_name ]['field'],
301
				'object_type' => $this->object_type,
302
				'object_id'   => $this->cmb->object_id( $object_id ),
303
				'cmb_id'      => $this->cmb->cmb_id,
304
			) );
305
306
			$this->cmb->get_field( $field )->render_column();
307
		}
308
	}
309
310
	/**
311
	 * Returns the column display.
312
	 * @since 2.2.2
313
	 */
314
	public function return_column_display( $empty, $custom_column, $object_id ) {
315
		ob_start();
316
		$this->column_display( $custom_column, $object_id );
317
		$column = ob_get_clean();
318
319
		return $column ? $column : $empty;
320
	}
321
322
	/**
323
	 * Output the CMB2 fields in an alternate context (not in a metabox).
324
	 * @since 2.2.4
325
	 */
326
	public function add_context_metaboxes() {
327
328
		if ( ! $this->show_on() ) {
329
			return;
330
		}
331
332
		$current_screen = get_current_screen();
333
334
		foreach ( $this->cmb->prop( 'object_types' ) as $object_type ) {
335
			$screen = convert_to_screen( $object_type );
336
337
			// If we're on the right post-type/object, stop searching...
338
			if ( isset( $screen->id ) && $screen->id === $current_screen->id ) {
339
340
				// Call our before hook.
341
				if ( ! empty( $this->cmb->prop( 'title' ) ) ) {
342
					$this->context_box_title_markup_open( $this->cmb );
0 ignored issues
show
Unused Code introduced by
The call to CMB2_hookup::context_box_title_markup_open() has too many arguments starting with $this->cmb.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
343
				}
344
345
				// Show the form.
346
				$this->cmb->show_form();
347
348
				// Call our after hook.
349
				if ( ! empty( $this->cmb->prop( 'title' ) ) ) {
350 1
					$this->context_box_title_markup_close();
351 1
				}
352
			}
353 1
		}
354
	}
355
356
	/**
357
	 * The opening markup for when we include a title.
358
	 *
359
	 * @return string
360 1
	 */
361 1
	public function context_box_title_markup_open() {
362 1
363 1
		$title = $this->cmb->prop( 'title' );
364
365
		$screen = get_current_screen();
366
		$hidden = get_hidden_meta_boxes( $screen );
367 1
368
		$classes = 'postbox context-' . $this->cmb->prop( 'context' ) . '-box context-box postbox-container ';
369
		$classes .= in_array( $this->cmb->cmb_id, $hidden ) ? ' hide-if-js' : '';
370
		$classes .= postbox_classes( $this->cmb->cmb_id, $screen->id );
371
372
		echo '<div id="' . $this->cmb->cmb_id . '" class="' . $classes . '">' . "\n";
373
374
			echo '<button type="button" class="handlediv button-link" aria-expanded="true">';
375
				echo '<span class="screen-reader-text">' . sprintf( __( 'Toggle panel: %s' ), $title ) . '</span>';
376
				echo '<span class="toggle-indicator" aria-hidden="true"></span>';
377
			echo '</button>';
378
379
			echo '<h2 class="hndle"><span>' . esc_attr( $title ) . '</span></h2>' . "\n";
380
			echo '<div class="inside">' . "\n";
381
	}
382
383
	/**
384
	 * The closing markup for when we include a title.
385
	 *
386
	 * @return string
387
	 */
388
	public function context_box_title_markup_close() {
389
390
		// Load the closing divs for a title box.
391
		echo '</div>' . "\n";
392
		echo '</div>' . "\n";
393
	}
394
395
	/**
396
	 * Add metaboxes (to 'post' or 'comment' object types)
397
	 * @since 1.0.0
398
	 */
399
	public function add_metaboxes() {
400
401
		if ( ! $this->show_on() ) {
402
			return;
403
		}
404
405
		/**
406
		 * To keep from registering an actual post-screen metabox,
407
		 * omit the 'title' attribute from the metabox registration array.
408
		 *
409
		 * (WordPress will not display metaboxes without titles anyway)
410
		 *
411
		 * This is a good solution if you want to output your metaboxes
412
		 * Somewhere else in the post-screen
413
		 */
414
		if ( ! $this->cmb->prop( 'title' ) ) {
415
			return;
416
		}
417
418
		foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
419
			if ( $this->cmb->prop( 'closed' ) ) {
420
				add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
421
			}
422
423
			if ( count( $this->cmb->tax_metaboxes_to_remove ) ) {
424
				$this->remove_default_tax_metaboxes( $post_type );
425
			}
426
427
			add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) );
428
		}
429
	}
430
431
	/**
432
	 * Remove the specified default taxonomy metaboxes for a post-type.
433
	 * @since 2.2.3
434
	 * @param string $post_type Post type to remove the metabox for.
435
	 */
436
	protected function remove_default_tax_metaboxes( $post_type ) {
437
		foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) {
438
			if ( ! taxonomy_exists( $taxonomy ) ) {
439
				continue;
440
			}
441
442
			$mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}";
443
			remove_meta_box( $mb_id, $post_type, 'side' );
444
		}
445
	}
446
447
	/**
448
	 * Add 'closed' class to metabox
449
	 * @since  2.0.0
450
	 * @param  array  $classes Array of classes
451
	 * @return array           Modified array of classes
452
	 */
453
	public function close_metabox_class( $classes ) {
454
		$classes[] = 'closed';
455
		return $classes;
456
	}
457
458
	/**
459
	 * Display metaboxes for a post or comment object
460
	 * @since  1.0.0
461
	 */
462
	public function metabox_callback() {
463
		$object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID();
464
		$this->cmb->show_form( $object_id, $this->object_type );
465
	}
466
467
	/**
468
	 * Display metaboxes for new user page
469
	 * @since  1.0.0
470
	 */
471
	public function user_new_metabox( $section ) {
0 ignored issues
show
Coding Style introduced by
user_new_metabox uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
472
		if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
473
			$object_id = $this->cmb->object_id();
474
			$this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
475
			$this->user_metabox();
476
		}
477
	}
478
479
	/**
480
	 * Display metaboxes for a user object
481
	 * @since  1.0.0
482
	 */
483
	public function user_metabox() {
484
		$this->show_form_for_type( 'user' );
485
	}
486
487
	/**
488
	 * Display metaboxes for a taxonomy term object
489
	 * @since  2.2.0
490
	 */
491
	public function term_metabox() {
492
		$this->show_form_for_type( 'term' );
493
	}
494
495
	/**
496
	 * Display metaboxes for an object type
497
	 * @since  2.2.0
498
	 * @param  string $type Object type
499
	 * @return void
500
	 */
501
	public function show_form_for_type( $type ) {
502
		if ( $type != $this->cmb->mb_object_type() ) {
503
			return;
504
		}
505
506
		if ( ! $this->show_on() ) {
507
			return;
508
		}
509
510
		if ( $this->cmb->prop( 'cmb_styles' ) ) {
511
			self::enqueue_cmb_css();
512
		}
513
		if ( $this->cmb->prop( 'enqueue_js' ) ) {
514
			self::enqueue_cmb_js();
515
		}
516
517
		$this->cmb->show_form( 0, $type );
518
	}
519
520
	/**
521
	 * Determines if metabox should be shown in current context
522
	 * @since  2.0.0
523
	 * @return bool Whether metabox should be added/shown
524
	 */
525
	public function show_on() {
526
		// If metabox is requesting to be conditionally shown
527
		$show = $this->cmb->should_show();
528
529
		/**
530
		 * Filter to determine if metabox should show. Default is true
531
		 *
532
		 * @param array  $show          Default is true, show the metabox
533
		 * @param mixed  $meta_box_args Array of the metabox arguments
534
		 * @param mixed  $cmb           The CMB2 instance
535
		 */
536
		$show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
537
538
		return $show;
539
	}
540
541
	/**
542
	 * Get the CMB priority property set to numeric hook priority.
543
	 * @since  2.2.0
544
	 * @param  integer $default Default display hook priority.
545
	 * @return integer          Hook priority.
546
	 */
547
	public function get_priority( $default = 10 ) {
548
		$priority = $this->cmb->prop( 'priority' );
549
550
		if ( ! is_numeric( $priority ) ) {
551
			switch ( $priority ) {
552
553
				case 'high':
554
					$priority = 5;
555
					break;
556
557
				case 'low':
558
					$priority = 20;
559
					break;
560
561
				default:
562
					$priority = $default;
563
					break;
564
			}
565
		}
566
567
		return $priority;
568
	}
569
570
	/**
571
	 * Save data from post metabox
572
	 * @since  1.0.0
573
	 * @param  int    $post_id Post ID
574
	 * @param  mixed  $post    Post object
575
	 * @return null
576
	 */
577
	public function save_post( $post_id, $post = false ) {
0 ignored issues
show
Coding Style introduced by
save_post uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
578
579
		$post_type = $post ? $post->post_type : get_post_type( $post_id );
580
581
		$do_not_pass_go = (
582
			! $this->can_save( $post_type )
583
			// check user editing permissions
584
			|| ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
585
			|| ! current_user_can( 'edit_post', $post_id )
586
		);
587
588
		if ( $do_not_pass_go ) {
589
			// do not collect $200
590
			return;
591
		}
592
593
		// take a trip to reading railroad – if you pass go collect $200
594
		$this->cmb->save_fields( $post_id, 'post', $_POST );
595
	}
596
597
	/**
598
	 * Save data from comment metabox
599
	 * @since  2.0.9
600
	 * @param  int    $comment_id Comment ID
601
	 * @return null
602
	 */
603
	public function save_comment( $comment_id ) {
0 ignored issues
show
Coding Style introduced by
save_comment uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
604
605
		$can_edit = current_user_can( 'moderate_comments', $comment_id );
606
607
		if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) {
608
			$this->cmb->save_fields( $comment_id, 'comment', $_POST );
609
		}
610
	}
611
612
	/**
613
	 * Save data from user fields
614
	 * @since  1.0.x
615
	 * @param  int   $user_id  User ID
616
	 * @return null
617
	 */
618
	public function save_user( $user_id ) {
0 ignored issues
show
Coding Style introduced by
save_user uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
619
		// check permissions
620
		if ( $this->can_save( 'user' ) ) {
621
			$this->cmb->save_fields( $user_id, 'user', $_POST );
622
		}
623
	}
624
625
	/**
626
	 * Save data from term fields
627
	 * @since  2.2.0
628
	 * @param  int    $term_id  Term ID
629
	 * @param  int    $tt_id    Term Taxonomy ID
630
	 * @param  string $taxonomy Taxonomy
631
	 * @return null
632
	 */
633
	public function save_term( $term_id, $tt_id, $taxonomy = '' ) {
0 ignored issues
show
Coding Style introduced by
save_term uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
634
		$taxonomy = $taxonomy ? $taxonomy : $tt_id;
635
636
		// check permissions
637
		if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) {
638
			$this->cmb->save_fields( $term_id, 'term', $_POST );
639
		}
640
	}
641
642
	/**
643
	 * Delete term meta when a term is deleted.
644
	 * @since  2.2.0
645
	 * @param  int    $term_id  Term ID
646
	 * @param  int    $tt_id    Term Taxonomy ID
647
	 * @param  string $taxonomy Taxonomy
648
	 * @return null
649
	 */
650
	public function delete_term( $term_id, $tt_id, $taxonomy = '' ) {
651
		if ( $this->taxonomy_can_save( $taxonomy ) ) {
652
653
			$data_to_delete = array();
654
			foreach ( $this->cmb->prop( 'fields' ) as $field ) {
655
				$data_to_delete[ $field['id'] ] = '';
656
			}
657
658
			$this->cmb->save_fields( $term_id, 'term', $data_to_delete );
659
		}
660
	}
661
662
	/**
663
	 * Determines if the current object is able to be saved
664
	 * @since  2.0.9
665
	 * @param  string  $type Current post_type or comment_type
666
	 * @return bool          Whether object can be saved
667
	 */
668
	public function can_save( $type = '' ) {
0 ignored issues
show
Coding Style introduced by
can_save uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
669
		return apply_filters( 'cmb2_can_save', (
670
			$this->cmb->prop( 'save_fields' )
671
			// check nonce
672
			&& isset( $_POST[ $this->cmb->nonce() ] )
673
			&& wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
674
			// check if autosave
675
			&& ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
676
			// get the metabox types & compare it to this type
677
			&& ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) )
678
			// Don't do updates during a switch-to-blog instance.
679
			&& ! ( is_multisite() && ms_is_switched() )
680
		) );
681
	}
682
683
	/**
684
	 * Determine if taxonomy of term being modified is cmb2-editable.
685
	 * @since  2.2.0
686
	 * @param  string $taxonomy Taxonomy of term being modified.
687
	 * @return bool             Whether taxonomy is editable.
688
	 */
689
	public function taxonomy_can_save( $taxonomy ) {
690
		if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) {
691
			return false;
692
		}
693
694
		$taxonomy_object = get_taxonomy( $taxonomy );
695
		// Can the user edit this term?
696
		if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
697
			return false;
698
		}
699
700
		return true;
701
	}
702
703
	/**
704
	 * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc).
705
	 * @since  2.2.2.1
706
	 */
707
	protected function maybe_enqueue_column_display_styles() {
708
		global $pagenow;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
709
		if (
710
			$pagenow
711
			&& $this->cmb->has_columns
712
			&& $this->cmb->prop( 'cmb_styles' )
713
			&& in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 )
714
			) {
715
			self::enqueue_cmb_css( 'cmb2-display-styles' );
716
		}
717
	}
718
719
	/**
720
	 * Includes CMB2 styles
721
	 * @since  2.0.0
722
	 */
723
	public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) {
724
		if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
725
			return false;
726
		}
727
728
		self::register_styles();
729
730
		/*
731
		 * White list the options as this method can be used as a hook callback
732
		 * and have a different argument passed.
733
		 */
734
		return wp_enqueue_style( 'cmb2-display-styles' === $handle ? $handle : 'cmb2-styles' );
735
	}
736
737
	/**
738
	 * Includes CMB2 JS
739
	 * @since  2.0.0
740
	 */
741
	public static function enqueue_cmb_js() {
742
		if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
743
			return false;
744
		}
745
746
		self::register_js();
747
		return true;
748
	}
749
750
}
751