PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.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
activitypub / includes / class-mention.php

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

156 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Mention class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use WP_Error;
11
12 /**
13 * ActivityPub Mention Class.
14 *
15 * @author Alex Kirk
16 */
17 class Mention {
18 /**
19 * Initialize the class, registering WordPress hooks.
20 */
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' ) );
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 );
27 }
28
29 /**
30 * Filter only the activity object and replace summery it with URLs
31 * add tag to user.
32 *
33 * @param array $object_array Array of activity.
34 *
35 * @return array The activity object array.
36 */
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 }
41
42 if ( ! empty( $object_array['content'] ) ) {
43 $object_array['content'] = self::the_content( $object_array['content'] );
44 }
45
46 return $object_array;
47 }
48
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' ) );
58 }
59
60 /**
61 * A callback for preg_replace to build the user links.
62 *
63 * @param array $result The preg_match results.
64 *
65 * @return string The final string.
66 */
67 public static function replace_with_links( $result ) {
68 $metadata = get_remote_metadata_by_actor( $result[0] );
69
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 }
82
83 $url = isset( $metadata['url'] ) ? object_to_uri( $metadata['url'] ) : object_to_uri( $metadata['id'] );
84
85 return \sprintf( '<a rel="mention" class="u-url mention" href="%1$s">@%2$s</a>', esc_url( $url ), esc_html( $username ) );
86 }
87
88 return $result[0];
89 }
90
91 /**
92 * Get the Inboxes for the mentioned Actors.
93 *
94 * @param array $mentioned The list of Actors that were mentioned.
95 *
96 * @return array The list of Inboxes.
97 */
98 public static function get_inboxes( $mentioned ) {
99 $inboxes = array();
100
101 foreach ( $mentioned as $actor ) {
102 $inbox = self::get_inbox_by_mentioned_actor( $actor );
103
104 if ( ! is_wp_error( $inbox ) && $inbox ) {
105 $inboxes[] = $inbox;
106 }
107 }
108
109 return $inboxes;
110 }
111
112 /**
113 * Get the inbox from the Remote-Profile of a mentioned Actor.
114 *
115 * @param string $actor The Actor URL.
116 *
117 * @return string|WP_Error The Inbox-URL or WP_Error if not found.
118 */
119 public static function get_inbox_by_mentioned_actor( $actor ) {
120 $metadata = get_remote_metadata_by_actor( $actor );
121
122 if ( \is_wp_error( $metadata ) ) {
123 return $metadata;
124 }
125
126 if ( isset( $metadata['endpoints']['sharedInbox'] ) ) {
127 return $metadata['endpoints']['sharedInbox'];
128 }
129
130 if ( \array_key_exists( 'inbox', $metadata ) ) {
131 return $metadata['inbox'];
132 }
133
134 return new WP_Error( 'activitypub_no_inbox', \__( 'No "Inbox" found', 'activitypub' ), $metadata );
135 }
136
137 /**
138 * Extract the mentions from the post_content.
139 *
140 * @param array $mentions The already found mentions.
141 * @param string $post_content The post content.
142 *
143 * @return array The discovered mentions.
144 */
145 public static function extract_mentions( $mentions, $post_content ) {
146 \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches );
147 foreach ( $matches[0] as $match ) {
148 $link = Webfinger::resolve( $match );
149 if ( ! is_wp_error( $link ) ) {
150 $mentions[ $match ] = $link;
151 }
152 }
153 return \array_unique( $mentions );
154 }
155 }
156