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