PluginProbe
Gutenberg / trunk
Gutenberg vtrunk
24.1.0 24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 All 404 releases
gutenberg / lib / compat / wordpress-7.1 / class-wp-icon-collections-registry.php

class-wp-icon-collections-registry.php in Gutenberg trunk, at lib/compat/wordpress-7.1/class-wp-icon-collections-registry.php

209 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Icon Collections: WP_Icon_Collections_Registry class
4 *
5 * @package gutenberg
6 */
7
8 if ( ! class_exists( 'WP_Icon_Collections_Registry' ) ) {
9
10 /**
11 * Core class used for managing registered icon collections.
12 */
13 class WP_Icon_Collections_Registry {
14 /**
15 * Registered icon collections array.
16 *
17 * @var array[]
18 */
19 protected $registered_collections = array();
20
21 /**
22 * Container for the main instance of the class.
23 *
24 * @var WP_Icon_Collections_Registry|null
25 */
26 protected static $instance = null;
27
28 /**
29 * Registers an icon collection.
30 *
31 * @param string $collection_slug Icon collection slug.
32 * @param array $collection_properties {
33 * List of properties for the icon collection.
34 *
35 * @type string $label Required. A human-readable label for the icon collection.
36 * @type string $description Optional. A human-readable description for the icon collection.
37 * }
38 * @return bool True if the collection was registered with success and false otherwise.
39 */
40 public function register( $collection_slug, $collection_properties ) {
41 if ( ! isset( $collection_slug ) || ! is_string( $collection_slug ) ) {
42 _doing_it_wrong(
43 __METHOD__,
44 __( 'Icon collection slug must be a string.', 'gutenberg' ),
45 '7.1.0'
46 );
47 return false;
48 }
49
50 if ( ! preg_match( '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/', $collection_slug ) ) {
51 _doing_it_wrong(
52 __METHOD__,
53 __( 'Icon collection slug must start and end with a lowercase letter or digit and contain only lowercase letters, digits, hyphens, and underscores.', 'gutenberg' ),
54 '7.1.0'
55 );
56 return false;
57 }
58
59 if ( $this->is_registered( $collection_slug ) ) {
60 _doing_it_wrong(
61 __METHOD__,
62 __( 'Icon collection is already registered.', 'gutenberg' ),
63 '7.1.0'
64 );
65 return false;
66 }
67
68 if ( ! is_array( $collection_properties ) ) {
69 _doing_it_wrong(
70 __METHOD__,
71 __( 'Icon collection properties must be an array.', 'gutenberg' ),
72 '7.1.0'
73 );
74 return false;
75 }
76
77 $allowed_keys = array( 'label', 'description' );
78 foreach ( array_keys( $collection_properties ) as $key ) {
79 if ( ! in_array( $key, $allowed_keys, true ) ) {
80 _doing_it_wrong(
81 __METHOD__,
82 sprintf(
83 /* translators: %s is the name of any user-provided key */
84 __( 'Invalid icon collection property: "%s".', 'gutenberg' ),
85 $key
86 ),
87 '7.1.0'
88 );
89 return false;
90 }
91 }
92
93 if ( ! isset( $collection_properties['label'] ) || ! is_string( $collection_properties['label'] ) ) {
94 _doing_it_wrong(
95 __METHOD__,
96 __( 'Icon collection label must be a string.', 'gutenberg' ),
97 '7.1.0'
98 );
99 return false;
100 }
101
102 if ( isset( $collection_properties['description'] ) && ! is_string( $collection_properties['description'] ) ) {
103 _doing_it_wrong(
104 __METHOD__,
105 __( 'Icon collection description must be a string.', 'gutenberg' ),
106 '7.1.0'
107 );
108 return false;
109 }
110
111 $defaults = array(
112 'description' => '',
113 );
114
115 $collection = array_merge(
116 $defaults,
117 $collection_properties,
118 array( 'slug' => $collection_slug )
119 );
120
121 $this->registered_collections[ $collection_slug ] = $collection;
122
123 return true;
124 }
125
126 /**
127 * Unregisters an icon collection.
128 *
129 * Any icons registered under the given collection are also unregistered.
130 *
131 * @param string $collection_slug Icon collection slug.
132 * @return bool True if the collection was unregistered with success and false otherwise.
133 */
134 public function unregister( $collection_slug ) {
135 if ( ! $this->is_registered( $collection_slug ) ) {
136 _doing_it_wrong(
137 __METHOD__,
138 sprintf(
139 /* translators: %s: Icon collection slug. */
140 __( 'Icon collection "%s" not found.', 'gutenberg' ),
141 $collection_slug
142 ),
143 '7.1.0'
144 );
145 return false;
146 }
147
148 $icons_registry = WP_Icons_Registry_Gutenberg::get_instance();
149 foreach ( $icons_registry->get_registered_icons() as $icon ) {
150 if ( isset( $icon['collection'] ) && $icon['collection'] === $collection_slug ) {
151 $icons_registry->unregister( $icon['name'] );
152 }
153 }
154
155 unset( $this->registered_collections[ $collection_slug ] );
156
157 return true;
158 }
159
160 /**
161 * Retrieves an array containing the properties of a registered icon collection.
162 *
163 * @param string $collection_slug Icon collection slug.
164 * @return array|null Registered collection properties, or `null` if the collection is not registered.
165 */
166 public function get_registered( $collection_slug ) {
167 if ( ! $this->is_registered( $collection_slug ) ) {
168 return null;
169 }
170
171 return $this->registered_collections[ $collection_slug ];
172 }
173
174 /**
175 * Retrieves all registered icon collections.
176 *
177 * @return array[] Array of arrays containing the registered icon collections properties.
178 */
179 public function get_all_registered() {
180 return array_values( $this->registered_collections );
181 }
182
183 /**
184 * Checks if an icon collection is registered.
185 *
186 * @param string|null $collection_slug Icon collection slug.
187 * @return bool True if the icon collection is registered, false otherwise.
188 */
189 public function is_registered( $collection_slug ) {
190 return isset( $collection_slug, $this->registered_collections[ $collection_slug ] );
191 }
192
193 /**
194 * Utility method to retrieve the main instance of the class.
195 *
196 * The instance will be created if it does not exist yet.
197 *
198 * @return WP_Icon_Collections_Registry The main instance.
199 */
200 public static function get_instance() {
201 if ( null === self::$instance ) {
202 self::$instance = new self();
203 }
204
205 return self::$instance;
206 }
207 }
208 }
209