wp/wp-includes/class-wp-block-pattern-categories-registry.php
changeset 16 a86126ab1dd4
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
       
     1 <?php
       
     2 /**
       
     3  * Blocks API: WP_Block_Pattern_Categories_Registry class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Blocks
       
     7  * @since 5.5.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Class used for interacting with block pattern categories.
       
    12  */
       
    13 final class WP_Block_Pattern_Categories_Registry {
       
    14 	/**
       
    15 	 * Registered block pattern categories array.
       
    16 	 *
       
    17 	 * @var array
       
    18 	 */
       
    19 	private $registered_categories = array();
       
    20 
       
    21 	/**
       
    22 	 * Container for the main instance of the class.
       
    23 	 *
       
    24 	 * @var WP_Block_Pattern_Categories_Registry|null
       
    25 	 */
       
    26 	private static $instance = null;
       
    27 
       
    28 	/**
       
    29 	 * Registers a pattern category.
       
    30 	 *
       
    31 	 * @since 5.5.0
       
    32 	 *
       
    33 	 * @param string $category_name       Pattern category name.
       
    34 	 * @param array  $category_properties Array containing the properties of the category: label.
       
    35 	 * @return bool True if the pattern was registered with success and false otherwise.
       
    36 	 */
       
    37 	public function register( $category_name, $category_properties ) {
       
    38 		if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
       
    39 			_doing_it_wrong( __METHOD__, __( 'Block pattern category name must be a string.' ), '5.5.0' );
       
    40 			return false;
       
    41 		}
       
    42 
       
    43 		$this->registered_categories[ $category_name ] = array_merge(
       
    44 			array( 'name' => $category_name ),
       
    45 			$category_properties
       
    46 		);
       
    47 
       
    48 		return true;
       
    49 	}
       
    50 
       
    51 	/**
       
    52 	 * Unregisters a pattern category.
       
    53 	 *
       
    54 	 * @since 5.5.0
       
    55 	 *
       
    56 	 * @param string $category_name     Pattern name including namespace.
       
    57 	 * @return bool True if the pattern was unregistered with success and false otherwise.
       
    58 	 */
       
    59 	public function unregister( $category_name ) {
       
    60 		if ( ! $this->is_registered( $category_name ) ) {
       
    61 			/* translators: 1: Block pattern name. */
       
    62 			$message = sprintf( __( 'Block pattern category "%1$s" not found.' ), $category_name );
       
    63 			_doing_it_wrong( __METHOD__, $message, '5.5.0' );
       
    64 			return false;
       
    65 		}
       
    66 
       
    67 		unset( $this->registered_categories[ $category_name ] );
       
    68 
       
    69 		return true;
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * Retrieves an array containing the properties of a registered pattern category.
       
    74 	 *
       
    75 	 * @since 5.5.0
       
    76 	 *
       
    77 	 * @param string $category_name Pattern category name.
       
    78 	 * @return array Registered pattern properties.
       
    79 	 */
       
    80 	public function get_registered( $category_name ) {
       
    81 		if ( ! $this->is_registered( $category_name ) ) {
       
    82 			return null;
       
    83 		}
       
    84 
       
    85 		return $this->registered_categories[ $category_name ];
       
    86 	}
       
    87 
       
    88 	/**
       
    89 	 * Retrieves all registered pattern categories.
       
    90 	 *
       
    91 	 * @since 5.5.0
       
    92 	 *
       
    93 	 * @return array Array of arrays containing the registered pattern categories properties.
       
    94 	 */
       
    95 	public function get_all_registered() {
       
    96 		return array_values( $this->registered_categories );
       
    97 	}
       
    98 
       
    99 	/**
       
   100 	 * Checks if a pattern category is registered.
       
   101 	 *
       
   102 	 * @since 5.5.0
       
   103 	 *
       
   104 	 * @param string $category_name       Pattern category name.
       
   105 	 * @return bool True if the pattern category is registered, false otherwise.
       
   106 	 */
       
   107 	public function is_registered( $category_name ) {
       
   108 		return isset( $this->registered_categories[ $category_name ] );
       
   109 	}
       
   110 
       
   111 	/**
       
   112 	 * Utility method to retrieve the main instance of the class.
       
   113 	 *
       
   114 	 * The instance will be created if it does not exist yet.
       
   115 	 *
       
   116 	 * @since 5.5.0
       
   117 	 *
       
   118 	 * @return WP_Block_Pattern_Categories_Registry The main instance.
       
   119 	 */
       
   120 	public static function get_instance() {
       
   121 		if ( null === self::$instance ) {
       
   122 			self::$instance = new self();
       
   123 		}
       
   124 
       
   125 		return self::$instance;
       
   126 	}
       
   127 }
       
   128 
       
   129 /**
       
   130  * Registers a new pattern category.
       
   131  *
       
   132  * @since 5.5.0
       
   133  *
       
   134  * @param string $category_name       Pattern category name.
       
   135  * @param array  $category_properties Array containing the properties of the category.
       
   136  * @return bool True if the pattern category was registered with success and false otherwise.
       
   137  */
       
   138 function register_block_pattern_category( $category_name, $category_properties ) {
       
   139 	return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
       
   140 }
       
   141 
       
   142 /**
       
   143  * Unregisters a pattern category.
       
   144  *
       
   145  * @since 5.5.0
       
   146  *
       
   147  * @param string $category_name       Pattern category name including namespace.
       
   148  * @return bool True if the pattern category was unregistered with success and false otherwise.
       
   149  */
       
   150 function unregister_block_pattern_category( $category_name ) {
       
   151 	return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
       
   152 }