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 / icons.php

icons.php in Gutenberg trunk, at lib/icons.php

100 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: registration of the core icon collections and their icons from the
4 * plugin's own `packages/icons` manifest, replacing the core registration.
5 *
6 * @package gutenberg
7 */
8
9 /**
10 * Registers the default icon collections for Gutenberg.
11 */
12 function gutenberg_register_default_icon_collections() {
13 wp_register_icon_collection(
14 'core',
15 array(
16 'label' => __( 'WordPress', 'gutenberg' ),
17 'description' => __( 'Core icon collection.', 'gutenberg' ),
18 )
19 );
20 wp_register_icon_collection(
21 'core-admin',
22 array(
23 'label' => __( 'WordPress Admin', 'gutenberg' ),
24 'description' => __( 'Icon collection used by the WordPress admin interface.', 'gutenberg' ),
25 'public' => false,
26 )
27 );
28 }
29
30 $default_icon_collections_priority = has_action( 'init', '_wp_register_default_icon_collections' );
31 if ( false !== $default_icon_collections_priority ) {
32 remove_action( 'init', '_wp_register_default_icon_collections', $default_icon_collections_priority );
33 }
34 add_action( 'init', 'gutenberg_register_default_icon_collections', 0 );
35
36 /**
37 * Registers the default core and core-admin icons from the Gutenberg manifest.
38 */
39 function gutenberg_register_default_icons() {
40 $icons_directory = gutenberg_dir_path() . 'packages/icons/src';
41 $icons_directory = trailingslashit( $icons_directory );
42 $manifest_path = $icons_directory . 'manifest.php';
43
44 if ( ! is_readable( $manifest_path ) ) {
45 wp_trigger_error(
46 __FUNCTION__,
47 __( 'Core icon collection manifest is missing or unreadable.', 'gutenberg' )
48 );
49 return;
50 }
51
52 $collection = include $manifest_path;
53
54 if ( empty( $collection ) ) {
55 wp_trigger_error(
56 __FUNCTION__,
57 __( 'Core icon collection manifest is empty or invalid.', 'gutenberg' )
58 );
59 return;
60 }
61
62 foreach ( $collection as $icon_name => $icon_data ) {
63 if (
64 empty( $icon_data['filePath'] )
65 || ! is_string( $icon_data['filePath'] )
66 ) {
67 _doing_it_wrong(
68 __FUNCTION__,
69 __( 'Core icon collection manifest must provide a valid "filePath" for each icon.', 'gutenberg' ),
70 '7.1.0'
71 );
72 return;
73 }
74
75 if ( empty( $icon_data['collections'] ) || ! is_array( $icon_data['collections'] ) ) {
76 _doing_it_wrong(
77 __FUNCTION__,
78 __( 'Core icon collection manifest must provide a non-empty "collections" array for each icon.', 'gutenberg' ),
79 '7.2.0'
80 );
81 return;
82 }
83
84 $icon_args = array(
85 'label' => $icon_data['label'],
86 'file_path' => $icons_directory . $icon_data['filePath'],
87 );
88
89 foreach ( $icon_data['collections'] as $collection_slug ) {
90 wp_register_icon( $collection_slug . '/' . $icon_name, $icon_args );
91 }
92 }
93 }
94
95 $default_icons_priority = has_action( 'init', '_wp_register_default_icons' );
96 if ( false !== $default_icons_priority ) {
97 remove_action( 'init', '_wp_register_default_icons', $default_icons_priority );
98 }
99 add_action( 'init', 'gutenberg_register_default_icons' );
100