Icons: Use snake_case file_path key in icon registry#79100
Conversation
The generated `manifest.php` and the PHP icon registry exposed the icon path under a camelCase `filePath` key, which is inconsistent with PHP/WP array-key conventions. Rename it to `file_path` across the manifest PHP generator, both registry classes, and the test docblock. The `manifest.json` source intentionally keeps `filePath` (JS convention). Because the Gutenberg override inherits `get_content()` from the base class, also override `get_content()` in `WP_Icons_Registry_Gutenberg` so content is read from the `file_path` property even when the base class comes from WordPress core (which may still use `filePath`), preventing a key mismatch that would silently break icon content retrieval. Co-Authored-By: Claude <[email protected]>
|
Size Change: 0 B Total Size: 8.46 MB |
Align the icon registry and generated manifest with WordPress core's snake_case array-key convention, which is the dominant style across core PHP. This updates the manifest entries, the registry validation and `get_content()` lookup, the allowed property keys, and the related docblocks and error messages. Mirrors the same rename made on the Gutenberg side in WordPress/gutenberg#79100. Co-Authored-By: Claude <[email protected]>
|
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. |
Following the upstream Gutenberg rename of the icon manifest key from filePath to file_path (WordPress/gutenberg#79100), adjust the copy:icon-library-manifest Grunt task to match the new file_path key when stripping the 'library/' path prefix, so regeneration keeps producing valid icon paths. Co-Authored-By: Claude <[email protected]>
|
Flaky tests detected in 84cdbb9. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/27949322398
|
file_path key in PHP icon manifest and registryfile_path key in icon registry
… in registry The previous commit renamed the icon path key to snake_case file_path across both the generated manifest.php and its generator. The manifest mirrors the JS manifest.json, which uses camelCase filePath, so the generated PHP manifest should keep filePath for consistency with its source. Revert the generator and manifest.php back to filePath. The PHP registry still exposes the path as file_path (PHP/WP array-key convention), so register_collection now reads the camelCase filePath from the manifest and converts it to file_path when registering. This confines the filePath-to-file_path conversion to the registry boundary. Co-Authored-By: Claude <[email protected]>
Align the icon registry and generated manifest with WordPress core's snake_case array-key convention, which is the dominant style across core PHP. This updates the manifest entries, the registry validation and `get_content()` lookup, the allowed property keys, and the related docblocks and error messages. Mirrors the same rename made on the Gutenberg side in WordPress/gutenberg#79100. Co-Authored-By: Claude <[email protected]>
Following the upstream Gutenberg rename of the icon manifest key from filePath to file_path (WordPress/gutenberg#79100), adjust the copy:icon-library-manifest Grunt task to match the new file_path key when stripping the 'library/' path prefix, so regeneration keeps producing valid icon paths. Co-Authored-By: Claude <[email protected]>
| /** | ||
| * Redefined to read the icon content from the `file_path` property. | ||
| * | ||
| * @param string $icon_name Icon name including namespace. | ||
| * @return string|null The content of the icon, if found. | ||
| */ | ||
| protected function get_content( $icon_name ) { | ||
| if ( ! isset( $this->registered_icons[ $icon_name ]['content'] ) ) { | ||
| $content = file_get_contents( | ||
| $this->registered_icons[ $icon_name ]['file_path'] | ||
| ); | ||
| $content = $this->sanitize_icon_content( $content ); | ||
|
|
||
| if ( empty( $content ) ) { | ||
| wp_trigger_error( | ||
| __METHOD__, | ||
| __( 'Icon content does not contain valid SVG markup.', 'gutenberg' ) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| $this->registered_icons[ $icon_name ]['content'] = $content; | ||
| } | ||
| return $this->registered_icons[ $icon_name ]['content']; | ||
| } | ||
|
|
There was a problem hiding this comment.
Why was this function added? It seems to expand the scope of this PR beyond its original purpose.
There was a problem hiding this comment.
This is a bit tricky, but if the Gutenberg plugin is active on WordPress 7.0, the WP_Icons_Registry base class already exists in core, so the if statement here evaluates to false. This means the old get_content method, which relies on an outdated filePath, will be used, causing unintended behavior. To prevent this, we need to completely override this method on the Gutenberg side.
| * @type string $label Required. A human-readable label for the icon. | ||
| * @type string $content Optional. SVG markup for the icon. | ||
| * If not provided, the content will be retrieved from the `filePath` if set. | ||
| * If both `content` and `filePath` are not set, the icon will not be registered. | ||
| * @type string $filePath Optional. The full path to the file containing the icon content. | ||
| * 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. |
There was a problem hiding this comment.
What adjustments are needed?
| } | ||
|
|
||
| $allowed_keys = array_fill_keys( array( 'label', 'content', 'filePath' ), 1 ); | ||
| $allowed_keys = array_fill_keys( array( 'label', 'content', 'file_path' ), 1 ); |
There was a problem hiding this comment.
What happens if I have been using filePath in my plugin? Should we add a compat layer for that (a _doing_it_wrong() + a graceful transformation to file_path?
There was a problem hiding this comment.
What happens if I have been using
filePathin my plugin?
WP_Icons_Registry does not currently allow the registration of third-party icons, and its register method is protected. Therefore, it is not fundamentally intended to be executed externally unless reflection methods are used. This means that, in practice, filePath should not be used externally. Consequently, we believe a compat layer is unnecessary.
In the 7.1 release, I intend to make the register method public to allow third-party icons to be registered. Since changing parameter names will be difficult once the method is public, I wanted to address this issue now.
There was a problem hiding this comment.
Makes sense, thanks. I'm not worried about folks going the reflection approach, since it doesn't rely on any public API contract.
The registry rename to snake_case `file_path` only updated test docblocks without exercising the property. Cover registering via `file_path`, reading content from the referenced file, and rejecting icons that supply both or neither of `content`/`file_path`. Co-Authored-By: Claude <[email protected]>
tyxla
left a comment
There was a problem hiding this comment.
I'm still seeing a few instances of filePath in the plugin - in the constructor methods of the icons registry classes. Did we intend to leave those intact, and if so, why?
| $name = 'test-plugin/file-path-icon'; | ||
| $settings = array( | ||
| 'label' => 'Icon', | ||
| 'file_path' => $file_path, |
There was a problem hiding this comment.
Should we also add a rejection test for when someone uses filePath during registration?
There was a problem hiding this comment.
I added these unit tests because there were no unit tests for icon registration using the file_path parameter in the first place. Currently, consumers cannot register custom icons at all, so I believe additional tests are unnecessary.
t-hamano
left a comment
There was a problem hiding this comment.
@tyxla Thanks for the review!
I'm still seeing a few instances of
filePathin the plugin - in the constructor methods of the icons registry classes. Did we intend to leave those intact, and if so, why?
This is intentional. The filePath within the constructor method originates from manifest.php when registering core icons, and this manifest.php is synchronized with the core. Therefore, simply changing these to file_path would prevent the synchronization process from functioning correctly.
While it's possible to modify the synchronization process itself, inconsistencies could arise unless the timing of merging core PRs and synchronizing the core with Gutenberg is strictly aligned. Since this filePath is not exposed externally, I believe it's acceptable to leave it as is.
| $name = 'test-plugin/file-path-icon'; | ||
| $settings = array( | ||
| 'label' => 'Icon', | ||
| 'file_path' => $file_path, |
There was a problem hiding this comment.
I added these unit tests because there were no unit tests for icon registration using the file_path parameter in the first place. Currently, consumers cannot register custom icons at all, so I believe additional tests are unnecessary.
|
Oh, and what's up with the plugin zip check? A one-off hiccup? |
|
It seems the CI error was temporary 👍 |
* Icons: Use snake_case `file_path` key in PHP icon manifest and registry The generated `manifest.php` and the PHP icon registry exposed the icon path under a camelCase `filePath` key, which is inconsistent with PHP/WP array-key conventions. Rename it to `file_path` across the manifest PHP generator, both registry classes, and the test docblock. The `manifest.json` source intentionally keeps `filePath` (JS convention). Because the Gutenberg override inherits `get_content()` from the base class, also override `get_content()` in `WP_Icons_Registry_Gutenberg` so content is read from the `file_path` property even when the base class comes from WordPress core (which may still use `filePath`), preventing a key mismatch that would silently break icon content retrieval. Co-Authored-By: Claude <[email protected]> * Add backport changelog * Icons: Keep camelCase filePath in manifest, convert to file_path only in registry The previous commit renamed the icon path key to snake_case file_path across both the generated manifest.php and its generator. The manifest mirrors the JS manifest.json, which uses camelCase filePath, so the generated PHP manifest should keep filePath for consistency with its source. Revert the generator and manifest.php back to filePath. The PHP registry still exposes the path as file_path (PHP/WP array-key convention), so register_collection now reads the camelCase filePath from the manifest and converts it to file_path when registering. This confines the filePath-to-file_path conversion to the registry boundary. Co-Authored-By: Claude <[email protected]> * Icons: Add unit tests for the `file_path` icon property The registry rename to snake_case `file_path` only updated test docblocks without exercising the property. Cover registering via `file_path`, reading content from the referenced file, and rejecting icons that supply both or neither of `content`/`file_path`. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Part of #75715
What?
Rename the camelCase
filePathkey to snake_casefile_pathin the PHP icon registry.Why?
The PHP-side icon path was exposed under a camelCase
filePathkey, which is inconsistent with PHP/WordPress array-key conventions.Furthermore, with #77260, we are attempting to build the foundation for custom icon registration. By shipping this PR, I expect to change the icon registration method as follows:
How?
This PR maintains full backward compatibility regardless of whether the base class (
WP_Icons_Registry) exists in core:lib/compat/wordpress-7.0/class-wp-icons-registry.phpdefines the class with the file_path key.class_existsguard, butWP_Icons_Registry_Gutenberg::get_content()overrides content retrieval to read fromfile_path, so it does not break even if core's base class still keys onfilePath.Testing Instructions
Use of AI Tools
This PR was authored with assistance from Claude Code. All changes were reviewed by the author.