| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks API: WP_Block_Styles_Registry class |
| 4 |
* |
| 5 |
* @package Gutenberg |
| 6 |
* @since 6.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class used for interacting with block styles. |
| 11 |
* |
| 12 |
* @since 6.2.0 |
| 13 |
*/ |
| 14 |
final class WP_Block_Styles_Registry { |
| 15 |
/** |
| 16 |
* Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays. |
| 17 |
* |
| 18 |
* @since 6.2.0 |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
private $registered_block_styles = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Container for the main instance of the class. |
| 25 |
* |
| 26 |
* @since 6.2.0 |
| 27 |
* @var WP_Block_Styles_Registry|null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Registers a block style. |
| 33 |
* |
| 34 |
* @since 6.2.0 |
| 35 |
* |
| 36 |
* @param string $block_name Block type name including namespace. |
| 37 |
* @param array $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added). |
| 38 |
* |
| 39 |
* @return boolean True if the block style was registered with success and false otherwise. |
| 40 |
*/ |
| 41 |
public function register( $block_name, $style_properties ) { |
| 42 |
|
| 43 |
if ( ! isset( $block_name ) || ! is_string( $block_name ) ) { |
| 44 |
$message = __( 'Block name name must be a string.', 'gutenberg' ); |
| 45 |
_doing_it_wrong( __METHOD__, $message, '6.2.0' ); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) { |
| 50 |
$message = __( 'Block style name must be a string.', 'gutenberg' ); |
| 51 |
_doing_it_wrong( __METHOD__, $message, '6.2.0' ); |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
$block_style_name = $style_properties['name']; |
| 56 |
|
| 57 |
if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) { |
| 58 |
$this->registered_block_styles[ $block_name ] = array(); |
| 59 |
} |
| 60 |
$this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties; |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Unregisters a block style. |
| 67 |
* |
| 68 |
* @param string $block_name Block type name including namespace. |
| 69 |
* @param array $block_style_name Block style name. |
| 70 |
* |
| 71 |
* @return boolean True if the block style was unregistered with success and false otherwise. |
| 72 |
*/ |
| 73 |
public function unregister( $block_name, $block_style_name ) { |
| 74 |
if ( ! $this->is_registered( $block_name, $block_style_name ) ) { |
| 75 |
/* translators: 1: block name, 2: block style name */ |
| 76 |
$message = sprintf( __( 'Block "%1$s" does not contain a style named "%2$s.".', 'gutenberg' ), $block_name, $block_style_name ); |
| 77 |
_doing_it_wrong( __METHOD__, $message, '6.2.0' ); |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
unset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Retrieves an array containing the properties of a registered block style. |
| 88 |
* |
| 89 |
* @since 6.2.0 |
| 90 |
* |
| 91 |
* @param string $block_name Block type name including namespace. |
| 92 |
* @param array $block_style_name Block style name. |
| 93 |
* |
| 94 |
* @return array Registered block style properties. |
| 95 |
*/ |
| 96 |
public function get_registered( $block_name, $block_style_name ) { |
| 97 |
if ( ! $this->is_registered( $block_name, $block_style_name ) ) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
return $this->registered_block_styles[ $block_name ][ $block_style_name ]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Retrieves all registered block styles. |
| 106 |
* |
| 107 |
* @since 6.2.0 |
| 108 |
* |
| 109 |
* @return array Array of arrays containing the registered block styles properties grouped per block, and per style. |
| 110 |
*/ |
| 111 |
public function get_all_registered() { |
| 112 |
return $this->registered_block_styles; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Retrieves registered block styles for a specific block. |
| 117 |
* |
| 118 |
* @since 6.2.0 |
| 119 |
* |
| 120 |
* @param string $block_name Block type name including namespace. |
| 121 |
* |
| 122 |
* @return array Array whose keys are block style names and whose value are block style properties. |
| 123 |
*/ |
| 124 |
public function get_registered_styles_for_block( $block_name ) { |
| 125 |
if ( isset( $this->registered_block_styles[ $block_name ] ) ) { |
| 126 |
return $this->registered_block_styles[ $block_name ]; |
| 127 |
} |
| 128 |
return array(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks if a block style is registered. |
| 133 |
* |
| 134 |
* @since 6.2.0 |
| 135 |
* |
| 136 |
* @param string $block_name Block type name including namespace. |
| 137 |
* @param array $block_style_name Block style name. |
| 138 |
* |
| 139 |
* @return bool True if the block style is registered, false otherwise. |
| 140 |
*/ |
| 141 |
public function is_registered( $block_name, $block_style_name ) { |
| 142 |
return isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Utility method to retrieve the main instance of the class. |
| 147 |
* |
| 148 |
* The instance will be created if it does not exist yet. |
| 149 |
* |
| 150 |
* @since 6.2.0 |
| 151 |
* |
| 152 |
* @return WP_Block_Styles_Registry The main instance. |
| 153 |
*/ |
| 154 |
public static function get_instance() { |
| 155 |
if ( null === self::$instance ) { |
| 156 |
self::$instance = new self(); |
| 157 |
} |
| 158 |
|
| 159 |
return self::$instance; |
| 160 |
} |
| 161 |
} |
| 162 |
|