14 */ |
14 */ |
15 final class WP_Block_Patterns_Registry { |
15 final class WP_Block_Patterns_Registry { |
16 /** |
16 /** |
17 * Registered patterns array. |
17 * Registered patterns array. |
18 * |
18 * |
|
19 * @since 5.5.0 |
19 * @var array |
20 * @var array |
20 */ |
21 */ |
21 private $registered_patterns = array(); |
22 private $registered_patterns = array(); |
22 |
23 |
23 /** |
24 /** |
24 * Container for the main instance of the class. |
25 * Container for the main instance of the class. |
25 * |
26 * |
|
27 * @since 5.5.0 |
26 * @var WP_Block_Patterns_Registry|null |
28 * @var WP_Block_Patterns_Registry|null |
27 */ |
29 */ |
28 private static $instance = null; |
30 private static $instance = null; |
29 |
31 |
30 /** |
32 /** |
37 * content, description, viewportWidth, categories, keywords. |
39 * content, description, viewportWidth, categories, keywords. |
38 * @return bool True if the pattern was registered with success and false otherwise. |
40 * @return bool True if the pattern was registered with success and false otherwise. |
39 */ |
41 */ |
40 public function register( $pattern_name, $pattern_properties ) { |
42 public function register( $pattern_name, $pattern_properties ) { |
41 if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) { |
43 if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) { |
42 _doing_it_wrong( __METHOD__, __( 'Pattern name must be a string.' ), '5.5.0' ); |
44 _doing_it_wrong( |
|
45 __METHOD__, |
|
46 __( 'Pattern name must be a string.' ), |
|
47 '5.5.0' |
|
48 ); |
43 return false; |
49 return false; |
44 } |
50 } |
45 |
51 |
46 if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) { |
52 if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) { |
47 _doing_it_wrong( __METHOD__, __( 'Pattern title must be a string.' ), '5.5.0' ); |
53 _doing_it_wrong( |
|
54 __METHOD__, |
|
55 __( 'Pattern title must be a string.' ), |
|
56 '5.5.0' |
|
57 ); |
48 return false; |
58 return false; |
49 } |
59 } |
50 |
60 |
51 if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { |
61 if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { |
52 _doing_it_wrong( __METHOD__, __( 'Pattern content must be a string.' ), '5.5.0' ); |
62 _doing_it_wrong( |
|
63 __METHOD__, |
|
64 __( 'Pattern content must be a string.' ), |
|
65 '5.5.0' |
|
66 ); |
53 return false; |
67 return false; |
54 } |
68 } |
55 |
69 |
56 $this->registered_patterns[ $pattern_name ] = array_merge( |
70 $this->registered_patterns[ $pattern_name ] = array_merge( |
57 $pattern_properties, |
71 $pattern_properties, |
69 * @param string $pattern_name Pattern name including namespace. |
83 * @param string $pattern_name Pattern name including namespace. |
70 * @return bool True if the pattern was unregistered with success and false otherwise. |
84 * @return bool True if the pattern was unregistered with success and false otherwise. |
71 */ |
85 */ |
72 public function unregister( $pattern_name ) { |
86 public function unregister( $pattern_name ) { |
73 if ( ! $this->is_registered( $pattern_name ) ) { |
87 if ( ! $this->is_registered( $pattern_name ) ) { |
74 /* translators: 1: Pattern name. */ |
88 _doing_it_wrong( |
75 $message = sprintf( __( 'Pattern "%1$s" not found.' ), $pattern_name ); |
89 __METHOD__, |
76 _doing_it_wrong( __METHOD__, $message, '5.5.0' ); |
90 /* translators: %s: Pattern name. */ |
|
91 sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ), |
|
92 '5.5.0' |
|
93 ); |
77 return false; |
94 return false; |
78 } |
95 } |
79 |
96 |
80 unset( $this->registered_patterns[ $pattern_name ] ); |
97 unset( $this->registered_patterns[ $pattern_name ] ); |
81 |
98 |
156 /** |
173 /** |
157 * Unregisters a pattern. |
174 * Unregisters a pattern. |
158 * |
175 * |
159 * @since 5.5.0 |
176 * @since 5.5.0 |
160 * |
177 * |
161 * @param string $pattern_name Pattern name including namespace. |
178 * @param string $pattern_name Pattern name including namespace. |
162 * @return bool True if the pattern was unregistered with success and false otherwise. |
179 * @return bool True if the pattern was unregistered with success and false otherwise. |
163 */ |
180 */ |
164 function unregister_block_pattern( $pattern_name ) { |
181 function unregister_block_pattern( $pattern_name ) { |
165 return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name ); |
182 return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name ); |
166 } |
183 } |