WordPress.org

Make WordPress Core

Ticket #43357: #43357-5 2020-04-30.patch

File #43357-5 2020-04-30.patch, 5.0 KB (added by arena, 19 months ago)

WP_Feed_Cache instead of WP_SimplePie_Cache

  • wp-includes/class-wp-feed-cache-transient.php

     
    1212 *
    1313 * @since 2.8.0
    1414 */
    15 class WP_Feed_Cache_Transient {
     15class WP_Feed_Cache_Transient implements SimplePie_Cache_Base {
    1616
    1717        /**
    1818         * Holds the transient name.
     
    3333        /**
    3434         * Holds the cache duration in seconds.
    3535         *
    36          * Defaults to 43200 seconds (12 hours).
    37          *
    38          * @since 2.8.0
     36         * @since 5.5.0
    3937         * @var int
    4038         */
    41         public $lifetime = 43200;
     39        public $lifetime;
    4240
    4341        /**
    4442         * Constructor.
     
    5149         * @param string $extension 'spi' or 'spc'.
    5250         */
    5351        public function __construct( $location, $filename, $extension ) {
    54                 $this->name     = 'feed_' . $filename;
    55                 $this->mod_name = 'feed_mod_' . $filename;
     52                $options = $this->parse_location( $location );
    5653
    57                 $lifetime = $this->lifetime;
    58                 /**
    59                  * Filters the transient lifetime of the feed cache.
    60                  *
    61                  * @since 2.8.0
    62                  *
    63                  * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
    64                  * @param string $filename Unique identifier for the cache object.
    65                  */
    66                 $this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $filename );
     54                $this->lifetime = $options['extras']['lifetime'];
     55
     56                $this->name     = 'feed_' . md5( "$filename:$extension" );
     57                $this->mod_name = 'feed_mod_' . md5( "$filename:$extension" );
    6758        }
    6859
    6960        /**
     61         * Parsing cache location.
     62         *
     63         * @since 5.5.0
     64         *
     65         * @param string $location  URL location (scheme is used to determine handler).
     66         * @return array $params.
     67         */
     68        public function parse_location( $location ) {
     69                $params = parse_url( $location );
     70                $params['extras'] = array();
     71                if ( isset( $params['query'] ) )
     72                {
     73                        parse_str( $params['query'], $params['extras'] );
     74                }
     75                return $params;
     76        }
     77
     78        /**
    7079         * Sets the transient.
    7180         *
    7281         * @since 2.8.0
  • wp-includes/class-wp-feed-cache.php

     
    1616 */
    1717class WP_Feed_Cache extends SimplePie_Cache {
    1818
     19        protected static $handlers = array(
     20                'wordpress' => 'WP_Feed_Cache_Transient',
     21        );
     22
    1923        /**
     24         * Should be deprecated in SimplePie 1.3.1 but still using it
     25         *
     26         * so keeping both functions for backward compatibility
     27         */
     28        public function create( $location, $filename, $extension ) {
     29                return self::get_handler($location, $filename, $extension);
     30        }
     31
     32        /**
    2033         * Creates a new SimplePie_Cache object.
    2134         *
    22          * @since 2.8.0
     35         * @since 5.5.0
    2336         *
    2437         * @param string $location  URL location (scheme is used to determine handler).
    2538         * @param string $filename  Unique identifier for cache object.
     
    2639         * @param string $extension 'spi' or 'spc'.
    2740         * @return WP_Feed_Cache_Transient Feed cache handler object that uses transients.
    2841         */
    29         public function create( $location, $filename, $extension ) {
     42        public static function get_handler( $location, $filename, $extension ) {
    3043                return new WP_Feed_Cache_Transient( $location, $filename, $extension );
    3144        }
    3245}
  • wp-includes/feed.php

     
    749749 * @return SimplePie|WP_Error SimplePie object on success or WP_Error object on failure.
    750750 */
    751751function fetch_feed( $url ) {
     752        /**
     753         * Filters the transient lifetime of the feed cache.
     754         *
     755         * @since 2.8.0
     756         *
     757         * @param int    $lifetime Cache duration in seconds. Default is 43200 seconds (12 hours).
     758         * @param string $filename Unique identifier for the cache object.
     759         */
     760
     761        $lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url );
     762
    752763        if ( ! class_exists( 'SimplePie', false ) ) {
    753764                require_once ABSPATH . WPINC . '/class-simplepie.php';
    754765        }
     
    760771
    761772        $feed = new SimplePie();
    762773
    763         $feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
    764         // We must manually overwrite $feed->sanitize because SimplePie's
    765         // constructor sets it before we have a chance to set the sanitization class.
    766         $feed->sanitize = new WP_SimplePie_Sanitize_KSES();
     774        $feed->registry->register( 'File',  'WP_SimplePie_File', true );
     775        $feed->registry->register( 'Cache', 'WP_Feed_Cache', true );
    767776
    768         $feed->set_cache_class( 'WP_Feed_Cache' );
    769         $feed->set_file_class( 'WP_SimplePie_File' );
     777        $feed->set_cache_duration( $lifetime );
     778        $feed->set_cache_location( 'wordpress://wordpress?lifetime=' . $lifetime );
     779        $feed->set_feed_url( $url );
    770780
    771         $feed->set_feed_url( $url );
    772         /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
    773         $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) );
    774781        /**
    775782         * Fires just before processing the SimplePie feed object.
    776783         *