PluginProbe
ActivityPub / 7.8.2
ActivityPub v7.8.2
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/class-mention.php +62 -81 1.0.87.8.2 View file →
@@ -1,114 +1,95 @@
1 1 <?php
2 +/**
3 + * Mention class file.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub;
3 9
4 -use WP_Error;
5 -use Activitypub\Webfinger;
10 +use Activitypub\Collection\Remote_Actors;
6 11
7 12 /**
8 - * ActivityPub Mention Class
13 + * ActivityPub Mention Class.
9 14 *
10 15 * @author Alex Kirk
11 16 */
12 17 class Mention {
13 18 /**
14 - * Initialize the class, registering WordPress hooks
19 + * Initialize the class, registering WordPress hooks.
15 20 */
16 21 public static function init() {
17 - \add_filter( 'the_content', array( self::class, 'the_content' ), 99, 2 );
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' ) );
18 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 );
19 27 }
20 28
21 29 /**
22 - * Filter to replace the mentions in the content with links
30 + * Filter only the activity object and replace summery it with URLs
31 + * add tag to user.
23 32 *
24 - * @param string $the_content the post-content
33 + * @param array $object_array Array of activity.
25 34 *
26 - * @return string the filtered post-content
35 + * @return array The activity object array.
27 36 */
28 - public static function the_content( $the_content ) {
29 - $tag_stack = array();
30 - $protected_tags = array(
31 - 'pre',
32 - 'code',
33 - 'textarea',
34 - 'style',
35 - 'a',
36 - );
37 - $content_with_links = '';
38 - $in_protected_tag = false;
39 - foreach ( wp_html_split( $the_content ) as $chunk ) {
40 - if ( preg_match( '#^<!--[\s\S]*-->$#i', $chunk, $m ) ) {
41 - $content_with_links .= $chunk;
42 - continue;
43 - }
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 + }
44 41
45 - if ( preg_match( '#^<(/)?([a-z-]+)\b[^>]*>$#i', $chunk, $m ) ) {
46 - $tag = strtolower( $m[2] );
47 - if ( '/' === $m[1] ) {
48 - // Closing tag.
49 - $i = array_search( $tag, $tag_stack );
50 - // We can only remove the tag from the stack if it is in the stack.
51 - if ( false !== $i ) {
52 - $tag_stack = array_slice( $tag_stack, 0, $i );
53 - }
54 - } else {
55 - // Opening tag, add it to the stack.
56 - $tag_stack[] = $tag;
57 - }
42 + if ( ! empty( $object_array['content'] ) ) {
43 + $object_array['content'] = self::the_content( $object_array['content'] );
44 + }
58 45
59 - // If we're in a protected tag, the tag_stack contains at least one protected tag string.
60 - // The protected tag state can only change when we encounter a start or end tag.
61 - $in_protected_tag = array_intersect( $tag_stack, $protected_tags );
46 + return $object_array;
47 + }
62 48
63 - // Never inspect tags.
64 - $content_with_links .= $chunk;
65 - continue;
66 - }
67 -
68 - if ( $in_protected_tag ) {
69 - // Don't inspect a chunk inside an inspected tag.
70 - $content_with_links .= $chunk;
71 - continue;
72 - }
73 -
74 - // Only reachable when there is no protected tag in the stack.
75 - $content_with_links .= \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ), $chunk );
76 - }
77 -
78 - return $content_with_links;
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' ) );
79 58 }
80 59
81 60 /**
82 - * A callback for preg_replace to build the user links
61 + * A callback for preg_replace to build the user links.
83 62 *
84 - * @param array $result the preg_match results
63 + * @param array $result The preg_match results.
85 64 *
86 - * @return string the final string
65 + * @return string The final string.
87 66 */
88 67 public static function replace_with_links( $result ) {
89 - $metadata = get_remote_metadata_by_actor( $result[0] );
68 + $post = Remote_Actors::fetch_by_acct( $result[0] );
90 69
91 - if ( ! empty( $metadata ) && ! is_wp_error( $metadata ) && ! empty( $metadata['url'] ) ) {
92 - $username = ltrim( $result[0], '@' );
93 - if ( ! empty( $metadata['name'] ) ) {
94 - $username = $metadata['name'];
95 - }
96 - if ( ! empty( $metadata['preferredUsername'] ) ) {
97 - $username = $metadata['preferredUsername'];
98 - }
99 - return \sprintf( '<a rel="mention" class="u-url mention" href="%s">@<span>%s</span></a>', esc_url( $metadata['url'] ), esc_html( $username ) );
70 + if ( \is_wp_error( $post ) ) {
71 + return $result[0];
100 72 }
101 73
102 - return $result[0];
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 ) );
103 84 }
104 85
105 86 /**
106 - * Get the Inboxes for the mentioned Actors
87 + * Get the Inboxes for the mentioned Actors.
107 88 *
108 - * @param array $mentioned The list of Actors that were mentioned
89 + * @param array $mentioned The list of Actors that were mentioned.
109 90 *
110 - * @return array The list of Inboxes
91 + * @return array The list of Inboxes.
111 92 */
112 93 public static function get_inboxes( $mentioned ) {
113 94 $inboxes = array();
114 95
@@ -123,13 +104,13 @@
123 104 return $inboxes;
124 105 }
125 106
126 107 /**
127 - * Get the inbox from the Remote-Profile of a mentioned Actor
108 + * Get the inbox from the Remote-Profile of a mentioned Actor.
128 109 *
129 - * @param string $actor The Actor-URL
110 + * @param string $actor The Actor URL.
130 111 *
131 - * @return string The Inbox-URL
112 + * @return string|\WP_Error The Inbox-URL or WP_Error if not found.
132 113 */
133 114 public static function get_inbox_by_mentioned_actor( $actor ) {
134 115 $metadata = get_remote_metadata_by_actor( $actor );
135 116
@@ -136,9 +117,9 @@
136 117 if ( \is_wp_error( $metadata ) ) {
137 118 return $metadata;
138 119 }
139 120
140 - if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
121 + if ( isset( $metadata['endpoints']['sharedInbox'] ) ) {
141 122 return $metadata['endpoints']['sharedInbox'];
142 123 }
143 124
144 125 if ( \array_key_exists( 'inbox', $metadata ) ) {
@@ -144,18 +125,18 @@
144 125 if ( \array_key_exists( 'inbox', $metadata ) ) {
145 126 return $metadata['inbox'];
146 127 }
147 128
148 - return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata );
129 + return new \WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata );
149 130 }
150 131
151 132 /**
152 133 * Extract the mentions from the post_content.
153 134 *
154 - * @param array $mentions The already found mentions.
135 + * @param array $mentions The already found mentions.
155 136 * @param string $post_content The post content.
156 137 *
157 - * @return mixed The discovered mentions.
138 + * @return array The discovered mentions.
158 139 */
159 140 public static function extract_mentions( $mentions, $post_content ) {
160 141 \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches );
161 142 foreach ( $matches[0] as $match ) {
@@ -163,7 +144,7 @@
163 144 if ( ! is_wp_error( $link ) ) {
164 145 $mentions[ $match ] = $link;
165 146 }
166 147 }
167 - return $mentions;
148 + return \array_unique( $mentions );
168 149 }
169 150 }