Completed
Pull Request — trunk (#541)
by Justin
06:39
created

CMB2_hookup::save_comment()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 12
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
	 * Array of all hooks done (to be run once)
18
	 * @var   array
19
	 * @since 2.0.0
20
	 */
21
	protected static $hooks_completed = array();
22
23
	/**
24
	 * Only allow JS registration once
25
	 * @var   bool
26
	 * @since 2.0.7
27
	 */
28
	protected static $js_registration_done = false;
29
30
	/**
31
	 * Only allow CSS registration once
32
	 * @var   bool
33
	 * @since 2.0.7
34
	 */
35
	protected static $css_registration_done = false;
36
37
	/**
38
	 * CMB taxonomies array for term meta
39
	 * @var   array
40
	 * @since 2.2.0
41
	 */
42
	protected $taxonomies = 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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cmb->mb_object_type() can also be of type false. However, the property $object_type is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
	}
53
54
	public function universal_hooks() {
55
56
		foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) {
57
			add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 );
58
		}
59
60
		if ( is_admin() ) {
61
			// register our scripts and styles for cmb
62
			$this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 );
63
			$this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) );
64
65
			switch ( $this->object_type ) {
66
				case 'post':
67
					return $this->post_hooks();
68
				case 'comment':
69
					return $this->comment_hooks();
70
				case 'user':
71
					return $this->user_hooks();
72
				case 'term':
73
					return $this->term_hooks();
74
			}
75
76
		}
77
	}
78
79
	public function post_hooks() {
80
		add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
81
		add_action( 'add_attachment', array( $this, 'save_post' ) );
82
		add_action( 'edit_attachment', array( $this, 'save_post' ) );
83
		add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
84
	}
85
86
	public function comment_hooks() {
87
		add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) );
88
		add_action( 'edit_comment', array( $this, 'save_comment' ) );
89
	}
90
91
	public function user_hooks() {
92
		$priority = $this->get_priority();
93
94
		add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority );
95
		add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority );
96
		add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority );
97
98
		add_action( 'personal_options_update', array( $this, 'save_user' ) );
99
		add_action( 'edit_user_profile_update', array( $this, 'save_user' ) );
100
		add_action( 'user_register', array( $this, 'save_user' ) );
101
	}
102
103
	public function term_hooks() {
104
		if ( ! function_exists( 'get_term_meta' ) ) {
105
			wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) );
106
		}
107
108
		if ( ! $this->cmb->prop( 'taxonomies' ) ) {
109
			wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) );
110
		}
111
112
		$this->taxonomies = (array) $this->cmb->prop( 'taxonomies' );
113
		$show_on_term_add = $this->cmb->prop( 'new_term_section' );
114
		$priority         = $this->get_priority( 8 );
115
116
		foreach ( $this->taxonomies as $taxonomy ) {
117
			// Display our form data
118
			add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 );
119
120
			$show_on_add = is_array( $show_on_term_add )
121
				? in_array( $taxonomy, $show_on_term_add )
122
				: (bool) $show_on_term_add;
123
124
			$show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb );
125
126
			// Display form in add-new section (unless specified not to)
127
			if ( $show_on_add ) {
128
				add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 );
129
			}
130
131
		}
132
133
		add_action( 'created_term', array( $this, 'save_term' ), 10, 3 );
134
		add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 );
135
		add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 );
136
	}
137
138
	/**
139
	 * Registers styles for CMB2
140
	 * @since 2.0.7
141
	 */
142 1
	protected static function register_styles() {
143 1
		if ( self::$css_registration_done ) {
144
			return;
145
		}
146
147
		// Only use minified files if SCRIPT_DEBUG is off
148 1
		$min   = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
149 1
		$front = is_admin() ? '' : '-front';
150 1
		$rtl = is_rtl() ? '-rtl' : '';
151
152
		// Filter required styles and register stylesheet
153 1
		$styles = apply_filters( 'cmb2_style_dependencies', array() );
154 1
		wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $styles );
155
156 1
		self::$css_registration_done = true;
157 1
	}
158
159
	/**
160
	 * Registers scripts for CMB2
161
	 * @since  2.0.7
162
	 */
163 1
	protected static function register_js() {
164 1
		if ( self::$js_registration_done ) {
165
			return;
166
		}
167
168 1
		$hook = is_admin() ? 'admin_footer' : 'wp_footer';
169 1
		add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 );
170
171 1
		self::$js_registration_done = true;
172 1
	}
173
174
	/**
175
	 * Registers scripts and styles for CMB2
176
	 * @since  1.0.0
177
	 */
178
	public static function register_scripts() {
179
		self::register_styles();
180
		self::register_js();
181
	}
182
183
	/**
184
	 * Enqueues scripts and styles for CMB2 in admin_head.
185
	 * @since  1.0.0
186
	 */
187
	public function do_scripts( $hook ) {
188
		$hooks = array(
189
			'post.php',
190
			'post-new.php',
191
			'page-new.php',
192
			'page.php',
193
			'comment.php',
194
			'edit-tags.php',
195
			'term.php',
196
			'user-new.php',
197
			'profile.php',
198
			'user-edit.php',
199
		);
200
		// only pre-enqueue our scripts/styles on the proper pages
201
		// show_form_for_type will have us covered if we miss something here.
202
		if ( in_array( $hook, $hooks, true ) ) {
203
			if ( $this->cmb->prop( 'cmb_styles' ) ) {
204
				self::enqueue_cmb_css();
205
			}
206
			if ( $this->cmb->prop( 'enqueue_js' ) ) {
207
				self::enqueue_cmb_js();
208
			}
209
		}
210
	}
211
212
	/**
213
	 * Add metaboxes (to 'post' or 'comment' object types)
214
	 * @since 1.0.0
215
	 */
216
	public function add_metaboxes() {
217
218
		if ( ! $this->show_on() ) {
219
			return;
220
		}
221
222
		foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) {
223
			/**
224
			 * To keep from registering an actual post-screen metabox,
225
			 * omit the 'title' attribute from the metabox registration array.
226
			 *
227
			 * (WordPress will not display metaboxes without titles anyway)
228
			 *
229
			 * This is a good solution if you want to output your metaboxes
230
			 * Somewhere else in the post-screen
231
			 */
232
			if ( $this->cmb->prop( 'title' ) ) {
233
234
				if ( $this->cmb->prop( 'closed' ) ) {
235
					add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) );
236
				}
237
238
				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' ) );
239
			}
240
		}
241
	}
242
243
	/**
244
	 * Add 'closed' class to metabox
245
	 * @since  2.0.0
246
	 * @param  array  $classes Array of classes
247
	 * @return array           Modified array of classes
248
	 */
249
	public function close_metabox_class( $classes ) {
250
		$classes[] = 'closed';
251
		return $classes;
252
	}
253
254
	/**
255
	 * Display metaboxes for a post or comment object
256
	 * @since  1.0.0
257
	 */
258
	public function metabox_callback() {
259
		$object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID();
260
		$this->cmb->show_form( $object_id, $this->object_type );
261
	}
262
263
	/**
264
	 * Display metaboxes for new user page
265
	 * @since  1.0.0
266
	 */
267
	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...
268
		if ( $section == $this->cmb->prop( 'new_user_section' ) ) {
269
			$object_id = $this->cmb->object_id();
270
			$this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id );
271
			$this->user_metabox();
272
		}
273
	}
274
275
	/**
276
	 * Display metaboxes for a user object
277
	 * @since  1.0.0
278
	 */
279
	public function user_metabox() {
280
		$this->show_form_for_type( 'user' );
281
	}
282
283
	/**
284
	 * Display metaboxes for a taxonomy term object
285
	 * @since  2.2.0
286
	 */
287
	public function term_metabox() {
288
		$this->show_form_for_type( 'term' );
289
	}
290
291
	/**
292
	 * Display metaboxes for an object type
293
	 * @since  2.2.0
294
	 * @param  string $type Object type
295
	 * @return void
296
	 */
297
	public function show_form_for_type( $type ) {
298
		if ( $type != $this->cmb->mb_object_type() ) {
299
			return;
300
		}
301
302
		if ( ! $this->show_on() ) {
303
			return;
304
		}
305
306
		if ( $this->cmb->prop( 'cmb_styles' ) ) {
307
			self::enqueue_cmb_css();
308
		}
309
		if ( $this->cmb->prop( 'enqueue_js' ) ) {
310
			self::enqueue_cmb_js();
311
		}
312
313
		$this->cmb->show_form( 0, $type );
314
	}
315
316
	/**
317
	 * Determines if metabox should be shown in current context
318
	 * @since  2.0.0
319
	 * @return bool Whether metabox should be added/shown
320
	 */
321
	public function show_on() {
322
		// If metabox is requesting to be conditionally shown
323
		$show = $this->cmb->should_show();
324
325
		/**
326
		 * Filter to determine if metabox should show. Default is true
327
		 *
328
		 * @param array  $show          Default is true, show the metabox
329
		 * @param mixed  $meta_box_args Array of the metabox arguments
330
		 * @param mixed  $cmb           The CMB2 instance
331
		 */
332
		$show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb );
333
334
		return $show;
335
	}
336
337
	/**
338
	 * Get the CMB priority property set to numeric hook priority.
339
	 * @since  2.2.0
340
	 * @param  integer $default Default display hook priority.
341
	 * @return integer          Hook priority.
342
	 */
343
	public function get_priority( $default = 10 ) {
344
		$priority = $this->cmb->prop( 'priority' );
345
346
		if ( ! is_numeric( $priority ) ) {
347
			switch ( $priority ) {
348
349
				case 'high':
350
					$priority = 5;
351
					break;
352
353
				case 'low':
354
					$priority = 20;
355
					break;
356
357
				default:
358
					$priority = $default;
359
					break;
360
			}
361
		}
362
363
		return $priority;
364
	}
365
366
	/**
367
	 * Save data from post metabox
368
	 * @since  1.0.0
369
	 * @param  int    $post_id Post ID
370
	 * @param  mixed  $post    Post object
371
	 * @return null
372
	 */
373
	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...
374
375
		$post_type = $post ? $post->post_type : get_post_type( $post_id );
376
377
		$do_not_pass_go = (
378
			! $this->can_save( $post_type )
379
			// check user editing permissions
380
			|| ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) )
381
			|| ! current_user_can( 'edit_post', $post_id )
382
		);
383
384
		if ( $do_not_pass_go ) {
385
			// do not collect $200
386
			return;
387
		}
388
389
		// take a trip to reading railroad – if you pass go collect $200
390
		$this->cmb->save_fields( $post_id, 'post', $_POST );
391
	}
392
393
	/**
394
	 * Save data from comment metabox
395
	 * @since  2.0.9
396
	 * @param  int    $comment_id Comment ID
397
	 * @return null
398
	 */
399
	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...
400
401
		$can_edit = current_user_can( 'moderate_comments', $comment_id );
402
403
		if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) {
404
			$this->cmb->save_fields( $comment_id, 'comment', $_POST );
405
		}
406
	}
407
408
	/**
409
	 * Save data from user fields
410
	 * @since  1.0.x
411
	 * @param  int   $user_id  User ID
412
	 * @return null
413
	 */
414
	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...
415
		// check permissions
416
		if ( $this->can_save( 'user' ) ) {
417
			$this->cmb->save_fields( $user_id, 'user', $_POST );
418
		}
419
	}
420
421
	/**
422
	 * Save data from term fields
423
	 * @since  2.2.0
424
	 * @param  int    $term_id  Term ID
425
	 * @param  int    $tt_id    Term Taxonomy ID
426
	 * @param  string $taxonomy Taxonomy
427
	 * @return null
428
	 */
429
	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...
430
		$taxonomy = $taxonomy ? $taxonomy : $tt_id;
431
432
		// check permissions
433
		if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) {
434
			$this->cmb->save_fields( $term_id, 'term', $_POST );
435
		}
436
	}
437
438
	/**
439
	 * Delete term meta when a term is deleted.
440
	 * @since  2.2.0
441
	 * @param  int    $term_id  Term ID
442
	 * @param  int    $tt_id    Term Taxonomy ID
443
	 * @param  string $taxonomy Taxonomy
444
	 * @return null
445
	 */
446
	public function delete_term( $term_id, $tt_id, $taxonomy = '' ) {
447
		if ( $this->taxonomy_can_save( $taxonomy ) ) {
448
449
			foreach ( $this->cmb->prop( 'fields' ) as $field ) {
450
				$data_to_delete[ $field['id'] ] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data_to_delete was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data_to_delete = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
451
			}
452
453
			$this->cmb->save_fields( $term_id, 'term', $data_to_delete );
0 ignored issues
show
Bug introduced by
The variable $data_to_delete does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
454
		}
455
	}
456
457
	/**
458
	 * Determines if the current object is able to be saved
459
	 * @since  2.0.9
460
	 * @param  string  $type Current post_type or comment_type
461
	 * @return bool          Whether object can be saved
462
	 */
463
	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...
464
		return (
465
			$this->cmb->prop( 'save_fields' )
466
			// check nonce
467
			&& isset( $_POST[ $this->cmb->nonce() ] )
468
			&& wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() )
469
			// check if autosave
470
			&& ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
471
			// get the metabox types & compare it to this type
472
			&& ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) )
473
		);
474
	}
475
476
	/**
477
	 * Determine if taxonomy of term being modified is cmb2-editable.
478
	 * @since  2.2.0
479
	 * @param  string $taxonomy Taxonomy of term being modified.
480
	 * @return bool             Whether taxonomy is editable.
481
	 */
482
	public function taxonomy_can_save( $taxonomy ) {
483
		if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) {
484
			return false;
485
		}
486
487
		$taxonomy_object = get_taxonomy( $taxonomy );
488
		// Can the user edit this term?
489
		if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
490
			return false;
491
		}
492
493
		return true;
494
	}
495
496
	/**
497
	 * Includes CMB2 styles
498
	 * @since  2.0.0
499
	 */
500 1
	public static function enqueue_cmb_css() {
501 1
		if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) {
502
			return false;
503
		}
504
505 1
		self::register_styles();
506 1
		return wp_enqueue_style( 'cmb2-styles' );
507
	}
508
509
	/**
510
	 * Includes CMB2 JS
511
	 * @since  2.0.0
512
	 */
513 1
	public static function enqueue_cmb_js() {
514 1
		if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) {
515
			return false;
516
		}
517
518 1
		self::register_js();
519 1
		return true;
520
	}
521
522
}
523