WordPress.org

Make WordPress Core

Ticket #19629: 19629.5.diff

File 19629.5.diff, 8.1 KB (added by mattheu, 6 years ago)

Refresh + Unit Tests

  • src/wp-admin/includes/deprecated.php

    diff --git src/wp-admin/includes/deprecated.php src/wp-admin/includes/deprecated.php
    index f6fa0a2..bca1b3b 100644
    function wp_dashboard_secondary_control() {} 
    11881188function _relocate_children( $old_ID, $new_ID ) {
    11891189        _deprecated_function( __FUNCTION__, '3.9' );
    11901190}
     1191
     1192
     1193/**
     1194 * Download an image from the specified URL and attach it to a post.
     1195 * This was once used by Press This
     1196 *
     1197 * @since 2.6.0
     1198 * @todo  add deprecated as of version
     1199 *
     1200 * @param string $file The URL of the image to download
     1201 * @param int $post_id The post ID the media is to be associated with
     1202 * @param string $desc Optional. Description of the image
     1203 * @return string|WP_Error Populated HTML img tag on success
     1204 */
     1205function media_sideload_image($file, $post_id, $desc = null) {
     1206        _deprecated_function( __FUNCTION__, '4.0' );
     1207        $attachment_id = media_handle_sideload( $file, $post_id, $desc );
     1208        if ( ! is_wp_error( $attachment_id ) ) {
     1209                return wp_get_attachment_image( $at, 'full' );
     1210        }
     1211}
     1212 No newline at end of file
  • src/wp-admin/includes/media.php

    diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php
    index d2cfb5c..d4923fd 100644
    function media_handle_upload($file_id, $post_id, $post_data = array(), $override 
    353353 *
    354354 * @since 2.6.0
    355355 *
    356  * @param array $file_array Array similar to a {@link $_FILES} upload array
     356 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array
    357357 * @param int $post_id The post ID the media is associated with
    358358 * @param string $desc Description of the sideloaded file
    359359 * @param array $post_data allows you to overwrite some of the attachment
    360360 * @return int|object The ID of the attachment or a WP_Error on failure
    361361 */
    362 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
    363         $overrides = array('test_form'=>false);
     362function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array() ) {
     363
     364        $downloading_url = is_string( $file );
     365
     366        if ( $downloading_url ) {
     367
     368                $file = array(
     369                        'name'     => basename( preg_replace( '/\?.*/', '', $file ) ), // fix file filename for query strings
     370                        'tmp_name' => download_url( $file ),
     371                );
     372
     373                // If error storing temporarily, return the error.
     374                if ( is_wp_error( $file['tmp_name'] ) ) {
     375                        return $file['tmp_name'];
     376                }
     377
     378        }
     379
     380        $overrides = array( 'test_form' => false );
    364381
    365382        $time = current_time( 'mysql' );
    366383        if ( $post = get_post( $post_id ) ) {
    function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 
    368385                        $time = $post->post_date;
    369386        }
    370387
    371         $file = wp_handle_sideload( $file_array, $overrides, $time );
    372         if ( isset($file['error']) )
     388        $file = wp_handle_sideload( $file, $overrides, $time );
     389
     390        if ( isset( $file['error'] ) ) {
     391
     392                if ( $downloading_url ) {
     393                        @unlink( $file['tmp_name'] );
     394                }
     395
    373396                return new WP_Error( 'upload_error', $file['error'] );
     397        }
    374398
    375399        $url = $file['url'];
    376400        $type = $file['type'];
    function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 
    404428
    405429        // Save the attachment metadata
    406430        $id = wp_insert_attachment($attachment, $file, $post_id);
    407         if ( !is_wp_error($id) )
     431
     432        if ( ! is_wp_error( $id ) ) {
    408433                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
     434        }
    409435
    410436        return $id;
    411437}
    function wp_media_upload_handler() { 
    796822}
    797823
    798824/**
    799  * Download an image from the specified URL and attach it to a post.
    800  *
    801  * @since 2.6.0
    802  *
    803  * @param string $file The URL of the image to download
    804  * @param int $post_id The post ID the media is to be associated with
    805  * @param string $desc Optional. Description of the image
    806  * @return string|WP_Error Populated HTML img tag on success
    807  */
    808 function media_sideload_image( $file, $post_id, $desc = null ) {
    809         if ( ! empty( $file ) ) {
    810                 // Set variables for storage
    811                 // fix file filename for query strings
    812                 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
    813                 $file_array['name'] = basename( $matches[0] );
    814                 // Download file to temp location
    815                 $file_array['tmp_name'] = download_url( $file );
    816 
    817                 // If error storing temporarily, return the error.
    818                 if ( is_wp_error( $file_array['tmp_name'] ) ) {
    819                         return $file_array['tmp_name'];
    820                 }
    821 
    822                 // do the validation and storage stuff
    823                 $id = media_handle_sideload( $file_array, $post_id, $desc );
    824                 // If error storing permanently, unlink
    825                 if ( is_wp_error( $id ) ) {
    826                         @unlink( $file_array['tmp_name'] );
    827                         return $id;
    828                 }
    829 
    830                 $src = wp_get_attachment_url( $id );
    831         }
    832 
    833         // Finally check to make sure the file has been saved, then return the html
    834         if ( ! empty( $src ) ) {
    835                 $alt = isset( $desc ) ? esc_attr( $desc ) : '';
    836                 $html = "<img src='$src' alt='$alt' />";
    837                 return $html;
    838         }
    839 }
    840 
    841 /**
    842825 * {@internal Missing Short Description}}
    843826 *
    844827 * @since 2.5.0
    function wp_read_audio_metadata( $file ) { 
    29702953        wp_add_id3_tag_data( $metadata, $data );
    29712954
    29722955        return $metadata;
    2973 }
     2956}
     2957 No newline at end of file
  • src/wp-admin/press-this.php

    diff --git src/wp-admin/press-this.php src/wp-admin/press-this.php
    index 1becc48..5dd30b4 100644
    function press_it() { 
    3838        $content = isset($_POST['content']) ? $_POST['content'] : '';
    3939
    4040        $upload = false;
    41         if ( !empty($_POST['photo_src']) && current_user_can('upload_files') ) {
    42                 foreach( (array) $_POST['photo_src'] as $key => $image) {
     41        if ( ! empty( $_POST['photo_src'] ) && current_user_can( 'upload_files' ) ) {
     42                foreach ( (array) $_POST['photo_src'] as $key => $image) {
    4343                        // see if files exist in content - we don't want to upload non-used selected files.
    44                         if ( strpos($_POST['content'], htmlspecialchars($image)) !== false ) {
    45                                 $desc = isset($_POST['photo_description'][$key]) ? $_POST['photo_description'][$key] : '';
    46                                 $upload = media_sideload_image($image, $post_ID, $desc);
     44                        if ( strpos( $_POST['content'], htmlspecialchars( $image ) ) !== false ) {
     45                                $desc          = isset($_POST['photo_description'][$key]) ? $_POST['photo_description'][$key] : '';
     46                                $attachment_id = media_handle_sideload( $image, $post_ID, $desc );
    4747
    4848                                // Replace the POSTED content <img> with correct uploaded ones. Regex contains fix for Magic Quotes
    49                                 if ( !is_wp_error($upload) )
    50                                         $content = preg_replace('/<img ([^>]*)src=\\\?(\"|\')'.preg_quote(htmlspecialchars($image), '/').'\\\?(\2)([^>\/]*)\/*>/is', $upload, $content);
     49                                if ( ! is_wp_error( $attachment_id ) ) {
     50                                        $content = preg_replace(
     51                                                '/<img ([^>]*)src=\\\?(\"|\')' . preg_quote( htmlspecialchars( $image ), '/' ) . '\\\?(\2)([^>\/]*)\/*>/is',
     52                                                wp_get_attachment_image( $attachment_id, 'full' ),
     53                                                $content
     54                                        );
     55                                }
    5156                        }
    5257                }
    5358        }
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 5983cbd..f948c21 100644
    VIDEO; 
    438438                $this->assertTrue( has_image_size( 'test-size' ) );
    439439        }
    440440
     441        /**
     442         * @ticket 19629
     443         */
     444        function test_sideload_media() {
     445
     446                $post_id = wp_insert_post( array(
     447                        'post_title'  => 'test sideload media post',
     448                        'post_status' => 'publish'
     449                ) );
     450
     451                // Test sideloading image.
     452                // Note source image has to be 'external'
     453                $url = 'https://s.w.org/about/images/logos/wordpress-logo-notext-rgb.png';
     454                $id = media_handle_sideload( $url, $post_id );
     455                $this->assertTrue( is_int( $id ) );
     456                wp_delete_attachment( $id, true );
     457
     458                // Test sideloading image by passing file array.
     459                // Note source image has to be 'external'
     460                $url = 'https://s.w.org/about/images/logos/wordpress-logo-notext-rgb.png';
     461                $id = media_handle_sideload( array( 'name' => 'test.png', 'tmp_name' => download_url( $url ) ), $post_id );
     462                $this->assertTrue( is_int( $id ) );
     463                wp_delete_attachment( $id, true );
     464
     465                // Test sideloading 404.
     466                $url = 'http://example.org/test-image-not-found.png';
     467                $id = media_handle_sideload( $url, $post_id );
     468                $this->assertTrue( is_wp_error( $id ) );
     469
     470                // Cleanup.
     471                wp_delete_post( $post_id, true );
     472
     473        }
     474
    441475}