From 976cdaef880e19b2e3910768f5105d7802340454 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 27 Aug 2024 14:39:03 -0400 Subject: [PATCH] Jetpack: Fix REST API warnings from Comments endpoint There are two bugs involved here: * The Subscriptions block is trying to hide comments by returning empty-string from the 'get_comment' filter, but that filter must return a WP_Comment object. Not even null would be right. * The REST API endpoint isn't checking for if `get_comment()` on the parent comment ID returns null. This fixes the second; I don't know what would be correct to fix the first. --- .../jetpack/changelog/fix-jetpack-comment-endpoint-warnings | 4 ++++ .../json-endpoints/class.wpcom-json-api-comment-endpoint.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/fix-jetpack-comment-endpoint-warnings diff --git a/projects/plugins/jetpack/changelog/fix-jetpack-comment-endpoint-warnings b/projects/plugins/jetpack/changelog/fix-jetpack-comment-endpoint-warnings new file mode 100644 index 0000000000000..cd8d8e645371b --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-jetpack-comment-endpoint-warnings @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +REST API: Avoid PHP warnings in comment endpoint when the parent comment does not exist. diff --git a/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-comment-endpoint.php b/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-comment-endpoint.php index 73666bdceeebc..d7c12196f3287 100644 --- a/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-comment-endpoint.php +++ b/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-comment-endpoint.php @@ -185,8 +185,8 @@ public function get_comment( $comment_id, $context ) { $response[ $key ] = (string) $status; break; case 'parent': // May be object or false. - if ( $comment->comment_parent ) { - $parent = get_comment( $comment->comment_parent ); + $parent = $comment->comment_parent ? get_comment( $comment->comment_parent ) : null; + if ( $parent ) { $response[ $key ] = (object) array( 'ID' => (int) $parent->comment_ID, 'type' => (string) ( $parent->comment_type ? $parent->comment_type : 'comment' ),