Make WordPress Core


Ignore:
Timestamp:
03/04/2018 05:13:35 PM (8 years ago)
Author:
SergeyBiryukov
Message:

Filesystem API: Allow download_url() to return the response code and body on error as an additional WP_Error object data.

The error response body size is limited to 1 KB by default to avoid taking up too much memory. The size can be increased using download_url_error_max_body_size filter.

Props soulseekah, campusboy1987, mihdan, SergeyBiryukov.
Fixes #43329.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r42761 r42773  
    994994        }
    995995
    996         if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
     996        $response_code = wp_remote_retrieve_response_code( $response );
     997
     998        if ( 200 != $response_code ) {
     999                $data = array(
     1000                        'code' => $response_code,
     1001                );
     1002
     1003                // Retrieve a sample of the response body for debugging purposes.
     1004                $tmpf = fopen( $tmpfname, 'rb' );
     1005                if ( $tmpf ) {
     1006                        /**
     1007                         * Filters the maximum error response body size in `download_url()`.
     1008                         *
     1009                         * @since 5.0.0
     1010                         *
     1011                         * @see download_url()
     1012                         *
     1013                         * @param int $size The maximum error response body size. Default 1 KB.
     1014                         */
     1015                        $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
     1016                        $data['body']  = fread( $tmpf, $response_size );
     1017                        fclose( $tmpf );
     1018                }
     1019
    9971020                unlink( $tmpfname );
    998                 return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
     1021                return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
    9991022        }
    10001023
Note: See TracChangeset for help on using the changeset viewer.