| 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' ]; |
| 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 |
* The Component_Registry object. |
| 81 |
* |
| 82 |
* @var object Component_Registry |
| 83 |
*/ |
| 84 |
private static $instance; |
| 85 |
|
| 86 |
/** |
| 87 |
* Creates the Component_Registry instance. |
| 88 |
* |
| 89 |
* @return Component_Registry |
| 90 |
*/ |
| 91 |
public static function instance() { |
| 92 |
if ( null === self::$instance ) { |
| 93 |
self::$instance = new self(); |
| 94 |
} |
| 95 |
return self::$instance; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Adds a component to the registry. |
| 100 |
* |
| 101 |
* @param array $data Component data. |
| 102 |
* @throws InvalidArgumentException If invalid data is provided or the component is already registered. |
| 103 |
*/ |
| 104 |
public static function add( array $data ) { |
| 105 |
|
| 106 |
if ( empty( $data['type'] ) || ! in_array( $data['type'], self::$supported_component_types, true ) ) { |
| 107 |
throw new InvalidArgumentException( esc_html__( 'You must supply a valid component type.', 'blockspare' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( empty( $data ) ) { |
| 111 |
throw new InvalidArgumentException( __( 'You must supply valid layout data to register a layout.', 'blockspare' ) ); |
| 112 |
} |
| 113 |
|
| 114 |
foreach ( self::$required_data_keys as $required_key ) { |
| 115 |
if ( ! array_key_exists( $required_key, $data ) || empty( $data[ $required_key ] ) ) { |
| 116 |
/* translators: %s: The missing key that is required to register a component. */ |
| 117 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a %s to register a layout.', 'blockspare' ), $required_key ) ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
switch ( $data['type'] ) { |
| 122 |
case 'layout': |
| 123 |
if ( ! empty( self::$layouts[ $data['key'] ] ) ) { |
| 124 |
/* translators: %s: The component's unique key. */ |
| 125 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s layout is already registered.', 'blockspare' ), $data['key'] ) ); |
| 126 |
} |
| 127 |
self::$layouts[ $data['key'] ] = $data; |
| 128 |
break; |
| 129 |
|
| 130 |
case 'section': |
| 131 |
if ( ! empty( self::$sections[ $data['key'] ] ) ) { |
| 132 |
/* translators: %s: The component's unique key. */ |
| 133 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 134 |
} |
| 135 |
self::$sections[ $data['key'] ] = $data; |
| 136 |
break; |
| 137 |
|
| 138 |
|
| 139 |
case 'block': |
| 140 |
if ( ! empty( self::$blocks[ $data['key'] ] ) ) { |
| 141 |
/* translators: %s: The component's unique key. */ |
| 142 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 143 |
} |
| 144 |
self::$blocks[ $data['key'] ] = $data; |
| 145 |
break; |
| 146 |
|
| 147 |
case 'page': |
| 148 |
if ( ! empty( self::$pages[ $data['key'] ] ) ) { |
| 149 |
/* translators: %s: The component's unique key. */ |
| 150 |
throw new InvalidArgumentException( sprintf( esc_html__( 'The %s section is already registered.', 'blockspare' ), $data['key'] ) ); |
| 151 |
} |
| 152 |
self::$pages[ $data['key'] ] = $data; |
| 153 |
break; |
| 154 |
case 'header': |
| 155 |
if ( ! empty( self::$headers[ $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 'footer': |
| 162 |
if ( ! empty( self::$footers[ $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 |
|
| 169 |
default: |
| 170 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::add(). */ |
| 171 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Removes an existing component from the registry. |
| 177 |
* |
| 178 |
* @param string $type The component type to unregister. |
| 179 |
* @param string $key The unique layout key to be removed. |
| 180 |
* |
| 181 |
* @throws Exception If the required data is not provided or the specified component is not registered. |
| 182 |
* @throws InvalidArgumentException If an unsupported component type is provided. |
| 183 |
*/ |
| 184 |
public static function remove( $type, $key ) { |
| 185 |
|
| 186 |
if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) { |
| 187 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */ |
| 188 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 189 |
} |
| 190 |
|
| 191 |
if ( empty( $key ) ) { |
| 192 |
/* translators: %s: This functions name. Will always be Blockspare\Layouts\Component_Registry::remove(). */ |
| 193 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component key in %s.', 'blockspare' ), __METHOD__ ) ); |
| 194 |
} |
| 195 |
|
| 196 |
$key = sanitize_key( $key ); |
| 197 |
|
| 198 |
switch ( $type ) { |
| 199 |
case 'layout': |
| 200 |
if ( empty( self::$layouts[ $key ] ) ) { |
| 201 |
/* translators: The requested components unique key. */ |
| 202 |
throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) ); |
| 203 |
} |
| 204 |
unset( self::$layouts[ $key ] ); |
| 205 |
break; |
| 206 |
|
| 207 |
case 'section': |
| 208 |
if ( empty( self::$sections[ $key ] ) ) { |
| 209 |
/* translators: The requested components unique key. */ |
| 210 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 211 |
} |
| 212 |
unset( self::$sections[ $key ] ); |
| 213 |
break; |
| 214 |
|
| 215 |
case 'block': |
| 216 |
if ( empty( self::$blocks[ $key ] ) ) { |
| 217 |
/* translators: The requested components unique key. */ |
| 218 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 219 |
} |
| 220 |
unset( self::$blocks[ $key ] ); |
| 221 |
break; |
| 222 |
|
| 223 |
case 'page': |
| 224 |
if ( empty( self::$pages[ $key ] ) ) { |
| 225 |
/* translators: The requested components unique key. */ |
| 226 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 227 |
} |
| 228 |
unset( self::$pages[ $key ] ); |
| 229 |
break; |
| 230 |
case 'header': |
| 231 |
if ( empty( self::$headers[ $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::$headers[ $key ] ); |
| 236 |
break; |
| 237 |
|
| 238 |
case 'footer': |
| 239 |
if ( empty( self::$footers[ $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::$footers[ $key ] ); |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Gets a component from the registry. |
| 251 |
* |
| 252 |
* @param string $type The component type. |
| 253 |
* @param string $key The component's unique key. |
| 254 |
* |
| 255 |
* @return mixed |
| 256 |
* @throws Exception If the required data is not provided or the specified component is not registered. |
| 257 |
* @throws InvalidArgumentException If an unsupported component type is provided. |
| 258 |
*/ |
| 259 |
public static function get( $type, $key ) { |
| 260 |
|
| 261 |
if ( empty( $type ) || ! in_array( $type, self::$supported_component_types, true ) ) { |
| 262 |
/* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */ |
| 263 |
throw new Exception( sprintf( esc_html__( 'You must supply a component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 264 |
} |
| 265 |
|
| 266 |
switch ( $type ) { |
| 267 |
case 'layout': |
| 268 |
if ( empty( self::$layouts[ $key ] ) ) { |
| 269 |
/* translators: The requested components unique key. */ |
| 270 |
throw new Exception( sprintf( esc_html__( 'The %s layout is not registered.', 'blockspare' ), $key ) ); |
| 271 |
} |
| 272 |
return self::$layouts[ $key ]; |
| 273 |
|
| 274 |
case 'section': |
| 275 |
if ( empty( self::$sections[ $key ] ) ) { |
| 276 |
/* translators: The requested components unique key. */ |
| 277 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 278 |
} |
| 279 |
return self::$sections[ $key ]; |
| 280 |
|
| 281 |
case 'block': |
| 282 |
if ( empty( self::$blocks[ $key ] ) ) { |
| 283 |
/* translators: The requested components unique key. */ |
| 284 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 285 |
} |
| 286 |
return self::$blocks[ $key ]; |
| 287 |
|
| 288 |
|
| 289 |
case 'page': |
| 290 |
if ( empty( self::$pages[ $key ] ) ) { |
| 291 |
/* translators: The requested components unique key. */ |
| 292 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 293 |
} |
| 294 |
return self::$pages[ $key ]; |
| 295 |
|
| 296 |
case 'header': |
| 297 |
if ( empty( self::$headers[ $key ] ) ) { |
| 298 |
/* translators: The requested components unique key. */ |
| 299 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 300 |
} |
| 301 |
return self::$headers[ $key ]; |
| 302 |
|
| 303 |
case 'footer': |
| 304 |
if ( empty( self::$footers[ $key ] ) ) { |
| 305 |
/* translators: The requested components unique key. */ |
| 306 |
throw new Exception( sprintf( esc_html__( 'The %s section is not registered.', 'blockspare' ), $key ) ); |
| 307 |
} |
| 308 |
return self::$footers[ $key ]; |
| 309 |
|
| 310 |
default: |
| 311 |
/* translators: This function name. Will always be Blockspare\Layouts\Component_Registry::get(). */ |
| 312 |
throw new InvalidArgumentException( sprintf( esc_html__( 'You must supply a valid component type in %s.', 'blockspare' ), __METHOD__ ) ); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the registered layouts. |
| 318 |
* |
| 319 |
* @return array |
| 320 |
*/ |
| 321 |
public static function layouts() { |
| 322 |
return self::$layouts; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Returns the registered sections. |
| 327 |
* |
| 328 |
* @return array |
| 329 |
*/ |
| 330 |
public static function sections() { |
| 331 |
return self::$sections; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
/** |
| 336 |
* Returns the registered sections. |
| 337 |
* |
| 338 |
* @return array |
| 339 |
*/ |
| 340 |
public static function blocks() { |
| 341 |
return self::$blocks; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Returns the registered pages. |
| 346 |
* |
| 347 |
* @return array |
| 348 |
*/ |
| 349 |
public static function pages() { |
| 350 |
return self::$pages; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Returns the registered headers. |
| 355 |
* |
| 356 |
* @return array |
| 357 |
*/ |
| 358 |
public static function headers() { |
| 359 |
return self::$headers; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Returns the registered footers. |
| 364 |
* |
| 365 |
* @return array |
| 366 |
*/ |
| 367 |
public static function footers() { |
| 368 |
return self::$footers; |
| 369 |
} |
| 370 |
} |
| 371 |
|