Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backport-changelog/7.2/13625.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/13625
Comment thread
t-hamano marked this conversation as resolved.
Resolved

* https://github.com/WordPress/gutenberg/pull/83261
* https://github.com/WordPress/gutenberg/pull/83277
3 changes: 0 additions & 3 deletions backport-changelog/7.2/13633.md

This file was deleted.

4 changes: 2 additions & 2 deletions lib/class-wp-icons-registry-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public function get_registered_icons( $search = '' ) {
* The base `$instance` slot is intentionally not redefined, so both
* `WP_Icons_Registry::get_instance()` (used by core) and this method share
* one instance. An existing base registry is upgraded, replaying any
* non-`core/` icons so they are not lost.
* non-`core/` and non-`core-admin/` icons so they are not lost.
*/
public static function get_instance() {
if ( ! self::$instance instanceof self ) {
Expand All @@ -373,7 +373,7 @@ public static function get_instance() {

if ( null !== $original_registry ) {
foreach ( $original_registry->get_registered_icons() as $icon ) {
if ( str_starts_with( $icon['name'], 'core/' ) ) {
if ( str_starts_with( $icon['name'], 'core/' ) || str_starts_with( $icon['name'], 'core-admin/' ) ) {
continue;
}
$icon_properties = array( 'label' => $icon['label'] );
Expand Down
25 changes: 22 additions & 3 deletions lib/icons.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Icons API: registration of the core icon collection and its icons from the
* Icons API: registration of the core icon collections and their icons from the
* plugin's own `packages/icons` manifest, replacing the core registration.
*
* @package gutenberg
Expand All @@ -17,6 +17,14 @@ function gutenberg_register_default_icon_collections() {
'description' => __( 'Core icon collection.', 'gutenberg' ),
)
);
wp_register_icon_collection(
'core-admin',
array(
'label' => __( 'WordPress Admin', 'gutenberg' ),
'description' => __( 'Icon collection used by the WordPress admin interface.', 'gutenberg' ),
'public' => false,
)
);
}

$default_icon_collections_priority = has_action( 'init', '_wp_register_default_icon_collections' );
Expand All @@ -26,7 +34,7 @@ function gutenberg_register_default_icon_collections() {
add_action( 'init', 'gutenberg_register_default_icon_collections', 0 );

/**
* Registers the default core icons from the Gutenberg manifest.
* Registers the default core and core-admin icons from the Gutenberg manifest.
*/
function gutenberg_register_default_icons() {
$icons_directory = gutenberg_dir_path() . 'packages/icons/src';
Expand Down Expand Up @@ -64,12 +72,23 @@ function gutenberg_register_default_icons() {
return;
}

if ( empty( $icon_data['collections'] ) || ! is_array( $icon_data['collections'] ) ) {
_doing_it_wrong(
__FUNCTION__,
__( 'Core icon collection manifest must provide a non-empty "collections" array for each icon.', 'gutenberg' ),
'7.2.0'
);
return;
}

$icon_args = array(
'label' => $icon_data['label'],
'file_path' => $icons_directory . $icon_data['filePath'],
);

wp_register_icon( 'core/' . $icon_name, $icon_args );
foreach ( $icon_data['collections'] as $collection_slug ) {
wp_register_icon( $collection_slug . '/' . $icon_name, $icon_args );
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/icons/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `manifest.json`: The `public` property is now a tri-state. Omitting it keeps an icon in the JS library only; `true` ships it to WordPress Core and exposes it through the icons REST API, making it selectable in the Icon block; `false` ships it and registers it in the `core` collection for server-side use via `wp_get_icon()`, while hiding it from the REST API and the Icon block. ([#82634](https://github.com/WordPress/gutenberg/pull/82634))
- The `brush`, `dashboard`, `link`, `media`, `page`, `pin`, `plugins`, `sites`, `tool`, `update` and `wordpress` icons now ship to WordPress Core as non-public icons: they are registered in the `core` collection and stay available to server-side code via `wp_get_icon()`, but are not exposed through the icons REST API and are not selectable in the Icon block. ([#82634](https://github.com/WordPress/gutenberg/pull/82634), [#79451](https://github.com/WordPress/gutenberg/pull/79451))
- `manifest.json`: The `public` field now only controls whether an icon ships to WordPress Core ([#83277](https://github.com/WordPress/gutenberg/pull/83277)).
- `manifest.json`: New `collections` property, listing the collections an icon is registered in once it ships to WordPress Core. It replaces the `public` property and introduces the `core-admin` collection, which is not public and holds the icons the WordPress admin interface renders. ([#83261](https://github.com/WordPress/gutenberg/pull/83261), [#79451](https://github.com/WordPress/gutenberg/pull/79451))

### Bug Fixes

Expand Down
4 changes: 3 additions & 1 deletion packages/icons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ To add a new icon to the library, follow these steps:
- `slug`: The icon identifier (should match the SVG filename without the `.svg` extension)
- `label`: The human-readable label for the icon. Use Title Case (for example, `My New Icon`).
- `filePath`: The relative path to the SVG file (e.g., `library/my-new-icon.svg`)
- `public` (optional): Set to `true` to ship the icon to WordPress Core and register it in the `core` collection. Omit it to keep the icon in the JS library only. **Important**: Once an icon ships to WordPress Core, removing it is difficult, so carefully consider this field before setting it.
- `collections` (optional): The collections the icon is registered in once it ships to WordPress Core. Omit it to keep the icon in the JS library only. **Important**: Once an icon ships to WordPress Core, removing it is difficult, so carefully consider this field before setting it. The available collections are:
- `core`: the public collection. Its icons are exposed through the SVG Icons API, which makes them selectable in the Icon block.
- `core-admin`: the collection the WordPress admin interface renders from. It is not public, so its icons are never exposed through the REST API or selectable in the Icon block, and unregistering `core` cannot take them away from the admin interface.

4. **Do not edit `manifest.php`**: The `manifest.php` file is automatically generated from `manifest.json` by the build script. Do not edit it manually, as your changes will be overwritten when the build runs.

Expand Down
15 changes: 11 additions & 4 deletions packages/icons/lib/generate-manifest-php.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ function generatePHPArray( manifest ) {
const key = formatPHPKey( item.slug, maxKeyLength );
const label = escapePHPString( item.label );
const filePath = escapePHPString( item.filePath );
const collections = item.collections
.map( ( collection ) => `'${ escapePHPString( collection ) }'` )
.join( ', ' );

return `${ key } => array(
'label' => _x( '${ label }', 'icon label', 'gutenberg' ),
'filePath' => '${ filePath }',
'label' => _x( '${ label }', 'icon label', 'gutenberg' ),
'filePath' => '${ filePath }',
'collections' => array( ${ collections } ),
),`;
} );

Expand All @@ -56,12 +60,15 @@ ${ phpEntries.join( '\n' ) }

/**
* Generates manifest.php from manifest.json.
* Only includes icons which have `public` set to true.
* Only includes icons which belong to at least one collection, since those are
* the icons shipped to WordPress Core.
*/
async function generateManifestPHP() {
const manifestJson = await readFile( MANIFEST_JSON_PATH, 'utf8' );
const manifest = JSON.parse( manifestJson );
const includedIcons = manifest.filter( ( item ) => item.public === true );
const includedIcons = manifest.filter(
( item ) => item.collections?.length > 0
);
const phpHeader = `<?php
// This file is automatically generated. Do not edit directly.
if ( ! defined( 'ABSPATH' ) ) {
Expand Down
36 changes: 26 additions & 10 deletions packages/icons/lib/validate-collection.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const { readdir, stat, readFile } = require( 'fs/promises' );
const ICON_LIBRARY_DIR = path.join( __dirname, '..', 'src', 'library' );
const ICON_VIEW_BOX = '0 0 24 24';

// The collections an icon may be shipped in.
const VALID_ICON_COLLECTIONS = [ 'core', 'core-admin' ];

function isStrokeBasedSvg( svgContent ) {
const svgTag = svgContent.match( /<svg\b[^>]*>/ )?.[ 0 ];
return /\sstyle=(["'])fill\s*:\s*none\s*;?\s*\1/.test( svgTag ?? '' );
Expand All @@ -13,7 +16,8 @@ function isStrokeBasedSvg( svgContent ) {
* Validating the icons collection checks that:
*
* - Each manifest entry has a matching SVG in library/, and vice versa.
* - Each manifest entry's `public` property, if present, is a boolean.
* - Each manifest entry's `collections` property, if present, is a non-empty array of
* known collection slugs.
* - Each SVG uses currentColor so icons inherit text color.
* - Each SVG uses viewBox="0 0 24 24".
* - Each stroke-based SVG contains at least one stroked graphical element.
Expand Down Expand Up @@ -62,16 +66,28 @@ async function validateCollection() {
manifestPaths.push( icon.filePath );

/*
* Verify that `public`, if present, is a boolean.
* Verify that `collections`, if present, lists known collection slugs. An icon
* without the property stays in the JS library and is not shipped to core.
*/
if ( 'public' in icon && typeof icon.public !== 'boolean' ) {
problems.push(
`- Invalid icon definition for icon '${
icon.slug
}': expected 'public' to be true or false, saw ${ JSON.stringify(
icon.public
) }`
);
if ( 'collections' in icon ) {
const { collections } = icon;
const isValid =
Array.isArray( collections ) &&
collections.length > 0 &&
collections.every( ( collection ) =>
VALID_ICON_COLLECTIONS.includes( collection )
) &&
new Set( collections ).size === collections.length;

if ( ! isValid ) {
problems.push(
`- Invalid icon definition for icon '${
icon.slug
}': expected 'collections' to be a non-empty array of unique slugs out of ${ VALID_ICON_COLLECTIONS.map(
( collection ) => `'${ collection }'`
).join( ', ' ) }, saw ${ JSON.stringify( collections ) }`
);
}
}

/*
Expand Down
Loading
Loading