updated plugin ActivityPub
version 3.3.3
This commit is contained in:
@ -1,28 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* ActivityPub Comment Class
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub;
|
||||
|
||||
use Activitypub\Collection\Users;
|
||||
use WP_Comment_Query;
|
||||
|
||||
use function Activitypub\is_user_disabled;
|
||||
use function Activitypub\is_single_user;
|
||||
|
||||
/**
|
||||
* ActivityPub Comment Class
|
||||
* ActivityPub Comment Class.
|
||||
*
|
||||
* This class is a helper/utils class that provides a collection of static
|
||||
* methods that are used to handle comments.
|
||||
*/
|
||||
class Comment {
|
||||
/**
|
||||
* Initialize the class, registering WordPress hooks
|
||||
* Initialize the class, registering WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
self::register_comment_types();
|
||||
|
||||
\add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 3 );
|
||||
\add_filter( 'comment_class', array( self::class, 'comment_class' ), 10, 3 );
|
||||
\add_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 3 );
|
||||
\add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
|
||||
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
|
||||
|
||||
\add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,9 +38,9 @@ class Comment {
|
||||
* We don't want to show the comment reply link for federated comments
|
||||
* if the user is disabled for federation.
|
||||
*
|
||||
* @param string $link The HTML markup for the comment reply link.
|
||||
* @param array $args An array of arguments overriding the defaults.
|
||||
* @param WP_Comment $comment The object of the comment being replied.
|
||||
* @param string $link The HTML markup for the comment reply link.
|
||||
* @param array $args An array of arguments overriding the defaults.
|
||||
* @param \WP_Comment $comment The object of the comment being replied.
|
||||
*
|
||||
* @return string The filtered HTML markup for the comment reply link.
|
||||
*/
|
||||
@ -49,7 +56,7 @@ class Comment {
|
||||
|
||||
$attrs = array(
|
||||
'selectedComment' => self::generate_id( $comment ),
|
||||
'commentId' => $comment->comment_ID,
|
||||
'commentId' => $comment->comment_ID,
|
||||
);
|
||||
|
||||
$div = sprintf(
|
||||
@ -62,6 +69,7 @@ class Comment {
|
||||
|
||||
/**
|
||||
* Create a link to reply to a federated comment.
|
||||
*
|
||||
* This function adds a title attribute to the reply link to inform the user
|
||||
* that the comment was received from the fediverse and the reply will be sent
|
||||
* to the original author.
|
||||
@ -73,7 +81,7 @@ class Comment {
|
||||
*/
|
||||
private static function create_fediverse_reply_link( $link, $args ) {
|
||||
$str_to_replace = sprintf( '>%s<', $args['reply_text'] );
|
||||
$replace_with = sprintf(
|
||||
$replace_with = sprintf(
|
||||
' title="%s">%s<',
|
||||
esc_attr__( 'This comment was received from the fediverse and your reply will be sent to the original author', 'activitypub' ),
|
||||
esc_html__( 'Reply with federation', 'activitypub' )
|
||||
@ -104,7 +112,7 @@ class Comment {
|
||||
}
|
||||
|
||||
if ( is_single_user() && \user_can( $current_user, 'publish_posts' ) ) {
|
||||
// On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user
|
||||
// On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user.
|
||||
$current_user = Users::BLOG_USER_ID;
|
||||
}
|
||||
|
||||
@ -200,7 +208,7 @@ class Comment {
|
||||
* @return boolean True if the comment should be federated, false otherwise.
|
||||
*/
|
||||
public static function should_be_federated( $comment ) {
|
||||
// we should not federate federated comments
|
||||
// We should not federate federated comments.
|
||||
if ( self::was_received( $comment ) ) {
|
||||
return false;
|
||||
}
|
||||
@ -208,29 +216,29 @@ class Comment {
|
||||
$comment = \get_comment( $comment );
|
||||
$user_id = $comment->user_id;
|
||||
|
||||
// comments without user can't be federated
|
||||
// Comments without user can't be federated.
|
||||
if ( ! $user_id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_single_user() && \user_can( $user_id, 'publish_posts' ) ) {
|
||||
// On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user
|
||||
// On a single user site, comments by users with the `publish_posts` capability will be federated as the blog user.
|
||||
$user_id = Users::BLOG_USER_ID;
|
||||
}
|
||||
|
||||
$is_user_disabled = is_user_disabled( $user_id );
|
||||
|
||||
// user is disabled for federation
|
||||
// User is disabled for federation.
|
||||
if ( $is_user_disabled ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// it is a comment to the post and can be federated
|
||||
// It is a comment to the post and can be federated.
|
||||
if ( empty( $comment->comment_parent ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check if parent comment is federated
|
||||
// Check if parent comment is federated.
|
||||
$parent_comment = \get_comment( $comment->comment_parent );
|
||||
|
||||
return ! self::is_local( $parent_comment );
|
||||
@ -264,18 +272,18 @@ class Comment {
|
||||
|
||||
/**
|
||||
* Verify if URL is a local comment, or if it is a previously received
|
||||
* remote comment (For threading comments locally)
|
||||
* remote comment (For threading comments locally).
|
||||
*
|
||||
* @param string $url The URL to check.
|
||||
*
|
||||
* @return int comment_ID or null if not found
|
||||
* @return string|null Comment ID or null if not found.
|
||||
*/
|
||||
public static function url_to_commentid( $url ) {
|
||||
if ( ! $url || ! filter_var( $url, \FILTER_VALIDATE_URL ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// check for local comment
|
||||
// Check for local comment.
|
||||
if ( \wp_parse_url( \home_url(), \PHP_URL_HOST ) === \wp_parse_url( $url, \PHP_URL_HOST ) ) {
|
||||
$query = \wp_parse_url( $url, \PHP_URL_QUERY );
|
||||
|
||||
@ -327,7 +335,7 @@ class Comment {
|
||||
* @return string[] An array of classes.
|
||||
*/
|
||||
public static function comment_class( $classes, $css_class, $comment_id ) {
|
||||
// check if ActivityPub comment
|
||||
// Check if ActivityPub comment.
|
||||
if ( 'activitypub' === get_comment_meta( $comment_id, 'protocol', true ) ) {
|
||||
$classes[] = 'activitypub-comment';
|
||||
}
|
||||
@ -335,11 +343,51 @@ class Comment {
|
||||
return $classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the public comment id via the WordPress comments meta.
|
||||
*
|
||||
* @param int $wp_comment_id The internal WordPress comment ID.
|
||||
* @param bool $fallback Whether the code should fall back to `source_url` if `source_id` is not set.
|
||||
*
|
||||
* @return string|null The ActivityPub id/url of the comment.
|
||||
*/
|
||||
public static function get_source_id( $wp_comment_id, $fallback = true ) {
|
||||
$comment_meta = \get_comment_meta( $wp_comment_id );
|
||||
|
||||
if ( ! empty( $comment_meta['source_id'][0] ) ) {
|
||||
return $comment_meta['source_id'][0];
|
||||
} elseif ( ! empty( $comment_meta['source_url'][0] ) && $fallback ) {
|
||||
return $comment_meta['source_url'][0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the public comment url via the WordPress comments meta.
|
||||
*
|
||||
* @param int $wp_comment_id The internal WordPress comment ID.
|
||||
* @param bool $fallback Whether the code should fall back to `source_id` if `source_url` is not set.
|
||||
*
|
||||
* @return string|null The ActivityPub id/url of the comment.
|
||||
*/
|
||||
public static function get_source_url( $wp_comment_id, $fallback = true ) {
|
||||
$comment_meta = \get_comment_meta( $wp_comment_id );
|
||||
|
||||
if ( ! empty( $comment_meta['source_url'][0] ) ) {
|
||||
return $comment_meta['source_url'][0];
|
||||
} elseif ( ! empty( $comment_meta['source_id'][0] ) && $fallback ) {
|
||||
return $comment_meta['source_id'][0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Link remote comments to source url.
|
||||
*
|
||||
* @param string $comment_link
|
||||
* @param object|WP_Comment $comment
|
||||
* @param string $comment_link The comment link.
|
||||
* @param object|\WP_Comment $comment The comment object.
|
||||
*
|
||||
* @return string $url
|
||||
*/
|
||||
@ -348,37 +396,30 @@ class Comment {
|
||||
return $comment_link;
|
||||
}
|
||||
|
||||
$comment_meta = \get_comment_meta( $comment->comment_ID );
|
||||
$public_comment_link = self::get_source_url( $comment->comment_ID );
|
||||
|
||||
if ( ! empty( $comment_meta['source_url'][0] ) ) {
|
||||
return $comment_meta['source_url'][0];
|
||||
} elseif ( ! empty( $comment_meta['source_id'][0] ) ) {
|
||||
return $comment_meta['source_id'][0];
|
||||
}
|
||||
|
||||
return $comment_link;
|
||||
return $public_comment_link ?? $comment_link;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates an ActivityPub URI for a comment
|
||||
*
|
||||
* @param WP_Comment|int $comment A comment object or comment ID
|
||||
* @param \WP_Comment|int $comment A comment object or comment ID.
|
||||
*
|
||||
* @return string ActivityPub URI for comment
|
||||
*/
|
||||
public static function generate_id( $comment ) {
|
||||
$comment = \get_comment( $comment );
|
||||
$comment_meta = \get_comment_meta( $comment->comment_ID );
|
||||
$comment = \get_comment( $comment );
|
||||
|
||||
// show external comment ID if it exists
|
||||
if ( ! empty( $comment_meta['source_id'][0] ) ) {
|
||||
return $comment_meta['source_id'][0];
|
||||
} elseif ( ! empty( $comment_meta['source_url'][0] ) ) {
|
||||
return $comment_meta['source_url'][0];
|
||||
// Show external comment ID if it exists.
|
||||
$public_comment_link = self::get_source_id( $comment->comment_ID );
|
||||
|
||||
if ( $public_comment_link ) {
|
||||
return $public_comment_link;
|
||||
}
|
||||
|
||||
// generate URI based on comment ID
|
||||
// Generate URI based on comment ID.
|
||||
return \add_query_arg( 'c', $comment->comment_ID, \trailingslashit( \home_url() ) );
|
||||
}
|
||||
|
||||
@ -392,7 +433,8 @@ class Comment {
|
||||
private static function post_has_remote_comments( $post_id ) {
|
||||
$comments = \get_comments(
|
||||
array(
|
||||
'post_id' => $post_id,
|
||||
'post_id' => $post_id,
|
||||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
@ -416,22 +458,22 @@ class Comment {
|
||||
*/
|
||||
public static function enqueue_scripts() {
|
||||
if ( ! \is_singular() || \is_user_logged_in() ) {
|
||||
// only on single pages, only for logged out users
|
||||
// Only on single pages, only for logged-out users.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! \post_type_supports( \get_post_type(), 'activitypub' ) ) {
|
||||
// post type does not support ActivityPub
|
||||
// Post type does not support ActivityPub.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! \comments_open() || ! \get_comments_number() ) {
|
||||
// no comments, no need to load the script
|
||||
// No comments, no need to load the script.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! self::post_has_remote_comments( \get_the_ID() ) ) {
|
||||
// no remote comments, no need to load the script
|
||||
// No remote comments, no need to load the script.
|
||||
return;
|
||||
}
|
||||
|
||||
@ -457,9 +499,171 @@ class Comment {
|
||||
\wp_enqueue_style(
|
||||
$handle,
|
||||
\plugins_url( 'build/remote-reply/style-index.css', __DIR__ ),
|
||||
[ 'wp-components' ],
|
||||
array( 'wp-components' ),
|
||||
$assets['version']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered custom comment types.
|
||||
*
|
||||
* @return array The registered custom comment types
|
||||
*/
|
||||
public static function get_comment_types() {
|
||||
global $activitypub_comment_types;
|
||||
|
||||
return $activitypub_comment_types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this a registered comment type.
|
||||
*
|
||||
* @param string $slug The name of the type.
|
||||
* @return boolean True if registered.
|
||||
*/
|
||||
public static function is_registered_comment_type( $slug ) {
|
||||
$slug = strtolower( $slug );
|
||||
$slug = sanitize_key( $slug );
|
||||
|
||||
return in_array( $slug, array_keys( self::get_comment_types() ), true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered custom comment types names.
|
||||
*
|
||||
* @return array The registered custom comment type names.
|
||||
*/
|
||||
public static function get_comment_type_names() {
|
||||
return array_values( wp_list_pluck( self::get_comment_types(), 'type' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a comment type.
|
||||
*
|
||||
* @param string $type The comment type.
|
||||
*
|
||||
* @return array The comment type.
|
||||
*/
|
||||
public static function get_comment_type( $type ) {
|
||||
$type = strtolower( $type );
|
||||
$type = sanitize_key( $type );
|
||||
$types = self::get_comment_types();
|
||||
|
||||
if ( in_array( $type, array_keys( $types ), true ) ) {
|
||||
$type_array = $types[ $type ];
|
||||
} else {
|
||||
$type_array = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the comment type.
|
||||
*
|
||||
* @param array $type_array The comment type.
|
||||
*/
|
||||
return apply_filters( "activitypub_comment_type_{$type}", $type_array );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a comment type attribute.
|
||||
*
|
||||
* @param string $type The comment type.
|
||||
* @param string $attr The attribute to get.
|
||||
*
|
||||
* @return mixed The value of the attribute.
|
||||
*/
|
||||
public static function get_comment_type_attr( $type, $attr ) {
|
||||
$type_array = self::get_comment_type( $type );
|
||||
|
||||
if ( $type_array && isset( $type_array[ $attr ] ) ) {
|
||||
$value = $type_array[ $attr ];
|
||||
} else {
|
||||
$value = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the comment type attribute.
|
||||
*
|
||||
* @param mixed $value The value of the attribute.
|
||||
* @param string $type The comment type.
|
||||
*/
|
||||
return apply_filters( "activitypub_comment_type_{$attr}", $value, $type );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the comment types used by the ActivityPub plugin.
|
||||
*/
|
||||
public static function register_comment_types() {
|
||||
register_comment_type(
|
||||
'announce',
|
||||
array(
|
||||
'label' => __( 'Reposts', 'activitypub' ),
|
||||
'singular' => __( 'Repost', 'activitypub' ),
|
||||
'description' => __( 'A repost on the indieweb is a post that is purely a 100% re-publication of another (typically someone else\'s) post.', 'activitypub' ),
|
||||
'icon' => '♻️',
|
||||
'class' => 'p-repost',
|
||||
'type' => 'repost',
|
||||
// translators: %1$s username, %2$s object format (post, audio, ...), %3$s URL, %4$s domain.
|
||||
'excerpt' => __( '… reposted this!', 'activitypub' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_comment_type(
|
||||
'like',
|
||||
array(
|
||||
'label' => __( 'Likes', 'activitypub' ),
|
||||
'singular' => __( 'Like', 'activitypub' ),
|
||||
'description' => __( 'A like is a popular webaction button and in some cases post type on various silos such as Facebook and Instagram.', 'activitypub' ),
|
||||
'icon' => '👍',
|
||||
'class' => 'p-like',
|
||||
'type' => 'like',
|
||||
// translators: %1$s username, %2$s object format (post, audio, ...), %3$s URL, %4$s domain.
|
||||
'excerpt' => __( '… liked this!', 'activitypub' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show avatars on Activities if set.
|
||||
*
|
||||
* @param array $types List of avatar enabled comment types.
|
||||
*
|
||||
* @return array show avatars on Activities
|
||||
*/
|
||||
public static function get_avatar_comment_types( $types ) {
|
||||
$comment_types = self::get_comment_type_names();
|
||||
$types = array_merge( $types, $comment_types );
|
||||
|
||||
return array_unique( $types );
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes likes and reposts from comment queries.
|
||||
*
|
||||
* @author Jan Boddez
|
||||
*
|
||||
* @see https://github.com/janboddez/indieblocks/blob/a2d59de358031056a649ee47a1332ce9e39d4ce2/includes/functions.php#L423-L432
|
||||
*
|
||||
* @param WP_Comment_Query $query Comment count.
|
||||
*/
|
||||
public static function comment_query( $query ) {
|
||||
if ( ! $query instanceof WP_Comment_Query ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_admin() || ! is_singular() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $query->query_vars['type__in'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $query->query_vars['count'] ) && true === $query->query_vars['count'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Exclude likes and reposts by the Webmention plugin.
|
||||
$query->query_vars['type__not_in'] = self::get_comment_type_names();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user