Skip to content

fix: add type guard to prevent PHP warning in preview link filter#834

Merged
GaryJones merged 1 commit intodevelopfrom
fix/preview-link-php-warning
Dec 12, 2025
Merged

fix: add type guard to prevent PHP warning in preview link filter#834
GaryJones merged 1 commit intodevelopfrom
fix/preview-link-php-warning

Conversation

@GaryJones
Copy link
Copy Markdown
Contributor

Summary

  • Adds guard clause to fix_preview_link_part_two() to prevent PHP warning when $post is not a valid WP_Post object
  • Uses instanceof WP_Post for stricter type checking (rather than is_object() which would accept any object)
  • Fixes indentation to use tabs matching surrounding code

Problem

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:

Attempt to read property 'post_type' on string

Attribution

Based on PR #747 by @dev4starter, rebased onto develop with improvements.

Test plan

  • Verify no PHP warnings when previewing posts with custom statuses
  • Verify preview links still work correctly for valid posts
  • CI passes

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_Post type guard after the existing is_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.
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@GaryJones GaryJones merged commit 2ee037c into develop Dec 12, 2025
20 checks passed
@GaryJones GaryJones deleted the fix/preview-link-php-warning branch December 12, 2025 16:28
@GaryJones GaryJones mentioned this pull request Jan 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants