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.
*/
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;
}

View file

@ -432,9 +432,9 @@ 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 ) ) {
@ -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;
}