Ticket #19629: 19629.6.diff
File 19629.6.diff, 8.0 KB (added by , 10 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() {} 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.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 370 370 * 371 371 * @since 2.6.0 372 372 * 373 * @param array $file_array Array similar to a {@link $_FILES} upload array373 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array 374 374 * @param int $post_id The post ID the media is associated with 375 375 * @param string $desc Description of the sideloaded file 376 376 * @param array $post_data allows you to overwrite some of the attachment 377 377 * @return int|object The ID of the attachment or a WP_Error on failure 378 378 */ 379 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { 380 $overrides = array('test_form'=>false); 379 function 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 ); 381 396 382 397 $time = current_time( 'mysql' ); 383 398 if ( $post = get_post( $post_id ) ) { … … function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 385 400 $time = $post->post_date; 386 401 } 387 402 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'] ); 390 407 return new WP_Error( 'upload_error', $file['error'] ); 408 } 391 409 392 410 $url = $file['url']; 393 411 $type = $file['type']; … … function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = 421 439 422 440 // Save the attachment metadata 423 441 $id = wp_insert_attachment($attachment, $file, $post_id); 424 if ( ! is_wp_error($id) )442 if ( ! is_wp_error( $id ) ) { 425 443 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 444 } 426 445 427 446 return $id; 428 447 } … … function wp_media_upload_handler() { 815 834 } 816 835 817 836 /** 818 * Download an image from the specified URL and attach it to a post.819 *820 * @since 2.6.0821 *822 * @param string $file The URL of the image to download823 * @param int $post_id The post ID the media is to be associated with824 * @param string $desc Optional. Description of the image825 * @return string|WP_Error Populated HTML img tag on success826 */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 /**863 837 * {@internal Missing Short Description}} 864 838 * 865 839 * @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() { 37 37 $post['post_title'] = isset($_POST['title']) ? $_POST['title'] : ''; 38 38 $content = isset($_POST['content']) ? $_POST['content'] : ''; 39 39 40 $upload = false;41 40 if ( !empty($_POST['photo_src']) && current_user_can('upload_files') ) { 42 41 foreach( (array) $_POST['photo_src'] as $key => $image) { 43 42 // See if files exist in content - we don't want to upload non-used selected files. 44 43 if ( strpos($_POST['content'], htmlspecialchars($image)) !== false ) { 45 44 $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 ); 47 46 48 47 // 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 } 51 55 } 52 56 } 53 57 } … … function press_it() { 61 65 $post['post_status'] = 'draft'; 62 66 63 67 // 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 ); 67 71 } else { 68 72 // Post formats. 69 73 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; 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 // 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 441 476 }