Make WordPress Core


Ignore:
Timestamp:
12/11/2025 01:53:26 AM (6 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.