2023-03-17 22:33:51 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Mention class file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-03-17 22:33:51 +00:00
|
|
|
namespace Activitypub;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
use WP_Error;
|
2024-06-27 12:10:38 +00:00
|
|
|
|
2023-03-17 22:33:51 +00:00
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* ActivityPub Mention Class.
|
2023-03-17 22:33:51 +00:00
|
|
|
*
|
|
|
|
* @author Alex Kirk
|
|
|
|
*/
|
|
|
|
class Mention {
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
2023-03-17 22:33:51 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_filter( 'the_content', array( self::class, 'the_content' ), 99, 1 );
|
|
|
|
\add_filter( 'comment_text', array( self::class, 'the_content' ), 10, 1 );
|
2024-10-09 12:44:17 +00:00
|
|
|
\add_filter( 'activitypub_extra_field_content', array( self::class, 'the_content' ), 10, 1 );
|
2023-10-22 22:20:53 +00:00
|
|
|
\add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 );
|
2024-10-09 12:44:17 +00:00
|
|
|
\add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 );
|
2023-03-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Filter only the activity object and replace summery it with URLs
|
|
|
|
* add tag to user.
|
2023-03-17 22:33:51 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $object_array Array of activity.
|
2023-03-17 22:33:51 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return array The activity object array.
|
2023-03-17 22:33:51 +00:00
|
|
|
*/
|
2024-10-09 12:44:17 +00:00
|
|
|
public static function filter_activity_object( $object_array ) {
|
|
|
|
if ( ! empty( $object_array['summary'] ) ) {
|
|
|
|
$object_array['summary'] = self::the_content( $object_array['summary'] );
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
if ( ! empty( $object_array['content'] ) ) {
|
|
|
|
$object_array['content'] = self::the_content( $object_array['content'] );
|
2023-10-22 22:20:53 +00:00
|
|
|
}
|
2023-03-17 22:33:51 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
return $object_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter to replace the mentions in the content with links.
|
|
|
|
*
|
|
|
|
* @param string $the_content The post content.
|
|
|
|
*
|
|
|
|
* @return string The filtered post-content.
|
|
|
|
*/
|
|
|
|
public static function the_content( $the_content ) {
|
|
|
|
return enrich_content_data( $the_content, '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ) );
|
2023-03-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* A callback for preg_replace to build the user links.
|
2023-03-17 22:33:51 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $result The preg_match results.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string The final string.
|
2023-03-17 22:33:51 +00:00
|
|
|
*/
|
|
|
|
public static function replace_with_links( $result ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
$metadata = get_remote_metadata_by_actor( $result[0] );
|
|
|
|
|
2024-06-27 12:10:38 +00:00
|
|
|
if (
|
|
|
|
! empty( $metadata ) &&
|
|
|
|
! is_wp_error( $metadata ) &&
|
|
|
|
( ! empty( $metadata['id'] ) || ! empty( $metadata['url'] ) )
|
|
|
|
) {
|
2023-03-17 22:33:51 +00:00
|
|
|
$username = ltrim( $result[0], '@' );
|
|
|
|
if ( ! empty( $metadata['name'] ) ) {
|
|
|
|
$username = $metadata['name'];
|
|
|
|
}
|
|
|
|
if ( ! empty( $metadata['preferredUsername'] ) ) {
|
|
|
|
$username = $metadata['preferredUsername'];
|
|
|
|
}
|
2024-02-08 12:31:25 +00:00
|
|
|
|
2024-06-27 12:10:38 +00:00
|
|
|
$url = isset( $metadata['url'] ) ? object_to_uri( $metadata['url'] ) : object_to_uri( $metadata['id'] );
|
2024-02-08 12:31:25 +00:00
|
|
|
|
|
|
|
return \sprintf( '<a rel="mention" class="u-url mention" href="%s">@<span>%s</span></a>', esc_url( $url ), esc_html( $username ) );
|
2023-03-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result[0];
|
|
|
|
}
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the Inboxes for the mentioned Actors.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $mentioned The list of Actors that were mentioned.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return array The list of Inboxes.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function get_inboxes( $mentioned ) {
|
|
|
|
$inboxes = array();
|
|
|
|
|
|
|
|
foreach ( $mentioned as $actor ) {
|
|
|
|
$inbox = self::get_inbox_by_mentioned_actor( $actor );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $inbox ) && $inbox ) {
|
|
|
|
$inboxes[] = $inbox;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $inboxes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Get the inbox from the Remote-Profile of a mentioned Actor.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param string $actor The Actor URL.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return string|WP_Error The Inbox-URL or WP_Error if not found.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
public static function get_inbox_by_mentioned_actor( $actor ) {
|
|
|
|
$metadata = get_remote_metadata_by_actor( $actor );
|
|
|
|
|
|
|
|
if ( \is_wp_error( $metadata ) ) {
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
if ( isset( $metadata['endpoints']['sharedInbox'] ) ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
return $metadata['endpoints']['sharedInbox'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( \array_key_exists( 'inbox', $metadata ) ) {
|
|
|
|
return $metadata['inbox'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata );
|
|
|
|
}
|
|
|
|
|
2023-03-17 22:33:51 +00:00
|
|
|
/**
|
|
|
|
* Extract the mentions from the post_content.
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $mentions The already found mentions.
|
2023-03-17 22:33:51 +00:00
|
|
|
* @param string $post_content The post content.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return array The discovered mentions.
|
2023-03-17 22:33:51 +00:00
|
|
|
*/
|
|
|
|
public static function extract_mentions( $mentions, $post_content ) {
|
|
|
|
\preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches );
|
|
|
|
foreach ( $matches[0] as $match ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
$link = Webfinger::resolve( $match );
|
2023-03-17 22:33:51 +00:00
|
|
|
if ( ! is_wp_error( $link ) ) {
|
|
|
|
$mentions[ $match ] = $link;
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 12:44:17 +00:00
|
|
|
return \array_unique( $mentions );
|
2023-03-17 22:33:51 +00:00
|
|
|
}
|
|
|
|
}
|