fix: add type guard to prevent PHP warning in preview link filter#834
Merged
fix: add type guard to prevent PHP warning in preview link filter#834
Conversation
The `fix_preview_link_part_two()` filter callback could receive a string URL instead of a WP_Post object when `get_post()` returns null, causing a PHP warning when attempting to access the `post_type` property. This guard clause ensures the parameter is a valid post object before proceeding with any property access. Uses `instanceof WP_Post` for stricter type checking rather than `is_object()`, which would incorrectly accept any object type. Fixes indentation to match surrounding code standards (tabs instead of spaces). Based on PR #747 by @dev4starter, rebased onto develop with improvements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR adds defensive type checking to the fix_preview_link_part_two() filter callback to prevent PHP warnings when $post is not a valid WP_Post object. This addresses an edge case where get_post() might return null, causing attempts to read properties on a string URL instead of a post object.
Key Changes:
- Added
instanceof WP_Posttype guard after the existingis_int()check - Returns early with the original permalink if the post object is invalid
- Includes a clarifying comment explaining the guard's purpose
| $post = get_post( $post ); | ||
| } | ||
|
|
||
| // Bail if $post is not a valid post object. |
There was a problem hiding this comment.
The comment style is inconsistent with other inline comments in the same function. Lines 1485, 1490, 1495, 1501, and 1508 use '//' without a space after it, while this new comment uses '// ' with a space. For consistency within this function, the comment should match the existing style and use '//' without a space after the slashes.
Suggested change
| // Bail if $post is not a valid post object. | |
| //Bail if $post is not a valid post object. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fix_preview_link_part_two()to prevent PHP warning when$postis not a valid WP_Post objectinstanceof WP_Postfor stricter type checking (rather thanis_object()which would accept any object)Problem
The
fix_preview_link_part_two()filter callback could receive a string URL instead of a WP_Post object whenget_post()returns null, causing a PHP warning:Attribution
Based on PR #747 by @dev4starter, rebased onto develop with improvements.
Test plan
🤖 Generated with Claude Code