| 1 |
<?php |
| 2 |
/** |
| 3 |
* Icons API functions. |
| 4 |
* |
| 5 |
* @package Gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'wp_register_icon_collection' ) ) { |
| 9 |
/** |
| 10 |
* Registers a new icon collection. |
| 11 |
* |
| 12 |
* @param string $slug Icon collection slug. |
| 13 |
* @param array $args { |
| 14 |
* Arguments for registering an icon collection. |
| 15 |
* |
| 16 |
* @type string $label Required. A human-readable label for the icon collection. |
| 17 |
* @type string $description Optional. A human-readable description for the icon collection. |
| 18 |
* } |
| 19 |
* @return bool True if the icon collection was registered successfully, else false. |
| 20 |
*/ |
| 21 |
function wp_register_icon_collection( $slug, $args ) { |
| 22 |
return WP_Icon_Collections_Registry::get_instance()->register( $slug, $args ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! function_exists( 'wp_unregister_icon_collection' ) ) { |
| 27 |
/** |
| 28 |
* Unregisters an icon collection. |
| 29 |
* |
| 30 |
* @param string $slug Icon collection slug. |
| 31 |
* @return bool True if the icon collection was unregistered successfully, else false. |
| 32 |
*/ |
| 33 |
function wp_unregister_icon_collection( $slug ) { |
| 34 |
return WP_Icon_Collections_Registry::get_instance()->unregister( $slug ); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! function_exists( 'wp_register_icon' ) ) { |
| 39 |
/** |
| 40 |
* Registers a new icon. |
| 41 |
* |
| 42 |
* @param string $icon_name Namespaced icon name in the form "collection/icon-name" |
| 43 |
* (e.g. "my-plugin/arrow-left"). The "core" collection is |
| 44 |
* reserved for WordPress core icons; third-party code should |
| 45 |
* register icons under its own collection rather than the |
| 46 |
* "core" collection. |
| 47 |
* @param array $args { |
| 48 |
* List of properties for the icon. |
| 49 |
* |
| 50 |
* @type string $label Required. A human-readable label for the icon. |
| 51 |
* @type string $content Optional. SVG markup for the icon. |
| 52 |
* If not provided, the content will be retrieved from the `file_path` if set. |
| 53 |
* If both `content` and `file_path` are not set, the icon will not be registered. |
| 54 |
* @type string $file_path Optional. The full path to the file containing the icon content. |
| 55 |
* } |
| 56 |
* @return bool True if the icon was registered successfully, else false. |
| 57 |
*/ |
| 58 |
function wp_register_icon( $icon_name, $args ) { |
| 59 |
return WP_Icons_Registry::get_instance()->register( $icon_name, $args ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! function_exists( 'wp_unregister_icon' ) ) { |
| 64 |
/** |
| 65 |
* Unregisters an icon. |
| 66 |
* |
| 67 |
* @param string $icon_name Namespaced icon name in the form "collection/icon-name" |
| 68 |
* (e.g. "core/arrow-left"). |
| 69 |
* @return bool True if the icon was unregistered successfully, else false. |
| 70 |
*/ |
| 71 |
function wp_unregister_icon( $icon_name ) { |
| 72 |
return WP_Icons_Registry::get_instance()->unregister( $icon_name ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Registers the default icon collections for Gutenberg. |
| 78 |
*/ |
| 79 |
function gutenberg_register_default_icon_collections() { |
| 80 |
wp_register_icon_collection( |
| 81 |
'core', |
| 82 |
array( |
| 83 |
'label' => __( 'WordPress', 'gutenberg' ), |
| 84 |
'description' => __( 'Core icon collection.', 'gutenberg' ), |
| 85 |
) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
$default_icon_collections_priority = has_action( 'init', '_wp_register_default_icon_collections' ); |
| 90 |
if ( false !== $default_icon_collections_priority ) { |
| 91 |
remove_action( 'init', '_wp_register_default_icon_collections', $default_icon_collections_priority ); |
| 92 |
} |
| 93 |
add_action( 'init', 'gutenberg_register_default_icon_collections', 0 ); |
| 94 |
|
| 95 |
/** |
| 96 |
* Registers the default core icons from the Gutenberg manifest. |
| 97 |
*/ |
| 98 |
function gutenberg_register_default_icons() { |
| 99 |
$icons_directory = gutenberg_dir_path() . 'packages/icons/src'; |
| 100 |
$icons_directory = trailingslashit( $icons_directory ); |
| 101 |
$manifest_path = $icons_directory . 'manifest.php'; |
| 102 |
|
| 103 |
if ( ! is_readable( $manifest_path ) ) { |
| 104 |
wp_trigger_error( |
| 105 |
__FUNCTION__, |
| 106 |
__( 'Core icon collection manifest is missing or unreadable.', 'gutenberg' ) |
| 107 |
); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$collection = include $manifest_path; |
| 112 |
|
| 113 |
if ( empty( $collection ) ) { |
| 114 |
wp_trigger_error( |
| 115 |
__FUNCTION__, |
| 116 |
__( 'Core icon collection manifest is empty or invalid.', 'gutenberg' ) |
| 117 |
); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
foreach ( $collection as $icon_name => $icon_data ) { |
| 122 |
if ( |
| 123 |
empty( $icon_data['filePath'] ) |
| 124 |
|| ! is_string( $icon_data['filePath'] ) |
| 125 |
) { |
| 126 |
_doing_it_wrong( |
| 127 |
__FUNCTION__, |
| 128 |
__( 'Core icon collection manifest must provide a valid "filePath" for each icon.', 'gutenberg' ), |
| 129 |
'7.1.0' |
| 130 |
); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
wp_register_icon( |
| 135 |
'core/' . $icon_name, |
| 136 |
array( |
| 137 |
'label' => $icon_data['label'], |
| 138 |
'file_path' => $icons_directory . $icon_data['filePath'], |
| 139 |
) |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$default_icons_priority = has_action( 'init', '_wp_register_default_icons' ); |
| 145 |
if ( false !== $default_icons_priority ) { |
| 146 |
remove_action( 'init', '_wp_register_default_icons', $default_icons_priority ); |
| 147 |
} |
| 148 |
add_action( 'init', 'gutenberg_register_default_icons' ); |
| 149 |
|
| 150 |
if ( ! function_exists( 'wp_get_icon' ) ) { |
| 151 |
/** |
| 152 |
* Returns the SVG markup for a registered icon. |
| 153 |
* |
| 154 |
* @since 7.1.0 |
| 155 |
* |
| 156 |
* @param string $name The namespaced icon name (e.g. 'core/plus', |
| 157 |
* 'core/arrow-down', 'my-plugin/custom-icon'). |
| 158 |
* @param array $args { |
| 159 |
* Optional. Arguments for the icon. |
| 160 |
* |
| 161 |
* @type int|null $size Width and height in pixels. Pass null to leave the |
| 162 |
* SVG's intrinsic dimensions untouched. Non-numeric |
| 163 |
* values are ignored. Default 24. |
| 164 |
* @type string $class Additional CSS class names. Multiple classes may be |
| 165 |
* provided as a space-separated string. |
| 166 |
* @type string $label Accessible label. If provided, the SVG gets |
| 167 |
* role="img" and aria-label. If omitted, the SVG |
| 168 |
* gets aria-hidden="true" and focusable="false". |
| 169 |
* } |
| 170 |
* @return string SVG markup for the icon, or empty string if not found. |
| 171 |
*/ |
| 172 |
function wp_get_icon( $name, $args = array() ) { |
| 173 |
$icon = WP_Icons_Registry::get_instance()->get_registered_icon( $name ); |
| 174 |
if ( is_null( $icon ) ) { |
| 175 |
return ''; |
| 176 |
} |
| 177 |
|
| 178 |
$svg = $icon['content']; |
| 179 |
if ( empty( $svg ) ) { |
| 180 |
return ''; |
| 181 |
} |
| 182 |
|
| 183 |
$args = wp_parse_args( |
| 184 |
$args, |
| 185 |
array( |
| 186 |
'size' => 24, |
| 187 |
'class' => '', |
| 188 |
'label' => '', |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
$processor = new WP_HTML_Tag_Processor( $svg ); |
| 193 |
if ( ! $processor->next_tag( 'svg' ) ) { |
| 194 |
return ''; |
| 195 |
} |
| 196 |
|
| 197 |
if ( is_numeric( $args['size'] ) ) { |
| 198 |
$size = absint( $args['size'] ); |
| 199 |
$processor->set_attribute( 'width', (string) $size ); |
| 200 |
$processor->set_attribute( 'height', (string) $size ); |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! empty( $args['class'] ) ) { |
| 204 |
foreach ( preg_split( '/\s+/', $args['class'], -1, PREG_SPLIT_NO_EMPTY ) as $class_name ) { |
| 205 |
$processor->add_class( $class_name ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if ( ! empty( $args['label'] ) ) { |
| 210 |
$processor->set_attribute( 'role', 'img' ); |
| 211 |
$processor->set_attribute( 'aria-label', $args['label'] ); |
| 212 |
$processor->remove_attribute( 'aria-hidden' ); |
| 213 |
$processor->remove_attribute( 'focusable' ); |
| 214 |
} else { |
| 215 |
$processor->set_attribute( 'aria-hidden', 'true' ); |
| 216 |
$processor->set_attribute( 'focusable', 'false' ); |
| 217 |
$processor->remove_attribute( 'role' ); |
| 218 |
$processor->remove_attribute( 'aria-label' ); |
| 219 |
} |
| 220 |
|
| 221 |
return $processor->get_updated_html(); |
| 222 |
} |
| 223 |
} |
| 224 |
|