Completed
Push — trunk ( 61c9c4...270210 )
by Justin
22:27 queued 16:53
created

CMB2_hookup::term_metabox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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