CMB2_Hookup_Base::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	 * @var   CMB2 object
17
	 * @since 2.0.2
18
	 */
19
	protected $cmb;
20
21
	/**
22
	 * The object type we are performing the hookup for
23
	 *
24
	 * @var   string
25
	 * @since 2.0.9
26
	 */
27
	protected $object_type = 'post';
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @since 2.0.0
33
	 * @param CMB2 $cmb The CMB2 object to hookup
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 per object.
44
	 *
45
	 * @since  2.0.0
46
	 * @param string   $action        The name of the filter to hook the $hook callback to.
47
	 * @param callback $hook          The callback to be run when the filter is applied.
48
	 * @param integer  $priority      Order the functions are executed
49
	 * @param int      $accepted_args The number of arguments the function accepts.
50
	 */
51
	public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) {
52
		static $hooks_completed = array();
53
54
		$args = func_get_args();
55
56
		// Get object hash.. This bypasses issues with serializing closures.
57
		if ( is_object( $hook ) ) {
58
			$args[1] = spl_object_hash( $args[1] );
59
		} elseif ( is_array( $hook ) && is_object( $hook[0] ) ) {
60
			$args[1][0] = spl_object_hash( $hook[0] );
61
		}
62
63
		$key = md5( serialize( $args ) );
64
65
		if ( ! isset( $hooks_completed[ $key ] ) ) {
66
			$hooks_completed[ $key ] = 1;
67
			add_filter( $action, $hook, $priority, $accepted_args );
68
		}
69
	}
70
71
}
72