PluginProbe
ActivityPub / 8.3.0
ActivityPub v8.3.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 / integration / stream / class-connector.php

class-connector.php in ActivityPub 8.3.0, at integration/stream/class-connector.php

255 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Stream Connector integration file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Integration\Stream;
9
10 use Activitypub\Collection\Actors;
11
12 use function Activitypub\url_to_authorid;
13 use function Activitypub\url_to_commentid;
14
15 /**
16 * Stream Connector for ActivityPub.
17 *
18 * This class is a Stream Connector for the Stream plugin.
19 *
20 * @see https://wordpress.org/plugins/stream/
21 */
22 class Connector extends \WP_Stream\Connector {
23 /**
24 * Connector slug.
25 *
26 * @var string
27 */
28 public $name = 'activitypub';
29
30 /**
31 * Actions registered for this connector.
32 *
33 * @var array
34 */
35 public $actions = array(
36 'activitypub_handled_follow',
37 'activitypub_sent_to_inbox',
38 'activitypub_outbox_processing_complete',
39 'activitypub_outbox_processing_batch_complete',
40 );
41
42 /**
43 * Return translated connector label.
44 *
45 * @return string
46 */
47 public function get_label() {
48 return \__( 'ActivityPub', 'activitypub' );
49 }
50
51 /**
52 * Return translated context labels.
53 *
54 * @return array
55 */
56 public function get_context_labels() {
57 return array();
58 }
59
60 /**
61 * Return translated action labels.
62 *
63 * @return array
64 */
65 public function get_action_labels() {
66 return array(
67 'processed' => \__( 'Processed', 'activitypub' ),
68 );
69 }
70
71 /**
72 * Add action links to Stream drop row in admin list screen
73 *
74 * @filter wp_stream_action_links_{connector}
75 *
76 * @param array $links Previous links registered.
77 * @param Record $record Stream record.
78 *
79 * @return array Action links
80 */
81 public function action_links( $links, $record ) {
82 if ( 'processed' === $record->action ) {
83 $error = \json_decode( $record->get_meta( 'error', true ), true );
84
85 if ( $error ) {
86 $message = \sprintf(
87 '<details><summary>%1$s</summary><pre>%2$s</pre></details>',
88 \__( 'Inbox Error', 'activitypub' ),
89 \wp_json_encode( $error )
90 );
91
92 $links[ $message ] = '';
93 }
94
95 $debug = \json_decode( $record->get_meta( 'debug', true ), true );
96
97 if ( $debug ) {
98 $message = \sprintf(
99 '<details><summary>%1$s</summary><pre>%2$s</pre></details>',
100 \__( 'Debug', 'activitypub' ),
101 \wp_json_encode( $debug )
102 );
103
104 $links[ $message ] = '';
105 }
106 }
107
108 return $links;
109 }
110
111 /**
112 * Callback for activitypub_handled_follow.
113 *
114 * @param array $activity The ActivityPub activity data.
115 * @param int|null $user_id The local user ID, or null if not applicable.
116 * @param mixed $state Status or WP_Error object indicating the result of the follow handling.
117 * @param \WP_Post|null $context The WP_Post object representing the remote actor/follower.
118 */
119 public function callback_activitypub_handled_follow( $activity, $user_id, $state, $context ) {
120 $actor_url = \is_object( $context ) && ! \is_wp_error( $context ) ? $context->guid : $activity['actor'];
121
122 $this->log(
123 \sprintf(
124 // translators: %s is a URL.
125 \__( 'New Follower: %s', 'activitypub' ),
126 $actor_url
127 ),
128 array(
129 'activity' => \wp_json_encode( $activity, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
130 'remote_actor' => \wp_json_encode( $context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
131 ),
132 null,
133 'notification',
134 'follow',
135 $user_id
136 );
137 }
138
139 /**
140 * Callback for activitypub_outbox_processing_complete.
141 *
142 * @param array $inboxes The inboxes.
143 * @param string $json The ActivityPub Activity JSON.
144 * @param int $actor_id The actor ID.
145 * @param int $outbox_item_id The Outbox item ID.
146 */
147 public function callback_activitypub_outbox_processing_complete( $inboxes, $json, $actor_id, $outbox_item_id ) {
148 $outbox_item = \get_post( $outbox_item_id );
149 $outbox_data = $this->prepare_outbox_data_for_response( $outbox_item );
150
151 $this->log(
152 \sprintf(
153 // translators: %s is a URL.
154 \__( 'Outbox processing complete: %s', 'activitypub' ),
155 $outbox_data['title']
156 ),
157 array(
158 'debug' => \wp_json_encode(
159 array(
160 'actor_id' => $actor_id,
161 'outbox_item_id' => $outbox_item_id,
162 )
163 ),
164 ),
165 $outbox_data['id'],
166 $outbox_data['type'],
167 'processed'
168 );
169 }
170
171 /**
172 * Callback for activitypub_outbox_processing_batch_complete.
173 *
174 * @param array $inboxes The inboxes.
175 * @param string $json The ActivityPub Activity JSON.
176 * @param int $actor_id The actor ID.
177 * @param int $outbox_item_id The Outbox item ID.
178 * @param int $batch_size The batch size.
179 * @param int $offset The offset.
180 */
181 public function callback_activitypub_outbox_processing_batch_complete( $inboxes, $json, $actor_id, $outbox_item_id, $batch_size, $offset ) {
182 $outbox_item = \get_post( $outbox_item_id );
183 $outbox_data = $this->prepare_outbox_data_for_response( $outbox_item );
184
185 $this->log(
186 // translators: %s is a URL.
187 \sprintf( \__( 'Outbox processing batch complete: %s', 'activitypub' ), $outbox_data['title'] ),
188 array(
189 'debug' => \wp_json_encode(
190 array(
191 'actor_id' => $actor_id,
192 'outbox_item_id' => $outbox_item_id,
193 'batch_size' => $batch_size,
194 'offset' => $offset,
195 )
196 ),
197 ),
198 $outbox_data['id'],
199 $outbox_data['type'],
200 'processed'
201 );
202 }
203
204 /**
205 * Get the title of the outbox object.
206 *
207 * @param \WP_Post $outbox_item The outbox item.
208 *
209 * @return array The title, object ID, and object type of the outbox object.
210 */
211 protected function prepare_outbox_data_for_response( $outbox_item ) {
212 $object_id = $outbox_item->ID;
213 $object_type = $outbox_item->post_type;
214 $object_title = $outbox_item->post_title;
215
216 $post_id = \url_to_postid( $outbox_item->post_title );
217 if ( $post_id ) {
218 $post = \get_post( $post_id );
219
220 $object_id = $post_id;
221 $object_type = $post->post_type;
222 $object_title = $post->post_title;
223 } else {
224 $comment_id = url_to_commentid( $outbox_item->post_title );
225 if ( $comment_id ) {
226 $comment = \get_comment( $comment_id );
227
228 $object_id = $comment_id;
229 $object_type = 'comments';
230 $object_title = $comment->comment_content;
231 } else {
232 $author_id = url_to_authorid( $outbox_item->post_title );
233 if ( null !== $author_id ) {
234 $object_id = $author_id;
235 $object_type = 'profiles';
236
237 if ( $author_id ) {
238 $object_title = \get_userdata( $author_id )->display_name;
239 } elseif ( Actors::BLOG_USER_ID === $author_id ) {
240 $object_title = \__( 'Blog User', 'activitypub' );
241 } elseif ( Actors::APPLICATION_USER_ID === $author_id ) {
242 $object_title = \__( 'Application User', 'activitypub' );
243 }
244 }
245 }
246 }
247
248 return array(
249 'id' => $object_id,
250 'type' => $object_type,
251 'title' => $object_title,
252 );
253 }
254 }
255