Completed
Pull Request — trunk (#541)
by Justin
07:44
created

CMB2_Hookup_Base   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 49
ccs 0
cts 11
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
universal_hooks() 0 1 ?
A once() 0 10 2
1
<?php
2
/**
3
 * Base class for hooking CMB2 into WordPress.
4
 *
5
 * @since  2.2.0
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
abstract class 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...
14
15
	/**
16
	 * Array of all hooks done (to be run once)
17
	 * @var   array
18
	 * @since 2.0.0
19
	 */
20
	protected static $hooks_completed = array();
21
22
	/**
23
	 * @var   CMB2 object
24
	 * @since 2.0.2
25
	 */
26
	protected $cmb;
27
28
	/**
29
	 * The object type we are performing the hookup for
30
	 * @var   string
31
	 * @since 2.0.9
32
	 */
33
	protected $object_type = 'post';
34
35
	public function __construct( CMB2 $cmb ) {
36
		$this->cmb = $cmb;
37
		$this->object_type = $this->cmb->mb_object_type();
38
	}
39
40
	abstract public function universal_hooks();
41
42
	/**
43
	 * Ensures WordPress hook only gets fired once
44
	 * @since  2.0.0
45
	 * @param string   $action        The name of the filter to hook the $hook callback to.
46
	 * @param callback $hook          The callback to be run when the filter is applied.
47
	 * @param integer  $priority      Order the functions are executed
48
	 * @param int      $accepted_args The number of arguments the function accepts.
49
	 */
50
	public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) {
51
		$key = md5( serialize( func_get_args() ) );
52
53
		if ( in_array( $key, self::$hooks_completed ) ) {
54
			return;
55
		}
56
57
		self::$hooks_completed[] = $key;
58
		add_filter( $action, $hook, $priority, $accepted_args );
59
	}
60
61
}
62