equal
deleted
inserted
replaced
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 * @since 5.5.0 |
18 * @var array |
18 * @var array[] |
19 */ |
19 */ |
20 private $registered_categories = array(); |
20 private $registered_categories = array(); |
|
21 |
|
22 /** |
|
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(); |
21 |
29 |
22 /** |
30 /** |
23 * Container for the main instance of the class. |
31 * Container for the main instance of the class. |
24 * |
32 * |
25 * @since 5.5.0 |
33 * @since 5.5.0 |
31 * Registers a pattern category. |
39 * Registers a pattern category. |
32 * |
40 * |
33 * @since 5.5.0 |
41 * @since 5.5.0 |
34 * |
42 * |
35 * @param string $category_name Pattern category name including namespace. |
43 * @param string $category_name Pattern category name including namespace. |
36 * @param array $category_properties Array containing the properties of the category: label. |
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 * } |
37 * @return bool True if the pattern was registered with success and false otherwise. |
49 * @return bool True if the pattern was registered with success and false otherwise. |
38 */ |
50 */ |
39 public function register( $category_name, $category_properties ) { |
51 public function register( $category_name, $category_properties ) { |
40 if ( ! isset( $category_name ) || ! is_string( $category_name ) ) { |
52 if ( ! isset( $category_name ) || ! is_string( $category_name ) ) { |
41 _doing_it_wrong( |
53 _doing_it_wrong( |
44 '5.5.0' |
56 '5.5.0' |
45 ); |
57 ); |
46 return false; |
58 return false; |
47 } |
59 } |
48 |
60 |
49 $this->registered_categories[ $category_name ] = array_merge( |
61 $category = array_merge( |
50 array( 'name' => $category_name ), |
62 array( 'name' => $category_name ), |
51 $category_properties |
63 $category_properties |
52 ); |
64 ); |
|
65 |
|
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 } |
53 |
74 |
54 return true; |
75 return true; |
55 } |
76 } |
56 |
77 |
57 /** |
78 /** |
72 ); |
93 ); |
73 return false; |
94 return false; |
74 } |
95 } |
75 |
96 |
76 unset( $this->registered_categories[ $category_name ] ); |
97 unset( $this->registered_categories[ $category_name ] ); |
|
98 unset( $this->registered_categories_outside_init[ $category_name ] ); |
77 |
99 |
78 return true; |
100 return true; |
79 } |
101 } |
80 |
102 |
81 /** |
103 /** |
97 /** |
119 /** |
98 * Retrieves all registered pattern categories. |
120 * Retrieves all registered pattern categories. |
99 * |
121 * |
100 * @since 5.5.0 |
122 * @since 5.5.0 |
101 * |
123 * |
102 * @return array Array of arrays containing the registered pattern categories properties. |
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. |
103 */ |
126 */ |
104 public function get_all_registered() { |
127 public function get_all_registered( $outside_init_only = false ) { |
105 return array_values( $this->registered_categories ); |
128 return array_values( |
|
129 $outside_init_only |
|
130 ? $this->registered_categories_outside_init |
|
131 : $this->registered_categories |
|
132 ); |
106 } |
133 } |
107 |
134 |
108 /** |
135 /** |
109 * Checks if a pattern category is registered. |
136 * Checks if a pattern category is registered. |
110 * |
137 * |
139 * Registers a new pattern category. |
166 * Registers a new pattern category. |
140 * |
167 * |
141 * @since 5.5.0 |
168 * @since 5.5.0 |
142 * |
169 * |
143 * @param string $category_name Pattern category name including namespace. |
170 * @param string $category_name Pattern category name including namespace. |
144 * @param array $category_properties Array containing the properties of the category. |
171 * @param array $category_properties List of properties for the block pattern. |
|
172 * See WP_Block_Pattern_Categories_Registry::register() for |
|
173 * accepted arguments. |
145 * @return bool True if the pattern category was registered with success and false otherwise. |
174 * @return bool True if the pattern category was registered with success and false otherwise. |
146 */ |
175 */ |
147 function register_block_pattern_category( $category_name, $category_properties ) { |
176 function register_block_pattern_category( $category_name, $category_properties ) { |
148 return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties ); |
177 return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties ); |
149 } |
178 } |