Skip to content

Commit 551fa31

Browse files
committed
Media: Add a $wp_error parameter to wp_insert_attachment() to give it parity with wp_insert_post().
Fixes #37813 Props grapplerulrich, mrahmadawais git-svn-id: https://develop.svn.wordpress.org/trunk@38408 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f59a3b5 commit 551fa31

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

src/wp-includes/post.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,7 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
28662866
* @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty.
28672867
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty.
28682868
* }
2869-
* @param bool $wp_error Optional. Whether to allow return of WP_Error on failure. Default false.
2869+
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
28702870
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
28712871
*/
28722872
function wp_insert_post( $postarr, $wp_error = false ) {
@@ -4694,15 +4694,17 @@ function is_local_attachment($url) {
46944694
* setting the value for the 'comment_status' key.
46954695
*
46964696
* @since 2.0.0
4697+
* @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
46974698
*
46984699
* @see wp_insert_post()
46994700
*
4700-
* @param string|array $args Arguments for inserting an attachment.
4701-
* @param string $file Optional. Filename.
4702-
* @param int $parent Optional. Parent post ID.
4703-
* @return int Attachment ID.
4701+
* @param string|array $args Arguments for inserting an attachment.
4702+
* @param string $file Optional. Filename.
4703+
* @param int $parent Optional. Parent post ID.
4704+
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
4705+
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
47044706
*/
4705-
function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
4707+
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) {
47064708
$defaults = array(
47074709
'file' => $file,
47084710
'post_parent' => 0
@@ -4716,7 +4718,7 @@ function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
47164718

47174719
$data['post_type'] = 'attachment';
47184720

4719-
return wp_insert_post( $data );
4721+
return wp_insert_post( $data, $wp_error );
47204722
}
47214723

47224724
/**

tests/phpunit/tests/media.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,25 @@ function _filter_36246( $data, $attachment_id ) {
17961796
);
17971797
return $data;
17981798
}
1799+
1800+
/**
1801+
* @ticket 37813
1802+
*/
1803+
public function test_return_type_when_inserting_attachment_with_error_in_data() {
1804+
$data = array(
1805+
'post_status' => 'public',
1806+
'post_content' => 'Attachment content',
1807+
'post_title' => 'Attachment Title',
1808+
'post_date' => '2012-02-30 00:00:00',
1809+
);
1810+
1811+
$attachment_id = wp_insert_attachment( $data, '', 0, true );
1812+
$this->assertWPError( $attachment_id );
1813+
$this->assertEquals( 'invalid_date', $attachment_id->get_error_code() );
1814+
1815+
$attachment_id = wp_insert_attachment( $data, '', 0 );
1816+
$this->assertSame( 0, $attachment_id );
1817+
}
17991818
}
18001819

18011820
/**

0 commit comments

Comments
 (0)