WordPress.org

Make WordPress Core

Ticket #17251: 17251.diff

File 17251.diff, 3.2 KB (added by mdawaffe, 7 years ago)
  • wp-includes/class-http.php

     
    196196        }
    197197
    198198        /**
     199         * Tests which transports are capabable of supporting the request.
     200         *
     201         * @since 3.2.0
     202         * @access public
     203         *
     204         * @param string $url URL to Request
     205         * @param array $args Request arguments
     206         *
     207         * @return string|false Class name for the first transport that claims to support the request.  False if no transport claims to support the request.
     208         */
     209        function get_first_available_transport( $url, $args ) {
     210                $request_order = array( 'curl', 'streams', 'fsockopen' );
     211
     212                // Loop over each transport on each HTTP request looking for one which will serve this request's needs
     213                foreach ( $request_order as $transport ) {
     214                        $class = 'WP_HTTP_' . $transport;
     215
     216                        // Check to see if this transport is a possibility, calls the transport statically
     217                        if ( !call_user_func( array( $class, 'test' ), $args, $url ) )
     218                                continue;
     219
     220                        return $class;
     221                }
     222
     223                return false;
     224        }
     225
     226        /**
    199227         * Dispatches a HTTP request to a supporting transport.
    200228         *
    201229         * Tests each transport in order to find a transport which matches the request arguements.
     
    216244         * @param array $args Request arguments
    217245         * @return array|object Array containing 'headers', 'body', 'response', 'cookies'. A WP_Error instance upon error
    218246         */
    219         private function _dispatch_request($url, $args) {
     247        private function _dispatch_request( $url, $args ) {
    220248                static $transports = array();
    221249
    222                 $request_order = array('curl', 'streams', 'fsockopen');
     250                $class = isset( $args['transport_class'] ) ? $args['transport_class'] : $this->get_first_available_transport( $url, $args );
     251                if ( !$class )
     252                        return new WP_Error( 'http_failure', __( 'There are no HTTP transports available which can complete the requested request.' ) );
    223253
    224                 // Loop over each transport on each HTTP request looking for one which will serve this requests needs
    225                 foreach ( $request_order as $transport ) {
    226                         $class = 'WP_HTTP_' . $transport;
     254                // Transport claims to support request, Instantate it and give it a whirl.
     255                if ( empty( $transports[$class] ) )
     256                        $transports[$class] = new $class;
    227257
    228                         // Check to see if this transport is a possibility, calls the transport statically
    229                         if ( ! call_user_func( array($class, 'test'), $args, $url) )
    230                                 continue;
     258                $response = $transports[$class]->request( $url, $args );
    231259
    232                         // Transport claims to support request, Instantate it and give it a whirl.
    233                         if ( empty( $transports[ $transport ] ) )
    234                                 $transports[ $transport ] = new $class;
     260                do_action( 'http_api_debug', $response, 'response', $class );
    235261
    236                         $response = $transports[ $transport ]->request( $url, $args );
     262                if ( is_wp_error( $response ) )
     263                        return $response;
    237264
    238                         do_action( 'http_api_debug', $response, 'response', $class );
    239 
    240                         if ( is_wp_error( $response ) )
    241                                 return $response;
    242 
    243                         return apply_filters( 'http_response', $response, $args, $url );
    244                 }
    245 
    246                 return new WP_Error('http_failure', __('There are no HTTP transports available which can complete the requested request.') );
     265                return apply_filters( 'http_response', $response, $args, $url );
    247266        }
    248267
    249268        /**