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, danielbachhuber.

Merges [43811] to trunk.

Fixes #43559.

git-svn-id: https://develop.svn.wordpress.org/trunk@44153 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2018-12-14 03:16:56 +00:00
parent e34fe23ff0
commit 88de9a1d7c
4 changed files with 89 additions and 4 deletions

View File

@ -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
}

View File

@ -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.

View File

@ -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 ) );
}
}

View File

@ -0,0 +1,71 @@
<?php
/**
* Admin ajax functions to be tested
*/
require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
/**
* Testing Add Meta AJAX functionality.
*
* @group ajax
*/
class Tests_Ajax_AddMeta extends WP_Ajax_UnitTestCase {
/**
* @ticket 43559
*/
public function test_post_add_meta_empty_is_allowed_ajax() {
$p = self::factory()->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 ) );
}
}