CMB2_Boxes

A CMB2 object instance registry for storing every CMB2 instance.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_Boxes.php  
  1. class CMB2_Boxes { 
  2.  
  3. /** 
  4. * Array of all metabox objects. 
  5. * @since 2.0.0 
  6. * @var array 
  7. */ 
  8. protected static $cmb2_instances = array(); 
  9.  
  10. /** 
  11. * Add a CMB2 instance object to the registry. 
  12. * @since 1.X.X 
  13. * @param CMB2 $cmb_instance CMB2 instance. 
  14. */ 
  15. public static function add( CMB2 $cmb_instance ) { 
  16. self::$cmb2_instances[ $cmb_instance->cmb_id ] = $cmb_instance; 
  17.  
  18. /** 
  19. * Remove a CMB2 instance object to the registry. 
  20. * @since 1.X.X 
  21. * @param string $cmb_id A CMB2 instance id. 
  22. */ 
  23. public static function remove( $cmb_id ) { 
  24. if ( array_key_exists( $cmb_id, self::$cmb2_instances ) ) { 
  25. unset( self::$cmb2_instances[ $cmb_id ] ); 
  26.  
  27. /** 
  28. * Retrieve a CMB2 instance by cmb id. 
  29. * @since 1.X.X 
  30. * @param string $cmb_id A CMB2 instance id. 
  31. * @return CMB2|bool False or CMB2 object instance. 
  32. */ 
  33. public static function get( $cmb_id ) { 
  34. if ( empty( self::$cmb2_instances ) || empty( self::$cmb2_instances[ $cmb_id ] ) ) { 
  35. return false; 
  36.  
  37. return self::$cmb2_instances[ $cmb_id ]; 
  38.  
  39. /** 
  40. * Retrieve all CMB2 instances registered. 
  41. * @since 1.X.X 
  42. * @return CMB2[] Array of all registered cmb2 instances. 
  43. */ 
  44. public static function get_all() { 
  45. return self::$cmb2_instances; 
  46.  
  47. /** 
  48. * Retrieve all CMB2 instances that have the specified property set. 
  49. * @since 2.2.3 
  50. * @param string $property Property name. 
  51. * @param mixed $ignore The value to ignore. 
  52. * @return CMB2[] Array of matching cmb2 instances. 
  53. */ 
  54. public static function get_by_property( $property, $ignore = null ) { 
  55. $by_property[ $property ] = array(); 
  56.  
  57. foreach ( self::$cmb2_instances as $cmb_id => $cmb ) { 
  58.  
  59. if ( $ignore === $cmb->prop( $property ) ) { 
  60. continue; 
  61.  
  62. $by_property[ $property ][ $cmb_id ] = $cmb; 
  63.  
  64. return $by_property[ $property ]; 
  65.