WordPress.org

Make WordPress Core

Ticket #19629: 19629.6.diff

File 19629.6.diff, 8.0 KB (added by mattheu, 6 years ago)
  • src/wp-admin/includes/deprecated.php

    diff --git a/src/wp-admin/includes/deprecated.php b/src/wp-admin/includes/deprecated.php
    index f6fa0a2..16cc7b2 100644
    a b 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.1' );
     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}
  • src/wp-admin/includes/media.php

    diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php
    index 0eeeaea..7e5f0e1 100644
    a b function media_handle_upload($file_id, $post_id, $post_data = array(), $override 
    370370 *
    371371 * @since 2.6.0
    372372 *
    373  * @param array $file_array Array similar to a {@link $_FILES} upload array
     373 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array
    374374 * @param int $post_id The post ID the media is associated with
    375375 * @param string $desc Description of the sideloaded file
    376376 * @param array $post_data allows you to overwrite some of the attachment
    377377 * @return int|object The ID of the attachment or a WP_Error on failure
    378378 */
    379 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
    380         $overrides = array('test_form'=>false);
     379function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array()) {
     380
     381        if ( is_string( $file ) ) {
     382
     383                $file = array(
     384                        'name'     => basename( preg_replace( '/\?.*/', '', $file ) ), // fix file filename for query strings
     385                        'tmp_name' => download_url( $file ),
     386                );
     387
     388                // If error storing temporarily, return the error.
     389                if ( is_wp_error( $file['tmp_name'] ) ) {
     390                        return $file['tmp_name'];
     391                }
     392
     393        }
     394
     395        $overrides = array( 'test_form' => false );
    381396
    382397        $time = current_time( 'mysql' );
    383398        if ( $post = get_post( $post_id ) ) {
    function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 
    385400                        $time = $post->post_date;
    386401        }
    387402
    388         $file = wp_handle_sideload( $file_array, $overrides, $time );
    389         if ( isset($file['error']) )
     403        $file = wp_handle_sideload( $file, $overrides, $time );
     404
     405        if ( isset($file['error']) ) {
     406                @unlink( $file['tmp_name'] );
    390407                return new WP_Error( 'upload_error', $file['error'] );
     408        }
    391409
    392410        $url = $file['url'];
    393411        $type = $file['type'];
    function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 
    421439
    422440        // Save the attachment metadata
    423441        $id = wp_insert_attachment($attachment, $file, $post_id);
    424         if ( !is_wp_error($id) )
     442        if ( ! is_wp_error( $id ) ) {
    425443                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
     444        }
    426445
    427446        return $id;
    428447}
    function wp_media_upload_handler() { 
    815834}
    816835
    817836/**
    818  * Download an image from the specified URL and attach it to a post.
    819  *
    820  * @since 2.6.0
    821  *
    822  * @param string $file The URL of the image to download
    823  * @param int $post_id The post ID the media is to be associated with
    824  * @param string $desc Optional. Description of the image
    825  * @return string|WP_Error Populated HTML img tag on success
    826  */
    827 function media_sideload_image( $file, $post_id, $desc = null ) {
    828         if ( ! empty( $file ) ) {
    829                 // Set variables for storage, fix file filename for query strings.
    830                 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
    831                 $file_array = array();
    832                 $file_array['name'] = basename( $matches[0] );
    833 
    834                 // Download file to temp location.
    835                 $file_array['tmp_name'] = download_url( $file );
    836 
    837                 // If error storing temporarily, return the error.
    838                 if ( is_wp_error( $file_array['tmp_name'] ) ) {
    839                         return $file_array['tmp_name'];
    840                 }
    841 
    842                 // Do the validation and storage stuff.
    843                 $id = media_handle_sideload( $file_array, $post_id, $desc );
    844 
    845                 // If error storing permanently, unlink.
    846                 if ( is_wp_error( $id ) ) {
    847                         @unlink( $file_array['tmp_name'] );
    848                         return $id;
    849                 }
    850 
    851                 $src = wp_get_attachment_url( $id );
    852         }
    853 
    854         // Finally check to make sure the file has been saved, then return the HTML.
    855         if ( ! empty( $src ) ) {
    856                 $alt = isset( $desc ) ? esc_attr( $desc ) : '';
    857                 $html = "<img src='$src' alt='$alt' />";
    858                 return $html;
    859         }
    860 }
    861 
    862 /**
    863837 * {@internal Missing Short Description}}
    864838 *
    865839 * @since 2.5.0
  • src/wp-admin/press-this.php

    diff --git a/src/wp-admin/press-this.php b/src/wp-admin/press-this.php
    index 5daa2f9..332edc2 100644
    a b function press_it() { 
    3737        $post['post_title'] = isset($_POST['title']) ? $_POST['title'] : '';
    3838        $content = isset($_POST['content']) ? $_POST['content'] : '';
    3939
    40         $upload = false;
    4140        if ( !empty($_POST['photo_src']) && current_user_can('upload_files') ) {
    4241                foreach( (array) $_POST['photo_src'] as $key => $image) {
    4342                        // See if files exist in content - we don't want to upload non-used selected files.
    4443                        if ( strpos($_POST['content'], htmlspecialchars($image)) !== false ) {
    4544                                $desc = isset($_POST['photo_description'][$key]) ? $_POST['photo_description'][$key] : '';
    46                                 $upload = media_sideload_image($image, $post_ID, $desc);
     45                                $attachment_id = media_handle_sideload( $image, $post_ID, $desc );
    4746
    4847                                // 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);
     48                                if ( ! is_wp_error( $attachment_id ) ) {
     49                                        $content = preg_replace(
     50                                                '/<img ([^>]*)src=\\\?(\"|\')' . preg_quote( htmlspecialchars( $image ), '/') . '\\\?(\2)([^>\/]*)\/*>/is',
     51                                                wp_get_attachment_image( $attachment_id, 'full' ),
     52                                                $content
     53                                        );
     54                                }
    5155                        }
    5256                }
    5357        }
    function press_it() { 
    6165                $post['post_status'] = 'draft';
    6266
    6367        // Error handling for media_sideload.
    64         if ( is_wp_error($upload) ) {
    65                 wp_delete_post($post_ID);
    66                 wp_die($upload);
     68        if ( is_wp_error( $attachment_id ) ) {
     69                wp_delete_post( $post_ID );
     70                wp_die( $attachment_id );
    6771        } else {
    6872                // Post formats.
    6973                if ( isset( $_POST['post_format'] ) ) {
  • tests/phpunit/tests/media.php

    diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
    index cc88693..324105a 100644
    a b 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                // Note source should return 404.
     467                $url = 'https://wordpress.org/image-not-found.png';
     468                $id = media_handle_sideload( $url, $post_id );
     469                $this->assertTrue( is_wp_error( $id ) );
     470
     471                // Cleanup.
     472                wp_delete_post( $post_id, true );
     473
     474        }
     475
    441476}