fix code smells

This commit is contained in:
Matthew Exon 2023-01-24 13:32:29 +08:00
parent 7fa58cf26c
commit 85ca37aa1d
2 changed files with 22 additions and 13 deletions

View file

@ -320,11 +320,16 @@ function url_to_authorid( $url ) {
* @return WP_Comment, or undef if no comment could be found. * @return WP_Comment, or undef if no comment could be found.
*/ */
function object_id_to_comment( $id ) { function object_id_to_comment( $id ) {
$comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $id ) ); $comment_query = new \WP_Comment_Query(
if ( !$comment_query->comments ) { array(
'meta_key' => 'source_id',
'meta_value' => $id,
)
);
if ( ! $comment_query->comments ) {
return; return;
} }
if ( count( $comment_query->comments ) > 1 ){ if ( count( $comment_query->comments ) > 1 ) {
\error_log( "More than one comment under {$id}" ); \error_log( "More than one comment under {$id}" );
return; return;
} }
@ -339,10 +344,10 @@ function object_id_to_comment( $id ) {
* @return int Post ID, or null on failure. * @return int Post ID, or null on failure.
*/ */
function object_to_post_id_by_field_name( $object, $field_name ) { function object_to_post_id_by_field_name( $object, $field_name ) {
if ( ! isset( $object['object'][$field_name] ) ) { if ( ! isset( $object['object'][ $field_name ] ) ) {
return; return;
} }
$result = \url_to_postid( $object['object'][$field_name] ); $result = \url_to_postid( $object['object'][ $field_name ] );
if ( $result > 0 ) { if ( $result > 0 ) {
return $result; return $result;
} }

View file

@ -446,7 +446,7 @@ class Inbox {
// Objects must have IDs // Objects must have IDs
if ( ! isset( $object['object']['id'] ) ) { if ( ! isset( $object['object']['id'] ) ) {
\error_log( "Comment provided without ID" ); \error_log( 'Comment provided without ID' );
return; return;
} }
$id = $object['object']['id']; $id = $object['object']['id'];
@ -465,9 +465,13 @@ class Inbox {
$parent_comment = \Activitypub\object_id_to_comment( $in_reply_to ); $parent_comment = \Activitypub\object_id_to_comment( $in_reply_to );
// save only replies and reactions // save only replies and reactions
$comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ) ?? $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' );
\Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ) ?? if ( ! $comment_post_id ) {
( $parent_comment ? $parent_comment->comment_post_ID : 0 ); $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 ) { if ( ! $comment_post_id ) {
return; return;
} }
@ -497,7 +501,7 @@ class Inbox {
*/ */
public static function handle_create( $object, $user_id ) { public static function handle_create( $object, $user_id ) {
$commentdata = self::convert_object_to_comment_data( $object ); $commentdata = self::convert_object_to_comment_data( $object );
if ( !$commentdata ) { if ( ! $commentdata ) {
return false; return false;
} }