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