Icons: Override WP_Icons_Registry singleton with Gutenberg icons registry#76455
Conversation
|
Flaky tests detected in 60bafaa. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/24122294045
|
There was a problem hiding this comment.
My understanding is that this class was only needed internally to reference the Gutenberg icon registry. Now that we use reflection, this class should no longer be necessary.
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
This "feels wrong" but also makes sense. The only other way I could think of was something I described here:
The other unknown is independent of using reflection or this technique: timing. That is, where do we hook this? In my mind, the earlier we do it, the smaller the chance that the icon registry has already been called by consumers. Meanwhile, we'd probably also want to check the original instance for any registrations and "replay" them onto the new instance, right? |
|
Thanks for the review!
Unfortunately, when I use hooks such as
You're right, addressed in 491c9fb |
|
We could just keep copies of the registry and REST API controllers, and sync them as needed. We're already doing the same for the global styles and theme.json. It's not fancy, but it works 😄 |
This seems like the simplest approach👍 |
| class WP_REST_Icons_Controller_Gutenberg extends WP_REST_Icons_Controller { | ||
| } |
There was a problem hiding this comment.
Currently, there is no need to expand the core controller, but it may be necessary in the future.
|
@t-hamano , I ran into this while fetching the icons using the REST API. I think once this PR is shipped, it will solve the issue. Currently, the class with private access modifiers is being accessed by Edit: I applied this PR's patch in my local, did not solve the issue 😢
|
|
The tests were failing because I accidentally deleted unrelated code. I have fixed everything and it is ready for re-review. |
mcsf
left a comment
There was a problem hiding this comment.
LGTM, just note the Reflection deprecations. Thanks for working on this :)
| function gutenberg_override_wp_icons_registry() { | ||
| $reflection = new ReflectionClass( WP_Icons_Registry::class ); | ||
| $property = $reflection->getProperty( 'instance' ); | ||
| $property->setAccessible( true ); |
There was a problem hiding this comment.
Considering the deprecation of setAccessible, should we do as we did here?
if ( PHP_VERSION_ID < 80100 ) {
$instance_property->setAccessible( true );
}| // If the original registry was already instantiated, replay any icons outside | ||
| // the `core/` namespace onto the Gutenberg registry so they are not lost. | ||
| if ( null !== $original_registry ) { | ||
| $register_method = new ReflectionMethod( WP_Icons_Registry_Gutenberg::class, 'register' ); |
…omment-type Reconcile emoji reactions with trunk's notes architecture refactor. Drop PR's old positioning API (reflowComments, boardOffsets, calculatedOffset, setHeights, setBlockRef) in favor of trunk's floating prop backed by useFloatingBoard. Preserve the reactions data flow (reactionsMap, onToggleReaction) on top of the new architecture. Drop require statements for icons registry files removed in trunk (#76455).

What?
This PR replaces the
WP_Icons_Registrysingleton withGutenberg_Icons_Registry_7_1so that all code usingWP_Icons_Registry::get_instance()receives Gutenberg’s icon registry instead of the Core one.Why?
WP_Icons_Registrywas released in 7.0, but in order to continue expanding this class for future releases, we createdGutenberg_Icons_Registry_7_1, which inherits from the coreWP_Icons_Registry. However, this Gutenberg class is only accessed via the REST API. This means that even if we addGutenberg_Icons_Registry_7_1::{new_method}, it cannot be called asWP_Icons_Registry::{new_method}. This is problematic because the icon registry may be accessed directly in the future, without going through the REST API.How?
This PR uses reflection to replace
WP_Icons_Registry::$instancewith the Gutenberg registry instance, ensuring that theWP_Icons_Registryclass always has the latest methods.Using reflection may feel a bit hacky, but as far as I know, it's the only way to override a singleton class.
Testing Instructions
This is one way to ensure that this PR works properly.