Make WordPress Core

Changeset 44153


Ignore:
Timestamp:
12/14/2018 03:16:56 AM (6 years ago)
Author:
desrosj
Message:

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.

Location:
trunk
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/includes/ajax-actions.php

    r43598 r44153  
    14441444            wp_die( __( 'Please provide a custom field name.' ) );
    14451445        }
    1446         if ( '' == trim( $value ) ) {
    1447             wp_die( __( 'Please provide a custom field value.' ) );
    1448         }
    14491446        if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) {
    14501447            wp_die( 0 ); // if meta doesn't exist
  • trunk/src/wp-admin/includes/post.php

    r44141 r44153  
    868868    }
    869869
    870     if ( ( '0' === $metavalue || ! empty( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) ) {
     870    if ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) {
    871871        /*
    872872         * We have a key/value pair. If both the select and the input
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r44122 r44153  
    800800        $this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] );
    801801    }
     802
     803    /**
     804     * @ticket 43559
     805     */
     806    public function test_post_add_meta_empty_is_allowed() {
     807        $p = self::factory()->post->create();
     808
     809        $_POST = array(
     810            'metakeyinput' => 'testkey',
     811            'metavalue'    => '',
     812        );
     813
     814        wp_set_current_user( self::$admin_id );
     815
     816        $this->assertNotFalse( add_meta( $p ) );
     817        $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) );
     818    }
    802819}
  • trunk/tests/phpunit/tests/ajax/AddMeta.php

    r43811 r44153  
    2222
    2323        $_POST = array(
    24             'post_id'      => $p,
    25             'metakeyinput' => 'testkey',
    26             'metavalue'    => '',
     24            'post_id'              => $p,
     25            'metakeyinput'         => 'testkey',
     26            'metavalue'            => '',
    2727            '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ),
    2828        );
     
    5151        $_POST = array(
    5252            '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ),
    53             'post_id' => $p,
    54             'meta' => array(
     53            'post_id'              => $p,
     54            'meta'                 => array(
    5555                $m => array(
    5656                    'key'   => 'testkey',
Note: See TracChangeset for help on using the changeset viewer.