PluginProbe
ActivityPub / 5.1.0
ActivityPub v5.1.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 / transformer / class-base.php

class-base.php in ActivityPub 5.1.0, at includes/transformer/class-base.php

376 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base Transformer Class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Transformer;
9
10 use WP_Comment;
11 use WP_Post;
12 use WP_Term;
13
14 use Activitypub\Activity\Activity;
15 use Activitypub\Collection\Actors;
16 use Activitypub\Collection\Replies;
17 use Activitypub\Activity\Base_Object;
18
19 /**
20 * WordPress Base Transformer.
21 *
22 * Transformers are responsible for transforming a WordPress objects into different ActivityPub
23 * Object-Types or Activities.
24 */
25 abstract class Base {
26 /**
27 * The WP_Post or WP_Comment object.
28 *
29 * This is the source object of the transformer.
30 *
31 * @var WP_Post|WP_Comment|Base_Object|string|array|WP_Term
32 */
33 protected $item;
34
35 /**
36 * The WP_Post or WP_Comment object.
37 *
38 * @deprecated version 5.0.0
39 *
40 * @var WP_Post|WP_Comment
41 */
42 protected $wp_object;
43
44 /**
45 * The content visibility.
46 *
47 * @var string
48 */
49 protected $content_visibility;
50
51 /**
52 * Static function to Transform a WordPress Object.
53 *
54 * This helps to chain the output of the Transformer.
55 *
56 * @param WP_Post|WP_Comment|Base_Object|string|array|WP_term $item The item that should be transformed.
57 *
58 * @return Base
59 */
60 public static function transform( $item ) {
61 return new static( $item );
62 }
63
64 /**
65 * Base constructor.
66 *
67 * @param WP_Post|WP_Comment|Base_Object|string|array|WP_Term $item The item that should be transformed.
68 */
69 public function __construct( $item ) {
70 $this->item = $item;
71 $this->wp_object = $item;
72 }
73
74 /**
75 * Transform all properties with available get(ter) functions.
76 *
77 * @param Base_Object $activity_object The ActivityPub Object.
78 *
79 * @return Base_Object|\WP_Error The transformed ActivityPub Object.
80 */
81 protected function transform_object_properties( $activity_object ) {
82 if ( ! $activity_object || \is_wp_error( $activity_object ) ) {
83 return $activity_object;
84 }
85
86 $vars = $activity_object->get_object_var_keys();
87
88 foreach ( $vars as $var ) {
89 $getter = 'get_' . $var;
90
91 if ( \method_exists( $this, $getter ) ) {
92 $value = \call_user_func( array( $this, $getter ) );
93
94 if ( isset( $value ) ) {
95 $setter = 'set_' . $var;
96
97 /**
98 * Filter the value before it is set to the Activity-Object `$activity_object`.
99 *
100 * @param mixed $value The value that should be set.
101 * @param mixed $item The Object.
102 */
103 $value = \apply_filters( "activitypub_transform_{$setter}", $value, $this->item );
104
105 /**
106 * Filter the value before it is set to the Activity-Object `$activity_object`.
107 *
108 * @param mixed $value The value that should be set.
109 * @param string $var The variable name.
110 * @param mixed $item The Object.
111 */
112 $value = \apply_filters( 'activitypub_transform_set', $value, $var, $this->item );
113
114 \call_user_func( array( $activity_object, $setter ), $value );
115 }
116 }
117 }
118
119 return $activity_object;
120 }
121
122 /**
123 * Transform the item into an ActivityPub Object.
124 *
125 * @return Base_Object|object The Activity-Object.
126 */
127 public function to_object() {
128 $activity_object = new Base_Object();
129 $activity_object = $this->transform_object_properties( $activity_object );
130
131 if ( \is_wp_error( $activity_object ) ) {
132 return $activity_object;
133 }
134
135 $activity_object = $this->set_audience( $activity_object );
136
137 return $activity_object;
138 }
139
140 /**
141 * Get the content visibility.
142 *
143 * @return string The content visibility.
144 */
145 public function get_content_visibility() {
146 if ( ! $this->content_visibility ) {
147 return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC;
148 }
149
150 return $this->content_visibility;
151 }
152
153 /**
154 * Set the content visibility.
155 *
156 * @param string $content_visibility The content visibility.
157 */
158 public function set_content_visibility( $content_visibility ) {
159 $this->content_visibility = $content_visibility;
160
161 return $this;
162 }
163
164 /**
165 * Set the audience.
166 *
167 * @param Base_Object $activity_object The ActivityPub Object.
168 *
169 * @return Base_Object The ActivityPub Object.
170 */
171 protected function set_audience( $activity_object ) {
172 $public = 'https://www.w3.org/ns/activitystreams#Public';
173 $actor = Actors::get_by_resource( $this->get_attributed_to() );
174 if ( ! $actor || is_wp_error( $actor ) ) {
175 $followers = array();
176 } else {
177 $followers = $actor->get_followers();
178 }
179 $mentions = array_values( $this->get_mentions() );
180
181 switch ( $this->get_content_visibility() ) {
182 case ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC:
183 $activity_object->add_to( $public );
184 $activity_object->add_cc( $followers );
185 $activity_object->add_cc( $mentions );
186 break;
187 case ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC:
188 $activity_object->add_to( $followers );
189 $activity_object->add_to( $mentions );
190 $activity_object->add_cc( $public );
191 break;
192 case ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE:
193 $activity_object->add_to( $mentions );
194 }
195
196 return $activity_object;
197 }
198
199 /**
200 * Transform the item to an ActivityPub ID.
201 *
202 * @return string The ID of the WordPress Object.
203 */
204 public function to_id() {
205 return $this->get_id();
206 }
207
208 /**
209 * Transforms the ActivityPub Object to an Activity
210 *
211 * @param string $type The Activity-Type.
212 *
213 * @return Activity The Activity.
214 */
215 public function to_activity( $type ) {
216 $object = $this->to_object();
217
218 $activity = new Activity();
219 $activity->set_type( $type );
220
221 // Pre-fill the Activity with data (for example cc and to).
222 $activity->set_object( $object );
223
224 // Use simple Object (only ID-URI) for Like and Announce.
225 if ( 'Like' === $type ) {
226 $activity->set_object( $object->get_id() );
227 }
228
229 return $activity;
230 }
231
232 /**
233 * Returns a generic locale based on the Blog settings.
234 *
235 * @return string The locale of the blog.
236 */
237 protected function get_locale() {
238 $lang = \strtolower( \strtok( \get_locale(), '_-' ) );
239
240 /**
241 * Filter the locale of the post.
242 *
243 * @param string $lang The locale of the post.
244 * @param mixed $item The post object.
245 *
246 * @return string The filtered locale of the post.
247 */
248 return apply_filters( 'activitypub_locale', $lang, $this->item );
249 }
250
251 /**
252 * Returns the default media type for an Object.
253 *
254 * @return string The media type.
255 */
256 public function get_media_type() {
257 return 'text/html';
258 }
259
260 /**
261 * Get the replies Collection.
262 *
263 * @return array|null The replies collection on success or null on failure.
264 */
265 public function get_replies() {
266 return Replies::get_collection( $this->item );
267 }
268
269 /**
270 * Returns the content map for the post.
271 *
272 * @return array|null The content map for the post or null if not set.
273 */
274 protected function get_content_map() {
275 if ( ! \method_exists( $this, 'get_content' ) || ! $this->get_content() ) {
276 return null;
277 }
278
279 return array(
280 $this->get_locale() => $this->get_content(),
281 );
282 }
283
284 /**
285 * Returns the name map for the post.
286 *
287 * @return array|null The name map for the post or null if not set.
288 */
289 protected function get_name_map() {
290 if ( ! \method_exists( $this, 'get_name' ) || ! $this->get_name() ) {
291 return null;
292 }
293
294 return array(
295 $this->get_locale() => $this->get_name(),
296 );
297 }
298
299 /**
300 * Returns the summary map for the post.
301 *
302 * @return array|null The summary map for the post or null if not set.
303 */
304 protected function get_summary_map() {
305 if ( ! \method_exists( $this, 'get_summary' ) || ! $this->get_summary() ) {
306 return null;
307 }
308
309 return array(
310 $this->get_locale() => $this->get_summary(),
311 );
312 }
313
314 /**
315 * Returns the tags for the post.
316 *
317 * @return array The tags for the post.
318 */
319 protected function get_tag() {
320 $tags = array();
321 $mentions = $this->get_mentions();
322
323 foreach ( $mentions as $mention => $url ) {
324 $tags[] = array(
325 'type' => 'Mention',
326 'href' => \esc_url( $url ),
327 'name' => \esc_html( $mention ),
328 );
329 }
330
331 return \array_unique( $tags, SORT_REGULAR );
332 }
333
334 /**
335 * Get the attributed to.
336 *
337 * @return string The attributed to.
338 */
339 protected function get_attributed_to() {
340 return null;
341 }
342
343 /**
344 * Extracts mentions from the content.
345 *
346 * @return array The mentions.
347 */
348 protected function get_mentions() {
349 $content = '';
350
351 if ( method_exists( $this, 'get_content' ) ) {
352 $content = $content . ' ' . $this->get_content();
353 }
354
355 if ( method_exists( $this, 'get_summary' ) ) {
356 $content = $content . ' ' . $this->get_summary();
357 }
358
359 /**
360 * Filter the mentions in the post content.
361 *
362 * @param array $mentions The mentions.
363 * @param string $content The post content.
364 * @param WP_Post $post The post object.
365 *
366 * @return array The filtered mentions.
367 */
368 return apply_filters(
369 'activitypub_extract_mentions',
370 array(),
371 $content,
372 $this->item
373 );
374 }
375 }
376