Make WordPress Core


Ignore:
Timestamp:
01/29/2026 01:05:19 AM (6 weeks ago)
Author:
peterwilsoncc
Message:

Feeds: Fix backward compatibility of fetch_feed().

In simplepie/simplepie#795 handling of multiple feed requests was deprecated, triggering the message Fetching multiple feeds with single SimplePie instance is deprecated since SimplePie 1.9.0, create one SimplePie instance per feed and use SimplePie::merge_items to get a single list of items.

This updates fetch_feed() to handle multiple requests using seperate SimplePie instances in order to retain backward compatibility.

A PHP 8.5 deprecation was throwing notices in the event an empty URL was passed to fetch_feed(), Using null as an array offset is deprecated, use an empty string instead.

This includes a workaround pending the release of a SimplePie version including simplepie/simplepie#949.

Reviewed by jorbin.
Merges [61551] to the 6.9 branch.

Fixes #64136.
Props audrasjb, jorbin, muryam, oglekler, ozgursar, presskopp, swissspidy, westonruter, wildworks, peterwilsoncc.

Location:
branches/6.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.9

  • branches/6.9/src/wp-includes/feed.php

    r60771 r61553  
    833833    $feed->get_registry()->register( SimplePie\File::class, 'WP_SimplePie_File', true );
    834834
    835     $feed->set_feed_url( $url );
    836835    /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
    837836    $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
     
    847846    do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
    848847
     848    if ( empty( $url ) ) {
     849        /*
     850         * @todo: Set $url to empty string once supported by SimplePie.
     851         *
     852         * The early return without proceeding is to work around a PHP 8.5
     853         * deprecation issue resolved in https://github.com/simplepie/simplepie/pull/949
     854         *
     855         * To avoid the duplicate code, this block can be replaced with `$url = '';` once SimplePie
     856         * is upgraded to a version that includes the fix.
     857         */
     858        $feed->init();
     859        $feed->set_output_encoding( get_bloginfo( 'charset' ) );
     860
     861        if ( $feed->error() ) {
     862            return new WP_Error( 'simplepie-error', $feed->error() );
     863        }
     864
     865        return $feed;
     866    } elseif ( is_array( $url ) && count( $url ) === 1 ) {
     867        $url = array_shift( $url );
     868    } elseif ( is_array( $url ) ) {
     869        $feeds            = array();
     870        $simplepie_errors = array();
     871        foreach ( $url as $feed_url ) {
     872            $simplepie_instance = clone $feed;
     873            $simplepie_instance->set_feed_url( $feed_url );
     874            $simplepie_instance->init();
     875            $simplepie_instance->set_output_encoding( get_bloginfo( 'charset' ) );
     876
     877            if ( $simplepie_instance->error() ) {
     878                $simplepie_errors[] = sprintf(
     879                    /* translators: %1$s is the feed URL, %2$s is the error message. */
     880                    __( 'Error fetching feed %1$s: %2$s' ),
     881                    esc_url( $feed_url ),
     882                    $simplepie_instance->error()
     883                );
     884                unset( $simplepie_instance );
     885                continue;
     886            }
     887
     888            $feeds[] = $simplepie_instance;
     889            unset( $simplepie_instance );
     890        }
     891
     892        if ( ! empty( $simplepie_errors ) ) {
     893            return new WP_Error( 'simplepie-error', $simplepie_errors );
     894        }
     895
     896        $feed->init();
     897        $feed->data['items'] = SimplePie\SimplePie::merge_items( $feeds );
     898        return $feed;
     899    }
     900
     901    $feed->set_feed_url( $url );
    849902    $feed->init();
    850903    $feed->set_output_encoding( get_bloginfo( 'charset' ) );
Note: See TracChangeset for help on using the changeset viewer.