From dfbe885115b8c1f6dbbbf3e0bd19f9ddc7321895 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 23 Oct 2018 18:55:45 +0000 Subject: [PATCH] Meta: Allow empty strings to be set by Custom Fields meta box. Because the REST API allows meta keys to have empty values, the Custom Fields meta box should permit the same behavior. Props charlestonsw, soulseekah. Fixes #43559. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43811 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 2 - src/wp-admin/includes/post.php | 2 +- tests/phpunit/tests/admin/includesPost.php | 17 ++++++ tests/phpunit/tests/ajax/AddMeta.php | 71 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/tests/ajax/AddMeta.php 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 ) ); + } +}