| 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 |
|