PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / trunk
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites vtrunk
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / inc / layout / registry.php

registry.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites trunk, at inc/layout/registry.php

406 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Layout Component Registry
4 *
5 * @package Blockspare
6 */
7
8 namespace Blockspare\Layouts;
9
10 use \Exception;
11 use \InvalidArgumentException;
12
13 /**
14 * Class Component_Registry
15 *
16 * @access private Not intended as a public API.
17 * Use the helper functions to manage layouts.
18 * This class will likely go away in the future.
19 */
20 final class Component_Registry {
21
22 /**
23 * Supported component types.
24 *
25 * @var array
26 */
27 private static $supported_component_types = [ 'layout', 'section','block','page','header','footer','templates' ];
28
29 /**
30 * Required keys for components.
31 *
32 * @var array
33 */
34 private static $required_data_keys = [ 'type', 'key', 'name', 'content', 'item' ];
35
36 /**
37 * Holds the registered layouts.
38 *
39 * @var array
40 */
41 private static $layouts = [];
42
43 /**
44 * Holds the registered sections.
45 *
46 * @var array
47 */
48 private static $sections = [];
49
50 /**
51 * Holds the registered blocks.
52 *
53 * @var array
54 */
55 private static $blocks = [];
56
57
58 /**
59 * Holds the registered pages.
60 *
61 * @var array
62 */
63 private static $pages = [];
64
65 /**
66 * Holds the registered headers.
67 *
68 * @var array
69 */
70 private static $headers = [];
71
72 /**
73 * Holds the registered footers.
74 *
75 * @var array
76 */
77 private static $footers = [];
78
79 /**
80 * Holds the registered templates.
81 *
82 * @var array
83 */
84 private static $templates = [];
85
86 /**
87 * The Component_Registry object.
88 *
89 * @var object Component_Registry
90 */
91 private static $instance;
92
93 /**
94 * Creates the Component_Registry instance.
95 *
96 * @return Component_Registry
97 */
98 public static function instance() {
99 if ( null === self::$instance ) {
100 self::$instance = new self();
101 }
102 return self::$instance;
103 }
104
105 /**
106 * Adds a component to the registry.
107 *
108 * @param array $data Component data.
109 * @throws InvalidArgumentException If invalid data is provided or the component is already registered.
110 */
111 public static function add( array $data ) {
112
113 if ( empty( $data['type'] ) || ! in_array( $data['type'], self::$supported_component_types, true ) ) {
114 throw new InvalidArgumentException( esc_html__( 'You must supply a valid component type.', 'blockspare' ) );
115 }
116
117 if ( empty( $data ) ) {
118 throw new InvalidArgumentException( __( 'You must supply valid layout data to register a layout.', 'blockspare' ) );
119 }
120
121 foreach ( self::$required_data_keys as $required_key ) {
122 if ( ! array_key_exists( $required_key, $data ) || empty( $data[ $required_key ] ) ) {
123 /* translators: %s: The missing key that is required to register a component. */
124 throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a %s to register a layout.', 'blockspare' ), $required_key ) );
125 }
126 }
127
128 switch ( $data['type'] ) {
129 case 'layout':
130 if ( ! empty( self::$layouts[ $data['key'] ] ) ) {
131 /* translators: %s: The component's unique key. */
132 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s layout is already registered.', 'blockspare' ), $data['key'] ) );
133 }
134 self::$layouts[ $data['key'] ] = $data;
135 break;
136
137 case 'section':
138 if ( ! empty( self::$sections[ $data['key'] ] ) ) {
139 /* translators: %s: The component's unique key. */
140 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
141 }
142 self::$sections[ $data['key'] ] = $data;
143 break;
144
145
146 case 'block':
147 if ( ! empty( self::$blocks[ $data['key'] ] ) ) {
148 /* translators: %s: The component's unique key. */
149 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
150 }
151 self::$blocks[ $data['key'] ] = $data;
152 break;
153
154 case 'page':
155 if ( ! empty( self::$pages[ $data['key'] ] ) ) {
156 /* translators: %s: The component's unique key. */
157 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
158 }
159 self::$pages[ $data['key'] ] = $data;
160 break;
161 case 'header':
162 if ( ! empty( self::$headers[ $data['key'] ] ) ) {
163 /* translators: %s: The component's unique key. */
164 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
165 }
166 self::$pages[ $data['key'] ] = $data;
167 break;
168 case 'footer':
169 if ( ! empty( self::$footers[ $data['key'] ] ) ) {
170 /* translators: %s: The component's unique key. */
171 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
172 }
173 self::$pages[ $data['key'] ] = $data;
174 break;
175
176 case 'templates':
177 if ( ! empty( self::$templates[ $data['key'] ] ) ) {
178 /* translators: %s: The component's unique key. */
179 throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) );
180 }
181 self::$pages[ $data['key'] ] = $data;
182 break;
183
184 default:
185 /* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::add(). */
186 throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) );
187 }
188 }
189
190 /**
191 * Removes an existing component from the registry.
192 *
193 * @param string $type The component type to unregister.
194 * @param string $key The unique layout key to be removed.
195 *
196 * @throws Exception If the required data is not provided or the specified component is not registered.
197 * @throws InvalidArgumentException If an unsupported component type is provided.
198 */
199 public static function remove( $type, $key ) {
200
201 if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) {
202 /* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */
203 throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) );
204 }
205
206 if ( empty( $key ) ) {
207 /* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */
208 throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component key in %s.', 'blockspare' ), __METHOD__ ) );
209 }
210
211 $key = sanitize_key( $key );
212
213 switch ( $type ) {
214 case 'layout':
215 if ( empty( self::$layouts[ $key ] ) ) {
216 /* translators: The requested components unique key. */
217 throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) );
218 }
219 unset( self::$layouts[ $key ] );
220 break;
221
222 case 'section':
223 if ( empty( self::$sections[ $key ] ) ) {
224 /* translators: The requested components unique key. */
225 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
226 }
227 unset( self::$sections[ $key ] );
228 break;
229
230 case 'block':
231 if ( empty( self::$blocks[ $key ] ) ) {
232 /* translators: The requested components unique key. */
233 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
234 }
235 unset( self::$blocks[ $key ] );
236 break;
237
238 case 'page':
239 if ( empty( self::$pages[ $key ] ) ) {
240 /* translators: The requested components unique key. */
241 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
242 }
243 unset( self::$pages[ $key ] );
244 break;
245 case 'header':
246 if ( empty( self::$headers[ $key ] ) ) {
247 /* translators: The requested components unique key. */
248 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
249 }
250 unset( self::$headers[ $key ] );
251 break;
252
253 case 'footer':
254 if ( empty( self::$footers[ $key ] ) ) {
255 /* translators: The requested components unique key. */
256 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
257 }
258 unset( self::$footers[ $key ] );
259 break;
260
261 case 'templates':
262 if ( empty( self::$templates[ $key ] ) ) {
263 /* translators: The requested components unique key. */
264 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
265 }
266 unset( self::$templates[ $key ] );
267 break;
268 }
269
270 }
271
272 /**
273 * Gets a component from the registry.
274 *
275 * @param string $type The component type.
276 * @param string $key The component's unique key.
277 *
278 * @return mixed
279 * @throws Exception If the required data is not provided or the specified component is not registered.
280 * @throws InvalidArgumentException If an unsupported component type is provided.
281 */
282 public static function get( $type, $key ) {
283
284 if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) {
285 /* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */
286 throw new Exception( sprintf( esc_html__( 'You must supply a component type in %s.', 'blockspare' ), __METHOD__ ) );
287 }
288
289 switch ( $type ) {
290 case 'layout':
291 if ( empty( self::$layouts[ $key ] ) ) {
292 /* translators: The requested components unique key. */
293 throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) );
294 }
295 return self::$layouts[ $key ];
296
297 case 'section':
298 if ( empty( self::$sections[ $key ] ) ) {
299 /* translators: The requested components unique key. */
300 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
301 }
302 return self::$sections[ $key ];
303
304 case 'block':
305 if ( empty( self::$blocks[ $key ] ) ) {
306 /* translators: The requested components unique key. */
307 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
308 }
309 return self::$blocks[ $key ];
310
311
312 case 'page':
313 if ( empty( self::$pages[ $key ] ) ) {
314 /* translators: The requested components unique key. */
315 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
316 }
317 return self::$pages[ $key ];
318
319 case 'header':
320 if ( empty( self::$headers[ $key ] ) ) {
321 /* translators: The requested components unique key. */
322 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
323 }
324 return self::$headers[ $key ];
325
326 case 'footer':
327 if ( empty( self::$footers[ $key ] ) ) {
328 /* translators: The requested components unique key. */
329 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
330 }
331 return self::$footers[ $key ];
332
333 case 'templates':
334 if ( empty( self::$templates[ $key ] ) ) {
335 /* translators: The requested components unique key. */
336 throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) );
337 }
338 return self::$templates[ $key ];
339
340 default:
341 /* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */
342 throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) );
343 }
344 }
345
346 /**
347 * Returns the registered layouts.
348 *
349 * @return array
350 */
351 public static function layouts() {
352 return self::$layouts;
353 }
354
355 /**
356 * Returns the registered sections.
357 *
358 * @return array
359 */
360 public static function sections() {
361 return self::$sections;
362 }
363
364
365 /**
366 * Returns the registered sections.
367 *
368 * @return array
369 */
370 public static function blocks() {
371 return self::$blocks;
372 }
373
374 /**
375 * Returns the registered pages.
376 *
377 * @return array
378 */
379 public static function pages() {
380 return self::$pages;
381 }
382
383 /**
384 * Returns the registered headers.
385 *
386 * @return array
387 */
388 public static function headers() {
389 return self::$headers;
390 }
391
392 /**
393 * Returns the registered footers.
394 *
395 * @return array
396 */
397 public static function footers() {
398 return self::$footers;
399 }
400
401
402 public static function templates() {
403 return self::$templates;
404 }
405 }
406