updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
|
||||
namespace Activitypub;
|
||||
|
||||
use Activitypub\Collection\Users;
|
||||
use Activitypub\Collection\Actors;
|
||||
use WP_Comment_Query;
|
||||
|
||||
/**
|
||||
@ -25,11 +25,14 @@ class Comment {
|
||||
|
||||
\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_filter( 'get_comment_link', array( self::class, 'remote_comment_link' ), 11, 2 );
|
||||
\add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue_scripts' ) );
|
||||
\add_action( 'pre_get_comments', array( static::class, 'comment_query' ) );
|
||||
|
||||
\add_filter( 'pre_comment_approved', array( static::class, 'pre_comment_approved' ), 10, 2 );
|
||||
\add_filter( 'get_avatar_comment_types', array( static::class, 'get_avatar_comment_types' ), 99 );
|
||||
\add_action( 'update_option_activitypub_allow_likes', array( self::class, 'maybe_update_comment_counts' ), 10, 2 );
|
||||
\add_action( 'update_option_activitypub_allow_reposts', array( self::class, 'maybe_update_comment_counts' ), 10, 2 );
|
||||
\add_filter( 'pre_wp_update_comment_count_now', array( static::class, 'pre_wp_update_comment_count_now' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,8 +49,7 @@ class Comment {
|
||||
*/
|
||||
public static function comment_reply_link( $link, $args, $comment ) {
|
||||
if ( self::are_comments_allowed( $comment ) ) {
|
||||
$user_id = get_current_user_id();
|
||||
if ( $user_id && self::was_received( $comment ) && \user_can( $user_id, 'activitypub' ) ) {
|
||||
if ( \current_user_can( 'activitypub' ) && self::was_received( $comment ) ) {
|
||||
return self::create_fediverse_reply_link( $link, $args );
|
||||
}
|
||||
|
||||
@ -60,10 +62,17 @@ class Comment {
|
||||
);
|
||||
|
||||
$div = sprintf(
|
||||
'<div class="activitypub-remote-reply" data-attrs="%s"></div>',
|
||||
'<div class="reply activitypub-remote-reply" data-attrs="%s"></div>',
|
||||
esc_attr( wp_json_encode( $attrs ) )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the HTML markup for the ActivityPub remote comment reply container.
|
||||
*
|
||||
* @param string $div The HTML markup for the remote reply container. Default is a div
|
||||
* with class 'activitypub-remote-reply' and data attributes for
|
||||
* the selected comment ID and internal comment ID.
|
||||
*/
|
||||
return apply_filters( 'activitypub_comment_reply_link', $div );
|
||||
}
|
||||
|
||||
@ -113,16 +122,10 @@ 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.
|
||||
$current_user = Users::BLOG_USER_ID;
|
||||
$current_user = Actors::BLOG_USER_ID;
|
||||
}
|
||||
|
||||
$is_user_disabled = is_user_disabled( $current_user );
|
||||
|
||||
if ( $is_user_disabled ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return user_can_activitypub( $current_user );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -221,15 +224,13 @@ class Comment {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_single_user() && \user_can( $user_id, 'publish_posts' ) ) {
|
||||
if ( is_single_user() && \user_can( $user_id, 'activitypub' ) ) {
|
||||
// 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;
|
||||
$user_id = Actors::BLOG_USER_ID;
|
||||
}
|
||||
|
||||
$is_user_disabled = is_user_disabled( $user_id );
|
||||
|
||||
// User is disabled for federation.
|
||||
if ( $is_user_disabled ) {
|
||||
// User is not allowed to federate comments.
|
||||
if ( ! user_can_activitypub( $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -256,6 +257,8 @@ class Comment {
|
||||
array(
|
||||
'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
|
||||
'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
|
||||
'orderby' => 'comment_date',
|
||||
'order' => 'DESC',
|
||||
)
|
||||
);
|
||||
|
||||
@ -263,10 +266,6 @@ class Comment {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( count( $comment_query->comments ) > 1 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $comment_query->comments[0];
|
||||
}
|
||||
|
||||
@ -479,7 +478,8 @@ class Comment {
|
||||
|
||||
$handle = 'activitypub-remote-reply';
|
||||
$data = array(
|
||||
'namespace' => ACTIVITYPUB_REST_NAMESPACE,
|
||||
'namespace' => ACTIVITYPUB_REST_NAMESPACE,
|
||||
'defaultAvatarUrl' => ACTIVITYPUB_PLUGIN_URL . 'assets/img/mp.jpg',
|
||||
);
|
||||
$js = sprintf( 'var _activityPubOptions = %s;', wp_json_encode( $data ) );
|
||||
$asset_file = ACTIVITYPUB_PLUGIN_DIR . 'build/remote-reply/index.asset.php';
|
||||
@ -495,6 +495,7 @@ class Comment {
|
||||
true
|
||||
);
|
||||
\wp_add_inline_script( $handle, $js, 'before' );
|
||||
\wp_set_script_translations( $handle, 'activitypub' );
|
||||
|
||||
\wp_enqueue_style(
|
||||
$handle,
|
||||
@ -505,6 +506,27 @@ class Comment {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the comment type by activity type.
|
||||
*
|
||||
* @param string $activity_type The activity type.
|
||||
*
|
||||
* @return array|null The comment type.
|
||||
*/
|
||||
public static function get_comment_type_by_activity_type( $activity_type ) {
|
||||
$activity_type = \strtolower( $activity_type );
|
||||
$activity_type = \sanitize_key( $activity_type );
|
||||
$comment_types = self::get_comment_types();
|
||||
|
||||
foreach ( $comment_types as $comment_type ) {
|
||||
if ( in_array( $activity_type, $comment_type['activity_types'], true ) ) {
|
||||
return $comment_type;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered custom comment types.
|
||||
*
|
||||
@ -519,41 +541,63 @@ class Comment {
|
||||
/**
|
||||
* Is this a registered comment type.
|
||||
*
|
||||
* @param string $slug The name of the type.
|
||||
* @param string $slug The slug of the type.
|
||||
*
|
||||
* @return boolean True if registered.
|
||||
*/
|
||||
public static function is_registered_comment_type( $slug ) {
|
||||
$slug = strtolower( $slug );
|
||||
$slug = sanitize_key( $slug );
|
||||
$slug = \strtolower( $slug );
|
||||
$slug = \sanitize_key( $slug );
|
||||
|
||||
return in_array( $slug, array_keys( self::get_comment_types() ), true );
|
||||
$comment_types = self::get_comment_types();
|
||||
|
||||
return isset( $comment_types[ $slug ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered custom comment types names.
|
||||
* Return the registered custom comment type slugs.
|
||||
*
|
||||
* @return array The registered custom comment type names.
|
||||
* @return array The registered custom comment type slugs.
|
||||
*/
|
||||
public static function get_comment_type_slugs() {
|
||||
return array_keys( self::get_comment_types() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the registered custom comment type slugs.
|
||||
*
|
||||
* @deprecated 4.5.0 Use get_comment_type_slugs instead.
|
||||
*
|
||||
* @return array The registered custom comment type slugs.
|
||||
*/
|
||||
public static function get_comment_type_names() {
|
||||
return array_values( wp_list_pluck( self::get_comment_types(), 'type' ) );
|
||||
_deprecated_function( __METHOD__, '4.5.0', 'get_comment_type_slugs' );
|
||||
|
||||
return self::get_comment_type_slugs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a comment type.
|
||||
* Get the custom comment type.
|
||||
*
|
||||
* Check if the type is registered, if not, check if it is a custom type.
|
||||
*
|
||||
* It looks for the array key in the registered types and returns the array.
|
||||
* If it is not found, it looks for the type in the custom types and returns the array.
|
||||
*
|
||||
* @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();
|
||||
$type = strtolower( $type );
|
||||
$type = sanitize_key( $type );
|
||||
|
||||
if ( in_array( $type, array_keys( $types ), true ) ) {
|
||||
$type_array = $types[ $type ];
|
||||
} else {
|
||||
$type_array = array();
|
||||
$comment_types = self::get_comment_types();
|
||||
$type_array = array();
|
||||
|
||||
// Check array keys.
|
||||
if ( in_array( $type, array_keys( $comment_types ), true ) ) {
|
||||
$type_array = $comment_types[ $type ];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -595,30 +639,40 @@ class Comment {
|
||||
*/
|
||||
public static function register_comment_types() {
|
||||
register_comment_type(
|
||||
'announce',
|
||||
'repost',
|
||||
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' ),
|
||||
'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',
|
||||
'collection' => 'reposts',
|
||||
'activity_types' => array( 'announce' ),
|
||||
'excerpt' => html_entity_decode( \__( '… reposted this!', 'activitypub' ) ),
|
||||
/* translators: %d: Number of reposts */
|
||||
'count_single' => _x( '%d repost', 'number of reposts', 'activitypub' ),
|
||||
/* translators: %d: Number of reposts */
|
||||
'count_plural' => _x( '%d reposts', 'number of reposts', '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' ),
|
||||
'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',
|
||||
'collection' => 'likes',
|
||||
'activity_types' => array( 'like' ),
|
||||
'excerpt' => html_entity_decode( \__( '… liked this!', 'activitypub' ) ),
|
||||
/* translators: %d: Number of likes */
|
||||
'count_single' => _x( '%d like', 'number of likes', 'activitypub' ),
|
||||
/* translators: %d: Number of likes */
|
||||
'count_plural' => _x( '%d likes', 'number of likes', 'activitypub' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
@ -631,7 +685,7 @@ class Comment {
|
||||
* @return array show avatars on Activities
|
||||
*/
|
||||
public static function get_avatar_comment_types( $types ) {
|
||||
$comment_types = self::get_comment_type_names();
|
||||
$comment_types = self::get_comment_type_slugs();
|
||||
$types = array_merge( $types, $comment_types );
|
||||
|
||||
return array_unique( $types );
|
||||
@ -651,19 +705,113 @@ class Comment {
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not exclude likes and reposts on ActivityPub requests.
|
||||
if ( defined( 'ACTIVITYPUB_REQUEST' ) && ACTIVITYPUB_REQUEST ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not exclude likes and reposts on REST requests.
|
||||
if ( \wp_is_serving_rest_request() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not exclude likes and reposts on admin pages or on non-singular pages.
|
||||
if ( is_admin() || ! is_singular() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $query->query_vars['type__in'] ) ) {
|
||||
// Do not exclude likes and reposts if the query is for comments.
|
||||
if ( ! empty( $query->query_vars['type__in'] ) || ! empty( $query->query_vars['type'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $query->query_vars['count'] ) && true === $query->query_vars['count'] ) {
|
||||
return;
|
||||
// Exclude likes and reposts by the ActivityPub plugin.
|
||||
$query->query_vars['type__not_in'] = self::get_comment_type_slugs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the comment status before it is set.
|
||||
*
|
||||
* @param string $approved The approved comment status.
|
||||
* @param array $commentdata The comment data.
|
||||
*
|
||||
* @return boolean `true` if the comment is approved, `false` otherwise.
|
||||
*/
|
||||
public static function pre_comment_approved( $approved, $commentdata ) {
|
||||
if ( $approved || \is_wp_error( $approved ) ) {
|
||||
return $approved;
|
||||
}
|
||||
|
||||
// Exclude likes and reposts by the Webmention plugin.
|
||||
$query->query_vars['type__not_in'] = self::get_comment_type_names();
|
||||
if ( '1' !== \get_option( 'comment_previously_approved' ) ) {
|
||||
return $approved;
|
||||
}
|
||||
|
||||
if (
|
||||
empty( $commentdata['comment_meta']['protocol'] ) ||
|
||||
'activitypub' !== $commentdata['comment_meta']['protocol']
|
||||
) {
|
||||
return $approved;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$author = $commentdata['comment_author'];
|
||||
$author_url = $commentdata['comment_author_url'];
|
||||
// phpcs:ignore
|
||||
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_url = %s and comment_approved = '1' LIMIT 1", $author, $author_url ) );
|
||||
|
||||
if ( 1 === (int) $ok_to_comment ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $approved;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update comment counts when interaction settings are disabled.
|
||||
*
|
||||
* Triggers a recount when likes or reposts are disabled to ensure accurate comment counts.
|
||||
*
|
||||
* @param mixed $old_value The old option value.
|
||||
* @param mixed $value The new option value.
|
||||
*/
|
||||
public static function maybe_update_comment_counts( $old_value, $value ) {
|
||||
if ( '1' === $old_value && '1' !== $value ) {
|
||||
Migration::update_comment_counts();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the comment count to exclude ActivityPub comment types.
|
||||
*
|
||||
* @param int|null $new_count The new comment count. Default null.
|
||||
* @param int $old_count The old comment count.
|
||||
* @param int $post_id Post ID.
|
||||
*
|
||||
* @return int|null The updated comment count, or null to use the default query.
|
||||
*/
|
||||
public static function pre_wp_update_comment_count_now( $new_count, $old_count, $post_id ) {
|
||||
if ( null === $new_count ) {
|
||||
$excluded_types = array_filter( self::get_comment_type_slugs(), array( self::class, 'is_comment_type_enabled' ) );
|
||||
|
||||
if ( ! empty( $excluded_types ) ) {
|
||||
global $wpdb;
|
||||
|
||||
// phpcs:ignore WordPress.DB
|
||||
$new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ('" . implode( "','", $excluded_types ) . "')", $post_id ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $new_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a comment type is enabled.
|
||||
*
|
||||
* @param string $comment_type The comment type.
|
||||
* @return bool True if the comment type is enabled.
|
||||
*/
|
||||
public static function is_comment_type_enabled( $comment_type ) {
|
||||
return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user