Make WordPress Core


Ignore:
Timestamp:
07/03/2015 12:00:10 PM (9 years ago)
Author:
dd32
Message:

Press This: Rewrite WP_Press_This::fetch_source_html() to utilise the HTTP API better without the need for a local temporary file.
This reverts [33061] but keeps it's custom UA string.

See #32864

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-press-this.php

    r33061 r33062  
    257257     */
    258258    public function fetch_source_html( $url ) {
    259         // Download source page to tmp file.
    260         $source_tmp_file = ( ! empty( $url ) ) ? download_url( $url, 30 ) : '';
    261         $source_content  = '';
    262 
    263         if ( ! is_wp_error( $source_tmp_file ) && file_exists( $source_tmp_file ) ) {
    264 
    265             // Get the content of the source page from the tmp file..
    266             $source_content = wp_kses(
    267                 @file_get_contents( $source_tmp_file ),
    268                 array(
    269                     'img' => array(
    270                         'src'      => array(),
    271                         'width'    => array(),
    272                         'height'   => array(),
    273                     ),
    274                     'iframe' => array(
    275                         'src'      => array(),
    276                     ),
    277                     'link' => array(
    278                         'rel'      => array(),
    279                         'itemprop' => array(),
    280                         'href'     => array(),
    281                     ),
    282                     'meta' => array(
    283                         'property' => array(),
    284                         'name'     => array(),
    285                         'content'  => array(),
    286                     )
    287                 )
    288             );
    289 
    290             // All done with backward compatibility. Let's do some cleanup, for good measure :)
    291             unlink( $source_tmp_file );
    292 
    293         } else if ( is_wp_error( $source_tmp_file ) ) {
    294             $source_content = new WP_Error( 'upload-error',  sprintf( __( 'ERROR: %s' ), sprintf( __( 'Could not download the source URL (native error: %s).' ), $source_tmp_file->get_error_message() ) ) );
    295         } else if ( ! file_exists( $source_tmp_file ) ) {
    296             $source_content = new WP_Error( 'no-local-file',  sprintf( __( 'ERROR: %s' ), __( 'Could not save or locate the temporary download file for the source URL.' ) ) );
    297         }
     259        global $wp_version;
     260
     261        if ( empty( $url ) ) {
     262            return new WP_Error( 'invalid-url', __( 'A valid URL was not provided.' ) );
     263        }
     264
     265        $remote_url = wp_safe_remote_get( $url, array(
     266            'timeout' => 30,
     267            // Use an explicit user-agent for Press This
     268            'user-agent' => 'Press This (WordPress/' . $wp_version . '); ' . get_bloginfo( 'url' )
     269        ) );
     270
     271        if ( is_wp_error( $remote_url ) ) {
     272            return $remote_url;
     273        }
     274
     275        $useful_html_elements = array(
     276            'img' => array(
     277                'src'      => true,
     278                'width'    => true,
     279                'height'   => true,
     280            ),
     281            'iframe' => array(
     282                'src'      => true,
     283            ),
     284            'link' => array(
     285                'rel'      => true,
     286                'itemprop' => true,
     287                'href'     => true,
     288            ),
     289            'meta' => array(
     290                'property' => true,
     291                'name'     => true,
     292                'content'  => true,
     293            )
     294        );
     295
     296        $source_content = wp_remote_retrieve_body( $remote_url );
     297        $source_content = wp_kses( $source_content, $useful_html_elements );
    298298
    299299        return $source_content;
     
    11691169
    11701170    /**
    1171      * Sets the user agent used for Press This HTTP requests.
    1172      *
    1173      * @since 4.3.0
    1174      * @access public
    1175      *
    1176      * @global string $wp_version
    1177      *
    1178      * @return string User agent.
    1179      */
    1180     public function ua_string() {
    1181         global $wp_version;
    1182 
    1183         $user_agent = 'Press This (WordPress/' . $wp_version . '); ' . get_bloginfo( 'url' );
    1184 
    1185         return $user_agent;
    1186     }
    1187 
    1188     /**
    11891171     * Serves the app's base HTML, which in turns calls the load script.
    11901172     *
     
    11981180    public function html() {
    11991181        global $wp_locale, $wp_version;
    1200 
    1201         // Set explicit user-agent for the $data outbound HTTP requests.
    1202         add_filter( 'http_headers_useragent', array( $this, 'ua_string' ) );
    12031182
    12041183        // Get data, new (POST) and old (GET).
Note: See TracChangeset for help on using the changeset viewer.