wp/wp-includes/class-wp-block-pattern-categories-registry.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    12  */
    12  */
    13 final class WP_Block_Pattern_Categories_Registry {
    13 final class WP_Block_Pattern_Categories_Registry {
    14 	/**
    14 	/**
    15 	 * Registered block pattern categories array.
    15 	 * Registered block pattern categories array.
    16 	 *
    16 	 *
       
    17 	 * @since 5.5.0
    17 	 * @var array
    18 	 * @var array
    18 	 */
    19 	 */
    19 	private $registered_categories = array();
    20 	private $registered_categories = array();
    20 
    21 
    21 	/**
    22 	/**
    22 	 * Container for the main instance of the class.
    23 	 * Container for the main instance of the class.
    23 	 *
    24 	 *
       
    25 	 * @since 5.5.0
    24 	 * @var WP_Block_Pattern_Categories_Registry|null
    26 	 * @var WP_Block_Pattern_Categories_Registry|null
    25 	 */
    27 	 */
    26 	private static $instance = null;
    28 	private static $instance = null;
    27 
    29 
    28 	/**
    30 	/**
    29 	 * Registers a pattern category.
    31 	 * Registers a pattern category.
    30 	 *
    32 	 *
    31 	 * @since 5.5.0
    33 	 * @since 5.5.0
    32 	 *
    34 	 *
    33 	 * @param string $category_name       Pattern category name.
    35 	 * @param string $category_name       Pattern category name including namespace.
    34 	 * @param array  $category_properties Array containing the properties of the category: label.
    36 	 * @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.
    37 	 * @return bool True if the pattern was registered with success and false otherwise.
    36 	 */
    38 	 */
    37 	public function register( $category_name, $category_properties ) {
    39 	public function register( $category_name, $category_properties ) {
    38 		if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
    40 		if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
    39 			_doing_it_wrong( __METHOD__, __( 'Block pattern category name must be a string.' ), '5.5.0' );
    41 			_doing_it_wrong(
       
    42 				__METHOD__,
       
    43 				__( 'Block pattern category name must be a string.' ),
       
    44 				'5.5.0'
       
    45 			);
    40 			return false;
    46 			return false;
    41 		}
    47 		}
    42 
    48 
    43 		$this->registered_categories[ $category_name ] = array_merge(
    49 		$this->registered_categories[ $category_name ] = array_merge(
    44 			array( 'name' => $category_name ),
    50 			array( 'name' => $category_name ),
    51 	/**
    57 	/**
    52 	 * Unregisters a pattern category.
    58 	 * Unregisters a pattern category.
    53 	 *
    59 	 *
    54 	 * @since 5.5.0
    60 	 * @since 5.5.0
    55 	 *
    61 	 *
    56 	 * @param string $category_name     Pattern name including namespace.
    62 	 * @param string $category_name Pattern category name including namespace.
    57 	 * @return bool True if the pattern was unregistered with success and false otherwise.
    63 	 * @return bool True if the pattern was unregistered with success and false otherwise.
    58 	 */
    64 	 */
    59 	public function unregister( $category_name ) {
    65 	public function unregister( $category_name ) {
    60 		if ( ! $this->is_registered( $category_name ) ) {
    66 		if ( ! $this->is_registered( $category_name ) ) {
    61 			/* translators: 1: Block pattern name. */
    67 			_doing_it_wrong(
    62 			$message = sprintf( __( 'Block pattern category "%1$s" not found.' ), $category_name );
    68 				__METHOD__,
    63 			_doing_it_wrong( __METHOD__, $message, '5.5.0' );
    69 				/* translators: %s: Block pattern name. */
       
    70 				sprintf( __( 'Block pattern category "%s" not found.' ), $category_name ),
       
    71 				'5.5.0'
       
    72 			);
    64 			return false;
    73 			return false;
    65 		}
    74 		}
    66 
    75 
    67 		unset( $this->registered_categories[ $category_name ] );
    76 		unset( $this->registered_categories[ $category_name ] );
    68 
    77 
    72 	/**
    81 	/**
    73 	 * Retrieves an array containing the properties of a registered pattern category.
    82 	 * Retrieves an array containing the properties of a registered pattern category.
    74 	 *
    83 	 *
    75 	 * @since 5.5.0
    84 	 * @since 5.5.0
    76 	 *
    85 	 *
    77 	 * @param string $category_name Pattern category name.
    86 	 * @param string $category_name Pattern category name including namespace.
    78 	 * @return array Registered pattern properties.
    87 	 * @return array Registered pattern properties.
    79 	 */
    88 	 */
    80 	public function get_registered( $category_name ) {
    89 	public function get_registered( $category_name ) {
    81 		if ( ! $this->is_registered( $category_name ) ) {
    90 		if ( ! $this->is_registered( $category_name ) ) {
    82 			return null;
    91 			return null;
    99 	/**
   108 	/**
   100 	 * Checks if a pattern category is registered.
   109 	 * Checks if a pattern category is registered.
   101 	 *
   110 	 *
   102 	 * @since 5.5.0
   111 	 * @since 5.5.0
   103 	 *
   112 	 *
   104 	 * @param string $category_name       Pattern category name.
   113 	 * @param string $category_name Pattern category name including namespace.
   105 	 * @return bool True if the pattern category is registered, false otherwise.
   114 	 * @return bool True if the pattern category is registered, false otherwise.
   106 	 */
   115 	 */
   107 	public function is_registered( $category_name ) {
   116 	public function is_registered( $category_name ) {
   108 		return isset( $this->registered_categories[ $category_name ] );
   117 		return isset( $this->registered_categories[ $category_name ] );
   109 	}
   118 	}
   129 /**
   138 /**
   130  * Registers a new pattern category.
   139  * Registers a new pattern category.
   131  *
   140  *
   132  * @since 5.5.0
   141  * @since 5.5.0
   133  *
   142  *
   134  * @param string $category_name       Pattern category name.
   143  * @param string $category_name       Pattern category name including namespace.
   135  * @param array  $category_properties Array containing the properties of the category.
   144  * @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.
   145  * @return bool True if the pattern category was registered with success and false otherwise.
   137  */
   146  */
   138 function register_block_pattern_category( $category_name, $category_properties ) {
   147 function register_block_pattern_category( $category_name, $category_properties ) {
   139 	return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
   148 	return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
   142 /**
   151 /**
   143  * Unregisters a pattern category.
   152  * Unregisters a pattern category.
   144  *
   153  *
   145  * @since 5.5.0
   154  * @since 5.5.0
   146  *
   155  *
   147  * @param string $category_name       Pattern category name including namespace.
   156  * @param string $category_name Pattern category name including namespace.
   148  * @return bool True if the pattern category was unregistered with success and false otherwise.
   157  * @return bool True if the pattern category was unregistered with success and false otherwise.
   149  */
   158  */
   150 function unregister_block_pattern_category( $category_name ) {
   159 function unregister_block_pattern_category( $category_name ) {
   151 	return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
   160 	return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
   152 }
   161 }