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
3 changes: 3 additions & 0 deletions backport-changelog/7.2/13633.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/13633

* https://github.com/WordPress/gutenberg/pull/83277
87 changes: 87 additions & 0 deletions lib/class-wp-icon-collections-registry-gutenberg.php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for backward compatibility. I add a dedicated collection registry for Gutenberg to ensure that the latest registry supporting public properties is always used across all WordPress versions supported by the Gutenberg plugin, regardless of the state of the core collection registry.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Icons API: Gutenberg icon collections registry.
*
* @package gutenberg
*/

/**
* Adds collection visibility to the WordPress icon collections registry.
*
* @since 7.2.0
*/
class WP_Icon_Collections_Registry_Gutenberg extends WP_Icon_Collections_Registry {

/**
* Registers an icon collection.
*
* @param string $collection_slug Icon collection slug.
* @param array $collection_properties {
* List of properties for the icon collection.
*
* @type string $label Required. A human-readable label for the icon collection.
* @type string $description Optional. A human-readable description for the icon collection.
* @type bool $public Optional. Whether the collection and its icons are exposed through
* the REST API and selectable in the editor's Icon block. Icons in
* non-public collections remain available via {@see wp_get_icon()}.
* Default true.
* }
* @return bool True if the collection was registered successfully, false otherwise.
*/
public function register( $collection_slug, $collection_properties ) {
if ( ! is_array( $collection_properties ) ) {
return parent::register( $collection_slug, $collection_properties );
}

if ( array_key_exists( 'public', $collection_properties ) && ! is_bool( $collection_properties['public'] ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Icon collection public property must be a boolean.', 'gutenberg' ),
'7.2.0'
);
return false;
}

$public = $collection_properties['public'] ?? true;
unset( $collection_properties['public'] );

if ( ! parent::register( $collection_slug, $collection_properties ) ) {
return false;
}

$this->registered_collections[ $collection_slug ]['public'] = $public;
return true;
}

/**
* Returns the shared registry instance, preserving existing collections.
*
* @return WP_Icon_Collections_Registry_Gutenberg The shared registry instance.
*/
public static function get_instance() {
if ( ! self::$instance instanceof self ) {
$original_registry = self::$instance;
$gutenberg_registry = new self();

if ( null !== $original_registry ) {
foreach ( $original_registry->get_all_registered() as $collection ) {
$slug = $collection['slug'];
unset( $collection['slug'] );
$gutenberg_registry->register( $slug, $collection );
}
}

self::$instance = $gutenberg_registry;
}

return self::$instance;
}
}

/**
* Upgrades the shared collection registry before default collections are registered.
*/
function gutenberg_override_wp_icon_collections_registry() {
WP_Icon_Collections_Registry_Gutenberg::get_instance();
}
add_action( 'init', 'gutenberg_override_wp_icon_collections_registry', -1 );
18 changes: 1 addition & 17 deletions lib/class-wp-icons-registry-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ protected function __construct() {}
* If not provided, the content will be retrieved from the `file_path` if set.
* If both `content` and `file_path` are not set, the icon will not be registered.
* @type string $file_path Optional. The full path to the file containing the icon content.
* @type bool $public Optional. Whether the icon is exposed through the REST API, and
* therefore selectable in the editor's Icon block. Non-public icons
* stay available to server-side code via {@see wp_get_icon()}.
* Default true.
* }
* @return bool True if the icon was registered with success and false otherwise.
*/
Expand Down Expand Up @@ -158,7 +154,7 @@ public function register( $icon_name, $icon_properties ) {
return false;
}

$allowed_keys = array_fill_keys( array( 'label', 'content', 'file_path', 'public' ), 1 );
$allowed_keys = array_fill_keys( array( 'label', 'content', 'file_path' ), 1 );
foreach ( array_keys( $icon_properties ) as $key ) {
if ( ! array_key_exists( $key, $allowed_keys ) ) {
_doing_it_wrong(
Expand Down Expand Up @@ -196,15 +192,6 @@ public function register( $icon_name, $icon_properties ) {
return false;
}

if ( isset( $icon_properties['public'] ) && ! is_bool( $icon_properties['public'] ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Icon public property must be a boolean.', 'gutenberg' ),
'7.2.0'
);
return false;
}

if (
( ! isset( $icon_properties['content'] ) && ! isset( $icon_properties['file_path'] ) ) ||
( isset( $icon_properties['content'] ) && isset( $icon_properties['file_path'] ) )
Expand Down Expand Up @@ -397,9 +384,6 @@ public static function get_instance() {
} else {
continue;
}
if ( isset( $icon['public'] ) ) {
$icon_properties['public'] = $icon['public'];
}
$gutenberg_registry->register( $icon['name'], $icon_properties );
}
}
Expand Down
61 changes: 61 additions & 0 deletions lib/class-wp-rest-icon-collections-controller-gutenberg.php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the collection registry, this is also intended to override the core REST controller.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* REST API: Gutenberg icon collections controller.
*
* Changes to this class should be synced to the corresponding class
* in WordPress core: src/wp-includes/rest-api/endpoints/class-wp-rest-icon-collections-controller.php.
*
* @package gutenberg
* @subpackage REST_API
*/

/**
* Gutenberg Icon Collections REST API Controller.
*
* @since 7.2.0
*/
class WP_REST_Icon_Collections_Controller_Gutenberg extends WP_REST_Icon_Collections_Controller {

/**
* Retrieves all public icon collections.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response Response object.
*/
public function get_items( $request ) {
$response = array();
$collections = WP_Icon_Collections_Registry::get_instance()->get_all_registered();
foreach ( $collections as $collection ) {
if ( ! $collection['public'] ) {
continue;
}
$prepared_collection = $this->prepare_item_for_response( $collection, $request );
$response[] = $this->prepare_response_for_collection( $prepared_collection );
}
return rest_ensure_response( $response );
}

/**
* Retrieves a specific public icon collection.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_item( $request ) {
$collection = WP_Icon_Collections_Registry::get_instance()->get_registered( $request['slug'] );

if ( null !== $collection && ! $collection['public'] ) {
return new WP_Error(
'rest_icon_collection_not_found',
sprintf(
/* translators: %s: Icon collection slug. */
__( 'Icon collection not found: "%s".', 'gutenberg' ),
$request['slug']
),
array( 'status' => 404 )
);
}

return parent::get_item( $request );
}
}
21 changes: 14 additions & 7 deletions lib/class-wp-rest-icons-controller-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ public function get_collection_params() {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$collection = $request->get_param( 'collection' );
$collection = $request->get_param( 'collection' );
$collections_registry = WP_Icon_Collections_Registry::get_instance();
$registered_collection = null !== $collection ? $collections_registry->get_registered( $collection ) : null;

if ( null !== $collection && ! WP_Icon_Collections_Registry::get_instance()->is_registered( $collection ) ) {
if ( null !== $collection && ( null === $registered_collection || ! $registered_collection['public'] ) ) {
return new WP_Error(
'rest_icon_collection_not_found',
sprintf(
Expand All @@ -116,10 +118,11 @@ public function get_items( $request ) {
$icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search );

foreach ( $icons as $icon ) {
if ( false === ( $icon['public'] ?? true ) ) {
if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) {
continue;
}
if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) {
$icon_collection = $collections_registry->get_registered( $icon['collection'] );
if ( null === $icon_collection || ! $icon_collection['public'] ) {
continue;
}
$prepared_icon = $this->prepare_item_for_response( $icon, $request );
Expand All @@ -132,16 +135,20 @@ public function get_items( $request ) {
/**
* Retrieves a specific icon from the registry.
*
* Icons registered as non-public are reported as not found. Icons registered
* without an explicit `public` property are public.
* Icons belonging to non-public collections are reported as not found.
*
* @param string $name Icon name.
* @return array|WP_Error Icon data on success, or WP_Error object on failure.
*/
public function get_icon( $name ) {
$icon = parent::get_icon( $name );

if ( ! is_wp_error( $icon ) && false === ( $icon['public'] ?? true ) ) {
if ( is_wp_error( $icon ) ) {
return $icon;
}

$collection = WP_Icon_Collections_Registry::get_instance()->get_registered( $icon['collection'] );
if ( null === $collection || ! $collection['public'] ) {
return new WP_Error(
'rest_icon_not_found',
sprintf(
Expand Down
8 changes: 4 additions & 4 deletions lib/compat/wordpress-7.1/icons.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*
* @type string $label Required. A human-readable label for the icon collection.
* @type string $description Optional. A human-readable description for the icon collection.
* @type bool $public Optional. Whether the collection and its icons are exposed through
* the REST API and selectable in the editor's Icon block. Icons in
* non-public collections remain available via {@see wp_get_icon()}.
* Default true.
* }
* @return bool True if the icon collection was registered successfully, else false.
*/
Expand Down Expand Up @@ -52,10 +56,6 @@ function wp_unregister_icon_collection( $slug ) {
* If not provided, the content will be retrieved from the `file_path` if set.
* If both `content` and `file_path` are not set, the icon will not be registered.
* @type string $file_path Optional. The full path to the file containing the icon content.
* @type bool $public Optional. Whether the icon is exposed through the REST API, and
* therefore selectable in the editor's Icon block. Non-public icons
* stay available to server-side code via {@see wp_get_icon()}.
* Default true.
* }
* @return bool True if the icon was registered successfully, else false.
*/
Expand Down
9 changes: 0 additions & 9 deletions lib/compat/wordpress-7.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ function gutenberg_allow_null_modified_wp_template_field() {
}
add_action( 'rest_api_init', 'gutenberg_allow_null_modified_wp_template_field' );

/**
* Registers the Icon Collections Registry REST API routes.
*/
function gutenberg_register_icon_collections_controller_endpoints() {
$icon_collections_controller = new WP_REST_Icon_Collections_Controller();
$icon_collections_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_icon_collections_controller_endpoints' );

/**
* Overrides the REST controller for the attachment post type to add support
* for filtering by multiple media types.
Expand Down
4 changes: 0 additions & 4 deletions lib/icons.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ function gutenberg_register_default_icons() {
'file_path' => $icons_directory . $icon_data['filePath'],
);

if ( isset( $icon_data['public'] ) ) {
$icon_args['public'] = $icon_data['public'];
}

wp_register_icon( 'core/' . $icon_name, $icon_args );
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function gutenberg_is_experiment_enabled( $name ) {
// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
require_once __DIR__ . '/class-wp-rest-edit-site-export-controller-gutenberg.php';
require_once __DIR__ . '/class-wp-icon-collections-registry-gutenberg.php';
require_once __DIR__ . '/class-wp-rest-icon-collections-controller-gutenberg.php';
require_once __DIR__ . '/class-wp-icons-registry-gutenberg.php';
require_once __DIR__ . '/class-wp-rest-icons-controller-gutenberg.php';
require_once __DIR__ . '/rest-api.php';
Expand Down
9 changes: 9 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ function gutenberg_register_icon_controller_endpoints() {
$icons_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_icon_controller_endpoints' );

/**
* Registers the Icon Collections Registry REST API routes.
*/
function gutenberg_register_icon_collections_controller_endpoints() {
$icon_collections_controller = new WP_REST_Icon_Collections_Controller_Gutenberg();
$icon_collections_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_icon_collections_controller_endpoints' );
1 change: 1 addition & 0 deletions packages/icons/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Redraw a further 236 icons as stroke-based for variable stroke-width support, following the convention introduced in [#78808](https://github.com/WordPress/gutenberg/pull/78808), and refine the already stroke-based `image` and `commentAuthorAvatar` drawings. Most retain their original footprint; the table icons, `capturePhoto`, and `image` are slightly smaller, and several drawings in the final batch change by design. ([#82540](https://github.com/WordPress/gutenberg/pull/82540), [#82754](https://github.com/WordPress/gutenberg/pull/82754))
- `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)).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe we can just remove the following old entry instead? It's still in the Unreleased section.

manifest.json: The public property is now a tri-state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly, this is confusing, but I believe that changelog entries should basically be append-only and past entries should not be removed. As long as README.md reflects the latest specifications, it shouldn't be an issue.


### Bug Fixes

Expand Down
6 changes: 2 additions & 4 deletions packages/icons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ 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): Controls whether the icon is shipped to WordPress core, and if so how far it is exposed. **Important**: Once an icon ships, removing it is difficult, so carefully consider this field before setting it.
- Omit the field to keep the icon in the JS library only. It is not shipped to core.
- `true` ships the icon to core and exposes it through the SVG Icons API, which makes it selectable in the Icon block.
- `false` ships the icon to core and registers it for server-side use through `wp_get_icon()`, but hides it from the REST API and the Icon block. Use this for icons that belong to the admin UI rather than to post content, such as admin menu icons.
- `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.

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.

After adding your icon, run `npm run build` to generate the TypeScript files and update `manifest.php`.
Expand Down
11 changes: 3 additions & 8 deletions packages/icons/lib/generate-manifest-php.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ function generatePHPArray( manifest ) {
const label = escapePHPString( item.label );
const filePath = escapePHPString( item.filePath );

const publicEntry =
item.public === false ? "\n\t\t'public' => false," : '';

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

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

/**
* Generates manifest.php from manifest.json.
* Only includes icons which have the `public` key set.
* Only includes icons which have `public` set to true.
*/
async function generateManifestPHP() {
const manifestJson = await readFile( MANIFEST_JSON_PATH, 'utf8' );
const manifest = JSON.parse( manifestJson );
const includedIcons = manifest.filter(
( item ) => typeof item.public === 'boolean'
);
const includedIcons = manifest.filter( ( item ) => item.public === true );
const phpHeader = `<?php
// This file is automatically generated. Do not edit directly.
if ( ! defined( 'ABSPATH' ) ) {
Expand Down
Loading
Loading