diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index db22db2924..f11b243e1a 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1443,9 +1443,6 @@ function wp_ajax_add_meta() { if ( '' == trim( $key ) ) { wp_die( __( 'Please provide a custom field name.' ) ); } - if ( '' == trim( $value ) ) { - wp_die( __( 'Please provide a custom field value.' ) ); - } if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) { wp_die( 0 ); // if meta doesn't exist } diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 2595f69f7c..adc17ba91f 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -867,7 +867,7 @@ function add_meta( $post_ID ) { $metavalue = trim( $metavalue ); } - if ( ( '0' === $metavalue || ! empty( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) ) { + if ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { /* * We have a key/value pair. If both the select and the input * for the key have data, the input takes precedence. diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 289f828e4c..ee3edb8aa0 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -799,4 +799,21 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { $this->assertArrayHasKey( $name, $blocks ); $this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] ); } + + /** + * @ticket 43559 + */ + public function test_post_add_meta_empty_is_allowed() { + $p = self::factory()->post->create(); + + $_POST = array( + 'metakeyinput' => 'testkey', + 'metavalue' => '', + ); + + wp_set_current_user( self::$admin_id ); + + $this->assertNotFalse( add_meta( $p ) ); + $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); + } } diff --git a/tests/phpunit/tests/ajax/AddMeta.php b/tests/phpunit/tests/ajax/AddMeta.php new file mode 100644 index 0000000000..6ca4f3a0f2 --- /dev/null +++ b/tests/phpunit/tests/ajax/AddMeta.php @@ -0,0 +1,71 @@ +post->create(); + + // Become an administrator. + $this->_setRole( 'administrator' ); + + $_POST = array( + 'post_id' => $p, + 'metakeyinput' => 'testkey', + 'metavalue' => '', + '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ), + ); + + // Make the request. + try { + $this->_handleAjax( 'add-meta' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); + } + + /** + * @ticket 43559 + */ + public function test_post_update_meta_empty_is_allowed_ajax() { + $p = self::factory()->post->create(); + + $m = add_post_meta( $p, 'testkey', 'hello' ); + + // Become an administrator. + $this->_setRole( 'administrator' ); + + $_POST = array( + '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ), + 'post_id' => $p, + 'meta' => array( + $m => array( + 'key' => 'testkey', + 'value' => '', + ), + ), + ); + + // Make the request. + try { + $this->_handleAjax( 'add-meta' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); + } +}