Skip to content

Commit

Permalink
Feeds: Avoid fatal error with empty blog_charset value.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
desrosj committed Nov 11, 2024
1 parent 05e274b commit 2762e5e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ function fetch_feed( $url ) {
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );

$feed->init();
$feed->set_output_encoding( get_option( 'blog_charset' ) );
$feed->set_output_encoding( get_bloginfo( 'charset' ) );

if ( $feed->error() ) {
return new WP_Error( 'simplepie-error', $feed->error() );
Expand Down
51 changes: 51 additions & 0 deletions tests/phpunit/tests/feed/fetchFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Tests the `fetch_feed` function.
*
* @package WordPress
* @subpackage UnitTests
* @since 6.7.0
*
* @group feed
*/
class Tests_Feed_fetchFeed extends WP_UnitTestCase {

public function set_up() {
parent::set_up();

add_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
}

/**
* @ticket 62354
*/
public function test_empty_charset_does_not_trigger_fatal_error() {
add_filter( 'pre_option_blog_charset', '__return_empty_string', 20 );

$feed = fetch_feed( 'https://wordpress.org/news/feed/' );

foreach ( $feed->get_items( 0, 1 ) as $item ) {
$content = $item->get_content();
}

$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 );
}

public function mocked_rss_response() {
$single_value_headers = array(
'Content-Type' => 'application/rss+xml; charset=UTF-8',
'link' => '<https://wordpress.org/news/wp-json/>; rel="https://api.w.org/"',
);

return array(
'headers' => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( $single_value_headers ),
'body' => file_get_contents( DIR_TESTDATA . '/feed/wordpress-org-news.xml' ),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => null,
);
}
}

0 comments on commit 2762e5e

Please sign in to comment.