Repository navigation
Docs: Document hooks that may pass a null post object - #15257
westonruter wants to merge 8 commits into
Conversation
… parameter may be null
The `post_password_required` and `the_password_form` filters are applied after `get_post()` without a null check, so they receive null when no post is found (e.g. calling `post_password_required()` outside the loop). `post_password_required()` even reaches the filter on its first branch because `empty( $post->post_password )` is true for null. The `media_view_settings` and `media_view_strings` filters receive null whenever `wp_enqueue_media()` is called without a `post` arg, such as on the Media Library screen, since `$post` is explicitly initialized to null there. Callbacks that type-hint `WP_Post` for these parameters fatal with a `TypeError` in those cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Use "or null if no post is found", as already used by the `get_the_modified_date`, `get_the_modified_time`, `post_password_required`, and `the_password_form` filters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`WP_REST_Posts_Controller::check_password_required()` is hooked to `post_password_required`, which can now be documented as passing null, and the method already handles that case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Twenty Sixteen, Twenty Seventeen, and Twenty Twenty-One callbacks for `wp_get_attachment_image_attributes` copied the hook's `WP_Post` docs. Update them to match the hook now that it documents a null `$attachment`. None of the callbacks dereference it unguarded, so this is docs only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`WP_Abilities_Content::allow_password_content()` said the core filter always passes a `WP_Post`, which contradicts the hook docs now that they allow null. Its guard already handles null. Also fix the grammar in `WP_REST_Posts_Controller::check_password_required()`: "The post been password checked" becomes "The post being password checked". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`twenty_twenty_one_password_form()` is hooked to `the_password_form`, which passes null when no post is found. The function already handles that, since it runs `$post` through `get_post()` and checks `empty( $post->ID )`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Document `$required` as `bool` and `$post` as `WP_Post|null`, matching the `post_password_required` filter instead of `mixed`. No native type hints are added, since an earlier callback returning a non-bool would then cause a fatal error. The existing `(bool)` cast and `instanceof WP_Post` check still guard at runtime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
Several hooks document their post parameter as `WP_Post` even though `get_post()` can return null before the hook fires, so callbacks that type-hint `WP_Post` fatal with a `TypeError`. This was reported for the Image Placeholders plugin, where a plugin filtered `post_thumbnail_id` to an ID with no post, leading `wp_get_attachment_image()` to pass a null `$attachment` to `wp_get_attachment_image_attributes`. The same applies to `post_password_required` and `the_password_form` when no post is found, and to `media_view_settings` and `media_view_strings` whenever `wp_enqueue_media()` is not called for a specific post. Core and bundled theme callbacks of these hooks are updated to match. Developed in #15257. See #65860. git-svn-id: https://develop.svn.wordpress.org/trunk@65075 602fd350-edb4-49c9-b593-d223f7449a82
Several hooks document their post parameter as `WP_Post` even though `get_post()` can return null before the hook fires, so callbacks that type-hint `WP_Post` fatal with a `TypeError`. This was reported for the Image Placeholders plugin, where a plugin filtered `post_thumbnail_id` to an ID with no post, leading `wp_get_attachment_image()` to pass a null `$attachment` to `wp_get_attachment_image_attributes`. The same applies to `post_password_required` and `the_password_form` when no post is found, and to `media_view_settings` and `media_view_strings` whenever `wp_enqueue_media()` is not called for a specific post. Core and bundled theme callbacks of these hooks are updated to match. Developed in WordPress/wordpress-develop#15257. See #65860. Built from https://develop.svn.wordpress.org/trunk@65075 git-svn-id: http://core.svn.wordpress.org/trunk@64225 1a063a9b-81f0-0310-95a4-ce76da25c4cd
✅ Committed in r65075 (9865c4a).
Several hooks document their post parameter as
WP_Posteven though they can passnull. Callbacks that type-hintWP_Postfor these parameters fatal with aTypeErrorwhen that happens. This was reported for the Image Placeholders plugin in WordPress/performance#2742, where YARPP's settings preview filterspost_thumbnail_idto an ID with no post.Hooks now documented as passing
WP_Post|null:wp_get_attachment_image_attributes:wp_get_attachment_image()only checks thatwp_get_attachment_image_src()succeeded, never thatget_post( $attachment_id )found a post.post_password_required:post_password_required()doesn't checkget_post(), and a null post reaches the filter through theempty( $post->post_password )early return, e.g. when called outside the loop.the_password_form:get_the_password_form()explicitly handles a missing post and then passes it to the filter as is.media_view_settingsandmedia_view_strings:wp_enqueue_media()initializes$postto null and only looks one up when apostarg is passed, so they get null on the Media Library screen, for example.The wording follows the existing "or null if no post is found" used by the
get_the_modified_dateandget_the_modified_timefilters.Core and bundled theme callbacks of these hooks are updated to match:
WP_REST_Posts_Controller::check_password_required()(also fixes "been" → "being").WP_Abilities_Content::allow_password_content(), narrowed frommixedto the filter contract (bool,WP_Post|null). No native type hints are added, since a non-bool returned by an earlier callback would then fatal.wp_get_attachment_image_attributescallbacks in Twenty Sixteen, Twenty Seventeen, and Twenty Twenty-One, and Twenty Twenty-One'sthe_password_formcallback.All other hooks in
wp-includesandwp-admindocumenting aWP_Postparameter were checked; they either guard against null before the hook fires or already dereference the post earlier.Trac ticket: Core-65860
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Auditing hook docblocks for parameters that can be null, drafting the docblock changes beyond the initial
wp_get_attachment_image_attributesfix, and writing this description; changes were reviewed and directed by me.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.
🤖 Generated with Claude Code