|
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 /** |
|
11 * Class used for interacting with patterns. |
|
12 * |
|
13 * @since 5.5.0 |
|
14 */ |
|
15 final class WP_Block_Patterns_Registry { |
|
16 /** |
|
17 * Registered patterns array. |
|
18 * |
|
19 * @var array |
|
20 */ |
|
21 private $registered_patterns = array(); |
|
22 |
|
23 /** |
|
24 * Container for the main instance of the class. |
|
25 * |
|
26 * @var WP_Block_Patterns_Registry|null |
|
27 */ |
|
28 private static $instance = null; |
|
29 |
|
30 /** |
|
31 * Registers a pattern. |
|
32 * |
|
33 * @since 5.5.0 |
|
34 * |
|
35 * @param string $pattern_name Pattern name including namespace. |
|
36 * @param array $pattern_properties Array containing the properties of the pattern: title, |
|
37 * content, description, viewportWidth, categories, keywords. |
|
38 * @return bool True if the pattern was registered with success and false otherwise. |
|
39 */ |
|
40 public function register( $pattern_name, $pattern_properties ) { |
|
41 if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) { |
|
42 _doing_it_wrong( __METHOD__, __( 'Pattern name must be a string.' ), '5.5.0' ); |
|
43 return false; |
|
44 } |
|
45 |
|
46 if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) { |
|
47 _doing_it_wrong( __METHOD__, __( 'Pattern title must be a string.' ), '5.5.0' ); |
|
48 return false; |
|
49 } |
|
50 |
|
51 if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { |
|
52 _doing_it_wrong( __METHOD__, __( 'Pattern content must be a string.' ), '5.5.0' ); |
|
53 return false; |
|
54 } |
|
55 |
|
56 $this->registered_patterns[ $pattern_name ] = array_merge( |
|
57 $pattern_properties, |
|
58 array( 'name' => $pattern_name ) |
|
59 ); |
|
60 |
|
61 return true; |
|
62 } |
|
63 |
|
64 /** |
|
65 * Unregisters a pattern. |
|
66 * |
|
67 * @since 5.5.0 |
|
68 * |
|
69 * @param string $pattern_name Pattern name including namespace. |
|
70 * @return bool True if the pattern was unregistered with success and false otherwise. |
|
71 */ |
|
72 public function unregister( $pattern_name ) { |
|
73 if ( ! $this->is_registered( $pattern_name ) ) { |
|
74 /* translators: 1: Pattern name. */ |
|
75 $message = sprintf( __( 'Pattern "%1$s" not found.' ), $pattern_name ); |
|
76 _doing_it_wrong( __METHOD__, $message, '5.5.0' ); |
|
77 return false; |
|
78 } |
|
79 |
|
80 unset( $this->registered_patterns[ $pattern_name ] ); |
|
81 |
|
82 return true; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Retrieves an array containing the properties of a registered pattern. |
|
87 * |
|
88 * @since 5.5.0 |
|
89 * |
|
90 * @param string $pattern_name Pattern name including namespace. |
|
91 * @return array Registered pattern properties. |
|
92 */ |
|
93 public function get_registered( $pattern_name ) { |
|
94 if ( ! $this->is_registered( $pattern_name ) ) { |
|
95 return null; |
|
96 } |
|
97 |
|
98 return $this->registered_patterns[ $pattern_name ]; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Retrieves all registered patterns. |
|
103 * |
|
104 * @since 5.5.0 |
|
105 * |
|
106 * @return array Array of arrays containing the registered patterns properties, |
|
107 * and per style. |
|
108 */ |
|
109 public function get_all_registered() { |
|
110 return array_values( $this->registered_patterns ); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Checks if a pattern is registered. |
|
115 * |
|
116 * @since 5.5.0 |
|
117 * |
|
118 * @param string $pattern_name Pattern name including namespace. |
|
119 * @return bool True if the pattern is registered, false otherwise. |
|
120 */ |
|
121 public function is_registered( $pattern_name ) { |
|
122 return isset( $this->registered_patterns[ $pattern_name ] ); |
|
123 } |
|
124 |
|
125 /** |
|
126 * Utility method to retrieve the main instance of the class. |
|
127 * |
|
128 * The instance will be created if it does not exist yet. |
|
129 * |
|
130 * @since 5.5.0 |
|
131 * |
|
132 * @return WP_Block_Patterns_Registry The main instance. |
|
133 */ |
|
134 public static function get_instance() { |
|
135 if ( null === self::$instance ) { |
|
136 self::$instance = new self(); |
|
137 } |
|
138 |
|
139 return self::$instance; |
|
140 } |
|
141 } |
|
142 |
|
143 /** |
|
144 * Registers a new pattern. |
|
145 * |
|
146 * @since 5.5.0 |
|
147 * |
|
148 * @param string $pattern_name Pattern name including namespace. |
|
149 * @param array $pattern_properties Array containing the properties of the pattern. |
|
150 * @return bool True if the pattern was registered with success and false otherwise. |
|
151 */ |
|
152 function register_block_pattern( $pattern_name, $pattern_properties ) { |
|
153 return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties ); |
|
154 } |
|
155 |
|
156 /** |
|
157 * Unregisters a pattern. |
|
158 * |
|
159 * @since 5.5.0 |
|
160 * |
|
161 * @param string $pattern_name Pattern name including namespace. |
|
162 * @return bool True if the pattern was unregistered with success and false otherwise. |
|
163 */ |
|
164 function unregister_block_pattern( $pattern_name ) { |
|
165 return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name ); |
|
166 } |