coding standards

This commit is contained in:
Django Doucet 2022-04-15 01:17:00 -06:00
parent 73e6be9782
commit 51643142aa
8 changed files with 187 additions and 198 deletions

View file

@ -121,7 +121,7 @@ class Activity_Dispatcher {
$activity = $activitypub_activity->to_json(); // phpcs:ignore
// Send reply to followers, skip if replying to followers (avoid duplicate replies)
if( in_array( $cc, $replyto ) || in_array( $cc, $mentions ) ) {
if ( in_array( $cc, $replyto ) || in_array( $cc, $mentions ) ) {
continue;
}
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
@ -143,7 +143,7 @@ class Activity_Dispatcher {
//will be forwarded to the parent_comment->author or post_author followers collection
//TODO verify that ... what?
$parent_comment = \get_comment( $activitypub_comment->comment_parent );
if ( !is_null( $parent_comment ) ) {
if ( ! is_null( $parent_comment ) ) {
$user_id = $parent_comment->user_id;
} else {
$original_post = \get_post( $activitypub_comment->comment_post_ID );
@ -151,12 +151,12 @@ class Activity_Dispatcher {
}
//remove user_id from $activitypub_comment
unset($activitypub_activity['user_id']);
unset( $activitypub_activity['user_id'] );
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
//Forward reply to followers, skip sender
if( in_array( $to, $replyto ) || ( $replyto == $to ) ) {
if ( in_array( $to, $replyto ) || ( $replyto == $to ) ) {
continue;
}

View file

@ -25,8 +25,8 @@ class Activitypub {
\add_action( 'transition_post_status', array( '\Activitypub\Activitypub', 'schedule_post_activity' ), 10, 3 );
\add_filter( 'preprocess_comment' , array( '\Activitypub\Activitypub', 'preprocess_comment' ) );
\add_filter( 'comment_post' , array( '\Activitypub\Activitypub', 'postprocess_comment' ), 10, 3 );
\add_filter( 'preprocess_comment', array( '\Activitypub\Activitypub', 'preprocess_comment' ) );
\add_filter( 'comment_post', array( '\Activitypub\Activitypub', 'postprocess_comment' ), 10, 3 );
\add_filter( 'wp_update_comment_data', array( '\Activitypub\Activitypub', 'comment_updated_published' ), 20, 3 );
\add_action( 'transition_comment_status', array( '\Activitypub\Activitypub', 'schedule_comment_activity' ), 20, 3 );
\add_action( 'edit_comment', array( '\Activitypub\Activitypub', 'edit_comment' ), 20, 2 );//schedule_admin_comment_activity
@ -149,7 +149,7 @@ class Activitypub {
*/
public static function preprocess_comment( $commentdata ) {
// only process replies from local actors
if ( !empty( $commentdata['user_id'] ) ) {
if ( ! empty( $commentdata['user_id'] ) ) {
$commentdata['comment_type'] = 'activitypub';
// transform webfinger mentions to links and add @mentions to cc
$tagged_content = \Activitypub\transform_tags( $commentdata['comment_content'] );
@ -167,8 +167,7 @@ class Activitypub {
//Admin users comments bypass transition_comment_status (auto approved)
if ( $commentdata['comment_type'] === 'activitypub' ) {
if (
( $comment_approved === 1 ) &&
if ( ( $comment_approved === 1 ) &&
! empty( $commentdata['user_id'] ) &&
( $user = get_userdata( $commentdata['user_id'] ) ) && // get the user data
in_array( 'administrator', $user->roles ) // check the roles
@ -183,21 +182,6 @@ class Activitypub {
// TODO comment test as anon / no auth_url, no fetchable status?
// TODO comment test as registered
// TODO comment test as anyother site settings
// $replyto = get_comment_meta( $comment_id, 'replyto', true );
//inbox forward prep
// if ( !empty( $ap_object ) ) {
// //if is remote user (has ap_object)
// //error_log( print_r( $ap_object, true ) );
// // TODO verify that deduplication check happens at object create.
// //if to/cc/audience contains local followers collection
// //$local_user = \get_comment_author_url( $comment_id );
// //$is_local_user = \Activitypub\url_to_authorid( $commentdata['comment_author_url'] );
// }
}
}
}
@ -232,7 +216,7 @@ class Activitypub {
\wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment->comment_ID ) );
} else {
$local_user = \get_author_posts_url( $ap_object['user_id'] );
if ( !is_null( $local_user ) ) {
if ( ! is_null( $local_user ) ) {
if ( in_array( $local_user, $ap_object['to'] )
|| in_array( $local_user, $ap_object['cc'] )
|| in_array( $local_user, $ap_object['audience'] )
@ -248,9 +232,7 @@ class Activitypub {
} elseif ( $old_status === $new_status ) {
//TODO Test with non-admin user
\wp_schedule_single_event( \time(), 'activitypub_send_update_comment_activity', array( $activitypub_comment->comment_ID ) );
} else {
//error_log( 'schedule_update_comment_activity: else?:' );
}
} else { }
}
/**
@ -261,7 +243,7 @@ class Activitypub {
public static function comment_append_edit_datetime( $comment_text, $comment, $args ) {
if ( 'activitypub' === $comment->comment_type ) {
$updated = \wp_date( 'Y-m-d H:i:s', \strtotime( \get_comment_meta( $comment->comment_ID, 'ap_last_modified', true ) ) );
if( $updated ) {
if ( $updated ) {
$append_updated = "<div>(Last edited on <time class='modified' datetime='{$updated}'>$updated</time>)</div>";
$comment_text .= $append_updated;
}

View file

@ -1,7 +1,7 @@
<?php
namespace Activitypub;
define('AS_PUBLIC', 'https://www.w3.org/ns/activitystreams#Public');
define( 'AS_PUBLIC', 'https://www.w3.org/ns/activitystreams#Public' );
/**
* Returns the ActivityPub default JSON-context
@ -58,7 +58,7 @@ function safe_remote_post( $url, $body, $user_id ) {
$response = \wp_safe_remote_post( $url, $args );
//\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
return $response;
}
@ -355,7 +355,6 @@ function url_to_authorid( $url ) {
/**
* Verify if in_replyto_url is a local comment,
* Or if it is a previously received remote comment
*
* return int comment_id
*/
function url_to_commentid( $in_replyto_url ) {
@ -366,10 +365,10 @@ function url_to_commentid( $in_replyto_url ) {
//rewrite for activitypub object id simplification
$url_maybe_id = \wp_parse_url( $in_replyto_url );
if ( $url_maybe_id['scheme'] . '://' . $url_maybe_id['host'] == site_url() ) {
if ( site_url() === $url_maybe_id['scheme'] . '://' . $url_maybe_id['host'] ) {
//is local post or comment
\parse_str( $url_maybe_id['query'], $reply_query );
if (isset( $reply_query['ap_comment_id'] ) && is_int( $reply_query['ap_comment_id'] ) ){
if ( isset( $reply_query['ap_comment_id'] ) && is_int( $reply_query['ap_comment_id'] ) ) {
//is local comment
return $reply_query['ap_comment_id'];
} else {
@ -383,12 +382,12 @@ function url_to_commentid( $in_replyto_url ) {
'type' => 'activitypub',
'meta_query' => array(
array(
'key' => 'source_url',//$object['object']['id']
'key' => 'source_url', //$object['object']['id']
'value' => $in_replyto_url,
)
)
),
),
);
$comments_query = new \WP_Comment_Query;
$comments_query = new \WP_Comment_Query();
$comments = $comments_query->query( $comment_args );
$found_comment_ids = array();
if ( $comments ) {
@ -404,15 +403,14 @@ function url_to_commentid( $in_replyto_url ) {
/**
* Verify if url is a wp_ap_comment,
* Or if it is a previously received remote comment
*
* return int comment_id
*/
function is_ap_comment() {
$comment_id = get_query_var( 'ap_comment_id', null );
if( ! is_null( $comment_id ) ) {
if ( ! is_null( $comment_id ) ) {
$comment = \get_comment( $comment_id );
// Only return local origin comments
if( $comment->user_id ) {
if ( $comment->user_id ) {
return $comment_id;
}
}
@ -421,14 +419,12 @@ function is_ap_comment() {
/**
* Verify if url is a /replies endoint,
*
* return int true
*/
function is_ap_replies() {
global $wp;
$replies = get_query_var( 'replies' );
//$page = get_query_var( 'collection_page' );
if( ( $replies ) ) {
if ( $replies ) {
return $replies;
}
return null;
@ -438,7 +434,6 @@ function is_ap_replies() {
* Get tagged users from received AP object meta
* @param string $object_id a comment_id to search
* @param boolean $post defaults to searching a comment_id
*
* @return array of tagged users
*/
function get_recipients( $object_id, $post = null ) {
@ -451,9 +446,9 @@ function get_recipients( $object_id, $post = null ) {
$ap_object = \unserialize( \get_comment_meta( $object_id, 'ap_object', true ) );
}
if ( !empty( $ap_object ) ) {
if ( ! empty( $ap_object ) ) {
$tagged_users_name[] = \Activitypub\url_to_webfinger( $ap_object['actor'] );
if ( !empty( $ap_object['object']['tag'] ) ) {
if ( ! empty( $ap_object['object']['tag'] ) ) {
$author_post_url = \get_author_posts_url( $ap_object['user_id'] );
foreach ( $ap_object['object']['tag'] as $tag ) {
if ( $author_post_url == $tag['href'] ) {
@ -473,8 +468,8 @@ function get_recipients( $object_id, $post = null ) {
*/
function get_summary( $comment_id ) {
$ap_object = \unserialize( \get_comment_meta( $comment_id, 'ap_object', true ) );
if ( !empty( $ap_object ) ) {
if ( !empty( $ap_object['object']['summary'] ) ) {
if ( ! empty( $ap_object ) ) {
if ( ! empty( $ap_object['object']['summary'] ) ) {
return \esc_attr( $ap_object['object']['summary'] );
}
}
@ -490,7 +485,7 @@ function transform_tags( $content ) {
//@Mentions
$mentions = null;
$webfinger_tags = \Activitypub\webfinger_extract( $content );
if ( !empty( $webfinger_tags) ) {
if ( ! empty( $webfinger_tags ) ) {
foreach ( $webfinger_tags[0] as $webfinger_tag ) {
$ap_profile = \Activitypub\Rest\Webfinger::webfinger_lookup( $webfinger_tag );
if ( ! empty( $ap_profile ) ) {
@ -522,7 +517,7 @@ function tag_user( $recipient ) {
* @return array of all matched webfinger
*/
function webfinger_extract( $string ) {
preg_match_all("/@[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
preg_match_all( '/@[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i', $string, $matches );
return $matches;
}
@ -543,7 +538,7 @@ function url_to_webfinger( $user_url ) {
$user_url = \untrailingslashit( $user_url );
$user_url_array = explode( '/', $user_url );
$user_name = end( $user_url_array );
$url_host = parse_url( $user_url , PHP_URL_HOST );
$url_host = parse_url( $user_url, PHP_URL_HOST );
$webfinger = '@' . $user_name . '@' . $url_host;
return $webfinger;
}
@ -574,7 +569,7 @@ function set_ap_comment_id( $comment ) {
$ap_comment_id = add_query_arg(
array(
'p' => $comment->comment_post_ID,
'ap_comment_id' => $comment->comment_ID,//should probably rename to ap_comment or something
'ap_comment_id' => $comment->comment_ID, //should probably rename to ap_comment or something
),
trailingslashit( site_url() )
);
@ -598,7 +593,7 @@ function get_audience( $object ) {
if ( in_array( AS_PUBLIC, $object['cc'] ) ) {
return 'unlisted';//is unlisted even relevant?
}
if ( !in_array( AS_PUBLIC, $object['to'] ) && !in_array( AS_PUBLIC, $object['cc'] ) ) {
if ( ! in_array( AS_PUBLIC, $object['to'] ) && ! in_array( AS_PUBLIC, $object['cc'] ) ) {
$author_post_url = get_author_posts_url( $object['user_id'] );
if ( in_array( $author_post_url, $object['cc'] ) ) {
return 'followers_only';

View file

@ -76,7 +76,7 @@ class Activity {
if ( $this->trash ) {
$this->deleted = $timestamp['deleted'];
}
if ( $this->updated) {
if ( $this->updated ) {
$this->updated = $timestamp['updated'];
}
}

View file

@ -58,8 +58,8 @@ class Comment {
),
'context' => $this->context,
//'source' => \get_comment_link( $comment ), //non-conforming, see https://www.w3.org/TR/activitypub/#source-property
'url' => \get_comment_link( $comment ),//link for mastodon
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),//audience logic
'url' => \get_comment_link( $comment ), //link for mastodon
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ), //audience logic
'cc' => $this->cc_recipients,
'tag' => $this->tags,
);
@ -171,9 +171,9 @@ class Comment {
//TODO Add audience logic get parent audience
//TODO shouldn't mentions go in 'to'?
$recipients = array( AS_PUBLIC );
$mentions = \get_comment_meta( $this->comment->comment_ID, 'mentions', true ) ;
if ( !empty( $mentions ) ) {
foreach ($mentions as $mention) {
$mentions = \get_comment_meta( $this->comment->comment_ID, 'mentions', true );
if ( ! empty( $mentions ) ) {
foreach ( $mentions as $mention ) {
$recipients[] = $mention['href'];
}
}
@ -184,9 +184,9 @@ class Comment {
* Mention user being replied to
*/
public function generate_tags() {
$mentions = \get_comment_meta( $this->comment->comment_ID, 'mentions', true ) ;
if ( !empty( $mentions ) ) {
foreach ($mentions as $mention) {
$mentions = \get_comment_meta( $this->comment->comment_ID, 'mentions', true );
if ( ! empty( $mentions ) ) {
foreach ( $mentions as $mention ) {
$mention_tags[] = array(
'type' => 'Mention',
'href' => $mention['href'],
@ -236,27 +236,33 @@ class Comment {
$children = \get_comments( $args );
$replies = null;
if ( $children ) {
$items = [];
foreach ( $children as $child_comment ){
$items = array();
foreach ( $children as $child_comment ) {
$comment_url = \add_query_arg(
array(
'p' => $child_comment->comment_post_ID,
'ap_comment_id' => $child_comment->comment_ID
'ap_comment_id' => $child_comment->comment_ID,
),
trailingslashit( site_url() )
);
$items[] = $comment_url;
}
$replies = (object) [
$replies = (object) array(
'type' => 'Collection',
'id' => \add_query_arg( array( 'replies' => '' ), $this->id ),
'first' => (object) [
'first' => (object) array(
'type' => 'CollectionPage',
'partOf'=> \add_query_arg( array( 'replies' => '' ), $this->id ),
'next' => \add_query_arg( array( 'replies' => '', 'page' => 1 ), $this->id ),
'items' => $items
],
];
'partOf' => \add_query_arg( array( 'replies' => '' ), $this->id ),
'next' => \add_query_arg(
array(
'replies' => '',
'page' => 1,
),
$this->id
),
'items' => $items,
),
);
}
return $replies;
}

View file

@ -188,16 +188,16 @@ class Post {
'status' => 'approve',
);
$comments = \get_comments( $args );
$items = [];
$items = array();
foreach ( $comments as $comment ){
foreach ( $comments as $comment ) {
// include self replies
if ( $this->post->post_author === $comment->user_id ) {
//$comment_url = $comment->comment_ID;
$comment_url = \add_query_arg( //
array(
'p' => $this->post->ID,
'ap_comment_id' => $comment->comment_ID
'ap_comment_id' => $comment->comment_ID,
),
trailingslashit( site_url() )
);
@ -206,16 +206,22 @@ class Post {
}
}
//\error_log( 'generate_replies: $comments' . print_r( $comments, true ) );
$replies = (object) [
$replies = (object) array(
'type' => 'Collection',
'id' => \add_query_arg( array( 'replies' => '' ), $this->id ),
'first' => (object) [
'first' => (object) array(
'type' => 'CollectionPage',
'partOf'=> \add_query_arg( array( 'replies' => '' ), $this->id ),
'next' => \add_query_arg( array( 'replies' => '', 'page' => 1 ), $this->id ),
'items' => $items
],
];
'partOf' => \add_query_arg( array( 'replies' => '' ), $this->id ),
'next' => \add_query_arg(
array(
'replies' => '',
'page' => 1,
),
$this->id
),
'items' => $items,
),
);
}
return $replies;
}

View file

@ -417,7 +417,7 @@ class Inbox {
$comment_parent_ID = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['inReplyTo'] ) );
if ( !is_null( $comment_parent_ID ) ) {
if ( ! is_null( $comment_parent_ID ) ) {
//inReplyTo a known local comment
$comment_parent = \get_comment( $comment_parent_ID );
$comment_post_ID = $comment_parent->comment_post_ID;
@ -435,21 +435,21 @@ class Inbox {
}
// if no name is set use peer username
if ( !empty( $meta['name'] ) ) {
if ( ! empty( $meta['name'] ) ) {
$name = \esc_attr( $meta['name'] );
} else {
$name = \esc_attr( $meta['preferredUsername'] );
}
// if avatar is set
if ( !empty( $meta['icon']['url'] ) ) {
if ( ! empty( $meta['icon']['url'] ) ) {
$avatar_url = \esc_attr( $meta['icon']['url'] );
}
//Only create WP_Comment for public replies to local posts
if ( ( in_array( AS_PUBLIC, $object['to'] )
|| in_array( AS_PUBLIC, $object['cc'] ) )
&& ( !empty( $comment_post_ID )
|| !empty ( $comment_parent )
&& ( ! empty( $comment_post_ID )
|| ! empty( $comment_parent )
) ) {
$commentdata = array(
@ -497,7 +497,7 @@ class Inbox {
//Determine comment_ID
$object_comment_ID = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['id'] ) );
if ( !is_null( $object_comment_ID ) ) {
if ( ! is_null( $object_comment_ID ) ) {
//found a local comment id
$commentdata = \get_comment( $object_comment_ID, ARRAY_A );
@ -541,7 +541,7 @@ class Inbox {
}
//Determine comment_ID
$object_comment_ID = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['id'] ) );
if ( !is_null( $object_comment_ID ) ) {
if ( ! is_null( $object_comment_ID ) ) {
//found a local comment id
$commentdata = \get_comment( $object_comment_ID, ARRAY_A );

View file

@ -129,7 +129,7 @@ class Webfinger {
*/
public static function webfinger_lookup( $webfinger ) {
$activity_profile = null;
if ( \substr($webfinger, 0, 1) === '@' ) {
if ( \substr( $webfinger, 0, 1 ) === '@' ) {
$webfinger = substr( $webfinger, 1 );
}
$url_host = \explode( '@', $webfinger );
@ -140,7 +140,7 @@ class Webfinger {
$ap_link = json_decode( $response['body'] );
if ( isset( $ap_link->links ) ) {
foreach ( $ap_link->links as $link ) {
if ( !property_exists( $link, 'type' ) ) {
if ( ! property_exists( $link, 'type' ) ) {
continue;
}
if ( isset( $link->type ) && $link->type === 'application/activity+json' ) {