forked from CMB2/CMB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMB2_Boxes.php
139 lines (121 loc) · 3.16 KB
/
CMB2_Boxes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* A CMB2 object instance registry for storing every CMB2 instance.
*
* @category WordPress_Plugin
* @package CMB2
* @author CMB2 team
* @license GPL-2.0+
* @link https://cmb2.io
*/
class CMB2_Boxes {
/**
* Array of all metabox objects.
*
* @since 2.0.0
* @var array
*/
protected static $cmb2_instances = array();
/**
* Add a CMB2 instance object to the registry.
*
* @since 1.X.X
*
* @param CMB2 $cmb_instance CMB2 instance.
*/
public static function add( CMB2 $cmb_instance ) {
self::$cmb2_instances[ $cmb_instance->cmb_id ] = $cmb_instance;
}
/**
* Remove a CMB2 instance object from the registry.
*
* @since 1.X.X
*
* @param string $cmb_id A CMB2 instance id.
*/
public static function remove( $cmb_id ) {
if ( array_key_exists( $cmb_id, self::$cmb2_instances ) ) {
unset( self::$cmb2_instances[ $cmb_id ] );
}
}
/**
* Retrieve a CMB2 instance by cmb id.
*
* @since 1.X.X
*
* @param string $cmb_id A CMB2 instance id.
*
* @return CMB2|bool False or CMB2 object instance.
*/
public static function get( $cmb_id ) {
if ( empty( self::$cmb2_instances ) || empty( self::$cmb2_instances[ $cmb_id ] ) ) {
return false;
}
return self::$cmb2_instances[ $cmb_id ];
}
/**
* Retrieve all CMB2 instances registered.
*
* @since 1.X.X
* @return CMB2[] Array of all registered cmb2 instances.
*/
public static function get_all() {
return self::$cmb2_instances;
}
/**
* Retrieve all CMB2 instances that have the specified property set.
*
* @since 2.4.0
* @param string $property Property name.
* @param mixed $compare (Optional) The value to compare.
* @return CMB2[] Array of matching cmb2 instances.
*/
public static function get_by( $property, $compare = 'nocompare' ) {
$boxes = array();
foreach ( self::$cmb2_instances as $cmb_id => $cmb ) {
$prop = $cmb->prop( $property );
if ( 'nocompare' === $compare ) {
if ( ! empty( $prop ) ) {
$boxes[ $cmb_id ] = $cmb;
}
continue;
}
if ( $compare === $prop ) {
$boxes[ $cmb_id ] = $cmb;
}
}
return $boxes;
}
/**
* Retrieve all CMB2 instances as long as they do not include the ignored property.
*
* @since 2.4.0
* @param string $property Property name.
* @param mixed $to_ignore The value to ignore.
* @return CMB2[] Array of matching cmb2 instances.
*/
public static function filter_by( $property, $to_ignore = null ) {
$boxes = array();
foreach ( self::$cmb2_instances as $cmb_id => $cmb ) {
if ( $to_ignore === $cmb->prop( $property ) ) {
continue;
}
$boxes[ $cmb_id ] = $cmb;
}
return $boxes;
}
/**
* Deprecated and left for back-compatibility. The original `get_by_property`
* method was misnamed and never actually used by CMB2 core.
*
* @since 2.2.3
*
* @param string $property Property name.
* @param mixed $to_ignore The value to ignore.
* @return CMB2[] Array of matching cmb2 instances.
*/
public static function get_by_property( $property, $to_ignore = null ) {
_deprecated_function( __METHOD__, '2.4.0', 'CMB2_Boxes::filter_by()' );
return self::filter_by( $property );
}
}