Make WordPress Core

Ticket #14786: 14786.11.patch

File 14786.11.patch, 6.3 KB (added by hakre, 15 years ago)

Transports defined in WP_Http::$transports; Filterable per context / args; WP_Http_ExtHTTP class name case changed to make data more simple (same casing for all); Changed case of other WP_HtTp* class names as well.

  • wp-includes/class-http.php

     
    3636 * @since 2.7.0
    3737 */
    3838class WP_Http {
     39        /**
     40         * Default transports (and order) provided
     41         * by WP_Http
     42         *
     43         * @since 3.1.0
     44         * @access public
     45         * @var array
     46         */
     47        var $transports = array(
     48                'exthttp'  ,
     49                'curl'     ,
     50                'streams'  ,
     51                'fopen'    ,
     52                'fsockopen',
     53        );
    3954
    4055        /**
    4156         * PHP4 style Constructor - Calls PHP5 Style Constructor
     
    5974         * @return WP_Http
    6075         */
    6176        function __construct() {
    62                 WP_Http::_getTransport();
    63                 WP_Http::_postTransport();
     77                foreach ( array( 'get', 'post' ) as $context )
     78                        WP_Http::_namedTransport( $context );
    6479        }
    6580
    6681        /**
     82         * Tests through all WP_Http objects and stores the first match
     83         * and returns.
     84         *
     85         * Test Order:
     86         *
     87         *   1. exthttp  : WP_Http_Exthttp
     88         *   2. curl     : WP_Http_Curl
     89         *   3. streams  : WP_Http_Streams
     90         *   4. fopen    : WP_Http_Fopen
     91         *   5. fsockopen: WP_Http_Fsockopen
     92         *
     93         * @since 3.1.0
     94         * @access private
     95         * @param string $context named context
     96         * @param array $args (optional) Request arguments
     97         * @return object|null Concrete HTTP Transport Object; Null if no transport was found.
     98         */
     99        function &_namedTransport( $context, $args = array() ) {
     100                static $working, $blocking, $nonblocking;
     101
     102                if ( !isset( $working[$context] ) ) {
     103                        $transports = apply_filters( 'http_transport_available_transports', $this->transports, $context, $args );
     104
     105                        foreach ( $transports as $name ) {
     106                                false === class_exists( ( $class = $name ) )
     107                                && ( $class = 'WP_Http_' . ucfirst( $name ) );
     108
     109                                if ( !call_user_func( array( $class, 'test' ), $args ) )
     110                                        continue;
     111
     112                                $wp_http = new $class();
     113                               
     114                                $working[$context][$name] = &$wp_http;
     115                                $blocking[$context][]     = &$wp_http;
     116                                $nonblocking[$context][]  = &$wp_http;
     117
     118                                break;
     119                        }
     120                }
     121
     122                do_action( 'http_transport_get_debug', $working[$context], $blocking[$context], $nonblocking[$context] );
     123
     124                if ( isset( $args['blocking'] ) && !$args['blocking'] )
     125                        return $nonblocking[$context];
     126                else
     127                        return $blocking[$context];
     128               
     129        }
     130
     131        /**
    67132         * Tests the WordPress HTTP objects for an object to use and returns it.
    68133         *
    69134         * Tests all of the objects and returns the object that passes. Also caches that object to be
     
    86151         * @return object|null Null if no transports are available, HTTP transport object.
    87152         */
    88153        function &_getTransport( $args = array() ) {
    89                 static $working_transport, $blocking_transport, $nonblocking_transport;
    90 
    91                 if ( is_null($working_transport) ) {
    92                         if ( true === WP_Http_ExtHttp::test($args) ) {
    93                                 $working_transport['exthttp'] = new WP_Http_ExtHttp();
    94                                 $blocking_transport[] = &$working_transport['exthttp'];
    95                         } else if ( true === WP_Http_Curl::test($args) ) {
    96                                 $working_transport['curl'] = new WP_Http_Curl();
    97                                 $blocking_transport[] = &$working_transport['curl'];
    98                         } else if ( true === WP_Http_Streams::test($args) ) {
    99                                 $working_transport['streams'] = new WP_Http_Streams();
    100                                 $blocking_transport[] = &$working_transport['streams'];
    101                         } else if ( true === WP_Http_Fopen::test($args) ) {
    102                                 $working_transport['fopen'] = new WP_Http_Fopen();
    103                                 $blocking_transport[] = &$working_transport['fopen'];
    104                         } else if ( true === WP_Http_Fsockopen::test($args) ) {
    105                                 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
    106                                 $blocking_transport[] = &$working_transport['fsockopen'];
    107                         }
    108 
    109                         foreach ( array('curl', 'streams', 'fopen', 'fsockopen', 'exthttp') as $transport ) {
    110                                 if ( isset($working_transport[$transport]) )
    111                                         $nonblocking_transport[] = &$working_transport[$transport];
    112                         }
    113                 }
    114 
    115                 do_action( 'http_transport_get_debug', $working_transport, $blocking_transport, $nonblocking_transport );
    116 
    117                 if ( isset($args['blocking']) && !$args['blocking'] )
    118                         return $nonblocking_transport;
    119                 else
    120                         return $blocking_transport;
     154                return $this->_namedTransport( 'get', $args );
    121155        }
    122156
    123157        /**
     
    136170         * @return object|null Null if no transports are available, HTTP transport object.
    137171         */
    138172        function &_postTransport( $args = array() ) {
    139                 static $working_transport, $blocking_transport, $nonblocking_transport;
    140 
    141                 if ( is_null($working_transport) ) {
    142                         if ( true === WP_Http_ExtHttp::test($args) ) {
    143                                 $working_transport['exthttp'] = new WP_Http_ExtHttp();
    144                                 $blocking_transport[] = &$working_transport['exthttp'];
    145                         } else if ( true === WP_Http_Curl::test($args) ) {
    146                                 $working_transport['curl'] = new WP_Http_Curl();
    147                                 $blocking_transport[] = &$working_transport['curl'];
    148                         } else if ( true === WP_Http_Streams::test($args) ) {
    149                                 $working_transport['streams'] = new WP_Http_Streams();
    150                                 $blocking_transport[] = &$working_transport['streams'];
    151                         } else if ( true === WP_Http_Fsockopen::test($args) ) {
    152                                 $working_transport['fsockopen'] = new WP_Http_Fsockopen();
    153                                 $blocking_transport[] = &$working_transport['fsockopen'];
    154                         }
    155 
    156                         foreach ( array('curl', 'streams', 'fsockopen', 'exthttp') as $transport ) {
    157                                 if ( isset($working_transport[$transport]) )
    158                                         $nonblocking_transport[] = &$working_transport[$transport];
    159                         }
    160                 }
    161 
    162                 do_action( 'http_transport_post_debug', $working_transport, $blocking_transport, $nonblocking_transport );
    163 
    164                 if ( isset($args['blocking']) && !$args['blocking'] )
    165                         return $nonblocking_transport;
    166                 else
    167                         return $blocking_transport;
     173                return $this->_namedTransport( 'post', $args );
    168174        }
    169175
    170176        /**
     
    914920                if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
    915921                        return false;
    916922
    917                 if ( isset($args['method']) && 'HEAD' == $args['method'] ) //This transport cannot make a HEAD request
     923                if ( isset( $args['method'] ) && 'GET' !== $args['method'] ) //This transport cannot make anything else then a GET request
    918924                        return false;
    919925
    920926                $use = true;
     
    11201126 * @subpackage HTTP
    11211127 * @since 2.7.0
    11221128 */
    1123 class WP_Http_ExtHTTP {
     1129class WP_Http_Exthttp {
    11241130        /**
    11251131         * Send a HTTP request to a URI using HTTP extension.
    11261132         *
     
    14911497 * @link http://core.trac.wordpress.org/ticket/4011 Proxy support ticket in WordPress.
    14921498 * @since 2.8
    14931499 */
    1494 class WP_HTTP_Proxy {
     1500class WP_Http_Proxy {
    14951501
    14961502        /**
    14971503         * Whether proxy connection should be used.