Conversation
Prevents a fatal error when uploading an image or extracting alt text in environments where the DOM extension (DOMDocument or DOMXPath) is not available. Props therssoftware. Fixes #66221.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
aaronjorbin
left a comment
There was a problem hiding this comment.
For the tests, it would be best to be thorough. The code notes that there are three possibilities for alt text, so each of those should get tested. Additionally, the tests will return different alt text based on the site local, that functionality should be tested.
| @@ -1086,7 +1086,12 @@ function wp_read_image_metadata( $file ) { | |||
| * @return string Embedded alternative text. | |||
There was a problem hiding this comment.
| * @return string Embedded alternative text. | |
| * @return string Embedded alternative text, empty when there is no alt text or DOM extension is not installed. |
I think the empty string should get noted.
| $this->assertSame( 'This is the Alt Text description to support accessibility in 2025.1', $out['alt'], 'Alt text does not match source.' ); | ||
| } | ||
|
|
||
| /** |
There was a problem hiding this comment.
These should go in their own test file since they are testing something else. tests/phpunit/tests/image/alttext.php feels like is a good choice
| * | ||
| * @covers ::wp_get_image_alttext | ||
| */ | ||
| public function test_wp_get_image_alttext() { |
There was a problem hiding this comment.
Since this will return an empty string when Dom is not available, it should be properly handled in the test as well.
There was a problem hiding this comment.
Thanks for the thorough review and guidance, @aaronjorbin!
I have addressed all the feedback:
- DocBlock: Updated the
@returntag description forwp_get_image_alttext()to note that an empty string is returned when alt text is absent or the DOM extension is not installed. - Dedicated Test File: Moved the tests out of
meta.phpinto a dedicated test class attests/phpunit/tests/image/alttext.php. - Thorough Test Coverage:
- Possibility 1: Exact match on site locale (e.g.
de_DE). - Possibility 2: Partial match on site locale (e.g.
es_ESmatchingxml:lang="es"). - Possibility 3: Fallback to
x-defaultwhen neither exact nor partial locale matches. - Tested that switching site locales returns the corresponding localized alt text.
- Tested edge cases (missing XMP, missing
AltTextAccessibilitynode).
- Possibility 1: Exact match on site locale (e.g.
- DOM Availability: All tests verify the extracted text when DOM is present, while gracefully expecting an empty string if the DOM extension is not installed on the test environment.
… DocBlock. - Updates the DocBlock return description in wp_get_image_alttext() to note empty string return when DOM is unavailable or alt text is missing. - Moves alt text tests from meta.php into tests/phpunit/tests/image/alttext.php. - Adds test cases for exact locale match, partial locale match, and x-default fallback. - Adds test coverage for returning different alt text when switching site locales. - Gracefully handles test assertions in environments where ext-dom is not available. See #66221.
Trac ticket: https://core.trac.wordpress.org/ticket/66221
When uploading an image (or when reading image metadata via
wp_read_image_metadata()),wp_get_image_alttext()is invoked to extract alternative text embedded in XMP metadata.Currently,
wp_get_image_alttext()instantiatesnew DOMDocument()andnew DOMXPath()without verifying if PHP'sdomextension (ext-dom) is installed or available on the host environment:While PHP's
domextension is strongly recommended by WordPress hosting requirements, it is not strictly required across all minimal hosting environments. WordPress core provides guards in other areas (such asiis7_rewrite_rule_exists(),wp_oembed_get(), andWP_Widget_Text) usingclass_exists( 'DOMDocument', false ).Changes Proposed
wp_get_image_alttext()against missingDOMDocumentorDOMXPathclasses early at the beginning of the function:file_get_contents()and running string searches when the metadata cannot be parsed.@covers ::wp_get_image_alttextand unit tests intests/phpunit/tests/image/meta.php:test_wp_get_image_alttext(): Tests extraction with a valid IPTC/XMP test image.test_wp_get_image_alttext_without_xmp(): Tests fallback with an image containing no XMP metadata.Testing Instructions
ext-dom, verify that uploading an image or callingwp_read_image_metadata()completes without a fatal error.AI Disclosure: Code changes and documentation were prepared with AI assistance, verified and tested manually against WordPress Core coding standards and PHPUnit test suite.