Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/wp-includes/class-wp-icon-collections-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function register( $collection_slug, $collection_properties ) {
* Any icons registered under the given collection are also unregistered.
*
* @since 7.1.0
* @since 7.3.0 Unregistering the collection's icons no longer loads their content.
*
* @param string $collection_slug Icon collection slug.
* @return bool True if the collection was unregistered successfully, false otherwise.
Expand All @@ -174,10 +175,14 @@ public function unregister( $collection_slug ) {
return false;
}

/*
* Match on the name prefix rather than using get_registered_icons(), which
* would read every file-based icon from disk just to unregister it.
*/
$icons_registry = WP_Icons_Registry::get_instance();
foreach ( $icons_registry->get_registered_icons() as $icon ) {
if ( isset( $icon['collection'] ) && $icon['collection'] === $collection_slug ) {
$icons_registry->unregister( $icon['name'] );
foreach ( $icons_registry->get_registered_icon_names() as $icon_name ) {
if ( str_starts_with( $icon_name, $collection_slug . '/' ) ) {
$icons_registry->unregister( $icon_name );
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/wp-includes/class-wp-icons-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,34 @@ public function get_registered_icons( $search = '' ) {
return $icons;
}

/**
* Retrieves the names of all registered icons.
*
* Unlike {@see WP_Icons_Registry::get_registered_icons()}, this does not
* load icon content, so no icon files are read from disk.
*
* @since 7.3.0
*
* @param string $search Optional. Search term matched against each icon's name,
* label, and keywords. Default empty string, which returns
* the name of every registered icon.
* @return string[] Namespaced names of the registered icons (e.g. "core/arrow-left").
*/
public function get_registered_icon_names( $search = '' ) {
if ( empty( $search ) ) {
return array_keys( $this->registered_icons );
}

$names = array();
foreach ( $this->registered_icons as $icon_name => $icon ) {
if ( $this->icon_matches_search( $icon, $search ) ) {
$names[] = $icon_name;
}
}

return $names;
}

/**
* Checks if an icon is registered.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,30 @@ public function get_items( $request ) {
);
}

$response = array();
$search = $request->get_param( 'search' );
$icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search );
$public_collections = array();
foreach ( $collections_registry->get_all_registered() as $registered ) {
if ( $registered['public'] ) {
$public_collections[ $registered['slug'] ] = true;
}
}

$response = array();
$search = $request->get_param( 'search' );
$icons_registry = WP_Icons_Registry::get_instance();

foreach ( $icons as $icon ) {
if ( null !== $collection && ( ! isset( $icon['collection'] ) || $icon['collection'] !== $collection ) ) {
/*
* Filter by name before retrieving each icon, so that content is only
* loaded for the icons included in the response.
*/
foreach ( $icons_registry->get_registered_icon_names( $search ) as $icon_name ) {
$icon_collection = strstr( $icon_name, '/', true );
if ( null !== $collection && $icon_collection !== $collection ) {
continue;
}
$icon_collection = isset( $icon['collection'] ) ? $collections_registry->get_registered( $icon['collection'] ) : null;
if ( null === $icon_collection || ! $icon_collection['public'] ) {
if ( ! isset( $public_collections[ $icon_collection ] ) ) {
continue;
}
$icon = $icons_registry->get_registered_icon( $icon_name );
$prepared_icon = $this->prepare_item_for_response( $icon, $request );
$response[] = $this->prepare_response_for_collection( $prepared_icon );
}
Expand Down
65 changes: 64 additions & 1 deletion tests/phpunit/tests/icons/wpIconCollectionsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function set_up() {
}

public function tear_down() {
foreach ( array( 'plugin-a', 'plugin-b', 'my-collection' ) as $slug ) {
foreach ( array( 'plugin-a', 'plugin-a-extra', 'plugin-b', 'my-collection' ) as $slug ) {
if ( $this->collections->is_registered( $slug ) ) {
$this->collections->unregister( $slug );
}
Expand Down Expand Up @@ -189,6 +189,69 @@ public function test_unregister_collection_cascades_to_icons() {
$icons->unregister( 'plugin-b/gamma' );
}

/**
* Unregistering a collection should not load the content of file-based icons.
*
* The icon below points at a file that does not exist, so reading its
* content would trigger an error.
*
* @ticket 66710
*
* @covers ::unregister
*/
public function test_unregister_collection_does_not_load_icon_files() {
$this->collections->register( 'plugin-a', array( 'label' => 'A' ) );
$this->register_icon(
'plugin-a/from-file',
array(
'label' => 'From File',
'file_path' => trailingslashit( get_temp_dir() ) . 'wp-icon-does-not-exist-66710.svg',
)
);

$error_action = new MockAction();
add_action( 'wp_trigger_error_always_run', array( $error_action, 'action' ) );
add_filter( 'wp_trigger_error_trigger_error', '__return_false' );

$this->assertTrue( $this->collections->unregister( 'plugin-a' ), 'The collection should be unregistered.' );
$this->assertFalse( WP_Icons_Registry::get_instance()->is_registered( 'plugin-a/from-file' ), 'The icon should be unregistered along with its collection.' );
$this->assertSame( 0, $error_action->get_call_count(), 'Icon content should not be loaded when unregistering a collection.' );
}

/**
* Unregistering a collection should not remove icons from a collection
* whose slug starts with the same characters.
*
* @ticket 66710
*
* @covers ::unregister
*/
public function test_unregister_collection_does_not_affect_collections_sharing_a_prefix() {
$this->collections->register( 'plugin-a', array( 'label' => 'A' ) );
$this->collections->register( 'plugin-a-extra', array( 'label' => 'A Extra' ) );

$icons = WP_Icons_Registry::get_instance();
$this->register_icon(
'plugin-a/alpha',
array(
'label' => 'Alpha',
'content' => '<svg></svg>',
)
);
$this->register_icon(
'plugin-a-extra/alpha',
array(
'label' => 'Alpha Extra',
'content' => '<svg></svg>',
)
);

$this->assertTrue( $this->collections->unregister( 'plugin-a' ) );

$this->assertFalse( $icons->is_registered( 'plugin-a/alpha' ) );
$this->assertTrue( $icons->is_registered( 'plugin-a-extra/alpha' ) );
}

/**
* Should fail to unregister a collection that was never registered.
*
Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/tests/icons/wpIconsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,4 +804,81 @@ public static function data_get_admin_icon_name_for_dashicon_without_replacement
'array' => array( array( 'dashicons-admin-post' ) ),
);
}

/**
* Should return the names of all registered icons without loading their content.
*
* @ticket 66710
*
* @covers ::get_registered_icon_names
*/
public function test_get_registered_icon_names_does_not_load_icon_content() {
$this->registry->register(
'test-collection/inline',
array(
'label' => 'Inline',
'content' => '<svg><path d="M0 0"/></svg>',
)
);
$this->registry->register(
'test-collection/missing-file',
array(
'label' => 'Missing File',
'file_path' => $this->create_temp_icon_file( null ),
)
);

$error_action = new MockAction();
add_action( 'wp_trigger_error_always_run', array( $error_action, 'action' ) );
add_filter( 'wp_trigger_error_trigger_error', '__return_false' );

$this->assertSame(
array( 'test-collection/inline', 'test-collection/missing-file' ),
$this->registry->get_registered_icon_names(),
'All registered icon names should be returned.'
);
$this->assertSame( 0, $error_action->get_call_count(), 'Icon content should not be loaded.' );
}

/**
* Should only return the names of icons matching the search term.
*
* @ticket 66710
*
* @covers ::get_registered_icon_names
*/
public function test_get_registered_icon_names_filters_by_search() {
$this->registry->register(
'test-collection/dove',
array(
'label' => 'Dove',
'content' => '<svg><path d="M0 0"/></svg>',
'keywords' => array( 'peace' ),
)
);
$this->registry->register(
'test-collection/arrow',
array(
'label' => 'Pointer',
'content' => '<svg><path d="M0 0"/></svg>',
)
);
$this->registry->register(
'test-collection/missing-file',
array(
'label' => 'Missing File',
'file_path' => $this->create_temp_icon_file( null ),
)
);

$error_action = new MockAction();
add_action( 'wp_trigger_error_always_run', array( $error_action, 'action' ) );
add_filter( 'wp_trigger_error_trigger_error', '__return_false' );

$this->assertSame( array( 'test-collection/dove' ), $this->registry->get_registered_icon_names( 'PEACE' ), 'Keywords should match case-insensitively.' );
$this->assertSame( array( 'test-collection/arrow' ), $this->registry->get_registered_icon_names( 'point' ), 'Labels should match.' );
$this->assertSame( array( 'test-collection/missing-file' ), $this->registry->get_registered_icon_names( 'missing-file' ), 'Names should match.' );
$this->assertSame( array(), $this->registry->get_registered_icon_names( 'nothing-matches' ), 'No names should be returned without a match.' );
$this->assertSame( 0, $error_action->get_call_count(), 'Icon content should not be loaded.' );
}
}
70 changes: 69 additions & 1 deletion tests/phpunit/tests/icons/wpRestIconsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function set_up() {
}

public function tear_down() {
foreach ( array( 'rest-visibility-public', 'rest-visibility-private' ) as $slug ) {
foreach ( array( 'rest-visibility-public', 'rest-visibility-private', 'rest-visibility-other' ) as $slug ) {
if ( WP_Icon_Collections_Registry::get_instance()->is_registered( $slug ) ) {
wp_unregister_icon_collection( $slug );
}
Expand Down Expand Up @@ -703,6 +703,74 @@ public function test_get_items_omits_icons_in_non_public_collections( $search )
$this->assertNotContains( 'rest-visibility-private/visibility-icon', $names );
}

/**
* Test that icon content is only loaded for the icons included in the response.
*
* The excluded icon points at a file that does not exist, so loading its
* content would trigger an error.
*
* @ticket 66710
*
* @dataProvider data_get_items_excluded_icons
*
* @covers ::get_items
*
* @param string $route REST API route.
* @param array $query REST API query parameters.
* @param string $excluded_collection Collection of the icon excluded from the response.
* @param bool $is_public Whether the excluded icon's collection is public.
*/
public function test_get_items_does_not_load_content_of_excluded_icons( $route, $query, $excluded_collection, $is_public ) {
$this->register_visibility_collections();
if ( ! WP_Icon_Collections_Registry::get_instance()->is_registered( $excluded_collection ) ) {
wp_register_icon_collection(
$excluded_collection,
array(
'label' => 'REST Visibility Other',
'public' => $is_public,
)
);
}
wp_register_icon(
$excluded_collection . '/visibility-missing-file',
array(
'label' => 'Visibility Missing File',
'file_path' => trailingslashit( get_temp_dir() ) . 'wp-icon-does-not-exist-66710.svg',
)
);

wp_set_current_user( self::$editor_id );

$error_action = new MockAction();
add_action( 'wp_trigger_error_always_run', array( $error_action, 'action' ) );
add_filter( 'wp_trigger_error_trigger_error', '__return_false' );

$request = new WP_REST_Request( 'GET', $route );
$request->set_query_params( $query );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The request should succeed.' );

$names = wp_list_pluck( $response->get_data(), 'name' );
$this->assertContains( 'rest-visibility-public/visibility-icon', $names, 'The public icon should be included.' );
$this->assertNotContains( $excluded_collection . '/visibility-missing-file', $names, 'The excluded icon should not be included.' );
$this->assertSame( 0, $error_action->get_call_count(), 'The content of the excluded icon should not be loaded.' );
}

/**
* Data provider for requests that exclude an icon from the response.
*
* @return array[]
*/
public static function data_get_items_excluded_icons() {
return array(
'all icons, non-public collection' => array( '/wp/v2/icons', array(), 'rest-visibility-private', false ),
'search icons, non-public collection' => array( '/wp/v2/icons', array( 'search' => 'visibility' ), 'rest-visibility-private', false ),
'collection route, other collection' => array( '/wp/v2/icons/rest-visibility-public', array(), 'rest-visibility-other', true ),
'collection param, other collection' => array( '/wp/v2/icons', array( 'collection' => 'rest-visibility-public' ), 'rest-visibility-other', true ),
);
}

/**
* Data provider for icon searches matching both public and non-public collections.
*
Expand Down
Loading