diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 582b2fd5f2..914d137658 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -3561,3 +3561,62 @@ function post_permalink( $post_id = 0 ) { return get_permalink( $post_id ); } +/** + * Perform a HTTP HEAD or GET request. + * + * If $file_path is a writable filename, this will do a GET request and write + * the file to that path. + * + * @since 2.5.0 + * @deprecated 4.4.0 Use WP_Http + * @see WP_Http + * + * @param string $url URL to fetch. + * @param string|bool $file_path Optional. File path to write request to. Default false. + * @param int $red Optional. The number of Redirects followed, Upon 5 being hit, + * returns false. Default 1. + * @return bool|string False on failure and string of headers if HEAD request. + */ +function wp_get_http( $url, $file_path = false, $red = 1 ) { + _deprecated_function( __FUNCTION__, '4.4', 'WP_Http' ); + + @set_time_limit( 60 ); + + if ( $red > 5 ) + return false; + + $options = array(); + $options['redirection'] = 5; + + if ( false == $file_path ) + $options['method'] = 'HEAD'; + else + $options['method'] = 'GET'; + + $response = wp_safe_remote_request( $url, $options ); + + if ( is_wp_error( $response ) ) + return false; + + $headers = wp_remote_retrieve_headers( $response ); + $headers['response'] = wp_remote_retrieve_response_code( $response ); + + // WP_HTTP no longer follows redirects for HEAD requests. + if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) { + return wp_get_http( $headers['location'], $file_path, ++$red ); + } + + if ( false == $file_path ) + return $headers; + + // GET request - write it to the supplied filename + $out_fp = fopen($file_path, 'w'); + if ( !$out_fp ) + return $headers; + + fwrite( $out_fp, wp_remote_retrieve_body( $response ) ); + fclose($out_fp); + clearstatcache(); + + return $headers; +} diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 9206f9bffd..2f55528c7b 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -563,62 +563,6 @@ function do_enclose( $content, $post_ID ) { } } -/** - * Perform a HTTP HEAD or GET request. - * - * If $file_path is a writable filename, this will do a GET request and write - * the file to that path. - * - * @since 2.5.0 - * - * @param string $url URL to fetch. - * @param string|bool $file_path Optional. File path to write request to. Default false. - * @param int $red Optional. The number of Redirects followed, Upon 5 being hit, - * returns false. Default 1. - * @return bool|string False on failure and string of headers if HEAD request. - */ -function wp_get_http( $url, $file_path = false, $red = 1 ) { - @set_time_limit( 60 ); - - if ( $red > 5 ) - return false; - - $options = array(); - $options['redirection'] = 5; - - if ( false == $file_path ) - $options['method'] = 'HEAD'; - else - $options['method'] = 'GET'; - - $response = wp_safe_remote_request( $url, $options ); - - if ( is_wp_error( $response ) ) - return false; - - $headers = wp_remote_retrieve_headers( $response ); - $headers['response'] = wp_remote_retrieve_response_code( $response ); - - // WP_HTTP no longer follows redirects for HEAD requests. - if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) { - return wp_get_http( $headers['location'], $file_path, ++$red ); - } - - if ( false == $file_path ) - return $headers; - - // GET request - write it to the supplied filename - $out_fp = fopen($file_path, 'w'); - if ( !$out_fp ) - return $headers; - - fwrite( $out_fp, wp_remote_retrieve_body( $response ) ); - fclose($out_fp); - clearstatcache(); - - return $headers; -} - /** * Retrieve HTTP Headers from URL. * diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php index 7bb60e863f..ce2631495f 100644 --- a/tests/phpunit/tests/http/functions.php +++ b/tests/phpunit/tests/http/functions.php @@ -42,45 +42,37 @@ class Tests_HTTP_Functions extends WP_UnitTestCase { function test_get_request() { $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; - $file = tempnam('/tmp', 'testfile'); - $headers = wp_get_http($url, $file); + $response = wp_remote_get( $url ); + $headers = wp_remote_retrieve_headers( $response ); // should return the same headers as a head request $this->assertInternalType( 'array', $headers, "Reply wasn't array." ); $this->assertEquals( 'image/jpeg', $headers['content-type'] ); $this->assertEquals( '40148', $headers['content-length'] ); - $this->assertEquals( '200', $headers['response'] ); - - // make sure the file is ok - $this->assertEquals( 40148, filesize($file) ); - $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); + $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); } function test_get_redirect() { // this will redirect to asdftestblog1.files.wordpress.com $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; - $file = tempnam('/tmp', 'testfile'); - $headers = wp_get_http($url, $file); + $response = wp_remote_get( $url ); + $headers = wp_remote_retrieve_headers( $response ); // should return the same headers as a head request $this->assertInternalType( 'array', $headers, "Reply wasn't array." ); $this->assertEquals( 'image/jpeg', $headers['content-type'] ); $this->assertEquals( '40148', $headers['content-length'] ); - $this->assertEquals( '200', $headers['response'] ); - - // make sure the file is ok - $this->assertEquals( 40148, filesize($file) ); - $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); + $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); } function test_get_redirect_limit_exceeded() { // this will redirect to asdftestblog1.files.wordpress.com $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; - $file = tempnam('/tmp', 'testfile'); + // pretend we've already redirected 5 times - $headers = wp_get_http( $url, $file, 6 ); - $this->assertFalse( $headers ); + $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); + $this->assertWPError( $response ); } }