Skip to content

Icons: Enforce strict name validation in register method#76079

Open
im3dabasia wants to merge 13 commits intoWordPress:trunkfrom
im3dabasia:fix/followup-72215
Open

Icons: Enforce strict name validation in register method#76079
im3dabasia wants to merge 13 commits intoWordPress:trunkfrom
im3dabasia:fix/followup-72215

Conversation

@im3dabasia
Copy link
Copy Markdown
Contributor

@im3dabasia im3dabasia commented Mar 3, 2026

What?

Follow-up: #72215 (comment) (3rd point)

Improves validation in WP_Icons_Registry::register and adds comprehensive unit tests for icon registration, including duplicate and invalid name handling.

Why?

This PR ensures icon names are strictly validated (must be strings, namespace-prefixed, and lowercase), mirroring the approach used in WP_Block_Type_Registry. It also adds tests to verify that invalid names and duplicate registrations are correctly rejected, improving reliability and consistency.

How?

  • Refactored register() to enforce:

    • Only string names are accepted.
    • Names must be namespace-prefixed (plugin/icon).
    • No uppercase characters allowed.
    • Duplicate registrations are rejected.
  • Added PHPUnit tests for:

    • Non-string names
    • Names without namespace
    • Names with uppercase characters
    • Duplicate registration (ensures count does not increase)
    • Valid registration

@im3dabasia im3dabasia requested a review from spacedmonkey as a code owner March 3, 2026 11:39
@im3dabasia im3dabasia added the [Package] Icons /packages/icons label Mar 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 3, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org>
Co-authored-by: mcsf <mcsf@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot removed the [Package] Icons /packages/icons label Mar 3, 2026
Copy link
Copy Markdown
Contributor Author

@im3dabasia im3dabasia left a comment

Choose a reason for hiding this comment

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

Hey @mcsf ,

I picked up one of the follow-up related issue from here: #72215 (comment). When you have a chance, I’d love to hear your thoughts on this PR.

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.

I am not sure if this is the best filename. Please guide me in naming this correctly; I will update it.

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.

This looks fine, thanks. :)

*
* @group icons
*/
class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase {
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.

Similarly, I think the class name is not the correct one here.

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.

I think we should use Gutenberg_Icons_Registry_7_1 instead.

@im3dabasia im3dabasia added [Package] Icons /packages/icons [Type] Experimental Experimental feature or API. labels Mar 3, 2026
@github-actions github-actions bot removed the [Package] Icons /packages/icons label Mar 3, 2026
@im3dabasia im3dabasia requested review from mcsf and t-hamano March 3, 2026 12:05
Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

We're currently working on some structural changes to further develop the icon registry on the Gutenberg plugin, so it might be worth coming back to this PR once that's complete.

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. [Feature] Icon Related to Icon registration API and Icon REST API and removed [Type] Experimental Experimental feature or API. labels Mar 3, 2026
Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Hi @im3dabasia, since #75878 has been merged, can you update this PR accordingly? We should update lib/compat/wordpress-7.1/class-gutenberg-icons-registry-7-1.php instead.

We also need to submit a core PR. See this core ticket: https://core.trac.wordpress.org/ticket/64847

If there's anything you don't understand, please feel free to ask 👍

*
* @group icons
*/
class Tests_Icons_WpIconsRegistry extends WP_UnitTestCase {
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.

I think we should use Gutenberg_Icons_Registry_7_1 instead.

@im3dabasia im3dabasia requested a review from t-hamano March 13, 2026 08:28
@im3dabasia
Copy link
Copy Markdown
Contributor Author

@t-hamano, I have updated the PR based on the feedback and also learned quite a bit from this PR (#75550), which is in a similar position to mine. I would appreciate your feedback on this PR whenever you have a moment.

Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

We also need to submit a core PR. See this core ticket: https://core.trac.wordpress.org/ticket/64847

Can you confirm this point? Specifically, the following steps are required:

@im3dabasia im3dabasia changed the title Icons Registry: Enforce strict name validation in register method Icons: Enforce strict name validation in register method Mar 13, 2026
@im3dabasia
Copy link
Copy Markdown
Contributor Author

  1. Create a core PR that is the same as this PR. Link the pull request you created to PR

I added the exact same test cases in Core under a more suitable file name: wpIconsRegistry.php. I also changed the class name to match what Core uses and used WP_Icons_Registry in set_up() instead of what this PR uses.

I chose to keep the tests in a separate file because they are not related to the REST API. The other file in that directory contains tests related to the REST API, whereas my tests directly test the functions without using the API. I believe having a separate file for this should not be an issue, but please let me know what you think.

I would appreciate some quick feedback on my PR (the Trac one). This is my first time doing this, and I really appreciate your constant guidance. Thanks!

  1. Create a backport changelog (ref)

I did this in the following commit: 6b9cd32

Copy link
Copy Markdown
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, @im3dabasia!

I pushed some commits directly to your fork. Have a look: it was all related to the test suite.

I also spot some small issues with the registration checks themselves. Once you've addressed those and once CI clears, we should be good to merge.

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.

This looks fine, thanks. :)

return false;
}

$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
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.

It's best to use the same pattern found in the REST controller, not just for consistency but because it's stricter:

'/' . $this->rest_base . '/(?P<name>[a-z][a-z0-9-]*/[a-z][a-z0-9-]*)',

return false;
}

if ( preg_match( '/[A-Z]+/', $icon_name ) ) {
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.

Nit: the + is unnecessary.

Suggested change
if ( preg_match( '/[A-Z]+/', $icon_name ) ) {
if ( preg_match( '/[A-Z]/', $icon_name ) ) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Icon Related to Icon registration API and Icon REST API [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants