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