0

I use a premium plugin (for theme dev.) that has included the WordPress Importer plugin and I get a deprecated notice:

"wp_get_http has been deprecated. Use WP_Http instead"

But the WP_Http send me to this function.

The question is how to use wp_http_supports in the following code instead of wp_get_http:

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
    $headers = wp_get_http( $url, $upload['file'] );

    // request failed
    if ( ! $headers ) {
        @unlink( $upload['file'] );
        return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
    }

    // make sure the fetch was successful
    if ( $headers['response'] != '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($headers['response']), get_status_header_desc($headers['response']) ) );
    }

    $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;
}

Any help will be greatly appreciated. Thanks.

1

WP_Http is a:

Core class used for managing HTTP transports and making HTTP requests.

so it should most likely be this link:

https://developer.wordpress.org/reference/classes/wp_http/

instead of:

https://developer.wordpress.org/reference/functions/WP_Http

that redirects you to:

https://developer.wordpress.org/reference/functions/wp_http_supports/

Then there are wrappers like wp_remote_get(), wp_remote_post(), wp_safe_remote_get(), wp_safe_remote_post(), wp_remote_retrieve_response_code(), download_url(), ... etc, that makes it easier to use.

Check out e.g. the HTTP API Codex page

Not sure what the use case is here, but there are also media_handle_sideload(), wp_handle_sideload(), ... that might be useful.

| improve this answer | |
  • Thanks! Using $headers = wp_http_supports( $url, $upload['file'] ); the plugin import all content excepting images: "Failed to import Media" – Knott Mar 23 '16 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.