From 0def38c30b73cb999df36ba24ae058e903dfbab4 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Mon, 6 Jun 2016 03:24:29 +0000 Subject: [PATCH] Autoload: Introduce shim for SPL autoloading. For PHP 5.2, SPL can be disabled. As SPL provides the support for multiple autoloaders, this needs to be shimmed if not available. Fixes #36926. Built from https://develop.svn.wordpress.org/trunk@37636 git-svn-id: http://core.svn.wordpress.org/trunk@37604 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-simplepie.php | 33 ++------------ wp-includes/compat.php | 81 +++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 86 insertions(+), 30 deletions(-) diff --git a/wp-includes/class-simplepie.php b/wp-includes/class-simplepie.php index 958ba463e9..6110413767 100644 --- a/wp-includes/class-simplepie.php +++ b/wp-includes/class-simplepie.php @@ -29,35 +29,10 @@ function wp_simplepie_autoload( $class ) { include( $file ); } -if ( function_exists( 'spl_autoload_register' ) ) { - /** - * We autoload classes we may not need. - * - * If SPL is disabled, we load all of SimplePie manually. - * - * Core.php is not loaded manually, because SimplePie_Core (a deprecated class) - * was never included in WordPress core. - */ - spl_autoload_register( 'wp_simplepie_autoload' ); -} else { - require ABSPATH . WPINC . '/SimplePie/Cache/Base.php'; - require ABSPATH . WPINC . '/SimplePie/Cache/DB.php'; - require ABSPATH . WPINC . '/SimplePie/Cache/File.php'; - require ABSPATH . WPINC . '/SimplePie/Cache/Memcache.php'; - require ABSPATH . WPINC . '/SimplePie/Cache/MySQL.php'; - require ABSPATH . WPINC . '/SimplePie/Caption.php'; - require ABSPATH . WPINC . '/SimplePie/Category.php'; - require ABSPATH . WPINC . '/SimplePie/Copyright.php'; - require ABSPATH . WPINC . '/SimplePie/Credit.php'; - require ABSPATH . WPINC . '/SimplePie/Decode/HTML/Entities.php'; - require ABSPATH . WPINC . '/SimplePie/Enclosure.php'; - require ABSPATH . WPINC . '/SimplePie/gzdecode.php'; - require ABSPATH . WPINC . '/SimplePie/HTTP/Parser.php'; - require ABSPATH . WPINC . '/SimplePie/Net/IPv6.php'; - require ABSPATH . WPINC . '/SimplePie/Rating.php'; - require ABSPATH . WPINC . '/SimplePie/Restriction.php'; - require ABSPATH . WPINC . '/SimplePie/Source.php'; -} +/** + * We autoload classes we may not need. + */ +spl_autoload_register( 'wp_simplepie_autoload' ); /** * SimplePie diff --git a/wp-includes/compat.php b/wp-includes/compat.php index cfb0f70aa8..64f4ba5170 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -434,3 +434,84 @@ if ( ! interface_exists( 'JsonSerializable' ) ) { if ( ! function_exists( 'random_int' ) ) { require ABSPATH . WPINC . '/random_compat/random.php'; } + +// SPL can be disabled on PHP 5.2 +if ( ! function_exists( 'spl_autoload_register' ) ): + $_wp_spl_autoloaders = array(); + + /** + * Autoloader compatibility callback. + * + * @param string $classname Class to attempt autoloading. + */ + function __autoload( $classname ) { + global $_wp_spl_autoloaders; + foreach ( $_wp_spl_autoloaders as $autoloader ) { + if ( ! is_callable( $autoloader ) ) { + // Avoid the extra warning if the autoloader isn't callable. + continue; + } + + call_user_func( $autoloader, $classname ); + + // If it has been autoloaded, stop processing. + if ( class_exists( $classname, false ) ) { + return; + } + } + } + + /** + * Register a function to be autoloaded. + * + * @param callable $autoload_function The function to register. + * @param boolean $throw Should the function throw an exception if the function isn't callable? + * @param boolean $prepend Should we prepend the function to the stack? + */ + function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) { + if ( $throw && ! is_callable( $autoload_function ) ) { + // String not translated to match PHP core. + throw new Exception( 'Function not callable' ); + } + + global $_wp_spl_autoloaders; + + // Don't allow multiple registration. + if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) { + return; + } + + if ( $prepend ) { + array_unshift( $_wp_spl_autoloaders, $autoload_function ); + } else { + $_wp_spl_autoloaders[] = $autoload_function; + } + } + + /** + * Unregister an autoloader function. + * + * @param callable $function The function to unregister. + * @return boolean True if the function was unregistered, false if it could not be. + */ + function spl_autoload_unregister( $function ) { + global $_wp_spl_autoloaders; + foreach ( $_wp_spl_autoloaders as &$autoloader ) { + if ( $autoloader === $function ) { + unset( $autoloader ); + return true; + } + } + + return false; + } + + /** + * Get the registered autoloader functions. + * + * @return array List of autoloader functions. + */ + function spl_autoload_functions() { + return $GLOBALS['_wp_spl_autoloaders']; + } +endif; diff --git a/wp-includes/version.php b/wp-includes/version.php index c116a8c2a6..cd7ec9e462 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.6-alpha-37635'; +$wp_version = '4.6-alpha-37636'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.