PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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
activitypub / includes / class-mention.php

class-mention.php in ActivityPub 3.2.4, at includes/class-mention.php

154 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 use WP_Error;
5 use Activitypub\Webfinger;
6
7 use function Activitypub\object_to_uri;
8 use function Activitypub\enrich_content_data;
9
10 /**
11 * ActivityPub Mention Class
12 *
13 * @author Alex Kirk
14 */
15 class Mention {
16 /**
17 * Initialize the class, registering WordPress hooks
18 */
19 public static function init() {
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 );
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 );
25 }
26
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 /**
48 * Filter to replace the mentions in the content with links
49 *
50 * @param string $the_content the post-content
51 *
52 * @return string the filtered post-content
53 */
54 public static function the_content( $the_content ) {
55 return enrich_content_data( $the_content, '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( self::class, 'replace_with_links' ) );
56 }
57
58 /**
59 * A callback for preg_replace to build the user links
60 *
61 * @param array $result the preg_match results
62 *
63 * @return string the final string
64 */
65 public static function replace_with_links( $result ) {
66 $metadata = get_remote_metadata_by_actor( $result[0] );
67
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 }
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 ) );
84 }
85
86 return $result[0];
87 }
88
89 /**
90 * Get the Inboxes for the mentioned Actors
91 *
92 * @param array $mentioned The list of Actors that were mentioned
93 *
94 * @return array The list of Inboxes
95 */
96 public static function get_inboxes( $mentioned ) {
97 $inboxes = array();
98
99 foreach ( $mentioned as $actor ) {
100 $inbox = self::get_inbox_by_mentioned_actor( $actor );
101
102 if ( ! is_wp_error( $inbox ) && $inbox ) {
103 $inboxes[] = $inbox;
104 }
105 }
106
107 return $inboxes;
108 }
109
110 /**
111 * Get the inbox from the Remote-Profile of a mentioned Actor
112 *
113 * @param string $actor The Actor-URL
114 *
115 * @return string The Inbox-URL
116 */
117 public static function get_inbox_by_mentioned_actor( $actor ) {
118 $metadata = get_remote_metadata_by_actor( $actor );
119
120 if ( \is_wp_error( $metadata ) ) {
121 return $metadata;
122 }
123
124 if ( isset( $metadata['endpoints'] ) && isset( $metadata['endpoints']['sharedInbox'] ) ) {
125 return $metadata['endpoints']['sharedInbox'];
126 }
127
128 if ( \array_key_exists( 'inbox', $metadata ) ) {
129 return $metadata['inbox'];
130 }
131
132 return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata );
133 }
134
135 /**
136 * Extract the mentions from the post_content.
137 *
138 * @param array $mentions The already found mentions.
139 * @param string $post_content The post content.
140 *
141 * @return mixed The discovered mentions.
142 */
143 public static function extract_mentions( $mentions, $post_content ) {
144 \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches );
145 foreach ( $matches[0] as $match ) {
146 $link = Webfinger::resolve( $match );
147 if ( ! is_wp_error( $link ) ) {
148 $mentions[ $match ] = $link;
149 }
150 }
151 return \array_unique( $mentions );
152 }
153 }
154