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