Ticket #19629: 19629.5.diff
File 19629.5.diff, 8.1 KB (added by , 10 years ago) |
---|
-
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() {} 1188 1188 function _relocate_children( $old_ID, $new_ID ) { 1189 1189 _deprecated_function( __FUNCTION__, '3.9' ); 1190 1190 } 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 */ 1205 function 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 353 353 * 354 354 * @since 2.6.0 355 355 * 356 * @param array $file_array Array similar to a {@link $_FILES} upload array356 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array 357 357 * @param int $post_id The post ID the media is associated with 358 358 * @param string $desc Description of the sideloaded file 359 359 * @param array $post_data allows you to overwrite some of the attachment 360 360 * @return int|object The ID of the attachment or a WP_Error on failure 361 361 */ 362 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { 363 $overrides = array('test_form'=>false); 362 function 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 ); 364 381 365 382 $time = current_time( 'mysql' ); 366 383 if ( $post = get_post( $post_id ) ) { … … function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 368 385 $time = $post->post_date; 369 386 } 370 387 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 373 396 return new WP_Error( 'upload_error', $file['error'] ); 397 } 374 398 375 399 $url = $file['url']; 376 400 $type = $file['type']; … … function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 404 428 405 429 // Save the attachment metadata 406 430 $id = wp_insert_attachment($attachment, $file, $post_id); 407 if ( !is_wp_error($id) ) 431 432 if ( ! is_wp_error( $id ) ) { 408 433 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 434 } 409 435 410 436 return $id; 411 437 } … … function wp_media_upload_handler() { 796 822 } 797 823 798 824 /** 799 * Download an image from the specified URL and attach it to a post.800 *801 * @since 2.6.0802 *803 * @param string $file The URL of the image to download804 * @param int $post_id The post ID the media is to be associated with805 * @param string $desc Optional. Description of the image806 * @return string|WP_Error Populated HTML img tag on success807 */808 function media_sideload_image( $file, $post_id, $desc = null ) {809 if ( ! empty( $file ) ) {810 // Set variables for storage811 // fix file filename for query strings812 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );813 $file_array['name'] = basename( $matches[0] );814 // Download file to temp location815 $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 stuff823 $id = media_handle_sideload( $file_array, $post_id, $desc );824 // If error storing permanently, unlink825 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 html834 if ( ! empty( $src ) ) {835 $alt = isset( $desc ) ? esc_attr( $desc ) : '';836 $html = "<img src='$src' alt='$alt' />";837 return $html;838 }839 }840 841 /**842 825 * {@internal Missing Short Description}} 843 826 * 844 827 * @since 2.5.0 … … function wp_read_audio_metadata( $file ) { 2970 2953 wp_add_id3_tag_data( $metadata, $data ); 2971 2954 2972 2955 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() { 38 38 $content = isset($_POST['content']) ? $_POST['content'] : ''; 39 39 40 40 $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) { 43 43 // 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 ); 47 47 48 48 // 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 } 51 56 } 52 57 } 53 58 } -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 5983cbd..f948c21 100644
VIDEO; 438 438 $this->assertTrue( has_image_size( 'test-size' ) ); 439 439 } 440 440 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 441 475 }