Completed
Pull Request — trunk (#836)
by Andrew
23:57 queued 15:47
created

CMB2_hookup::metabox_callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 6
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
		foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
333
			$this->cmb->show_form();
334
		}
335
	}
336
337
	/**
338
	 * Add metaboxes (to 'post' or 'comment' object types)
339
	 * @since 1.0.0
340
	 */
341
	public function add_metaboxes() {
342
343
		if ( ! $this->show_on() ) {
344
			return;
345
		}
346
347
		/**
348
		 * To keep from registering an actual post-screen metabox,
349
		 * omit the 'title' attribute from the metabox registration array.
350 1
		 *
351 1
		 * (WordPress will not display metaboxes without titles anyway)
352
		 *
353 1
		 * This is a good solution if you want to output your metaboxes
354
		 * Somewhere else in the post-screen
355
		 */
356
		if ( ! $this->cmb->prop( 'title' ) ) {
357
			return;
358
		}
359
360 1
		foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
361 1
			if ( $this->cmb->prop( 'closed' ) ) {
362 1
				add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
363 1
			}
364
365
			if ( count( $this->cmb->tax_metaboxes_to_remove ) ) {
366
				$this->remove_default_tax_metaboxes( $post_type );
367 1
			}
368
369
			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' ) );
370
		}
371
	}
372
373
	/**
374
	 * Remove the specified default taxonomy metaboxes for a post-type.
375
	 * @since 2.2.3
376
	 * @param string $post_type Post type to remove the metabox for.
377
	 */
378
	protected function remove_default_tax_metaboxes( $post_type ) {
379
		foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) {
380
			if ( ! taxonomy_exists( $taxonomy ) ) {
381
				continue;
382
			}
383
384
			$mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}";
385
			remove_meta_box( $mb_id, $post_type, 'side' );
386
		}
387
	}
388
389
	/**
390
	 * Add 'closed' class to metabox
391
	 * @since  2.0.0
392
	 * @param  array  $classes Array of classes
393
	 * @return array           Modified array of classes
394
	 */
395
	public function close_metabox_class( $classes ) {
396
		$classes[] = 'closed';
397
		return $classes;
398
	}
399
400
	/**
401
	 * Display metaboxes for a post or comment object
402
	 * @since  1.0.0
403
	 */
404
	public function metabox_callback() {
405
		$object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID();
406
		$this->cmb->show_form( $object_id, $this->object_type );
407
	}
408
409
	/**
410
	 * Display metaboxes for new user page
411
	 * @since  1.0.0
412
	 */
413
	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...
414
		if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
415
			$object_id = $this->cmb->object_id();
416
			$this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
417
			$this->user_metabox();
418
		}
419
	}
420
421
	/**
422
	 * Display metaboxes for a user object
423
	 * @since  1.0.0
424
	 */
425
	public function user_metabox() {
426
		$this->show_form_for_type( 'user' );
427
	}
428
429
	/**
430
	 * Display metaboxes for a taxonomy term object
431
	 * @since  2.2.0
432
	 */
433
	public function term_metabox() {
434
		$this->show_form_for_type( 'term' );
435
	}
436
437
	/**
438
	 * Display metaboxes for an object type
439
	 * @since  2.2.0
440
	 * @param  string $type Object type
441
	 * @return void
442
	 */
443
	public function show_form_for_type( $type ) {
444
		if ( $type != $this->cmb->mb_object_type() ) {
445
			return;
446
		}
447
448
		if ( ! $this->show_on() ) {
449
			return;
450
		}
451
452
		if ( $this->cmb->prop( 'cmb_styles' ) ) {
453
			self::enqueue_cmb_css();
454
		}
455
		if ( $this->cmb->prop( 'enqueue_js' ) ) {
456
			self::enqueue_cmb_js();
457
		}
458
459
		$this->cmb->show_form( 0, $type );
460
	}
461
462
	/**
463
	 * Determines if metabox should be shown in current context
464
	 * @since  2.0.0
465
	 * @return bool Whether metabox should be added/shown
466
	 */
467
	public function show_on() {
468
		// If metabox is requesting to be conditionally shown
469
		$show = $this->cmb->should_show();
470
471
		/**
472
		 * Filter to determine if metabox should show. Default is true
473
		 *
474
		 * @param array  $show          Default is true, show the metabox
475
		 * @param mixed  $meta_box_args Array of the metabox arguments
476
		 * @param mixed  $cmb           The CMB2 instance
477
		 */
478
		$show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
479
480
		return $show;
481
	}
482
483
	/**
484
	 * Get the CMB priority property set to numeric hook priority.
485
	 * @since  2.2.0
486
	 * @param  integer $default Default display hook priority.
487
	 * @return integer          Hook priority.
488
	 */
489
	public function get_priority( $default = 10 ) {
490
		$priority = $this->cmb->prop( 'priority' );
491
492
		if ( ! is_numeric( $priority ) ) {
493
			switch ( $priority ) {
494
495
				case 'high':
496
					$priority = 5;
497
					break;
498
499
				case 'low':
500
					$priority = 20;
501
					break;
502
503
				default:
504
					$priority = $default;
505
					break;
506
			}
507
		}
508
509
		return $priority;
510
	}
511
512
	/**
513
	 * Save data from post metabox
514
	 * @since  1.0.0
515
	 * @param  int    $post_id Post ID
516
	 * @param  mixed  $post    Post object
517
	 * @return null
518
	 */
519
	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...
520
521
		$post_type = $post ? $post->post_type : get_post_type( $post_id );
522
523
		$do_not_pass_go = (
524
			! $this->can_save( $post_type )
525
			// check user editing permissions
526
			|| ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
527
			|| ! current_user_can( 'edit_post', $post_id )
528
		);
529
530
		if ( $do_not_pass_go ) {
531
			// do not collect $200
532
			return;
533
		}
534
535
		// take a trip to reading railroad – if you pass go collect $200
536
		$this->cmb->save_fields( $post_id, 'post', $_POST );
537
	}
538
539
	/**
540
	 * Save data from comment metabox
541
	 * @since  2.0.9
542
	 * @param  int    $comment_id Comment ID
543
	 * @return null
544
	 */
545
	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...
546
547
		$can_edit = current_user_can( 'moderate_comments', $comment_id );
548
549
		if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) {
550
			$this->cmb->save_fields( $comment_id, 'comment', $_POST );
551
		}
552
	}
553
554
	/**
555
	 * Save data from user fields
556
	 * @since  1.0.x
557
	 * @param  int   $user_id  User ID
558
	 * @return null
559
	 */
560
	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...
561
		// check permissions
562
		if ( $this->can_save( 'user' ) ) {
563
			$this->cmb->save_fields( $user_id, 'user', $_POST );
564
		}
565
	}
566
567
	/**
568
	 * Save data from term fields
569
	 * @since  2.2.0
570
	 * @param  int    $term_id  Term ID
571
	 * @param  int    $tt_id    Term Taxonomy ID
572
	 * @param  string $taxonomy Taxonomy
573
	 * @return null
574
	 */
575
	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...
576
		$taxonomy = $taxonomy ? $taxonomy : $tt_id;
577
578
		// check permissions
579
		if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) {
580
			$this->cmb->save_fields( $term_id, 'term', $_POST );
581
		}
582
	}
583
584
	/**
585
	 * Delete term meta when a term is deleted.
586
	 * @since  2.2.0
587
	 * @param  int    $term_id  Term ID
588
	 * @param  int    $tt_id    Term Taxonomy ID
589
	 * @param  string $taxonomy Taxonomy
590
	 * @return null
591
	 */
592
	public function delete_term( $term_id, $tt_id, $taxonomy = '' ) {
593
		if ( $this->taxonomy_can_save( $taxonomy ) ) {
594
595
			$data_to_delete = array();
596
			foreach ( $this->cmb->prop( 'fields' ) as $field ) {
597
				$data_to_delete[ $field['id'] ] = '';
598
			}
599
600
			$this->cmb->save_fields( $term_id, 'term', $data_to_delete );
601
		}
602
	}
603
604
	/**
605
	 * Determines if the current object is able to be saved
606
	 * @since  2.0.9
607
	 * @param  string  $type Current post_type or comment_type
608
	 * @return bool          Whether object can be saved
609
	 */
610
	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...
611
		return apply_filters( 'cmb2_can_save', (
612
			$this->cmb->prop( 'save_fields' )
613
			// check nonce
614
			&& isset( $_POST[ $this->cmb->nonce() ] )
615
			&& wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
616
			// check if autosave
617
			&& ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
618
			// get the metabox types & compare it to this type
619
			&& ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) )
620
			// Don't do updates during a switch-to-blog instance.
621
			&& ! ( is_multisite() && ms_is_switched() )
622
		) );
623
	}
624
625
	/**
626
	 * Determine if taxonomy of term being modified is cmb2-editable.
627
	 * @since  2.2.0
628
	 * @param  string $taxonomy Taxonomy of term being modified.
629
	 * @return bool             Whether taxonomy is editable.
630
	 */
631
	public function taxonomy_can_save( $taxonomy ) {
632
		if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) {
633
			return false;
634
		}
635
636
		$taxonomy_object = get_taxonomy( $taxonomy );
637
		// Can the user edit this term?
638
		if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
639
			return false;
640
		}
641
642
		return true;
643
	}
644
645
	/**
646
	 * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc).
647
	 * @since  2.2.2.1
648
	 */
649
	protected function maybe_enqueue_column_display_styles() {
650
		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...
651
		if (
652
			$pagenow
653
			&& $this->cmb->has_columns
654
			&& $this->cmb->prop( 'cmb_styles' )
655
			&& in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 )
656
			) {
657
			self::enqueue_cmb_css( 'cmb2-display-styles' );
658
		}
659
	}
660
661
	/**
662
	 * Includes CMB2 styles
663
	 * @since  2.0.0
664
	 */
665
	public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) {
666
		if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
667
			return false;
668
		}
669
670
		self::register_styles();
671
672
		/*
673
		 * White list the options as this method can be used as a hook callback
674
		 * and have a different argument passed.
675
		 */
676
		return wp_enqueue_style( 'cmb2-display-styles' === $handle ? $handle : 'cmb2-styles' );
677
	}
678
679
	/**
680
	 * Includes CMB2 JS
681
	 * @since  2.0.0
682
	 */
683
	public static function enqueue_cmb_js() {
684
		if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
685
			return false;
686
		}
687
688
		self::register_js();
689
		return true;
690
	}
691
692
}
693