diff --git a/backport-changelog/7.2/13633.md b/backport-changelog/7.2/13633.md new file mode 100644 index 00000000000000..fc8e9ad8ca5e99 --- /dev/null +++ b/backport-changelog/7.2/13633.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/13633 + +* https://github.com/WordPress/gutenberg/pull/83277 diff --git a/lib/class-wp-icon-collections-registry-gutenberg.php b/lib/class-wp-icon-collections-registry-gutenberg.php new file mode 100644 index 00000000000000..8b3fe293f5171b --- /dev/null +++ b/lib/class-wp-icon-collections-registry-gutenberg.php @@ -0,0 +1,87 @@ +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 ); diff --git a/lib/class-wp-icons-registry-gutenberg.php b/lib/class-wp-icons-registry-gutenberg.php index baae1cec20dc60..39b2794ff83481 100644 --- a/lib/class-wp-icons-registry-gutenberg.php +++ b/lib/class-wp-icons-registry-gutenberg.php @@ -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. */ @@ -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( @@ -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'] ) ) @@ -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 ); } } diff --git a/lib/class-wp-rest-icon-collections-controller-gutenberg.php b/lib/class-wp-rest-icon-collections-controller-gutenberg.php new file mode 100644 index 00000000000000..2c749e3cf80f64 --- /dev/null +++ b/lib/class-wp-rest-icon-collections-controller-gutenberg.php @@ -0,0 +1,61 @@ +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 ); + } +} diff --git a/lib/class-wp-rest-icons-controller-gutenberg.php b/lib/class-wp-rest-icons-controller-gutenberg.php index 8ef4035921065a..faa9ae5791b127 100644 --- a/lib/class-wp-rest-icons-controller-gutenberg.php +++ b/lib/class-wp-rest-icons-controller-gutenberg.php @@ -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( @@ -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 ); @@ -132,8 +135,7 @@ 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. @@ -141,7 +143,12 @@ public function get_items( $request ) { 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( diff --git a/lib/compat/wordpress-7.1/icons.php b/lib/compat/wordpress-7.1/icons.php index 8eec638e3ee23b..4e4f5ad6c2a91e 100644 --- a/lib/compat/wordpress-7.1/icons.php +++ b/lib/compat/wordpress-7.1/icons.php @@ -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. */ @@ -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. */ diff --git a/lib/compat/wordpress-7.1/rest-api.php b/lib/compat/wordpress-7.1/rest-api.php index 3b9b0671fe12b9..bd7acf7f608075 100644 --- a/lib/compat/wordpress-7.1/rest-api.php +++ b/lib/compat/wordpress-7.1/rest-api.php @@ -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. diff --git a/lib/icons.php b/lib/icons.php index 03d1db97496874..6732ea0907ce36 100644 --- a/lib/icons.php +++ b/lib/icons.php @@ -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 ); } } diff --git a/lib/load.php b/lib/load.php index c629b223f815f5..ef00a8477f2391 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; diff --git a/lib/rest-api.php b/lib/rest-api.php index 529cff55cbc271..e8d95b9cb16d8b 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -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' ); diff --git a/packages/icons/CHANGELOG.md b/packages/icons/CHANGELOG.md index 9cdebbf36222aa..bc0a4361c53441 100644 --- a/packages/icons/CHANGELOG.md +++ b/packages/icons/CHANGELOG.md @@ -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)). ### Bug Fixes diff --git a/packages/icons/README.md b/packages/icons/README.md index ef49eb1d050c5a..4209a53cf78153 100644 --- a/packages/icons/README.md +++ b/packages/icons/README.md @@ -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`. diff --git a/packages/icons/lib/generate-manifest-php.cjs b/packages/icons/lib/generate-manifest-php.cjs index 5c940462c80e7e..8b33091f6f0b59 100644 --- a/packages/icons/lib/generate-manifest-php.cjs +++ b/packages/icons/lib/generate-manifest-php.cjs @@ -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 }', ),`; } ); @@ -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 = ` _x( 'Block Table', 'icon label', 'gutenberg' ), 'filePath' => 'library/block-table.svg', ), - 'brush' => array( - 'label' => _x( 'Brush', 'icon label', 'gutenberg' ), - 'filePath' => 'library/brush.svg', - 'public' => false, - ), 'calendar' => array( 'label' => _x( 'Calendar', 'icon label', 'gutenberg' ), 'filePath' => 'library/calendar.svg', @@ -146,11 +141,6 @@ 'label' => _x( 'Create', 'icon label', 'gutenberg' ), 'filePath' => 'library/create.svg', ), - 'dashboard' => array( - 'label' => _x( 'Dashboard', 'icon label', 'gutenberg' ), - 'filePath' => 'library/dashboard.svg', - 'public' => false, - ), 'desktop' => array( 'label' => _x( 'Desktop', 'icon label', 'gutenberg' ), 'filePath' => 'library/desktop.svg', @@ -219,20 +209,10 @@ 'label' => _x( 'Language', 'icon label', 'gutenberg' ), 'filePath' => 'library/language.svg', ), - 'link' => array( - 'label' => _x( 'Link', 'icon label', 'gutenberg' ), - 'filePath' => 'library/link.svg', - 'public' => false, - ), 'map-marker' => array( 'label' => _x( 'Map Marker', 'icon label', 'gutenberg' ), 'filePath' => 'library/map-marker.svg', ), - 'media' => array( - 'label' => _x( 'Media', 'icon label', 'gutenberg' ), - 'filePath' => 'library/media.svg', - 'public' => false, - ), 'menu' => array( 'label' => _x( 'Menu', 'icon label', 'gutenberg' ), 'filePath' => 'library/menu.svg', @@ -253,11 +233,6 @@ 'label' => _x( 'Next', 'icon label', 'gutenberg' ), 'filePath' => 'library/next.svg', ), - 'page' => array( - 'label' => _x( 'Page', 'icon label', 'gutenberg' ), - 'filePath' => 'library/page.svg', - 'public' => false, - ), 'paragraph' => array( 'label' => _x( 'Paragraph', 'icon label', 'gutenberg' ), 'filePath' => 'library/paragraph.svg', @@ -274,16 +249,6 @@ 'label' => _x( 'People', 'icon label', 'gutenberg' ), 'filePath' => 'library/people.svg', ), - 'pin' => array( - 'label' => _x( 'Pin', 'icon label', 'gutenberg' ), - 'filePath' => 'library/pin.svg', - 'public' => false, - ), - 'plugins' => array( - 'label' => _x( 'Plugins', 'icon label', 'gutenberg' ), - 'filePath' => 'library/plugins.svg', - 'public' => false, - ), 'plus' => array( 'label' => _x( 'Plus', 'icon label', 'gutenberg' ), 'filePath' => 'library/plus.svg', @@ -340,11 +305,6 @@ 'label' => _x( 'Shuffle', 'icon label', 'gutenberg' ), 'filePath' => 'library/shuffle.svg', ), - 'sites' => array( - 'label' => _x( 'Sites', 'icon label', 'gutenberg' ), - 'filePath' => 'library/sites.svg', - 'public' => false, - ), 'star-empty' => array( 'label' => _x( 'Star Empty', 'icon label', 'gutenberg' ), 'filePath' => 'library/star-empty.svg', @@ -389,16 +349,6 @@ 'label' => _x( 'Tip', 'icon label', 'gutenberg' ), 'filePath' => 'library/tip.svg', ), - 'tool' => array( - 'label' => _x( 'Tool', 'icon label', 'gutenberg' ), - 'filePath' => 'library/tool.svg', - 'public' => false, - ), - 'update' => array( - 'label' => _x( 'Update', 'icon label', 'gutenberg' ), - 'filePath' => 'library/update.svg', - 'public' => false, - ), 'upload' => array( 'label' => _x( 'Upload', 'icon label', 'gutenberg' ), 'filePath' => 'library/upload.svg', @@ -407,9 +357,4 @@ 'label' => _x( 'Verse', 'icon label', 'gutenberg' ), 'filePath' => 'library/verse.svg', ), - 'wordpress' => array( - 'label' => _x( 'WordPress', 'icon label', 'gutenberg' ), - 'filePath' => 'library/wordpress.svg', - 'public' => false, - ), ); diff --git a/phpunit/class-wp-icon-collections-registry-test.php b/phpunit/class-wp-icon-collections-registry-test.php index ad8e7be5bf1d8c..a4448c3177ac63 100644 --- a/phpunit/class-wp-icon-collections-registry-test.php +++ b/phpunit/class-wp-icon-collections-registry-test.php @@ -195,4 +195,80 @@ public function test_unregister_collection_cascades_to_icons() { public function test_unregister_unknown_collection() { $this->assertFalse( $this->collections->unregister( 'ghost' ) ); } + + /** + * Should register collections as public by default. + */ + public function test_register_collection_defaults_to_public() { + $this->collections->register( 'my-collection', array( 'label' => 'My Collection' ) ); + + $this->assertTrue( $this->collections->get_registered( 'my-collection' )['public'] ); + } + + /** + * Should preserve explicitly configured collection visibility. + * + * @dataProvider data_boolean_public_properties + * + * @param bool $is_public Whether the collection is public. + */ + public function test_register_collection_accepts_boolean_public_property( $is_public ) { + $result = $this->collections->register( + 'my-collection', + array( + 'label' => 'My Collection', + 'public' => $is_public, + ) + ); + + $this->assertTrue( $result ); + $this->assertSame( $is_public, $this->collections->get_registered( 'my-collection' )['public'] ); + } + + /** + * Provides supported collection visibility values. + * + * @return array[] + */ + public function data_boolean_public_properties() { + return array( + 'public collection' => array( true ), + 'non-public collection' => array( false ), + ); + } + + /** + * Should reject collection visibility values that are not booleans. + * + * @dataProvider data_non_boolean_public_properties + * @expectedIncorrectUsage WP_Icon_Collections_Registry_Gutenberg::register + * + * @param mixed $is_public Invalid collection visibility value. + */ + public function test_register_collection_rejects_non_boolean_public_property( $is_public ) { + $result = $this->collections->register( + 'my-collection', + array( + 'label' => 'My Collection', + 'public' => $is_public, + ) + ); + + $this->assertFalse( $result ); + $this->assertFalse( $this->collections->is_registered( 'my-collection' ) ); + } + + /** + * Provides unsupported collection visibility values. + * + * @return array[] + */ + public function data_non_boolean_public_properties() { + return array( + 'null' => array( null ), + 'string' => array( 'false' ), + 'integer' => array( 0 ), + 'array' => array( array() ), + ); + } } diff --git a/phpunit/class-wp-icons-registry-gutenberg-test.php b/phpunit/class-wp-icons-registry-gutenberg-test.php index 9941071ed9aaa2..02265404e9b844 100644 --- a/phpunit/class-wp-icons-registry-gutenberg-test.php +++ b/phpunit/class-wp-icons-registry-gutenberg-test.php @@ -497,25 +497,6 @@ public function test_register_icon_without_content_or_file_path() { $this->assertFalse( $this->registry->is_registered( $name ) ); } - /** - * Should fail to register an icon whose `public` property is not a boolean. - * - * @expectedIncorrectUsage WP_Icons_Registry_Gutenberg::register - */ - public function test_register_icon_rejects_non_boolean_public_property() { - $result = $this->registry->register( - 'test-collection/invalid-visibility', - array( - 'label' => 'Icon', - 'content' => '', - 'public' => 'yes', - ) - ); - - $this->assertFalse( $result ); - $this->assertFalse( $this->registry->is_registered( 'test-collection/invalid-visibility' ) ); - } - /** * Provides every SVG file shipped in the `@wordpress/icons` library. * diff --git a/phpunit/class-wp-rest-icon-controller-test.php b/phpunit/class-wp-rest-icon-controller-test.php index 52b6281f63dfd6..d10916e92430d1 100644 --- a/phpunit/class-wp-rest-icon-controller-test.php +++ b/phpunit/class-wp-rest-icon-controller-test.php @@ -37,6 +37,33 @@ public function set_up() { if ( empty( WP_Icons_Registry_Gutenberg::get_instance()->get_registered_icons() ) ) { gutenberg_register_default_icons(); } + + $collections = array( + 'test-public' => true, + 'test-private' => false, + ); + foreach ( $collections as $slug => $is_public ) { + wp_register_icon_collection( + $slug, + array( + 'label' => $slug, + 'public' => $is_public, + ) + ); + wp_register_icon( + $slug . '/visibility-icon', + array( + 'label' => 'Visibility Icon', + 'content' => '', + ) + ); + } + } + + public function tear_down() { + wp_unregister_icon_collection( 'test-public' ); + wp_unregister_icon_collection( 'test-private' ); + parent::tear_down(); } /** @@ -292,30 +319,105 @@ public function test_get_item_requires_authentication() { } /** - * Test that icons registered as non-public are omitted from the collection. + * Test that icons in non-public collections are omitted from lists and search results. + * + * @dataProvider data_icon_visibility_searches + * + * @param string $search Icon search term. */ - public function test_get_items_omits_non_public_icons() { + public function test_get_items_omits_non_public_collections( $search ) { wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/icons' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/icons' ); + $request->set_param( 'search', $search ); $response = rest_get_server()->dispatch( $request ); $this->assertSame( 200, $response->get_status() ); $names = wp_list_pluck( $response->get_data(), 'name' ); - $this->assertContains( 'core/plus', $names ); - $this->assertNotContains( 'core/wordpress', $names ); + $this->assertContains( 'test-public/visibility-icon', $names ); + $this->assertNotContains( 'test-private/visibility-icon', $names ); + } + + /** + * Provides icon searches that match both public and non-public collections. + * + * @return array[] + */ + public function data_icon_visibility_searches() { + return array( + 'all icons' => array( '' ), + 'search icons' => array( 'visibility' ), + ); } /** - * Test that an icon registered as non-public is not readable by name. + * Test that an icon in a non-public collection is not readable by name. */ - public function test_get_item_returns_404_for_non_public_icon() { + public function test_get_item_returns_404_for_non_public_collection() { wp_set_current_user( self::$editor_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/icons/core/wordpress' ); + $request = new WP_REST_Request( 'GET', '/wp/v2/icons/test-private/visibility-icon' ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_icon_not_found', $response, 404 ); } + + /** + * Test that non-public collections cannot be requested by URL or query parameter. + * + * @dataProvider data_non_public_collection_requests + * + * @param string $route REST API route. + * @param array $query REST API query parameters. + */ + public function test_get_collection_icons_returns_404_for_non_public_collection( $route, $query ) { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', $route ); + $request->set_query_params( $query ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 ); + } + + /** + * Provides requests scoped to a non-public collection. + * + * @return array[] + */ + public function data_non_public_collection_requests() { + return array( + 'collection route' => array( '/wp/v2/icons/test-private', array() ), + 'collection query' => array( '/wp/v2/icons', array( 'collection' => 'test-private' ) ), + ); + } + + /** + * Test that non-public collections are omitted from the collection list. + */ + public function test_get_collections_omits_non_public_collections() { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + + $slugs = wp_list_pluck( $response->get_data(), 'slug' ); + $this->assertContains( 'test-public', $slugs ); + $this->assertNotContains( 'test-private', $slugs ); + } + + /** + * Test that non-public collections are not readable by slug. + */ + public function test_get_collection_returns_404_for_non_public_collection() { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/test-private' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 ); + } }