| 1 |
<?php |
| 2 |
/** |
| 3 |
* Layout block related functions. |
| 4 |
* |
| 5 |
* @package Blockspare |
| 6 |
*/ |
| 7 |
|
| 8 |
use Blockspare\Layouts\Component_Registry; |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers layout components with the Component Registry |
| 12 |
* for use in the Layouts block. |
| 13 |
* |
| 14 |
* @param array $data The component data. |
| 15 |
* |
| 16 |
* @return bool|WP_Error |
| 17 |
*/ |
| 18 |
function blockspare_register_layout_component( array $data ) { |
| 19 |
|
| 20 |
|
| 21 |
$registry = Component_Registry::instance(); |
| 22 |
|
| 23 |
try { |
| 24 |
$registry::add( $data ); |
| 25 |
return true; |
| 26 |
} catch ( Exception $exception ) { |
| 27 |
return new WP_Error( esc_html( $exception->getMessage() ) ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Unregisters the specified layout component from the Component Registry |
| 33 |
* for use in the Layouts block. |
| 34 |
* |
| 35 |
* @return mixed Boolean true if component unregistered. WP_Error object if an error occurs. |
| 36 |
* @param string $type The component type to be unregistered. |
| 37 |
* @param string $key The unique layout key to be unregistered. |
| 38 |
*/ |
| 39 |
function blockspare_unregister_layout_component( $type, $key ) { |
| 40 |
$registry = Component_Registry::instance(); |
| 41 |
try { |
| 42 |
$registry::remove( $type, $key ); |
| 43 |
return true; |
| 44 |
} catch ( Exception $exception ) { |
| 45 |
return new WP_Error( esc_html( $exception->getMessage() ) ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Retrieves the specified layout component. |
| 51 |
* |
| 52 |
* @param string $type The layout component type. |
| 53 |
* @param string $key The layout component's unique key. |
| 54 |
* |
| 55 |
* @return mixed|WP_Error |
| 56 |
*/ |
| 57 |
function blockspare_get_layout_component( $type, $key ) { |
| 58 |
|
| 59 |
if ( empty( $type ) ) { |
| 60 |
return new WP_Error( esc_html__( 'You must supply a type to retrieve a layout component.', 'blockspare' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( empty( $key ) ) { |
| 64 |
return new WP_Error( esc_html__( 'You must supply a key to retrieve a layout component.', 'blockspare' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
$type = sanitize_key( $type ); |
| 68 |
|
| 69 |
$key = sanitize_key( $key ); |
| 70 |
|
| 71 |
$registry = Component_Registry::instance(); |
| 72 |
|
| 73 |
try { |
| 74 |
return $registry::get( $type, $key ); |
| 75 |
} catch ( Exception $exception ) { |
| 76 |
return new WP_Error( esc_html( $exception->getMessage() ) ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Gets the registered layouts. |
| 82 |
* |
| 83 |
* @return array Array of registered layouts. |
| 84 |
*/ |
| 85 |
function blockspare_get_layouts() { |
| 86 |
$registry = Component_Registry::instance(); |
| 87 |
return $registry::layouts(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Gets the registered sections. |
| 92 |
* |
| 93 |
* @return array Array of registered sections. |
| 94 |
*/ |
| 95 |
function blockspare_get_sections() { |
| 96 |
$registry = Component_Registry::instance(); |
| 97 |
return $registry::sections(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Gets the registered blocks. |
| 103 |
* |
| 104 |
* @return array Array of registered blocks. |
| 105 |
*/ |
| 106 |
function blockspare_get_blocks() { |
| 107 |
$registry = Component_Registry::instance(); |
| 108 |
return $registry::blocks(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets the registered pages. |
| 113 |
* |
| 114 |
* @return array Array of registered pages. |
| 115 |
*/ |
| 116 |
function blockspare_get_pages() { |
| 117 |
$registry = Component_Registry::instance(); |
| 118 |
return $registry::pages(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|