Make WordPress Core


Ignore:
Timestamp:
09/30/2024 10:48:16 PM (7 months ago)
Author:
SergeyBiryukov
Message:

External Libraries: Update the SimplePie library to version 1.8.0.

The most notable change in this update is that all code is now namespaced and uses PSR-4 classes, though there is a compatibility layer available for extenders using the older class names, so plugin or theme authors directly using SimplePie can decide for themselves when they want to change to using the namespaced names for SimplePie classes.

Note: This commit includes additional fixes for PHP 8.4 compatibility (PR 875, PR 888) from the one-dot-eight branch of SimplePie, which is expected to be released as SimplePie 1.8.1 soon.

References:

Follow-up to [47733], [49176], [52393], [52413].

Props jrf, peterwilsoncc, chaion07, cu121, markparnell, audrasjb, costdev, Presskopp, desrosj, faisal03, mukesh27, SergeyBiryukov.
See #55604.

Location:
trunk/src/wp-includes/SimplePie/src
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/SimplePie/src/Registry.php

    r59140 r59141  
    11<?php
     2
    23/**
    34 * SimplePie
     
    67 * Takes the hard work out of managing a complete RSS/Atom solution.
    78 *
    8  * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
     9 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
    910 * All rights reserved.
    1011 *
     
    4243 */
    4344
     45namespace SimplePie;
     46
     47use SimplePie\Content\Type\Sniffer;
     48use SimplePie\Parse\Date;
     49use SimplePie\XML\Declaration\Parser as DeclarationParser;
     50
    4451/**
    4552 * Handles creating objects and calling methods
    4653 *
    47  * Access this via {@see SimplePie::get_registry()}
     54 * Access this via {@see \SimplePie\SimplePie::get_registry()}
    4855 *
    4956 * @package SimplePie
    5057 */
    51 class SimplePie_Registry
     58class Registry
    5259{
    53     /**
    54      * Default class mapping
    55      *
    56      * Overriding classes *must* subclass these.
    57      *
    58      * @var array
    59      */
    60     protected $default = array(
    61         'Cache' => 'SimplePie_Cache',
    62         'Locator' => 'SimplePie_Locator',
    63         'Parser' => 'SimplePie_Parser',
    64         'File' => 'SimplePie_File',
    65         'Sanitize' => 'SimplePie_Sanitize',
    66         'Item' => 'SimplePie_Item',
    67         'Author' => 'SimplePie_Author',
    68         'Category' => 'SimplePie_Category',
    69         'Enclosure' => 'SimplePie_Enclosure',
    70         'Caption' => 'SimplePie_Caption',
    71         'Copyright' => 'SimplePie_Copyright',
    72         'Credit' => 'SimplePie_Credit',
    73         'Rating' => 'SimplePie_Rating',
    74         'Restriction' => 'SimplePie_Restriction',
    75         'Content_Type_Sniffer' => 'SimplePie_Content_Type_Sniffer',
    76         'Source' => 'SimplePie_Source',
    77         'Misc' => 'SimplePie_Misc',
    78         'XML_Declaration_Parser' => 'SimplePie_XML_Declaration_Parser',
    79         'Parse_Date' => 'SimplePie_Parse_Date',
    80     );
    81 
    82     /**
    83      * Class mapping
    84      *
    85      * @see register()
    86      * @var array
    87      */
    88     protected $classes = array();
    89 
    90     /**
    91      * Legacy classes
    92      *
    93      * @see register()
    94      * @var array
    95      */
    96     protected $legacy = array();
    97 
    98     /**
    99      * Constructor
    100      *
    101      * No-op
    102      */
    103     public function __construct() { }
    104 
    105     /**
    106      * Register a class
    107      *
    108      * @param string $type See {@see $default} for names
    109      * @param string $class Class name, must subclass the corresponding default
    110      * @param bool $legacy Whether to enable legacy support for this class
    111      * @return bool Successfulness
    112      */
    113     public function register($type, $class, $legacy = false)
    114     {
    115         if (!@is_subclass_of($class, $this->default[$type]))
    116         {
    117             return false;
    118         }
    119 
    120         $this->classes[$type] = $class;
    121 
    122         if ($legacy)
    123         {
    124             $this->legacy[] = $class;
    125         }
    126 
    127         return true;
    128     }
    129 
    130     /**
    131      * Get the class registered for a type
    132      *
    133      * Where possible, use {@see create()} or {@see call()} instead
    134      *
    135      * @param string $type
    136      * @return string|null
    137      */
    138     public function get_class($type)
    139     {
    140         if (!empty($this->classes[$type]))
    141         {
    142             return $this->classes[$type];
    143         }
    144         if (!empty($this->default[$type]))
    145         {
    146             return $this->default[$type];
    147         }
    148 
    149         return null;
    150     }
    151 
    152     /**
    153      * Create a new instance of a given type
    154      *
    155      * @param string $type
    156      * @param array $parameters Parameters to pass to the constructor
    157      * @return object Instance of class
    158      */
    159     public function &create($type, $parameters = array())
    160     {
    161         $class = $this->get_class($type);
    162 
    163         if (in_array($class, $this->legacy))
    164         {
    165             switch ($type)
    166             {
    167                 case 'locator':
    168                     // Legacy: file, timeout, useragent, file_class, max_checked_feeds, content_type_sniffer_class
    169                     // Specified: file, timeout, useragent, max_checked_feeds
    170                     $replacement = array($this->get_class('file'), $parameters[3], $this->get_class('content_type_sniffer'));
    171                     array_splice($parameters, 3, 1, $replacement);
    172                     break;
    173             }
    174         }
    175 
    176         if (!method_exists($class, '__construct'))
    177         {
    178             $instance = new $class;
    179         }
    180         else
    181         {
    182             $reflector = new ReflectionClass($class);
    183             $instance = $reflector->newInstanceArgs($parameters);
    184         }
    185 
    186         if (method_exists($instance, 'set_registry'))
    187         {
    188             $instance->set_registry($this);
    189         }
    190         return $instance;
    191     }
    192 
    193     /**
    194      * Call a static method for a type
    195      *
    196      * @param string $type
    197      * @param string $method
    198      * @param array $parameters
    199      * @return mixed
    200      */
    201     public function &call($type, $method, $parameters = array())
    202     {
    203         $class = $this->get_class($type);
    204 
    205         if (in_array($class, $this->legacy))
    206         {
    207             switch ($type)
    208             {
    209                 case 'Cache':
    210                     // For backwards compatibility with old non-static
    211                     // Cache::create() methods in PHP < 8.0.
    212                     // No longer supported as of PHP 8.0.
    213                     if ($method === 'get_handler')
    214                     {
    215                         $result = @call_user_func_array(array($class, 'create'), $parameters);
    216                         return $result;
    217                     }
    218                     break;
    219             }
    220         }
    221 
    222         $result = call_user_func_array(array($class, $method), $parameters);
    223         return $result;
    224     }
     60    /**
     61     * Default class mapping
     62     *
     63     * Overriding classes *must* subclass these.
     64     *
     65     * @var array<class-string, class-string>
     66     */
     67    protected $default = [
     68        Cache::class => Cache::class,
     69        Locator::class => Locator::class,
     70        Parser::class => Parser::class,
     71        File::class => File::class,
     72        Sanitize::class => Sanitize::class,
     73        Item::class => Item::class,
     74        Author::class => Author::class,
     75        Category::class => Category::class,
     76        Enclosure::class => Enclosure::class,
     77        Caption::class => Caption::class,
     78        Copyright::class => Copyright::class,
     79        Credit::class => Credit::class,
     80        Rating::class => Rating::class,
     81        Restriction::class => Restriction::class,
     82        Sniffer::class => Sniffer::class,
     83        Source::class => Source::class,
     84        Misc::class => Misc::class,
     85        DeclarationParser::class => DeclarationParser::class,
     86        Date::class => Date::class,
     87    ];
     88
     89    /**
     90     * Class mapping
     91     *
     92     * @see register()
     93     * @var array
     94     */
     95    protected $classes = [];
     96
     97    /**
     98     * Legacy classes
     99     *
     100     * @see register()
     101     * @var array<class-string>
     102     */
     103    protected $legacy = [];
     104
     105    /**
     106     * Legacy types
     107     *
     108     * @see register()
     109     * @var array<string, class-string>
     110     */
     111    private $legacyTypes = [
     112        'Cache' => Cache::class,
     113        'Locator' => Locator::class,
     114        'Parser' => Parser::class,
     115        'File' => File::class,
     116        'Sanitize' => Sanitize::class,
     117        'Item' => Item::class,
     118        'Author' => Author::class,
     119        'Category' => Category::class,
     120        'Enclosure' => Enclosure::class,
     121        'Caption' => Caption::class,
     122        'Copyright' => Copyright::class,
     123        'Credit' => Credit::class,
     124        'Rating' => Rating::class,
     125        'Restriction' => Restriction::class,
     126        'Content_Type_Sniffer' => Sniffer::class,
     127        'Source' => Source::class,
     128        'Misc' => Misc::class,
     129        'XML_Declaration_Parser' => DeclarationParser::class,
     130        'Parse_Date' => Date::class,
     131    ];
     132
     133    /**
     134     * Constructor
     135     *
     136     * No-op
     137     */
     138    public function __construct()
     139    {
     140    }
     141
     142    /**
     143     * Register a class
     144     *
     145     * @param string $type See {@see $default} for names
     146     * @param class-string $class Class name, must subclass the corresponding default
     147     * @param bool $legacy Whether to enable legacy support for this class
     148     * @return bool Successfulness
     149     */
     150    public function register($type, $class, $legacy = false)
     151    {
     152        if (array_key_exists($type, $this->legacyTypes)) {
     153            // trigger_error(sprintf('"%s"(): Using argument #1 ($type) with value "%s" is deprecated since SimplePie 1.8.0, use class-string "%s" instead.', __METHOD__, $type, $this->legacyTypes[$type]), \E_USER_DEPRECATED);
     154
     155            $type = $this->legacyTypes[$type];
     156        }
     157
     158        if (!array_key_exists($type, $this->default)) {
     159            return false;
     160        }
     161
     162        if (!class_exists($class)) {
     163            return false;
     164        }
     165
     166        /** @var string */
     167        $base_class = $this->default[$type];
     168
     169        if (!is_subclass_of($class, $base_class)) {
     170            return false;
     171        }
     172
     173        $this->classes[$type] = $class;
     174
     175        if ($legacy) {
     176            $this->legacy[] = $class;
     177        }
     178
     179        return true;
     180    }
     181
     182    /**
     183     * Get the class registered for a type
     184     *
     185     * Where possible, use {@see create()} or {@see call()} instead
     186     *
     187     * @template T
     188     * @param class-string<T> $type
     189     * @return class-string<T>|null
     190     */
     191    public function get_class($type)
     192    {
     193        if (array_key_exists($type, $this->legacyTypes)) {
     194            // trigger_error(sprintf('"%s"(): Using argument #1 ($type) with value "%s" is deprecated since SimplePie 1.8.0, use class-string "%s" instead.', __METHOD__, $type, $this->legacyTypes[$type]), \E_USER_DEPRECATED);
     195
     196            $type = $this->legacyTypes[$type];
     197        }
     198
     199        if (!array_key_exists($type, $this->default)) {
     200            return null;
     201        }
     202
     203        $class = $this->default[$type];
     204
     205        if (array_key_exists($type, $this->classes)) {
     206            $class = $this->classes[$type];
     207        }
     208
     209        return $class;
     210    }
     211
     212    /**
     213     * Create a new instance of a given type
     214     *
     215     * @template T class-string $type
     216     * @param class-string<T> $type
     217     * @param array $parameters Parameters to pass to the constructor
     218     * @return T Instance of class
     219     */
     220    public function &create($type, $parameters = [])
     221    {
     222        $class = $this->get_class($type);
     223
     224        if (!method_exists($class, '__construct')) {
     225            $instance = new $class();
     226        } else {
     227            $reflector = new \ReflectionClass($class);
     228            $instance = $reflector->newInstanceArgs($parameters);
     229        }
     230
     231        if ($instance instanceof RegistryAware) {
     232            $instance->set_registry($this);
     233        } elseif (method_exists($instance, 'set_registry')) {
     234            trigger_error(sprintf('Using the method "set_registry()" without implementing "%s" is deprecated since SimplePie 1.8.0, implement "%s" in "%s".', RegistryAware::class, RegistryAware::class, $class), \E_USER_DEPRECATED);
     235            $instance->set_registry($this);
     236        }
     237        return $instance;
     238    }
     239
     240    /**
     241     * Call a static method for a type
     242     *
     243     * @param class-string $type
     244     * @param string $method
     245     * @param array $parameters
     246     * @return mixed
     247     */
     248    public function &call($type, $method, $parameters = [])
     249    {
     250        $class = $this->get_class($type);
     251
     252        if (in_array($class, $this->legacy)) {
     253            switch ($type) {
     254                case Cache::class:
     255                    // For backwards compatibility with old non-static
     256                    // Cache::create() methods in PHP < 8.0.
     257                    // No longer supported as of PHP 8.0.
     258                    if ($method === 'get_handler') {
     259                        $result = @call_user_func_array([$class, 'create'], $parameters);
     260                        return $result;
     261                    }
     262                    break;
     263            }
     264        }
     265
     266        $result = call_user_func_array([$class, $method], $parameters);
     267        return $result;
     268    }
    225269}
     270
     271class_alias('SimplePie\Registry', 'SimplePie_Registry');
Note: See TracChangeset for help on using the changeset viewer.