Skip to content

Commit

Permalink
Meta: Allow empty strings to be set by Custom Fields meta box.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danielbachhuber committed Oct 23, 2018
1 parent 8c0b678 commit dfbe885
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/wp-admin/includes/ajax-actions.php
Expand Up @@ -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' ) ||
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/post.php
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Expand Up @@ -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 ) );
}
}
71 changes: 71 additions & 0 deletions tests/phpunit/tests/ajax/AddMeta.php
@@ -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 ) );
}
}

0 comments on commit dfbe885

Please sign in to comment.