From ef9ac59043bfdd08a09cdbdea4aed4ce372d7b8a Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Sun, 23 Feb 2025 20:54:11 +0530 Subject: [PATCH 01/11] Tests: Add unit test for empty comment tags in export --- src/wp-admin/includes/export.php | 2 +- tests/phpunit/tests/admin/exportWp.php | 51 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index 9d5903a98c5c3..592fe342f135b 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -685,7 +685,7 @@ function wxr_filter_postmeta( $return_me, $meta_key ) { endforeach; $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) ); - $comments = array_map( 'get_comment', $_comments ); + $comments = array_filter( array_map( 'get_comment', $_comments ) ); foreach ( $comments as $c ) : ?> diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index ebb60018c0e7a..a9a13bf684119 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -290,4 +290,55 @@ private function populate_args_post_authors( array &$args, $expected_ids ) { $post_ids_key = $expected_ids[0]; $args['author'] = self::$post_ids[ $post_ids_key ]['post_author']; } + + /** + * @ticket 61244 + * @covers ::export_wp + */ + public function test_export_wp_should_not_include_empty_comments_when_filtered() { + $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); + self::factory()->comment->create_post_comments( $post_id, 3 ); + + // Add filter to make get_comment return false. + add_action( + 'export_wp', + function () { + add_filter( 'get_comment', '__return_false' ); + } + ); + + ob_start(); + export_wp(); + $xml = ob_get_clean(); + + $xml_obj = simplexml_load_string( $xml ); + + // Convert all tags to string for easier checking. + $comment_tags = $xml_obj->xpath( '//wp:comment' ); + + // Verify there are no comment tags in the export. + $this->assertEmpty( $comment_tags, 'No tags should be present when comments are filtered out.' ); + } + + /** + * @ticket 61244 + * @covers ::export_wp + */ + public function test_export_wp_includes_comments_when_not_filtered() { + $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); + $comment_count = 3; + self::factory()->comment->create_post_comments( $post_id, $comment_count ); + + ob_start(); + export_wp(); + $xml = ob_get_clean(); + + $xml_obj = simplexml_load_string( $xml ); + + // Count tags. + $comment_tags = $xml_obj->xpath( '//wp:comment' ); + + // Verify the correct number of comment tags are present. + $this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' ); + } } From aae934818e94ce9f629fa66f03882c00f3d87745 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 3 Jul 2025 13:45:14 +0530 Subject: [PATCH 02/11] Tests: Use `get_the_export()` method instead of output buffering --- tests/phpunit/tests/admin/exportWp.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index a9a13bf684119..3be2533747d19 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -307,16 +307,10 @@ function () { } ); - ob_start(); - export_wp(); - $xml = ob_get_clean(); - - $xml_obj = simplexml_load_string( $xml ); + $xml_obj = $this->get_the_export( array() ); - // Convert all tags to string for easier checking. $comment_tags = $xml_obj->xpath( '//wp:comment' ); - // Verify there are no comment tags in the export. $this->assertEmpty( $comment_tags, 'No tags should be present when comments are filtered out.' ); } @@ -329,16 +323,10 @@ public function test_export_wp_includes_comments_when_not_filtered() { $comment_count = 3; self::factory()->comment->create_post_comments( $post_id, $comment_count ); - ob_start(); - export_wp(); - $xml = ob_get_clean(); - - $xml_obj = simplexml_load_string( $xml ); + $xml_obj = $this->get_the_export( array() ); - // Count tags. $comment_tags = $xml_obj->xpath( '//wp:comment' ); - // Verify the correct number of comment tags are present. $this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' ); } } From 6e82432793a97c633be6d7b1dad55802ecdc13b8 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Thu, 3 Jul 2025 14:07:12 +0530 Subject: [PATCH 03/11] Tests: Make `get_comment` filter return `null` --- tests/phpunit/tests/admin/exportWp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index 3be2533747d19..fe94dc9a455e7 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -299,11 +299,11 @@ public function test_export_wp_should_not_include_empty_comments_when_filtered() $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); self::factory()->comment->create_post_comments( $post_id, 3 ); - // Add filter to make get_comment return false. + // Add filter to make get_comment return null. add_action( 'export_wp', function () { - add_filter( 'get_comment', '__return_false' ); + add_filter( 'get_comment', '__return_null' ); } ); From b330e888276ede931f3859b1838c0cac59939243 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Thu, 20 Nov 2025 12:20:54 +0530 Subject: [PATCH 04/11] Fix formatting of comments array in export function --- src/wp-admin/includes/export.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index 592fe342f135b..43898f18f865a 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -685,7 +685,7 @@ function wxr_filter_postmeta( $return_me, $meta_key ) { endforeach; $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) ); - $comments = array_filter( array_map( 'get_comment', $_comments ) ); + $comments = array_filter( array_map( 'get_comment', $_comments ) ); foreach ( $comments as $c ) : ?> From f2b6da4a0acd2024f87e0641554024b57c0786e6 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Thu, 20 Nov 2025 12:22:35 +0530 Subject: [PATCH 05/11] Tests: Change export_wp filter callback to static function --- tests/phpunit/tests/admin/exportWp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index fe94dc9a455e7..47f3514806f88 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -302,7 +302,7 @@ public function test_export_wp_should_not_include_empty_comments_when_filtered() // Add filter to make get_comment return null. add_action( 'export_wp', - function () { + static function () { add_filter( 'get_comment', '__return_null' ); } ); From 834aad6d2a839183e10c587df614df3bc9714bdc Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Fri, 28 Nov 2025 19:16:08 +0530 Subject: [PATCH 06/11] Tests: Remove redundant @covers annotation from export_wp tests and clean up variable assignments --- tests/phpunit/tests/admin/exportWp.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index 47f3514806f88..56afe52bacad5 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -293,7 +293,6 @@ private function populate_args_post_authors( array &$args, $expected_ids ) { /** * @ticket 61244 - * @covers ::export_wp */ public function test_export_wp_should_not_include_empty_comments_when_filtered() { $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); @@ -307,24 +306,20 @@ static function () { } ); - $xml_obj = $this->get_the_export( array() ); - + $xml_obj = $this->get_the_export( array() ); $comment_tags = $xml_obj->xpath( '//wp:comment' ); - $this->assertEmpty( $comment_tags, 'No tags should be present when comments are filtered out.' ); } /** * @ticket 61244 - * @covers ::export_wp */ public function test_export_wp_includes_comments_when_not_filtered() { $post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) ); $comment_count = 3; self::factory()->comment->create_post_comments( $post_id, $comment_count ); - $xml_obj = $this->get_the_export( array() ); - + $xml_obj = $this->get_the_export( array() ); $comment_tags = $xml_obj->xpath( '//wp:comment' ); $this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' ); From 9560368f88867d629a4dc11a14ad2d83250b8121 Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Sat, 6 Dec 2025 20:50:51 +0530 Subject: [PATCH 07/11] Refactor comment filtering in export_wp function to ensure only valid WP_Comment instances are included --- src/wp-admin/includes/export.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/export.php b/src/wp-admin/includes/export.php index 43898f18f865a..4fe697e7e6d4d 100644 --- a/src/wp-admin/includes/export.php +++ b/src/wp-admin/includes/export.php @@ -685,7 +685,12 @@ function wxr_filter_postmeta( $return_me, $meta_key ) { endforeach; $_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) ); - $comments = array_filter( array_map( 'get_comment', $_comments ) ); + $comments = array_filter( + array_map( 'get_comment', $_comments ), + static function ( $comment ) { + return $comment instanceof WP_Comment; + } + ); foreach ( $comments as $c ) : ?> From 141982e3caced66b592e4418e4e28560657c45cc Mon Sep 17 00:00:00 2001 From: Himanshu Pathak Date: Sat, 6 Dec 2025 20:51:27 +0530 Subject: [PATCH 08/11] Fix get_comment function to return null for invalid WP_Comment instances --- src/wp-includes/comment.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 7ce49765237a7..bb4abf2cd7f7f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -240,9 +240,12 @@ function get_comment( $comment = null, $output = OBJECT ) { * * @since 2.3.0 * - * @param WP_Comment $_comment Comment data. + * @param WP_Comment|null $_comment Comment data. */ $_comment = apply_filters( 'get_comment', $_comment ); + if ( ! ( $_comment instanceof WP_Comment ) ) { + return null; + } if ( OBJECT === $output ) { return $_comment; From 7623f271eedab7f6f8395958a97a7336a19db2fa Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 10 Dec 2025 17:08:26 -0800 Subject: [PATCH 09/11] Use assertCount instead of assertEmpty --- tests/phpunit/tests/admin/exportWp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index 56afe52bacad5..9b9f16fb976bb 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -308,7 +308,7 @@ static function () { $xml_obj = $this->get_the_export( array() ); $comment_tags = $xml_obj->xpath( '//wp:comment' ); - $this->assertEmpty( $comment_tags, 'No tags should be present when comments are filtered out.' ); + $this->assertCount( 0, $comment_tags, 'No tags should be present when comments are filtered out.' ); } /** From 2bb1e6515841e875e5361c7e70f2aed5a95b1ea7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 10 Dec 2025 17:09:50 -0800 Subject: [PATCH 10/11] Remove extra space --- tests/phpunit/tests/admin/exportWp.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/admin/exportWp.php b/tests/phpunit/tests/admin/exportWp.php index 9b9f16fb976bb..059dcab1a8351 100644 --- a/tests/phpunit/tests/admin/exportWp.php +++ b/tests/phpunit/tests/admin/exportWp.php @@ -321,7 +321,6 @@ public function test_export_wp_includes_comments_when_not_filtered() { $xml_obj = $this->get_the_export( array() ); $comment_tags = $xml_obj->xpath( '//wp:comment' ); - $this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' ); } } From 11abe93cd112d0e0310bda5df28ccc47845373cc Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 10 Dec 2025 17:50:27 -0800 Subject: [PATCH 11/11] Add dedicated test for get_comment --- tests/phpunit/tests/comment.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 592ad317003ee..11e78140f1020 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -1896,4 +1896,20 @@ public function test_wp_trash_comment_only_top_level_notes_trigger_child_deletio // Verify the sibling note is NOT trashed (no cascade since child is not top-level). $this->assertSame( '1', get_comment( $sibling_note )->comment_approved ); } + + /** + * @ticket 61244 + * + * @covers ::get_comment + */ + public function test_get_comment_filter() { + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) ); + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( $comment_id, (int) $comment->comment_ID, 'Expected the same comment.' ); + + add_filter( 'get_comment', '__return_null' ); + $this->assertNull( get_comment( $comment_id ), 'Expected get_comment() to return null when get_comment filter returns null.' ); + } }