Skip to content

Commit 2762e5e

Browse files
committed
Feeds: Avoid fatal error with empty blog_charset value.
After the SimplePie library was updated to version `1.8.0` in [59141], an edge case has been discovered where a fatal error can encountered if the `blog_charset` option is missing or empty. In `fetch_feed()`, this option is retrieved using `get_option()` instead of `get_bloginfo( ‘charset’ )`. The latter will detect this scenario and apply a default value of `UTF-8` and is already used interchangeably throughout Core. This switches to `get_bloginfo( ‘charset’ )` instead to prevent this edge case. Props david.binda, davidbaumwald, SergeyBiryukov, sabernhardt, azaozz, peterwilsoncc. Fixes #62354. git-svn-id: https://develop.svn.wordpress.org/trunk@59382 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 05e274b commit 2762e5e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/wp-includes/feed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ function fetch_feed( $url ) {
842842
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
843843

844844
$feed->init();
845-
$feed->set_output_encoding( get_option( 'blog_charset' ) );
845+
$feed->set_output_encoding( get_bloginfo( 'charset' ) );
846846

847847
if ( $feed->error() ) {
848848
return new WP_Error( 'simplepie-error', $feed->error() );
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Tests the `fetch_feed` function.
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 6.7.0
8+
*
9+
* @group feed
10+
*/
11+
class Tests_Feed_fetchFeed extends WP_UnitTestCase {
12+
13+
public function set_up() {
14+
parent::set_up();
15+
16+
add_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
17+
}
18+
19+
/**
20+
* @ticket 62354
21+
*/
22+
public function test_empty_charset_does_not_trigger_fatal_error() {
23+
add_filter( 'pre_option_blog_charset', '__return_empty_string', 20 );
24+
25+
$feed = fetch_feed( 'https://wordpress.org/news/feed/' );
26+
27+
foreach ( $feed->get_items( 0, 1 ) as $item ) {
28+
$content = $item->get_content();
29+
}
30+
31+
$this->assertStringContainsString( '<a href="https://learn.wordpress.org/">Learn WordPress</a> is a learning resource providing workshops, quizzes, courses, lesson plans, and discussion groups so that anyone, from beginners to advanced users, can learn to do more with WordPress.', $content );
32+
}
33+
34+
public function mocked_rss_response() {
35+
$single_value_headers = array(
36+
'Content-Type' => 'application/rss+xml; charset=UTF-8',
37+
'link' => '<https://wordpress.org/news/wp-json/>; rel="https://api.w.org/"',
38+
);
39+
40+
return array(
41+
'headers' => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
42+
'body' => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
43+
'response' => array(
44+
'code' => 200,
45+
'message' => 'OK',
46+
),
47+
'cookies' => array(),
48+
'filename' => null,
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)