forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wp-patterns-registry.php
137 lines (121 loc) · 3.66 KB
/
class-wp-patterns-registry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Blocks API: WP_Patterns_Registry class
*
* @package Gutenberg
*/
/**
* Class used for interacting with patterns.
*/
final class WP_Patterns_Registry {
/**
* Registered patterns array.
*
* @var array
*/
private $registered_patterns = array();
/**
* Container for the main instance of the class.
*
* @var WP_Patterns_Registry|null
*/
private static $instance = null;
/**
* Registers a pattern.
*
* @param string $pattern_name Pattern name including namespace.
* @param array $pattern_properties Array containing the properties of the pattern: label, content.
* @return boolean True if the pattern was registered with success and false otherwise.
*/
public function register( $pattern_name, $pattern_properties ) {
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
$message = __( 'Pattern name must be a string.', 'gutenberg' );
_doing_it_wrong( __METHOD__, $message, '7.8.0' );
return false;
}
$this->registered_patterns[ $pattern_name ] = $pattern_properties;
return true;
}
/**
* Unregisters a pattern.
*
* @param string $pattern_name Pattern name including namespace.
* @return boolean True if the pattern was unregistered with success and false otherwise.
*/
public function unregister( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
/* translators: 1: Pattern name. */
$message = sprintf( __( 'Pattern "%1$s" not found.', 'gutenberg' ), $pattern_name );
_doing_it_wrong( __METHOD__, $message, '7.8.0' );
return false;
}
unset( $this->registered_patterns[ $pattern_name ] );
return true;
}
/**
* Retrieves an array containing the properties of a registered pattern.
*
* @param string $pattern_name Pattern name including namespace.
* @return array Registered pattern properties.
*/
public function get_registered( $pattern_name ) {
if ( ! $this->is_registered( $pattern_name ) ) {
return null;
}
return $this->registered_patterns[ $pattern_name ];
}
/**
* Retrieves all registered patterns.
*
* @return array Array of arrays containing the registered patterns properties,
* and per style.
*/
public function get_all_registered() {
return array_values( $this->registered_patterns );
}
/**
* Checks if a pattern is registered.
*
* @param string $pattern_name Pattern name including namespace.
* @return bool True if the pattern is registered, false otherwise.
*/
public function is_registered( $pattern_name ) {
return isset( $this->registered_patterns[ $pattern_name ] );
}
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 5.3.0
*
* @return WP_Patterns_Registry The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Registers a new pattern.
*
* @param string $pattern_name Pattern name including namespace.
* @param array $pattern_properties Array containing the properties of the pattern.
*
* @return boolean True if the pattern was registered with success and false otherwise.
*/
function register_pattern( $pattern_name, $pattern_properties ) {
return WP_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
}
/**
* Unregisters a pattern.
*
* @param string $pattern_name Pattern name including namespace.
*
* @return boolean True if the pattern was unregistered with success and false otherwise.
*/
function unregister_pattern( $pattern_name ) {
return WP_Patterns_Registry::get_instance()->unregister( $pattern_name );
}