From 85ca37aa1d991a4f5be68fe35de96d9c9110bd96 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:32:29 +0800 Subject: [PATCH] fix code smells --- includes/functions.php | 15 ++++++++++----- includes/rest/class-inbox.php | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index caae289..8872e84 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -320,11 +320,16 @@ function url_to_authorid( $url ) { * @return WP_Comment, or undef if no comment could be found. */ function object_id_to_comment( $id ) { - $comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $id ) ); - if ( !$comment_query->comments ) { + $comment_query = new \WP_Comment_Query( + array( + 'meta_key' => 'source_id', + 'meta_value' => $id, + ) + ); + if ( ! $comment_query->comments ) { return; } - if ( count( $comment_query->comments ) > 1 ){ + if ( count( $comment_query->comments ) > 1 ) { \error_log( "More than one comment under {$id}" ); return; } @@ -339,10 +344,10 @@ function object_id_to_comment( $id ) { * @return int Post ID, or null on failure. */ function object_to_post_id_by_field_name( $object, $field_name ) { - if ( ! isset( $object['object'][$field_name] ) ) { + if ( ! isset( $object['object'][ $field_name ] ) ) { return; } - $result = \url_to_postid( $object['object'][$field_name] ); + $result = \url_to_postid( $object['object'][ $field_name ] ); if ( $result > 0 ) { return $result; } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 8dc8ae4..5c3cd17 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -432,10 +432,10 @@ class Inbox { * @return array Comment data suitable for creating a comment. */ public static function convert_object_to_comment_data( $object ) { - if ( ! isset( $object['object'] ) ) { + if ( ! isset( $object['object'] ) ) { return false; - } - + } + // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email @@ -446,7 +446,7 @@ class Inbox { // Objects must have IDs if ( ! isset( $object['object']['id'] ) ) { - \error_log( "Comment provided without ID" ); + \error_log( 'Comment provided without ID' ); return; } $id = $object['object']['id']; @@ -465,9 +465,13 @@ class Inbox { $parent_comment = \Activitypub\object_id_to_comment( $in_reply_to ); // save only replies and reactions - $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ) ?? - \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ) ?? - ( $parent_comment ? $parent_comment->comment_post_ID : 0 ); + $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ); + if ( ! $comment_post_id ) { + $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ); + } + if ( ! $comment_post_id ) { + $comment_post_id = $parent_comment->comment_post_ID; + } if ( ! $comment_post_id ) { return; } @@ -497,7 +501,7 @@ class Inbox { */ public static function handle_create( $object, $user_id ) { $commentdata = self::convert_object_to_comment_data( $object ); - if ( !$commentdata ) { + if ( ! $commentdata ) { return false; }