updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -7,10 +7,12 @@
|
||||
|
||||
namespace Activitypub\Collection;
|
||||
|
||||
use Activitypub\Webfinger;
|
||||
use WP_Comment_Query;
|
||||
use Activitypub\Comment;
|
||||
|
||||
use function Activitypub\object_to_uri;
|
||||
use function Activitypub\is_post_disabled;
|
||||
use function Activitypub\url_to_commentid;
|
||||
use function Activitypub\object_id_to_comment;
|
||||
use function Activitypub\get_remote_metadata_by_actor;
|
||||
@ -27,7 +29,7 @@ class Interactions {
|
||||
*
|
||||
* @param array $activity The activity-object.
|
||||
*
|
||||
* @return array|false The comment data or false on failure.
|
||||
* @return int|false|\WP_Error The comment ID or false or WP_Error on failure.
|
||||
*/
|
||||
public static function add_comment( $activity ) {
|
||||
$commentdata = self::activity_to_comment( $activity );
|
||||
@ -36,7 +38,8 @@ class Interactions {
|
||||
return false;
|
||||
}
|
||||
|
||||
$in_reply_to = \esc_url_raw( $activity['object']['inReplyTo'] );
|
||||
$in_reply_to = object_to_uri( $activity['object']['inReplyTo'] );
|
||||
$in_reply_to = \esc_url_raw( $in_reply_to );
|
||||
$comment_post_id = \url_to_postid( $in_reply_to );
|
||||
$parent_comment_id = url_to_commentid( $in_reply_to );
|
||||
|
||||
@ -46,8 +49,7 @@ class Interactions {
|
||||
$comment_post_id = $parent_comment->comment_post_ID;
|
||||
}
|
||||
|
||||
// Not a reply to a post or comment.
|
||||
if ( ! $comment_post_id ) {
|
||||
if ( is_post_disabled( $comment_post_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -97,27 +99,26 @@ class Interactions {
|
||||
}
|
||||
|
||||
$url = object_to_uri( $activity['object'] );
|
||||
$comment_post_id = url_to_postid( $url );
|
||||
$comment_post_id = \url_to_postid( $url );
|
||||
$parent_comment_id = url_to_commentid( $url );
|
||||
|
||||
if ( ! $comment_post_id && $parent_comment_id ) {
|
||||
$parent_comment = get_comment( $parent_comment_id );
|
||||
$parent_comment = \get_comment( $parent_comment_id );
|
||||
$comment_post_id = $parent_comment->comment_post_ID;
|
||||
}
|
||||
|
||||
if ( ! $comment_post_id ) {
|
||||
if ( ! $comment_post_id || is_post_disabled( $comment_post_id ) ) {
|
||||
// Not a reply to a post or comment.
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = $activity['type'];
|
||||
$comment_type = Comment::get_comment_type_by_activity_type( $activity['type'] );
|
||||
|
||||
if ( ! Comment::is_registered_comment_type( $type ) ) {
|
||||
if ( ! $comment_type ) {
|
||||
// Not a valid comment type.
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment_type = Comment::get_comment_type( $type );
|
||||
$comment_content = $comment_type['excerpt'];
|
||||
|
||||
$commentdata['comment_post_ID'] = $comment_post_id;
|
||||
@ -178,20 +179,19 @@ class Interactions {
|
||||
$actor = object_to_uri( $meta['url'] );
|
||||
}
|
||||
|
||||
$args = array(
|
||||
$args = array(
|
||||
'nopaging' => true,
|
||||
'author_url' => $actor,
|
||||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'protocol',
|
||||
'value' => 'activitypub',
|
||||
'compare' => '=',
|
||||
'key' => 'protocol',
|
||||
'value' => 'activitypub',
|
||||
),
|
||||
),
|
||||
);
|
||||
$comment_query = new WP_Comment_Query( $args );
|
||||
return $comment_query->comments;
|
||||
|
||||
return get_comments( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,7 +229,7 @@ class Interactions {
|
||||
*/
|
||||
public static function activity_to_comment( $activity ) {
|
||||
$comment_content = null;
|
||||
$actor = object_to_uri( $activity['actor'] );
|
||||
$actor = object_to_uri( $activity['actor'] ?? null );
|
||||
$actor = get_remote_metadata_by_actor( $actor );
|
||||
|
||||
// Check Actor-Meta.
|
||||
@ -246,22 +246,29 @@ class Interactions {
|
||||
return false;
|
||||
}
|
||||
|
||||
$url = object_to_uri( $actor['url'] );
|
||||
$url = object_to_uri( $actor['url'] ?? $actor['id'] );
|
||||
|
||||
if ( ! $url ) {
|
||||
object_to_uri( $actor['id'] );
|
||||
$url = object_to_uri( $actor['id'] );
|
||||
}
|
||||
|
||||
if ( isset( $activity['object']['content'] ) ) {
|
||||
$comment_content = \addslashes( $activity['object']['content'] );
|
||||
}
|
||||
|
||||
$webfinger = Webfinger::uri_to_acct( $url );
|
||||
if ( is_wp_error( $webfinger ) ) {
|
||||
$webfinger = '';
|
||||
} else {
|
||||
$webfinger = str_replace( 'acct:', '', $webfinger );
|
||||
}
|
||||
|
||||
$commentdata = array(
|
||||
'comment_author' => \esc_attr( $comment_author ),
|
||||
'comment_author_url' => \esc_url_raw( $url ),
|
||||
'comment_content' => $comment_content,
|
||||
'comment_type' => 'comment',
|
||||
'comment_author_email' => '',
|
||||
'comment_author_email' => $webfinger,
|
||||
'comment_meta' => array(
|
||||
'source_id' => \esc_url_raw( object_to_uri( $activity['object'] ) ),
|
||||
'protocol' => 'activitypub',
|
||||
@ -289,7 +296,7 @@ class Interactions {
|
||||
*/
|
||||
public static function persist( $commentdata, $action = self::INSERT ) {
|
||||
// Disable flood control.
|
||||
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
|
||||
\remove_action( 'check_comment_flood', 'check_comment_flood_db' );
|
||||
// Do not require email for AP entries.
|
||||
\add_filter( 'pre_option_require_name_email', '__return_false' );
|
||||
// No nonce possible for this submission route.
|
||||
@ -307,7 +314,7 @@ class Interactions {
|
||||
$state = \wp_update_comment( $commentdata, true );
|
||||
}
|
||||
|
||||
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10 );
|
||||
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ) );
|
||||
\remove_filter( 'pre_option_require_name_email', '__return_false' );
|
||||
// Restore flood control.
|
||||
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
|
||||
@ -318,4 +325,25 @@ class Interactions {
|
||||
return $state; // Either WP_Comment, false, a WP_Error, 0, or 1!
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total number of interactions by type for a given ID.
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
* @param string $type The type of interaction to count.
|
||||
*
|
||||
* @return int The total number of interactions.
|
||||
*/
|
||||
public static function count_by_type( $post_id, $type ) {
|
||||
return \get_comments(
|
||||
array(
|
||||
'post_id' => $post_id,
|
||||
'status' => 'approve',
|
||||
'type' => $type,
|
||||
'count' => true,
|
||||
'paging' => false,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user