Skip to content

Commit f47fedd

Browse files
committed
General: Improve resilience of feed_links_extra() when global $post is not set.
This obtains the global post via `get_queried_object()` when `is_singular()`. Developed in #10401 Props westonruter, johnjamesjacoby, Presskopp, abcd95, dilipbheda, sabernhardt, awetz583, indirabiswas27. Fixes #63263. git-svn-id: https://develop.svn.wordpress.org/trunk@61056 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b63c6d2 commit f47fedd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/wp-includes/general-template.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,9 +3326,9 @@ function feed_links_extra( $args = array() ) {
33263326
*/
33273327
$args = apply_filters( 'feed_links_extra_args', $args );
33283328

3329-
if ( is_singular() ) {
3330-
$id = 0;
3331-
$post = get_post( $id );
3329+
$queried_object = get_queried_object();
3330+
if ( is_singular() && $queried_object instanceof WP_Post ) {
3331+
$post = $queried_object;
33323332

33333333
/** This filter is documented in wp-includes/general-template.php */
33343334
$show_comments_feed = apply_filters( 'feed_links_show_comments_feed', true );
@@ -3348,12 +3348,17 @@ function feed_links_extra( $args = array() ) {
33483348
*/
33493349
$show_post_comments_feed = apply_filters( 'feed_links_extra_show_post_comments_feed', $show_comments_feed );
33503350

3351-
if ( $show_post_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
3351+
if ( $show_post_comments_feed && ( comments_open( $post ) || pings_open( $post ) || (int) $post->comment_count > 0 ) ) {
33523352
$title = sprintf(
33533353
$args['singletitle'],
33543354
get_bloginfo( 'name' ),
33553355
$args['separator'],
3356-
the_title_attribute( array( 'echo' => false ) )
3356+
the_title_attribute(
3357+
array(
3358+
'echo' => false,
3359+
'post' => $post,
3360+
)
3361+
)
33573362
);
33583363

33593364
$feed_link = get_post_comments_feed_link( $post->ID );

tests/phpunit/tests/general/feedLinksExtra.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,15 @@ public function data_feed_links_extra_should_output_nothing_when_filters_return_
634634
),
635635
);
636636
}
637+
638+
/**
639+
* @ticket 63263
640+
*/
641+
public function test_feed_links_extra_should_work_fail_if_global_post_empty() {
642+
$post_id = self::factory()->post->create();
643+
$this->go_to( get_permalink( $post_id ) );
644+
$GLOBALS['post'] = null;
645+
646+
$this->assertNotEmpty( get_echo( 'feed_links_extra' ) );
647+
}
637648
}

0 commit comments

Comments
 (0)