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 / class-wp-icon-collections-registry-gutenberg.php

class-wp-icon-collections-registry-gutenberg.php in Gutenberg trunk, at lib/class-wp-icon-collections-registry-gutenberg.php

88 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Icons API: Gutenberg icon collections registry.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Adds collection visibility to the WordPress icon collections registry.
10 *
11 * @since 7.2.0
12 */
13 class WP_Icon_Collections_Registry_Gutenberg extends WP_Icon_Collections_Registry {
14
15 /**
16 * Registers an icon collection.
17 *
18 * @param string $collection_slug Icon collection slug.
19 * @param array $collection_properties {
20 * List of properties for the icon collection.
21 *
22 * @type string $label Required. A human-readable label for the icon collection.
23 * @type string $description Optional. A human-readable description for the icon collection.
24 * @type bool $public Optional. Whether the collection and its icons are exposed through
25 * the REST API and selectable in the editor's Icon block. Icons in
26 * non-public collections remain available via {@see wp_get_icon()}.
27 * Default true.
28 * }
29 * @return bool True if the collection was registered successfully, false otherwise.
30 */
31 public function register( $collection_slug, $collection_properties ) {
32 if ( ! is_array( $collection_properties ) ) {
33 return parent::register( $collection_slug, $collection_properties );
34 }
35
36 if ( array_key_exists( 'public', $collection_properties ) && ! is_bool( $collection_properties['public'] ) ) {
37 _doing_it_wrong(
38 __METHOD__,
39 __( 'Icon collection public property must be a boolean.', 'gutenberg' ),
40 '7.2.0'
41 );
42 return false;
43 }
44
45 $public = $collection_properties['public'] ?? true;
46 unset( $collection_properties['public'] );
47
48 if ( ! parent::register( $collection_slug, $collection_properties ) ) {
49 return false;
50 }
51
52 $this->registered_collections[ $collection_slug ]['public'] = $public;
53 return true;
54 }
55
56 /**
57 * Returns the shared registry instance, preserving existing collections.
58 *
59 * @return WP_Icon_Collections_Registry_Gutenberg The shared registry instance.
60 */
61 public static function get_instance() {
62 if ( ! self::$instance instanceof self ) {
63 $original_registry = self::$instance;
64 $gutenberg_registry = new self();
65
66 if ( null !== $original_registry ) {
67 foreach ( $original_registry->get_all_registered() as $collection ) {
68 $slug = $collection['slug'];
69 unset( $collection['slug'] );
70 $gutenberg_registry->register( $slug, $collection );
71 }
72 }
73
74 self::$instance = $gutenberg_registry;
75 }
76
77 return self::$instance;
78 }
79 }
80
81 /**
82 * Upgrades the shared collection registry before default collections are registered.
83 */
84 function gutenberg_override_wp_icon_collections_registry() {
85 WP_Icon_Collections_Registry_Gutenberg::get_instance();
86 }
87 add_action( 'init', 'gutenberg_override_wp_icon_collections_registry', -1 );
88