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