Make WordPress Core

Changeset 61369


Ignore:
Timestamp:
12/11/2025 01:53:26 AM (3 days ago)
Author:
westonruter
Message:

Export: Update export_wp() to handle get_comment() returning null due to filter.

The get_comment filter now explicitly documents returning null in addition to a WP_Comment object. This allows the filter to be used to exclude comments from an export. The get_comment() function already supported returning null.

Developed in https://github.com/WordPress/wordpress-develop/pull/8383

Props abcd95, WPExplorer, desrosj, mukesh27, westonruter, SirLouen, lbones, mdibrahimk48, audrasjb, jorbin, wildworks, hellofromTonya, saurabh.dhariwal, mabfahad.
Fixes #61244.

Location:
trunk
Files:
4 edited

Legend:

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

    r60632 r61369  
    686686
    687687                $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
    688                 $comments  = array_map( 'get_comment', $_comments );
     688                $comments  = array_filter(
     689                    array_map( 'get_comment', $_comments ),
     690                    static function ( $comment ) {
     691                        return $comment instanceof WP_Comment;
     692                    }
     693                );
    689694                foreach ( $comments as $c ) :
    690695                    ?>
  • trunk/src/wp-includes/comment.php

    r61336 r61369  
    241241     * @since 2.3.0
    242242     *
    243      * @param WP_Comment $_comment Comment data.
     243     * @param WP_Comment|null $_comment Comment data.
    244244     */
    245245    $_comment = apply_filters( 'get_comment', $_comment );
     246    if ( ! ( $_comment instanceof WP_Comment ) ) {
     247        return null;
     248    }
    246249
    247250    if ( OBJECT === $output ) {
  • trunk/tests/phpunit/tests/admin/exportWp.php

    r57681 r61369  
    291291        $args['author'] = self::$post_ids[ $post_ids_key ]['post_author'];
    292292    }
     293
     294    /**
     295     * @ticket 61244
     296     */
     297    public function test_export_wp_should_not_include_empty_comments_when_filtered() {
     298        $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
     299        self::factory()->comment->create_post_comments( $post_id, 3 );
     300
     301        // Add filter to make get_comment return null.
     302        add_action(
     303            'export_wp',
     304            static function () {
     305                add_filter( 'get_comment', '__return_null' );
     306            }
     307        );
     308
     309        $xml_obj      = $this->get_the_export( array() );
     310        $comment_tags = $xml_obj->xpath( '//wp:comment' );
     311        $this->assertCount( 0, $comment_tags, 'No <wp:comment> tags should be present when comments are filtered out.' );
     312    }
     313
     314    /**
     315     * @ticket 61244
     316     */
     317    public function test_export_wp_includes_comments_when_not_filtered() {
     318        $post_id       = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
     319        $comment_count = 3;
     320        self::factory()->comment->create_post_comments( $post_id, $comment_count );
     321
     322        $xml_obj      = $this->get_the_export( array() );
     323        $comment_tags = $xml_obj->xpath( '//wp:comment' );
     324        $this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' );
     325    }
    293326}
  • trunk/tests/phpunit/tests/comment.php

    r61248 r61369  
    18971897        $this->assertSame( '1', get_comment( $sibling_note )->comment_approved );
    18981898    }
     1899
     1900    /**
     1901     * @ticket 61244
     1902     *
     1903     * @covers ::get_comment
     1904     */
     1905    public function test_get_comment_filter() {
     1906        $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
     1907
     1908        $comment = get_comment( $comment_id );
     1909        $this->assertInstanceOf( WP_Comment::class, $comment );
     1910        $this->assertSame( $comment_id, (int) $comment->comment_ID, 'Expected the same comment.' );
     1911
     1912        add_filter( 'get_comment', '__return_null' );
     1913        $this->assertNull( get_comment( $comment_id ), 'Expected get_comment() to return null when get_comment filter returns null.' );
     1914    }
    18991915}
Note: See TracChangeset for help on using the changeset viewer.