16
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Blocks API: WP_Block_Patterns_Registry class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Blocks |
|
7 |
* @since 5.5.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
19
|
11 |
* Class used for interacting with block patterns. |
16
|
12 |
* |
|
13 |
* @since 5.5.0 |
|
14 |
*/ |
|
15 |
final class WP_Block_Patterns_Registry { |
|
16 |
/** |
19
|
17 |
* Registered block patterns array. |
16
|
18 |
* |
18
|
19 |
* @since 5.5.0 |
19
|
20 |
* @var array[] |
16
|
21 |
*/ |
|
22 |
private $registered_patterns = array(); |
|
23 |
|
|
24 |
/** |
19
|
25 |
* Patterns registered outside the `init` action. |
|
26 |
* |
|
27 |
* @since 6.0.0 |
|
28 |
* @var array[] |
|
29 |
*/ |
|
30 |
private $registered_patterns_outside_init = array(); |
|
31 |
|
|
32 |
/** |
16
|
33 |
* Container for the main instance of the class. |
|
34 |
* |
18
|
35 |
* @since 5.5.0 |
16
|
36 |
* @var WP_Block_Patterns_Registry|null |
|
37 |
*/ |
|
38 |
private static $instance = null; |
|
39 |
|
|
40 |
/** |
19
|
41 |
* Registers a block pattern. |
16
|
42 |
* |
|
43 |
* @since 5.5.0 |
19
|
44 |
* @since 5.8.0 Added support for the `blockTypes` property. |
16
|
45 |
* |
19
|
46 |
* @param string $pattern_name Block pattern name including namespace. |
|
47 |
* @param array $pattern_properties { |
|
48 |
* List of properties for the block pattern. |
|
49 |
* |
|
50 |
* @type string $title Required. A human-readable title for the pattern. |
|
51 |
* @type string $content Required. Block HTML markup for the pattern. |
|
52 |
* @type string $description Optional. Visually hidden text used to describe the pattern in the |
|
53 |
* inserter. A description is optional, but is strongly |
|
54 |
* encouraged when the title does not fully describe what the |
|
55 |
* pattern does. The description will help users discover the |
|
56 |
* pattern while searching. |
|
57 |
* @type int $viewportWidth Optional. The intended width of the pattern to allow for a scaled |
|
58 |
* preview within the pattern inserter. |
|
59 |
* @type array $categories Optional. A list of registered pattern categories used to group block |
|
60 |
* patterns. Block patterns can be shown on multiple categories. |
|
61 |
* A category must be registered separately in order to be used |
|
62 |
* here. |
|
63 |
* @type array $blockTypes Optional. A list of block names including namespace that could use |
|
64 |
* the block pattern in certain contexts (placeholder, transforms). |
|
65 |
* The block pattern is available in the block editor inserter |
|
66 |
* regardless of this list of block names. |
|
67 |
* Certain blocks support further specificity besides the block name |
|
68 |
* (e.g. for `core/template-part` you can specify areas |
|
69 |
* like `core/template-part/header` or `core/template-part/footer`). |
|
70 |
* @type array $keywords Optional. A list of aliases or keywords that help users discover the |
|
71 |
* pattern while searching. |
|
72 |
* } |
16
|
73 |
* @return bool True if the pattern was registered with success and false otherwise. |
|
74 |
*/ |
|
75 |
public function register( $pattern_name, $pattern_properties ) { |
|
76 |
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) { |
18
|
77 |
_doing_it_wrong( |
|
78 |
__METHOD__, |
|
79 |
__( 'Pattern name must be a string.' ), |
|
80 |
'5.5.0' |
|
81 |
); |
16
|
82 |
return false; |
|
83 |
} |
|
84 |
|
|
85 |
if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) { |
18
|
86 |
_doing_it_wrong( |
|
87 |
__METHOD__, |
|
88 |
__( 'Pattern title must be a string.' ), |
|
89 |
'5.5.0' |
|
90 |
); |
16
|
91 |
return false; |
|
92 |
} |
|
93 |
|
|
94 |
if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { |
18
|
95 |
_doing_it_wrong( |
|
96 |
__METHOD__, |
|
97 |
__( 'Pattern content must be a string.' ), |
|
98 |
'5.5.0' |
|
99 |
); |
16
|
100 |
return false; |
|
101 |
} |
|
102 |
|
19
|
103 |
$pattern = array_merge( |
16
|
104 |
$pattern_properties, |
|
105 |
array( 'name' => $pattern_name ) |
|
106 |
); |
19
|
107 |
$this->registered_patterns[ $pattern_name ] = $pattern; |
|
108 |
|
|
109 |
// If the pattern is registered inside an action other than `init`, store it |
|
110 |
// also to a dedicated array. Used to detect deprecated registrations inside |
|
111 |
// `admin_init` or `current_screen`. |
|
112 |
if ( current_action() && 'init' !== current_action() ) { |
|
113 |
$this->registered_patterns_outside_init[ $pattern_name ] = $pattern; |
|
114 |
} |
16
|
115 |
|
|
116 |
return true; |
|
117 |
} |
|
118 |
|
|
119 |
/** |
19
|
120 |
* Unregisters a block pattern. |
16
|
121 |
* |
|
122 |
* @since 5.5.0 |
|
123 |
* |
19
|
124 |
* @param string $pattern_name Block pattern name including namespace. |
16
|
125 |
* @return bool True if the pattern was unregistered with success and false otherwise. |
|
126 |
*/ |
|
127 |
public function unregister( $pattern_name ) { |
|
128 |
if ( ! $this->is_registered( $pattern_name ) ) { |
18
|
129 |
_doing_it_wrong( |
|
130 |
__METHOD__, |
|
131 |
/* translators: %s: Pattern name. */ |
|
132 |
sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ), |
|
133 |
'5.5.0' |
|
134 |
); |
16
|
135 |
return false; |
|
136 |
} |
|
137 |
|
|
138 |
unset( $this->registered_patterns[ $pattern_name ] ); |
19
|
139 |
unset( $this->registered_patterns_outside_init[ $pattern_name ] ); |
16
|
140 |
|
|
141 |
return true; |
|
142 |
} |
|
143 |
|
|
144 |
/** |
19
|
145 |
* Retrieves an array containing the properties of a registered block pattern. |
16
|
146 |
* |
|
147 |
* @since 5.5.0 |
|
148 |
* |
19
|
149 |
* @param string $pattern_name Block pattern name including namespace. |
16
|
150 |
* @return array Registered pattern properties. |
|
151 |
*/ |
|
152 |
public function get_registered( $pattern_name ) { |
|
153 |
if ( ! $this->is_registered( $pattern_name ) ) { |
|
154 |
return null; |
|
155 |
} |
|
156 |
|
|
157 |
return $this->registered_patterns[ $pattern_name ]; |
|
158 |
} |
|
159 |
|
|
160 |
/** |
19
|
161 |
* Retrieves all registered block patterns. |
16
|
162 |
* |
|
163 |
* @since 5.5.0 |
|
164 |
* |
19
|
165 |
* @param bool $outside_init_only Return only patterns registered outside the `init` action. |
|
166 |
* @return array[] Array of arrays containing the registered block patterns properties, |
|
167 |
* and per style. |
16
|
168 |
*/ |
19
|
169 |
public function get_all_registered( $outside_init_only = false ) { |
|
170 |
return array_values( |
|
171 |
$outside_init_only |
|
172 |
? $this->registered_patterns_outside_init |
|
173 |
: $this->registered_patterns |
|
174 |
); |
16
|
175 |
} |
|
176 |
|
|
177 |
/** |
19
|
178 |
* Checks if a block pattern is registered. |
16
|
179 |
* |
|
180 |
* @since 5.5.0 |
|
181 |
* |
19
|
182 |
* @param string $pattern_name Block pattern name including namespace. |
16
|
183 |
* @return bool True if the pattern is registered, false otherwise. |
|
184 |
*/ |
|
185 |
public function is_registered( $pattern_name ) { |
|
186 |
return isset( $this->registered_patterns[ $pattern_name ] ); |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* Utility method to retrieve the main instance of the class. |
|
191 |
* |
|
192 |
* The instance will be created if it does not exist yet. |
|
193 |
* |
|
194 |
* @since 5.5.0 |
|
195 |
* |
|
196 |
* @return WP_Block_Patterns_Registry The main instance. |
|
197 |
*/ |
|
198 |
public static function get_instance() { |
|
199 |
if ( null === self::$instance ) { |
|
200 |
self::$instance = new self(); |
|
201 |
} |
|
202 |
|
|
203 |
return self::$instance; |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
/** |
19
|
208 |
* Registers a new block pattern. |
16
|
209 |
* |
|
210 |
* @since 5.5.0 |
|
211 |
* |
19
|
212 |
* @param string $pattern_name Block pattern name including namespace. |
|
213 |
* @param array $pattern_properties List of properties for the block pattern. |
|
214 |
* See WP_Block_Patterns_Registry::register() for accepted arguments. |
16
|
215 |
* @return bool True if the pattern was registered with success and false otherwise. |
|
216 |
*/ |
|
217 |
function register_block_pattern( $pattern_name, $pattern_properties ) { |
|
218 |
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties ); |
|
219 |
} |
|
220 |
|
|
221 |
/** |
19
|
222 |
* Unregisters a block pattern. |
16
|
223 |
* |
|
224 |
* @since 5.5.0 |
|
225 |
* |
19
|
226 |
* @param string $pattern_name Block pattern name including namespace. |
16
|
227 |
* @return bool True if the pattern was unregistered with success and false otherwise. |
|
228 |
*/ |
|
229 |
function unregister_block_pattern( $pattern_name ) { |
|
230 |
return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name ); |
|
231 |
} |