| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | -use Activitypub\Collection\Remote_Actors; | |
| 10 | +use WP_Error; | |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * ActivityPub Mention Class. |
| 14 | 14 | * |
| @@ -18,11 +18,11 @@ | ||
| 18 | 18 | /** |
| 19 | 19 | * Initialize the class, registering WordPress hooks. |
| 20 | 20 | */ |
| 21 | 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' ) ); | |
| 22 | + \add_filter( 'the_content', array( self::class, 'the_content' ), 99, 1 ); | |
| 23 | + \add_filter( 'comment_text', array( self::class, 'the_content' ), 10, 1 ); | |
| 24 | + \add_filter( 'activitypub_extra_field_content', array( self::class, 'the_content' ), 10, 1 ); | |
| 25 | 25 | \add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 ); |
| 26 | 26 | \add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); |
| 27 | 27 | } |
| 28 | 28 | |
| @@ -64,24 +64,29 @@ | ||
| 64 | 64 | * |
| 65 | 65 | * @return string The final string. |
| 66 | 66 | */ |
| 67 | 67 | public static function replace_with_links( $result ) { |
| 68 | - $post = Remote_Actors::fetch_by_acct( $result[0] ); | |
| 68 | + $metadata = get_remote_metadata_by_actor( $result[0] ); | |
| 69 | 69 | |
| 70 | - if ( \is_wp_error( $post ) ) { | |
| 71 | - return $result[0]; | |
| 72 | - } | |
| 70 | + if ( | |
| 71 | + ! empty( $metadata ) && | |
| 72 | + ! is_wp_error( $metadata ) && | |
| 73 | + ( ! empty( $metadata['id'] ) || ! empty( $metadata['url'] ) ) | |
| 74 | + ) { | |
| 75 | + $username = ltrim( $result[0], '@' ); | |
| 76 | + if ( ! empty( $metadata['name'] ) ) { | |
| 77 | + $username = $metadata['name']; | |
| 78 | + } | |
| 79 | + if ( ! empty( $metadata['preferredUsername'] ) ) { | |
| 80 | + $username = $metadata['preferredUsername']; | |
| 81 | + } | |
| 73 | 82 | |
| 74 | - $actor = Remote_Actors::get_actor( $post ); | |
| 83 | + $url = isset( $metadata['url'] ) ? object_to_uri( $metadata['url'] ) : object_to_uri( $metadata['id'] ); | |
| 75 | 84 | |
| 76 | - if ( \is_wp_error( $actor ) ) { | |
| 77 | - return $result[0]; | |
| 85 | + return \sprintf( '<a rel="mention" class="u-url mention" href="%1$s">@%2$s</a>', esc_url( $url ), esc_html( $username ) ); | |
| 78 | 86 | } |
| 79 | 87 | |
| 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 ) ); | |
| 88 | + return $result[0]; | |
| 84 | 89 | } |
| 85 | 90 | |
| 86 | 91 | /** |
| 87 | 92 | * Get the Inboxes for the mentioned Actors. |
| @@ -108,9 +113,9 @@ | ||
| 108 | 113 | * Get the inbox from the Remote-Profile of a mentioned Actor. |
| 109 | 114 | * |
| 110 | 115 | * @param string $actor The Actor URL. |
| 111 | 116 | * |
| 112 | - * @return string|\WP_Error The Inbox-URL or WP_Error if not found. | |
| 117 | + * @return string|WP_Error The Inbox-URL or WP_Error if not found. | |
| 113 | 118 | */ |
| 114 | 119 | public static function get_inbox_by_mentioned_actor( $actor ) { |
| 115 | 120 | $metadata = get_remote_metadata_by_actor( $actor ); |
| 116 | 121 | |
| @@ -125,9 +130,9 @@ | ||
| 125 | 130 | if ( \array_key_exists( 'inbox', $metadata ) ) { |
| 126 | 131 | return $metadata['inbox']; |
| 127 | 132 | } |
| 128 | 133 | |
| 129 | - return new \WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); | |
| 134 | + return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata ); | |
| 130 | 135 | } |
| 131 | 136 | |
| 132 | 137 | /** |
| 133 | 138 | * Extract the mentions from the post_content. |