Ticket #19629: 19629-enhancement.diff
File 19629-enhancement.diff, 3.5 KB (added by , 9 years ago) |
---|
-
src/wp-admin/includes/media.php
374 374 * 375 375 * @since 2.6.0 376 376 * 377 * @param array $file_array Array similar to a {@link $_FILES} upload array377 * @param string|array $file A URL as a string, or an array similar to a {@link $_FILES} upload array 378 378 * @param int $post_id The post ID the media is associated with 379 379 * @param string $desc Description of the sideloaded file 380 380 * @param array $post_data allows you to overwrite some of the attachment 381 381 * @return int|object The ID of the attachment or a WP_Error on failure 382 382 */ 383 function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) { 384 $overrides = array('test_form'=>false); 383 function media_handle_sideload( $file, $post_id, $desc = null, $post_data = array()) { 385 384 385 if ( is_string( $file ) ) { 386 387 $file = array( 388 'name' => basename( preg_replace( '/\?.*/', '', $file ) ), // fix file filename for query strings 389 'tmp_name' => download_url( $file ), 390 ); 391 392 // If error storing temporarily, return the error. 393 if ( is_wp_error( $file['tmp_name'] ) ) { 394 return $file['tmp_name']; 395 } 396 397 } 398 399 $overrides = array( 'test_form' => false ); 400 386 401 $time = current_time( 'mysql' ); 387 402 if ( $post = get_post( $post_id ) ) { 388 403 if ( substr( $post->post_date, 0, 4 ) > 0 ) … … 389 404 $time = $post->post_date; 390 405 } 391 406 392 $file = wp_handle_sideload( $file_array, $overrides, $time ); 393 if ( isset($file['error']) ) 407 $file = wp_handle_sideload( $file, $overrides, $time ); 408 409 if ( isset($file['error']) ) { 410 @unlink( $file['tmp_name'] ); 394 411 return new WP_Error( 'upload_error', $file['error'] ); 412 } 395 413 396 414 $url = $file['url']; 397 415 $type = $file['type']; … … 424 442 425 443 // Save the attachment metadata 426 444 $id = wp_insert_attachment($attachment, $file, $post_id); 427 if ( ! is_wp_error($id) )445 if ( ! is_wp_error( $id ) ) { 428 446 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 447 } 429 448 430 449 return $id; 431 450 } -
tests/phpunit/tests/media.php
572 572 $this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt ); 573 573 } 574 574 575 /** 576 * @ticket 19629 577 */ 578 function test_sideload_media() { 579 580 $post_id = wp_insert_post( array( 581 'post_title' => 'test sideload media post', 582 'post_status' => 'publish' 583 ) ); 584 585 // Test sideloading image. 586 // Note source image has to be 'external' 587 $url = 'https://s.w.org/about/images/logos/wordpress-logo-notext-rgb.png'; 588 $id = media_handle_sideload( $url, $post_id ); 589 $this->assertTrue( is_int( $id ) ); 590 wp_delete_attachment( $id, true ); 591 592 // Test sideloading image by passing file array. 593 // Note source image has to be 'external' 594 $url = 'https://s.w.org/about/images/logos/wordpress-logo-notext-rgb.png'; 595 $id = media_handle_sideload( array( 'name' => 'test.png', 'tmp_name' => download_url( $url ) ), $post_id ); 596 $this->assertTrue( is_int( $id ) ); 597 wp_delete_attachment( $id, true ); 598 599 // Test sideloading 404. 600 // Note source should return 404. 601 $url = 'https://wordpress.org/image-not-found.png'; 602 $id = media_handle_sideload( $url, $post_id ); 603 $this->assertTrue( is_wp_error( $id ) ); 604 605 // Cleanup. 606 wp_delete_post( $post_id, true ); 607 608 } 609 575 610 }