Post: Ensure get_post_custom_values() non-array returns grace fully.#11640
Post: Ensure get_post_custom_values() non-array returns grace fully.#11640liaisontw wants to merge 1 commit intoWordPress:trunkfrom
Conversation
|
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. |
e2c6b6d to
7e9f7a4
Compare
7e9f7a4 to
94ce388
Compare
94ce388 to
0f16735
Compare
Description
This PR improves the robustness of get_post_custom_values() by adding an explicit array check before accessing the data.
Currently, get_post_custom_values() assumes that get_post_custom() always returns an array. However, in certain scenarios (such as an invalid post_id or when no global post is set), get_post_custom() can return false. Attempting to access an array offset on a boolean value triggers a "Cannot use a scalar value as an array" warning in PHP 8.2+.
Changes included in this PR:
Array Validation: Added an is_array() check in get_post_custom_values() to safely handle non-array returns from get_post_custom().
Consistency: Aligns the behavior of get_post_custom_values() with get_post_custom_keys(), ensuring both return early when data is unavailable.
Unit Tests: Introduced a new test file tests/phpunit/tests/post/getPostCustom.php covering happy paths, invalid IDs, and empty key scenarios to prevent future regressions.
Trac ticket: https://core.trac.wordpress.org/ticket/65044
Use of AI Tools
AI assistance: Yes
Tool(s): Gemini
Model(s): Gemini 3 Flash
Used for: Structuring the PHPUnit test suite and drafting the technical justification for the PR. I have personally verified the behavior against PHP 8.2 and ensured the logic aligns with WordPress Core’s historical handling of post meta.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.