Make WordPress Core

Changeset 48235


Ignore:
Timestamp:
06/30/2020 02:11:00 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Comments: Make wp_update_comment() return false instead of 0 for an invalid comment or post ID.

This addresses an inconsistency where 0 could mean one of the three scenarios:

  • Invalid comment ID.
  • Invalid comment post ID.
  • No DB rows updated. This is not an error and should not be treated as one.

With this change, wp_update_comment() always returns either false or a WP_Error object on failure, depending on the value of the $wp_error parameter.

Follow-up to [48154], [48215], [48216], [48218], [48230].

Props dd32, jnylen0, enrico.sorcinelli.
Fixes #39732. See #38700, #39735.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r48230 r48235  
    24232423 * @since 4.9.0 Add updating comment meta during comment update.
    24242424 * @since 5.5.0 The `$wp_error` parameter was added.
     2425 * @since 5.5.0 The return values for an invalid comment or post ID
     2426 *              were changed to false instead of 0.
    24252427 *
    24262428 * @global wpdb $wpdb WordPress database abstraction object.
     
    24402442            return new WP_Error( 'invalid_comment_id', __( 'Invalid comment ID.' ) );
    24412443        } else {
    2442             return 0;
     2444            return false;
    24432445        }
    24442446    }
     
    24492451            return new WP_Error( 'invalid_post_id', __( 'Invalid post ID.' ) );
    24502452        } else {
    2451             return 0;
     2453            return false;
    24522454        }
    24532455    }
  • trunk/tests/phpunit/tests/comment.php

    r48231 r48235  
    3232
    3333    public function test_wp_update_comment() {
    34         $post     = self::factory()->post->create_and_get(
     34        $post  = self::factory()->post->create_and_get(
    3535            array(
    3636                'post_title' => 'some-post',
     
    3838            )
    3939        );
    40         $post2    = self::factory()->post->create_and_get(
     40        $post2 = self::factory()->post->create_and_get(
    4141            array(
    4242                'post_title' => 'some-post-2',
     
    4444            )
    4545        );
     46
    4647        $comments = self::factory()->comment->create_post_comments( $post->ID, 5 );
    47         $result   = wp_update_comment(
     48
     49        $result = wp_update_comment(
    4850            array(
    4951                'comment_ID'     => $comments[0],
     
    5254        );
    5355        $this->assertEquals( 1, $result );
     56
    5457        $comment = get_comment( $comments[0] );
    5558        $this->assertEquals( $comments[1], $comment->comment_parent );
     59
    5660        $result = wp_update_comment(
    5761            array(
     
    6165        );
    6266        $this->assertEquals( 0, $result );
    63         $result  = wp_update_comment(
     67
     68        $result = wp_update_comment(
    6469            array(
    6570                'comment_ID'      => $comments[0],
     
    6772            )
    6873        );
     74
    6975        $comment = get_comment( $comments[0] );
    7076        $this->assertEquals( $post2->ID, $comment->comment_post_ID );
     
    9399    public function test_wp_update_comment_updates_comment_meta() {
    94100        $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     101
    95102        wp_update_comment(
    96103            array(
     
    102109            )
    103110        );
     111
    104112        $this->assertEquals( 'fire', get_comment_meta( $comment_id, 'sauce', true ) );
    105113    }
     
    141149        $comment = get_comment( $comment_id );
    142150        $this->assertEquals( $updated_comment_text, $comment->comment_content );
     151    }
     152
     153    /**
     154     * @ticket 39732
     155     */
     156    public function test_wp_update_comment_returns_false_for_invalid_comment_or_post_id() {
     157        $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     158
     159        $update = wp_update_comment(
     160            array(
     161                'comment_ID'      => -1,
     162                'comment_post_ID' => self::$post_id,
     163            )
     164        );
     165        $this->assertSame( false, $update );
     166
     167        $update = wp_update_comment(
     168            array(
     169                'comment_ID'      => $comment_id,
     170                'comment_post_ID' => -1,
     171            )
     172        );
     173        $this->assertSame( false, $update );
    143174    }
    144175
Note: See TracChangeset for help on using the changeset viewer.