diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 8fb348c6d387..fcc67eba1944 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1341,8 +1341,6 @@ function wp_ajax_add_meta() { $value = wp_unslash( $_POST['meta'][$mid]['value'] ); 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 if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 015c7d32ec9f..a95ff78bc696 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -814,7 +814,7 @@ function add_meta( $post_ID ) { if ( is_string( $metavalue ) ) $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 f57b27f203e0..3d782e2dba69 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -707,4 +707,21 @@ function test_get_block_editor_server_block_settings() { $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 000000000000..a20deede629c --- /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 ) ); + } +}