Make WordPress Core

Ticket #33709: 33709.3.diff

File 33709.3.diff, 6.4 KB (added by swissspidy, 10 years ago)

Add @see param, write WP_Http correctly.

  • src/wp-includes/deprecated.php

    diff --git src/wp-includes/deprecated.php src/wp-includes/deprecated.php
    index 582b2fd..914d137 100644
    function post_permalink( $post_id = 0 ) { 
    35613561        return get_permalink( $post_id );
    35623562}
    35633563
     3564/**
     3565 * Perform a HTTP HEAD or GET request.
     3566 *
     3567 * If $file_path is a writable filename, this will do a GET request and write
     3568 * the file to that path.
     3569 *
     3570 * @since 2.5.0
     3571 * @deprecated 4.4.0 Use WP_Http
     3572 * @see WP_Http
     3573 *
     3574 * @param string      $url       URL to fetch.
     3575 * @param string|bool $file_path Optional. File path to write request to. Default false.
     3576 * @param int         $red       Optional. The number of Redirects followed, Upon 5 being hit,
     3577 *                               returns false. Default 1.
     3578 * @return bool|string False on failure and string of headers if HEAD request.
     3579 */
     3580function wp_get_http( $url, $file_path = false, $red = 1 ) {
     3581        _deprecated_function( __FUNCTION__, '4.4', 'WP_Http' );
     3582
     3583        @set_time_limit( 60 );
     3584
     3585        if ( $red > 5 )
     3586                return false;
     3587
     3588        $options = array();
     3589        $options['redirection'] = 5;
     3590
     3591        if ( false == $file_path )
     3592                $options['method'] = 'HEAD';
     3593        else
     3594                $options['method'] = 'GET';
     3595
     3596        $response = wp_safe_remote_request( $url, $options );
     3597
     3598        if ( is_wp_error( $response ) )
     3599                return false;
     3600
     3601        $headers = wp_remote_retrieve_headers( $response );
     3602        $headers['response'] = wp_remote_retrieve_response_code( $response );
     3603
     3604        // WP_HTTP no longer follows redirects for HEAD requests.
     3605        if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
     3606                return wp_get_http( $headers['location'], $file_path, ++$red );
     3607        }
     3608
     3609        if ( false == $file_path )
     3610                return $headers;
     3611
     3612        // GET request - write it to the supplied filename
     3613        $out_fp = fopen($file_path, 'w');
     3614        if ( !$out_fp )
     3615                return $headers;
     3616
     3617        fwrite( $out_fp,  wp_remote_retrieve_body( $response ) );
     3618        fclose($out_fp);
     3619        clearstatcache();
     3620
     3621        return $headers;
     3622}
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 9206f9b..2f55528 100644
    function do_enclose( $content, $post_ID ) { 
    564564}
    565565
    566566/**
    567  * Perform a HTTP HEAD or GET request.
    568  *
    569  * If $file_path is a writable filename, this will do a GET request and write
    570  * the file to that path.
    571  *
    572  * @since 2.5.0
    573  *
    574  * @param string      $url       URL to fetch.
    575  * @param string|bool $file_path Optional. File path to write request to. Default false.
    576  * @param int         $red       Optional. The number of Redirects followed, Upon 5 being hit,
    577  *                               returns false. Default 1.
    578  * @return bool|string False on failure and string of headers if HEAD request.
    579  */
    580 function wp_get_http( $url, $file_path = false, $red = 1 ) {
    581         @set_time_limit( 60 );
    582 
    583         if ( $red > 5 )
    584                 return false;
    585 
    586         $options = array();
    587         $options['redirection'] = 5;
    588 
    589         if ( false == $file_path )
    590                 $options['method'] = 'HEAD';
    591         else
    592                 $options['method'] = 'GET';
    593 
    594         $response = wp_safe_remote_request( $url, $options );
    595 
    596         if ( is_wp_error( $response ) )
    597                 return false;
    598 
    599         $headers = wp_remote_retrieve_headers( $response );
    600         $headers['response'] = wp_remote_retrieve_response_code( $response );
    601 
    602         // WP_HTTP no longer follows redirects for HEAD requests.
    603         if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
    604                 return wp_get_http( $headers['location'], $file_path, ++$red );
    605         }
    606 
    607         if ( false == $file_path )
    608                 return $headers;
    609 
    610         // GET request - write it to the supplied filename
    611         $out_fp = fopen($file_path, 'w');
    612         if ( !$out_fp )
    613                 return $headers;
    614 
    615         fwrite( $out_fp,  wp_remote_retrieve_body( $response ) );
    616         fclose($out_fp);
    617         clearstatcache();
    618 
    619         return $headers;
    620 }
    621 
    622 /**
    623567 * Retrieve HTTP Headers from URL.
    624568 *
    625569 * @since 1.5.1
  • tests/phpunit/tests/http/functions.php

    diff --git tests/phpunit/tests/http/functions.php tests/phpunit/tests/http/functions.php
    index 7bb60e8..2547b2d 100644
    class Tests_HTTP_Functions extends WP_UnitTestCase { 
    4242
    4343        function test_get_request() {
    4444                $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    45                 $file = tempnam('/tmp', 'testfile');
    4645
    47                 $headers = wp_get_http($url, $file);
     46                $response = wp_remote_get( $url );
     47                $headers = wp_remote_retrieve_headers( $response );
    4848
    4949                // should return the same headers as a head request
    5050                $this->assertInternalType( 'array', $headers, "Reply wasn't array." );
    5151                $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    5252                $this->assertEquals( '40148', $headers['content-length'] );
    53                 $this->assertEquals( '200', $headers['response'] );
    54 
    55                 // make sure the file is ok
    56                 $this->assertEquals( 40148, filesize($file) );
    57                 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
     53                $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
    5854        }
    5955
    6056        function test_get_redirect() {
    6157                // this will redirect to asdftestblog1.files.wordpress.com
    6258                $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    63                 $file = tempnam('/tmp', 'testfile');
    6459
    65                 $headers = wp_get_http($url, $file);
     60                $response = wp_remote_get($url);
     61                $headers = wp_remote_retrieve_headers( $response );
    6662
    6763                // should return the same headers as a head request
    6864                $this->assertInternalType( 'array', $headers, "Reply wasn't array." );
    6965                $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    7066                $this->assertEquals( '40148', $headers['content-length'] );
    71                 $this->assertEquals( '200', $headers['response'] );
    72 
    73                 // make sure the file is ok
    74                 $this->assertEquals( 40148, filesize($file) );
    75                 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
     67                $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
    7668        }
    7769
    7870        function test_get_redirect_limit_exceeded() {
    7971                // this will redirect to asdftestblog1.files.wordpress.com
    8072                $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    81                 $file = tempnam('/tmp', 'testfile');
     73
    8274                // pretend we've already redirected 5 times
    83                 $headers = wp_get_http( $url, $file, 6 );
    84                 $this->assertFalse( $headers );
     75                $response = wp_remote_get( $url, array( 'redirection' => -1 ) );
     76                $this->assertWPError( $response );
    8577        }
    8678}