| @@ -3,8 +3,11 @@ | ||
| 3 | 3 | |
| 4 | 4 | use WP_Error; |
| 5 | 5 | use Activitypub\Webfinger; |
| 6 | 6 | |
| 7 | +use function Activitypub\object_to_uri; | |
| 8 | +use function Activitypub\enrich_content_data; | |
| 9 | + | |
| 7 | 10 | /** |
| 8 | 11 | * ActivityPub Mention Class |
| 9 | 12 | * |
| 10 | 13 | * @author Alex Kirk |
| @@ -15,12 +18,34 @@ | ||
| 15 | 18 | */ |
| 16 | 19 | public static function init() { |
| 17 | 20 | \add_filter( 'the_content', array( self::class, 'the_content' ), 99, 1 ); |
| 18 | 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 ); | |
| 19 | 23 | \add_filter( 'activitypub_extract_mentions', array( self::class, 'extract_mentions' ), 99, 2 ); |
| 24 | + \add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); | |
| 20 | 25 | } |
| 21 | 26 | |
| 22 | 27 | /** |
| 28 | + * Filter only the activity object and replace summery it with URLs | |
| 29 | + * add tag to user | |
| 30 | + * | |
| 31 | + * @param $object_array array of activity | |
| 32 | + * | |
| 33 | + * @return array the activity object array | |
| 34 | + */ | |
| 35 | + public static function filter_activity_object( $object_array ) { | |
| 36 | + if ( ! empty( $object_array['summary'] ) ) { | |
| 37 | + $object_array['summary'] = self::the_content( $object_array['summary'] ); | |
| 38 | + } | |
| 39 | + | |
| 40 | + if ( ! empty( $object_array['content'] ) ) { | |
| 41 | + $object_array['content'] = self::the_content( $object_array['content'] ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $object_array; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 23 | 48 | * Filter to replace the mentions in the content with links |
| 24 | 49 | * |
| 25 | 50 | * @param string $the_content the post-content |
| 26 | 51 | * |
| @@ -26,62 +51,9 @@ | ||
| 26 | 51 | * |
| 27 | 52 | * @return string the filtered post-content |
| 28 | 53 | */ |
| 29 | 54 | public static function the_content( $the_content ) { |
| 30 | - // small protection against execution timeouts: limit to 1 MB | |
| 31 | - if ( mb_strlen( $the_content ) > MB_IN_BYTES ) { | |
| 32 | - return $the_content; | |
| 33 | - } | |
| 34 | - $tag_stack = array(); | |
| 35 | - $protected_tags = array( | |
| 36 | - 'pre', | |
| 37 | - 'code', | |
| 38 | - 'textarea', | |
| 39 | - 'style', | |
| 40 | - 'a', | |
| 41 | - ); | |
| 42 | - $content_with_links = ''; | |
| 43 | - $in_protected_tag = false; | |
| 44 | - foreach ( wp_html_split( $the_content ) as $chunk ) { | |
| 45 | - if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) { | |
| 46 | - $content_with_links .= $chunk; | |
| 47 | - continue; | |
| 48 | - } | |
| 49 | - | |
| 50 | - if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) { | |
| 51 | - $tag = strtolower( $m[2] ); | |
| 52 | - if ( '/' === $m[1] ) { | |
| 53 | - // Closing tag. | |
| 54 | - $i = array_search( $tag, $tag_stack ); | |
| 55 | - // We can only remove the tag from the stack if it is in the stack. | |
| 56 | - if ( false !== $i ) { | |
| 57 | - $tag_stack = array_slice( $tag_stack, 0, $i ); | |
| 58 | - } | |
| 59 | - } else { | |
| 60 | - // Opening tag, add it to the stack. | |
| 61 | - $tag_stack[] = $tag; | |
| 62 | - } | |
| 63 | - | |
| 64 | - // If we're in a protected tag, the tag_stack contains at least one protected tag string. | |
| 65 | - // The protected tag state can only change when we encounter a start or end tag. | |
| 66 | - $in_protected_tag = array_intersect( $tag_stack, $protected_tags ); | |
| 67 | - | |
| 68 | - // Never inspect tags. | |
| 69 | - $content_with_links .= $chunk; | |
| 70 | - continue; | |
| 71 | - } | |
| 72 | - | |
| 73 | - if ( $in_protected_tag ) { | |
| 74 | - // Don't inspect a chunk inside an inspected tag. | |
| 75 | - $content_with_links .= $chunk; | |
| 76 | - continue; | |
| 77 | - } | |
| 78 | - | |
| 79 | - // Only reachable when there is no protected tag in the stack. | |
| 80 | - $content_with_links .= \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ), $chunk ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - return $content_with_links; | |
| 55 | + return enrich_content_data( $the_content, '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ) ); | |
| 84 | 56 | } |
| 85 | 57 | |
| 86 | 58 | /** |
| 87 | 59 | * A callback for preg_replace to build the user links |
| @@ -92,9 +64,13 @@ | ||
| 92 | 64 | */ |
| 93 | 65 | public static function replace_with_links( $result ) { |
| 94 | 66 | $metadata = get_remote_metadata_by_actor( $result[0] ); |
| 95 | 67 | |
| 96 | - if ( ! empty( $metadata ) && ! is_wp_error( $metadata ) && ! empty( $metadata['url'] ) ) { | |
| 68 | + if ( | |
| 69 | + ! empty( $metadata ) && | |
| 70 | + ! is_wp_error( $metadata ) && | |
| 71 | + ( ! empty( $metadata['id'] ) || ! empty( $metadata['url'] ) ) | |
| 72 | + ) { | |
| 97 | 73 | $username = ltrim( $result[0], '@' ); |
| 98 | 74 | if ( ! empty( $metadata['name'] ) ) { |
| 99 | 75 | $username = $metadata['name']; |
| 100 | 76 | } |
| @@ -100,9 +76,12 @@ | ||
| 100 | 76 | } |
| 101 | 77 | if ( ! empty( $metadata['preferredUsername'] ) ) { |
| 102 | 78 | $username = $metadata['preferredUsername']; |
| 103 | 79 | } |
| 104 | - return \sprintf( '<a rel="mention" class="u-url mention" href="%s">@<span>%s</span></a>', esc_url( $metadata['url'] ), esc_html( $username ) ); | |
| 80 | + | |
| 81 | + $url = isset( $metadata['url'] ) ? object_to_uri( $metadata['url'] ) : object_to_uri( $metadata['id'] ); | |
| 82 | + | |
| 83 | + return \sprintf( '<a rel="mention" class="u-url mention" href="%s">@<span>%s</span></a>', esc_url( $url ), esc_html( $username ) ); | |
| 105 | 84 | } |
| 106 | 85 | |
| 107 | 86 | return $result[0]; |
| 108 | 87 | } |
| @@ -168,7 +147,7 @@ | ||
| 168 | 147 | if ( ! is_wp_error( $link ) ) { |
| 169 | 148 | $mentions[ $match ] = $link; |
| 170 | 149 | } |
| 171 | 150 | } |
| 172 | - return $mentions; | |
| 151 | + return \array_unique( $mentions ); | |
| 173 | 152 | } |
| 174 | 153 | } |