/** * Tests the WordPress HTTP objects for an object to use and returns it. * * Tests all of the objects and returns the object that passes. Also caches * that object to be used later. This is for posting content to a URL and * is used when there is a body. The plain Fopen Transport can not be used * to send content, but the streams transport can. This is a limitation that * is addressed here, by just not including that transport. * * @since 2.7.0 * @access private * * @param array $args Request args, default us an empty array * @return object|null Null if no transports are available, HTTP transport object. */ function &_postTransport($args = array()) { static $working_transport, $blocking_transport, $nonblocking_transport; if (is_null($working_transport)) { if (true === WP_Http_ExtHttp::test($args)) { $working_transport['exthttp'] = new WP_Http_ExtHttp(); $blocking_transport[] =& $working_transport['exthttp']; } else { if (true === WP_Http_Curl::test($args)) { $working_transport['curl'] = new WP_Http_Curl(); $blocking_transport[] =& $working_transport['curl']; } else { if (true === WP_Http_Streams::test($args)) { $working_transport['streams'] = new WP_Http_Streams(); $blocking_transport[] =& $working_transport['streams']; } else { if (true === WP_Http_Fsockopen::test($args)) { $working_transport['fsockopen'] = new WP_Http_Fsockopen(); $blocking_transport[] =& $working_transport['fsockopen']; } } } } foreach (array('curl', 'streams', 'fsockopen', 'exthttp') as $transport) { if (isset($working_transport[$transport])) { $nonblocking_transport[] =& $working_transport[$transport]; } } } do_action('http_transport_post_debug', $working_transport, $blocking_transport, $nonblocking_transport); if (isset($args['blocking']) && !$args['blocking']) { return $nonblocking_transport; } else { return $blocking_transport; } }
function have_streams($args, $url = NULL) { return WP_Http_Streams::test($args); }
/** * Tests the WordPress HTTP objects for an object to use and returns it. * * Tests all of the objects and returns the object that passes. Also caches * that object to be used later. This is for posting content to a URL and * is used when there is a body. The plain Fopen Transport can not be used * to send content, but the streams transport can. This is a limitation that * is addressed here, by just not including that transport. * * @since 2.7.0 * @access private * * @param array $args Request args, default us an empty array * @return object|null Null if no transports are available, HTTP transport object. */ function &_postTransport($args = array()) { static $working_transport, $blocking_transport, $nonblocking_transport; if (is_null($working_transport)) { if (true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true)) { $working_transport['exthttp'] = new WP_Http_ExtHttp(); $blocking_transport[] =& $working_transport['exthttp']; } else { if (true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true)) { $working_transport['streams'] = new WP_Http_Streams(); $blocking_transport[] =& $working_transport['streams']; } else { if (true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true)) { $working_transport['fsockopen'] = new WP_Http_Fsockopen(); $blocking_transport[] =& $working_transport['fsockopen']; } } } foreach (array('streams', 'fsockopen', 'exthttp') as $transport) { if (isset($working_transport[$transport])) { $nonblocking_transport[] =& $working_transport[$transport]; } } } if (has_filter('http_transport_post_debug')) { do_action('http_transport_post_debug', $working_transport, $blocking_transport, $nonblocking_transport); } if (isset($args['blocking']) && !$args['blocking']) { return $nonblocking_transport; } else { return $blocking_transport; } }
/** * Attempt to download a remote file attachment * * @param string $url URL of item to fetch * @param array $post Attachment details * @return array|WP_Error Local file location details on success, WP_Error otherwise */ function fetch_remote_file($url, $post) { // extract the file name and extension from the url $file_name = basename($url); // get placeholder file in the upload dir with a unique, sanitized filename $upload = wp_upload_bits($file_name, 0, '', $post['upload_date']); if ($upload['error']) { return new WP_Error('upload_dir_error', $upload['error']); } // fetch the remote url and write it to the placeholder file $wp_http = new WP_Http_Streams(); $http_response = $wp_http->request($url, array('filename' => $upload['file'], 'stream' => true, 'sslcertificates' => '', 'decompress' => false)); // request failed if (is_wp_error($http_response)) { @unlink($upload['file']); return new WP_Error('import_file_error', __('Remote server did not respond', 'wordpress-importer')); } // make sure the fetch was successful $response = $http_response['response']; if ($response['code'] != '200') { @unlink($upload['file']); return new WP_Error('import_file_error', sprintf(__('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($response['code']), get_status_header_desc($response['code']))); } $headers = $http_response['headers']; $filesize = filesize($upload['file']); if (isset($headers['content-length']) && $filesize != $headers['content-length']) { @unlink($upload['file']); return new WP_Error('import_file_error', __('Remote file is incorrect size', 'wordpress-importer')); } if (0 == $filesize) { @unlink($upload['file']); return new WP_Error('import_file_error', __('Zero size file downloaded', 'wordpress-importer')); } $max_size = (int) $this->max_attachment_size(); if (!empty($max_size) && $filesize > $max_size) { @unlink($upload['file']); return new WP_Error('import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size))); } // keep track of the old and new urls so we can substitute them later $this->url_remap[$url] = $upload['url']; $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed? // keep track of the destination if the remote url is redirected somewhere else if (isset($headers['x-final-location']) && $headers['x-final-location'] != $url) { $this->url_remap[$headers['x-final-location']] = $upload['url']; } return $upload; }