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