WordPress.org

Make WordPress Core

Ticket #19629: 19629.4.diff

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

    diff --git wp-admin/includes/deprecated.php wp-admin/includes/deprecated.php
    index 36b4ca2..9bcd36c 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__, '3.9' );
     1207        return wp_get_attachment_image( media_handle_sideload( $file, $post_id, $desc ), 'full' );
     1208}
  • wp-admin/includes/media.php

    diff --git wp-admin/includes/media.php wp-admin/includes/media.php
    index 06cd1c1..0cf6420 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()) {
     362function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array()) {
     363        $downloading_url = is_string( $file );
     364
     365        if ( $downloading_url ) {
     366                // Download file to temp location
     367                $tmp = download_url( $file );
     368
     369                if ( is_wp_error( $tmp ) )
     370                        return $tmp;
     371
     372                $file_array = array(
     373                        'name' => preg_replace( '/\?.*/', '', $file ), // Strip query string (might need to be better)
     374                        'tmp_name' => $tmp,
     375                );
     376        }
     377
    363378        $overrides = array('test_form'=>false);
    364379
    365380        $time = current_time( 'mysql' );
    function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 
    369384        }
    370385
    371386        $file = wp_handle_sideload( $file_array, $overrides, $time );
    372         if ( isset($file['error']) )
     387        if ( isset( $file['error'] ) ) {
     388                if ( $downloading_url )
     389                        @unlink( $tmp );
    373390                return new WP_Error( 'upload_error', $file['error'] );
     391        }
    374392
    375393        $url = $file['url'];
    376394        $type = $file['type'];
    function wp_media_upload_handler() { 
    791809}
    792810
    793811/**
    794  * Download an image from the specified URL and attach it to a post.
    795  *
    796  * @since 2.6.0
    797  *
    798  * @param string $file The URL of the image to download
    799  * @param int $post_id The post ID the media is to be associated with
    800  * @param string $desc Optional. Description of the image
    801  * @return string|WP_Error Populated HTML img tag on success
    802  */
    803 function media_sideload_image($file, $post_id, $desc = null) {
    804         if ( ! empty($file) ) {
    805                 // Download file to temp location
    806                 $tmp = download_url( $file );
    807 
    808                 // Set variables for storage
    809                 // fix file filename for query strings
    810                 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
    811                 $file_array['name'] = basename($matches[0]);
    812                 $file_array['tmp_name'] = $tmp;
    813 
    814                 // If error storing temporarily, unlink
    815                 if ( is_wp_error( $tmp ) ) {
    816                         @unlink($file_array['tmp_name']);
    817                         $file_array['tmp_name'] = '';
    818                 }
    819 
    820                 // do the validation and storage stuff
    821                 $id = media_handle_sideload( $file_array, $post_id, $desc );
    822                 // If error storing permanently, unlink
    823                 if ( is_wp_error($id) ) {
    824                         @unlink($file_array['tmp_name']);
    825                         return $id;
    826                 }
    827 
    828                 $src = wp_get_attachment_url( $id );
    829         }
    830 
    831         // Finally check to make sure the file has been saved, then return the html
    832         if ( ! empty($src) ) {
    833                 $alt = isset($desc) ? esc_attr($desc) : '';
    834                 $html = "<img src='$src' alt='$alt' />";
    835                 return $html;
    836         }
    837 }
    838 
    839 /**
    840812 * {@internal Missing Short Description}}
    841813 *
    842814 * @since 2.5.0
  • wp-admin/press-this.php

    diff --git wp-admin/press-this.php wp-admin/press-this.php
    index 1becc48..6c34282 100644
    function press_it() { 
    4343                        // see if files exist in content - we don't want to upload non-used selected files.
    4444                        if ( strpos($_POST['content'], htmlspecialchars($image)) !== false ) {
    4545                                $desc = isset($_POST['photo_description'][$key]) ? $_POST['photo_description'][$key] : '';
    46                                 $upload = media_sideload_image($image, $post_ID, $desc);
    47 
     46                                $upload = wp_get_attachment_image( media_handle_sideload( $image, $post_id, $desc ), 'full' );
    4847                                // Replace the POSTED content <img> with correct uploaded ones. Regex contains fix for Magic Quotes
    49                                 if ( !is_wp_error($upload) )
     48                                if ( ! empty($upload) ) {
    5049                                        $content = preg_replace('/<img ([^>]*)src=\\\?(\"|\')'.preg_quote(htmlspecialchars($image), '/').'\\\?(\2)([^>\/]*)\/*>/is', $upload, $content);
     50                                }
     51
    5152                        }
    5253                }
    5354        }