| 1 |
<?php |
| 2 |
/** |
| 3 |
* Mention class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub; |
| 9 |
|
| 10 |
use Activitypub\Collection\Remote_Actors; |
| 11 |
|
| 12 |
/** |
| 13 |
* ActivityPub Mention Class. |
| 14 |
* |
| 15 |
* @author Alex Kirk |
| 16 |
*/ |
| 17 |
class Mention { |
| 18 |
/** |
| 19 |
* Initialize the class, registering WordPress hooks. |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
\add_filter( 'the_content', array( self::class, 'the_content' ), 99 ); |
| 23 |
\add_filter( 'comment_text', array( self::class, 'the_content' ) ); |
| 24 |
\add_filter( 'activitypub_extra_field_content', array( self::class, 'the_content' ) ); |
| 25 |
\add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 ); |
| 26 |
\add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Filter only the activity object and replace summery it with URLs |
| 31 |
* add tag to user. |
| 32 |
* |
| 33 |
* @param array $object_array Array of activity. |
| 34 |
* |
| 35 |
* @return array The activity object array. |
| 36 |
*/ |
| 37 |
public static function filter_activity_object( $object_array ) { |
| 38 |
if ( ! empty( $object_array['summary'] ) ) { |
| 39 |
$object_array['summary'] = self::the_content( $object_array['summary'] ); |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! empty( $object_array['content'] ) ) { |
| 43 |
$object_array['content'] = self::the_content( $object_array['content'] ); |
| 44 |
} |
| 45 |
|
| 46 |
return $object_array; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Filter to replace the mentions in the content with links. |
| 51 |
* |
| 52 |
* @param string $the_content The post content. |
| 53 |
* |
| 54 |
* @return string The filtered post-content. |
| 55 |
*/ |
| 56 |
public static function the_content( $the_content ) { |
| 57 |
return enrich_content_data( $the_content, '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* A callback for preg_replace to build the user links. |
| 62 |
* |
| 63 |
* @param array $result The preg_match results. |
| 64 |
* |
| 65 |
* @return string The final string. |
| 66 |
*/ |
| 67 |
public static function replace_with_links( $result ) { |
| 68 |
$post = Remote_Actors::fetch_by_acct( $result[0] ); |
| 69 |
|
| 70 |
if ( \is_wp_error( $post ) ) { |
| 71 |
return $result[0]; |
| 72 |
} |
| 73 |
|
| 74 |
$actor = Remote_Actors::get_actor( $post ); |
| 75 |
|
| 76 |
if ( \is_wp_error( $actor ) ) { |
| 77 |
return $result[0]; |
| 78 |
} |
| 79 |
|
| 80 |
$username = $actor->get_preferred_username() ?: $actor->get_name() ?: Sanitize::webfinger( $result[0] ); |
| 81 |
$url = object_to_uri( $actor->get_url() ?: $actor->get_id() ); |
| 82 |
|
| 83 |
return \sprintf( '<a rel="mention" class="u-url mention" href="%1$s">@%2$s</a>', \esc_url( $url ), \esc_html( $username ) ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the Inboxes for the mentioned Actors. |
| 88 |
* |
| 89 |
* @param array $mentioned The list of Actors that were mentioned. |
| 90 |
* |
| 91 |
* @return array The list of Inboxes. |
| 92 |
*/ |
| 93 |
public static function get_inboxes( $mentioned ) { |
| 94 |
$inboxes = array(); |
| 95 |
|
| 96 |
foreach ( $mentioned as $actor ) { |
| 97 |
$inbox = self::get_inbox_by_mentioned_actor( $actor ); |
| 98 |
|
| 99 |
if ( ! \is_wp_error( $inbox ) && $inbox ) { |
| 100 |
$inboxes[] = $inbox; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return $inboxes; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the inbox from the Remote-Profile of a mentioned Actor. |
| 109 |
* |
| 110 |
* @param string $actor The Actor URL. |
| 111 |
* |
| 112 |
* @return string|\WP_Error The Inbox-URL or WP_Error if not found. |
| 113 |
*/ |
| 114 |
public static function get_inbox_by_mentioned_actor( $actor ) { |
| 115 |
$metadata = get_remote_metadata_by_actor( $actor ); |
| 116 |
|
| 117 |
if ( \is_wp_error( $metadata ) ) { |
| 118 |
return $metadata; |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset( $metadata['endpoints']['sharedInbox'] ) ) { |
| 122 |
return $metadata['endpoints']['sharedInbox']; |
| 123 |
} |
| 124 |
|
| 125 |
if ( \array_key_exists( 'inbox', $metadata ) ) { |
| 126 |
return $metadata['inbox']; |
| 127 |
} |
| 128 |
|
| 129 |
return new \WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Extract the mentions from the post_content. |
| 134 |
* |
| 135 |
* @param array $mentions The already found mentions. |
| 136 |
* @param string $post_content The post content. |
| 137 |
* |
| 138 |
* @return array The discovered mentions. |
| 139 |
*/ |
| 140 |
public static function extract_mentions( $mentions, $post_content ) { |
| 141 |
\preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches ); |
| 142 |
foreach ( $matches[0] as $match ) { |
| 143 |
$link = Webfinger::resolve( $match ); |
| 144 |
if ( ! \is_wp_error( $link ) ) { |
| 145 |
$mentions[ $match ] = $link; |
| 146 |
} |
| 147 |
} |
| 148 |
return \array_unique( $mentions ); |
| 149 |
} |
| 150 |
} |
| 151 |
|