#52814 closed defect (bug) (fixed)
Avoid notices in `get_post_comments_feed_link()`
Reported by: | dd32 | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.8 | Priority: | low |
Severity: | trivial | Version: | |
Component: | Feeds | Keywords: | has-patch has-unit-tests has-testing-info commit |
Focuses: | Cc: |
Description
When an atom feed request is made to a non-existent page, get_post_comments_feed_link() produces PHP Notices such as the following:
E_NOTICE: Trying to get property 'post_type' of non-object in wp-includes/link-template.php:752
Example of a URLs which can trigger this is:
OPTIONS /2021/01/feed/atom/atom/ GET /404-page/feed/atom/
(Options bypasses the canonical redirect, and outputs a post-comments feed for the page "2021/01/feed/atom")
While the code path that results in this function being called without a valid post is unexpected and the resulting url generated in the atom feed is invalid, it silences these notices, which is good for sites that log/monitor these things (Such as WordPress.org)
Attachments (2)
Change History (12)
#2
@
4 years ago
- Milestone changed from Awaiting Review to 5.8
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
This ticket was mentioned in PR #1354 on WordPress/wordpress-develop by hellofromtonya.
4 years ago
#4
- Keywords has-unit-tests added; needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/52814
- Adds unit test
- Applies
52814.diff
props to @dd32 - Replaces implicit loose comparison in guard clause to bail out if not an instance of
WP_Post
. Why?WP_Post
is the expected data type _if_ the post exists.
@
4 years ago
Testing before and after applying the patch. Before: Warning in PHP 8. After: No notice or warning.
#5
@
4 years ago
- Keywords has-testing-info added
Testing Instructions
Setup:
Create a must-use file in wp-content/mu-plugins/test.php
and paste this code into the file:
<?php add_action( 'loop_start', function() { if ( is_admin() ) { return; } $post_id = PHP_INT_MAX; $link = get_post_comments_feed_link( $post_id ); } );
Test
- Load the home page
- In less than PHP 8, a notice is generated. In PHP 8, a warning is generated like this one:
[08-Jun-2021 20:51:04 UTC] PHP Warning: Attempt to read property "post_type" on null in /[path to your project]/wp-includes/link-template.php on line 752 [08-Jun-2021 20:51:04 UTC] PHP Stack trace: [08-Jun-2021 20:51:04 UTC] PHP 1. {main}() /[path to your project]/index.php:0 [08-Jun-2021 20:51:04 UTC] PHP 2. require() /[path to your project]/index.php:17 [08-Jun-2021 20:51:04 UTC] PHP 3. require_once() /U[path to your project]/wp-blog-header.php:19 [08-Jun-2021 20:51:04 UTC] PHP 4. include() /[path to your project]/wp-includes/template-loader.php:106 [08-Jun-2021 20:51:04 UTC] PHP 5. the_post() /[path to your project]/wp-content/themes/twentytwenty/index.php:88 [08-Jun-2021 20:51:04 UTC] PHP 6. WP_Query->the_post() /[path to your project]/wp-includes/query.php:975 [08-Jun-2021 20:51:04 UTC] PHP 7. do_action_ref_array() /[path to your project]/wp-includes/class-wp-query.php:3326 [08-Jun-2021 20:51:04 UTC] PHP 8. WP_Hook->do_action() /[path to your project]/wp-includes/plugin.php:518 [08-Jun-2021 20:51:04 UTC] PHP 9. WP_Hook->apply_filters() /[path to your project]/wp-includes/class-wp-hook.php:327 [08-Jun-2021 20:51:04 UTC] PHP 10. {closure:/[path to your project]/wp-content/mu-plugins/test.php:3-10}() /[path to your project]/wp-includes/class-wp-hook.php:303 [08-Jun-2021 20:51:04 UTC] PHP 11. get_post_comments_feed_link() /[path to your project]/wp-content/mu-plugins/test.php:9
- Apply the PR
- Retest => Result: Page should render and no warning/notice generated
#6
@
4 years ago
- Keywords commit added
Marking for commit
.
A few enhancements to Dion's patch:
- Unit test added
- Checks for the instance of
WP_Post
which is the expected data type if the post exists - Retains the return signature of string data type for backwards compatibility, documentation, etc by returning an empty string if the post does not exist
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
4 years ago
This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.
4 years ago
hellofromtonya commented on PR #1354:
4 years ago
#10
Merged via changeset https://core.trac.wordpress.org/changeset/51121
Hi there!
I checked the 52814.diff and found one doc block change.
* @return string|false The permalink for the comments feed for the given post.
Replace to
* @return string|false The permalink for the comments feed for the given post. False if the post does not exist.