Make WordPress Core

Ticket #40109: 41134.3.patch

File 41134.3.patch, 5.9 KB (added by ayeshrajans, 8 years ago)

Patch to move the spl_autoload polyfils and autoload declaration

  • wp-includes/compat.php

    diff --git wp-includes/compat.php wp-includes/compat.php
    index 0ccee2d49..1487c32d5 100644
    function array_replace_recursive( $base = array(), $replacements = array() ) { 
    488488        }
    489489endif;
    490490
    491 // SPL can be disabled on PHP 5.2
    492 if ( ! function_exists( 'spl_autoload_register' ) ):
    493         $_wp_spl_autoloaders = array();
    494 
    495         /**
    496          * Autoloader compatibility callback.
    497          *
    498          * @since 4.6.0
    499          *
    500          * @param string $classname Class to attempt autoloading.
    501          */
    502         function __autoload( $classname ) {
    503                 global $_wp_spl_autoloaders;
    504                 foreach ( $_wp_spl_autoloaders as $autoloader ) {
    505                         if ( ! is_callable( $autoloader ) ) {
    506                                 // Avoid the extra warning if the autoloader isn't callable.
    507                                 continue;
    508                         }
    509 
    510                         call_user_func( $autoloader, $classname );
    511 
    512                         // If it has been autoloaded, stop processing.
    513                         if ( class_exists( $classname, false ) ) {
    514                                 return;
    515                         }
    516                 }
    517         }
    518 
    519         /**
    520          * Registers a function to be autoloaded.
    521          *
    522          * @since 4.6.0
    523          *
    524          * @param callable $autoload_function The function to register.
    525          * @param bool     $throw             Optional. Whether the function should throw an exception
    526          *                                    if the function isn't callable. Default true.
    527          * @param bool     $prepend           Whether the function should be prepended to the stack.
    528          *                                    Default false.
    529          */
    530         function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
    531                 if ( $throw && ! is_callable( $autoload_function ) ) {
    532                         // String not translated to match PHP core.
    533                         throw new Exception( 'Function not callable' );
    534                 }
    535 
    536                 global $_wp_spl_autoloaders;
    537 
    538                 // Don't allow multiple registration.
    539                 if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
    540                         return;
    541                 }
    542 
    543                 if ( $prepend ) {
    544                         array_unshift( $_wp_spl_autoloaders, $autoload_function );
    545                 } else {
    546                         $_wp_spl_autoloaders[] = $autoload_function;
    547                 }
    548         }
    549 
    550         /**
    551          * Unregisters an autoloader function.
    552          *
    553          * @since 4.6.0
    554          *
    555          * @param callable $function The function to unregister.
    556          * @return bool True if the function was unregistered, false if it could not be.
    557          */
    558         function spl_autoload_unregister( $function ) {
    559                 global $_wp_spl_autoloaders;
    560                 foreach ( $_wp_spl_autoloaders as &$autoloader ) {
    561                         if ( $autoloader === $function ) {
    562                                 unset( $autoloader );
    563                                 return true;
    564                         }
    565                 }
    566 
    567                 return false;
    568         }
    569 
    570         /**
    571          * Retrieves the registered autoloader functions.
    572          *
    573          * @since 4.6.0
    574          *
    575          * @return array List of autoloader functions.
    576          */
    577         function spl_autoload_functions() {
    578                 return $GLOBALS['_wp_spl_autoloaders'];
    579         }
    580 endif;
     491/**
     492 * Polyfill for the SPL autoloader. In PHP 5.2 (but not 5.3 and later), SPL can
     493 * be disabled, and PHP 7.2 raises notices if the compiler finds an __autoload()
     494 * function declaration. Function availability is checked here, and the
     495 * autoloader is included only if necessary.
     496 *
     497 * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
     498 */
     499if ( ! function_exists( 'spl_autoload_register' ) ) {
     500  require_once ABSPATH . WPINC . '/spl-autoload-compat.php';
     501}
  • new file wp-includes/spl-autoload-compat.php

    diff --git wp-includes/spl-autoload-compat.php wp-includes/spl-autoload-compat.php
    new file mode 100644
    index 000000000..d9997fced
    - +  
     1<?php
     2/**
     3 * Polyfill for SPL autoload feature. This file is a separate one to prevent
     4 * compiler notices on the deprecated __autoload() function. See Trac #41134.
     5 *
     6 * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.
     7 *
     8 * @package PHP
     9 * @access private
     10 */
     11
     12$_wp_spl_autoloaders = array();
     13
     14/**
     15 * Autoloader compatibility callback.
     16 *
     17 * @since 4.6.0
     18 *
     19 * @param string $classname Class to attempt autoloading.
     20 */
     21function __autoload( $classname ) {
     22  global $_wp_spl_autoloaders;
     23  foreach ( $_wp_spl_autoloaders as $autoloader ) {
     24    if ( ! is_callable( $autoloader ) ) {
     25      // Avoid the extra warning if the autoloader isn't callable.
     26      continue;
     27    }
     28
     29    call_user_func( $autoloader, $classname );
     30
     31    // If it has been autoloaded, stop processing.
     32    if ( class_exists( $classname, false ) ) {
     33      return;
     34    }
     35  }
     36}
     37
     38/**
     39 * Registers a function to be autoloaded.
     40 *
     41 * @since 4.6.0
     42 *
     43 * @param callable $autoload_function The function to register.
     44 * @param bool     $throw             Optional. Whether the function should throw an exception
     45 *                                    if the function isn't callable. Default true.
     46 * @param bool     $prepend           Whether the function should be prepended to the stack.
     47 *                                    Default false.
     48 */
     49function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
     50  if ( $throw && ! is_callable( $autoload_function ) ) {
     51    // String not translated to match PHP core.
     52    throw new Exception( 'Function not callable' );
     53  }
     54
     55  global $_wp_spl_autoloaders;
     56
     57  // Don't allow multiple registration.
     58  if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
     59    return;
     60  }
     61
     62  if ( $prepend ) {
     63    array_unshift( $_wp_spl_autoloaders, $autoload_function );
     64  } else {
     65    $_wp_spl_autoloaders[] = $autoload_function;
     66  }
     67}
     68
     69/**
     70 * Unregisters an autoloader function.
     71 *
     72 * @since 4.6.0
     73 *
     74 * @param callable $function The function to unregister.
     75 * @return bool True if the function was unregistered, false if it could not be.
     76 */
     77function spl_autoload_unregister( $function ) {
     78  global $_wp_spl_autoloaders;
     79  foreach ( $_wp_spl_autoloaders as &$autoloader ) {
     80    if ( $autoloader === $function ) {
     81      unset( $autoloader );
     82      return true;
     83    }
     84  }
     85
     86  return false;
     87}
     88
     89/**
     90 * Retrieves the registered autoloader functions.
     91 *
     92 * @since 4.6.0
     93 *
     94 * @return array List of autoloader functions.
     95 */
     96function spl_autoload_functions() {
     97  return $GLOBALS['_wp_spl_autoloaders'];
     98}