PluginProbe
ActivityPub / 5.9.0
ActivityPub v5.9.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 / model / class-user.php

class-user.php in ActivityPub 5.9.0, at includes/model/class-user.php

474 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User model file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Model;
9
10 use Activitypub\Activity\Actor;
11 use Activitypub\Collection\Extra_Fields;
12 use Activitypub\Signature;
13
14 use function Activitypub\is_blog_public;
15 use function Activitypub\get_rest_url_by_path;
16 use function Activitypub\get_attribution_domains;
17 use function Activitypub\user_can_activitypub;
18
19 /**
20 * User class.
21 *
22 * @method int get__id() Gets the WordPress user ID.
23 */
24 class User extends Actor {
25 /**
26 * The local User-ID (WP_User).
27 *
28 * @var int
29 */
30 protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
31
32 /**
33 * The Featured-Posts.
34 *
35 * @see https://docs.joinmastodon.org/spec/activitypub/#featured
36 *
37 * @context {
38 * "@id": "http://joinmastodon.org/ns#featured",
39 * "@type": "@id"
40 * }
41 *
42 * @var string
43 */
44 protected $featured;
45
46 /**
47 * Whether the User is discoverable.
48 *
49 * @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
50 *
51 * @context http://joinmastodon.org/ns#discoverable
52 *
53 * @var boolean
54 */
55 protected $discoverable = true;
56
57 /**
58 * Whether the User is indexable.
59 *
60 * @context http://joinmastodon.org/ns#indexable
61 *
62 * @var boolean
63 */
64 protected $indexable;
65
66 /**
67 * The WebFinger Resource.
68 *
69 * @var string
70 */
71 protected $webfinger;
72
73 /**
74 * Constructor.
75 *
76 * @param int $user_id Optional. The WordPress user ID. Default null.
77 */
78 public function __construct( $user_id = null ) {
79 if ( $user_id ) {
80 $this->_id = $user_id;
81
82 /**
83 * Fires when a model actor is constructed.
84 *
85 * @param User $this The User object.
86 */
87 \do_action( 'activitypub_construct_model_actor', $this );
88 }
89 }
90
91 /**
92 * The type of the object.
93 *
94 * @return string The type of the object.
95 */
96 public function get_type() {
97 return 'Person';
98 }
99
100 /**
101 * Generate a User object from a WP_User.
102 *
103 * @param int $user_id The user ID.
104 *
105 * @return \WP_Error|User The User object or \WP_Error if user not found.
106 */
107 public static function from_wp_user( $user_id ) {
108 if ( ! user_can_activitypub( $user_id ) ) {
109 return new \WP_Error(
110 'activitypub_user_not_found',
111 \__( 'User not found', 'activitypub' ),
112 array( 'status' => 404 )
113 );
114 }
115
116 return new static( $user_id );
117 }
118
119 /**
120 * Get the user ID.
121 *
122 * @return string The user ID.
123 */
124 public function get_id() {
125 $id = parent::get_id();
126
127 if ( $id ) {
128 return $id;
129 }
130
131 $permalink = \get_user_option( 'activitypub_use_permalink_as_id', $this->_id );
132
133 if ( '1' === $permalink ) {
134 return $this->get_url();
135 }
136
137 return \add_query_arg( 'author', $this->_id, \trailingslashit( \home_url() ) );
138 }
139
140 /**
141 * Get the Username.
142 *
143 * @return string The Username.
144 */
145 public function get_name() {
146 return \get_the_author_meta( 'display_name', $this->_id );
147 }
148
149 /**
150 * Get the User description.
151 *
152 * @return string The User description.
153 */
154 public function get_summary() {
155 $description = get_user_option( 'activitypub_description', $this->_id );
156 if ( empty( $description ) ) {
157 $description = get_user_meta( $this->_id, 'description', true );
158 }
159 return \wpautop( \wp_kses( $description, 'default' ) );
160 }
161
162 /**
163 * Get the User url.
164 *
165 * @return string The User url.
166 */
167 public function get_url() {
168 return \esc_url( \get_author_posts_url( $this->_id ) );
169 }
170
171 /**
172 * Returns the User URL with @-Prefix for the username.
173 *
174 * @return string The User URL with @-Prefix for the username.
175 */
176 public function get_alternate_url() {
177 return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
178 }
179
180 /**
181 * Get the preferred username.
182 *
183 * @return string The preferred username.
184 */
185 public function get_preferred_username() {
186 return \get_the_author_meta( 'login', $this->_id );
187 }
188
189 /**
190 * Get the User icon.
191 *
192 * @return string[] The User icon.
193 */
194 public function get_icon() {
195 $icon = \get_user_option( 'activitypub_icon', $this->_id );
196 if ( false !== $icon && wp_attachment_is_image( $icon ) ) {
197 return array(
198 'type' => 'Image',
199 'url' => esc_url( wp_get_attachment_url( $icon ) ),
200 );
201 }
202
203 $icon = \esc_url(
204 \get_avatar_url(
205 $this->_id,
206 array( 'size' => 120 )
207 )
208 );
209
210 return array(
211 'type' => 'Image',
212 'url' => $icon,
213 );
214 }
215
216 /**
217 * Returns the header image.
218 *
219 * @return string[]|null The header image.
220 */
221 public function get_image() {
222 $header_image = get_user_option( 'activitypub_header_image', $this->_id );
223 $image_url = null;
224
225 if ( ! $header_image && \has_header_image() ) {
226 $image_url = \get_header_image();
227 }
228
229 if ( $header_image ) {
230 $image_url = \wp_get_attachment_url( $header_image );
231 }
232
233 if ( $image_url ) {
234 return array(
235 'type' => 'Image',
236 'url' => esc_url( $image_url ),
237 );
238 }
239
240 return null;
241 }
242
243 /**
244 * Returns the date the user was created.
245 *
246 * @return false|string The date the user was created.
247 */
248 public function get_published() {
249 return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
250 }
251
252 /**
253 * Returns the public key.
254 *
255 * @return string[] The public key.
256 */
257 public function get_public_key() {
258 return array(
259 'id' => $this->get_id() . '#main-key',
260 'owner' => $this->get_id(),
261 'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
262 );
263 }
264
265 /**
266 * Returns the Inbox-API-Endpoint.
267 *
268 * @return string The Inbox-Endpoint.
269 */
270 public function get_inbox() {
271 return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
272 }
273
274 /**
275 * Returns the Outbox-API-Endpoint.
276 *
277 * @return string The Outbox-Endpoint.
278 */
279 public function get_outbox() {
280 return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
281 }
282
283 /**
284 * Returns the Followers-API-Endpoint.
285 *
286 * @return string The Followers-Endpoint.
287 */
288 public function get_followers() {
289 return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
290 }
291
292 /**
293 * Returns the Following-API-Endpoint.
294 *
295 * @return string The Following-Endpoint.
296 */
297 public function get_following() {
298 return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
299 }
300
301 /**
302 * Returns the Featured-API-Endpoint.
303 *
304 * @return string The Featured-Endpoint.
305 */
306 public function get_featured() {
307 return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
308 }
309
310 /**
311 * Returns the endpoints.
312 *
313 * @return string[]|null The endpoints.
314 */
315 public function get_endpoints() {
316 $endpoints = null;
317
318 if ( \get_option( 'activitypub_shared_inbox' ) ) {
319 $endpoints = array(
320 'sharedInbox' => get_rest_url_by_path( 'inbox' ),
321 );
322 }
323
324 return $endpoints;
325 }
326
327 /**
328 * Extend the User-Output with Attachments.
329 *
330 * @return array The extended User-Output.
331 */
332 public function get_attachment() {
333 $extra_fields = Extra_Fields::get_actor_fields( $this->_id );
334 return Extra_Fields::fields_to_attachments( $extra_fields );
335 }
336
337 /**
338 * Returns a user@domain type of identifier for the user.
339 *
340 * @return string The Webfinger-Identifier.
341 */
342 public function get_webfinger() {
343 return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
344 }
345
346 /**
347 * Returns the canonical URL.
348 *
349 * @return string The canonical URL.
350 */
351 public function get_canonical_url() {
352 return $this->get_url();
353 }
354
355 /**
356 * Returns the streams.
357 *
358 * @return null The streams.
359 */
360 public function get_streams() {
361 return null;
362 }
363
364 /**
365 * Returns the tag.
366 *
367 * @return array The tag.
368 */
369 public function get_tag() {
370 return array();
371 }
372
373 /**
374 * Returns the indexable state.
375 *
376 * @return bool Whether the user is indexable.
377 */
378 public function get_indexable() {
379 if ( is_blog_public() ) {
380 return true;
381 } else {
382 return false;
383 }
384 }
385
386 /**
387 * Update the username.
388 *
389 * @param string $value The new value.
390 * @return int|\WP_Error The updated user ID or \WP_Error on failure.
391 */
392 public function update_name( $value ) {
393 $userdata = array(
394 'ID' => $this->_id,
395 'display_name' => $value,
396 );
397 return \wp_update_user( $userdata );
398 }
399
400 /**
401 * Update the User description.
402 *
403 * @param string $value The new value.
404 * @return bool True if the attribute was updated, false otherwise.
405 */
406 public function update_summary( $value ) {
407 return \update_user_option( $this->_id, 'activitypub_description', $value );
408 }
409
410 /**
411 * Update the User icon.
412 *
413 * @param int $value The new value. Should be an attachment ID.
414 * @return bool True if the attribute was updated, false otherwise.
415 */
416 public function update_icon( $value ) {
417 if ( ! wp_attachment_is_image( $value ) ) {
418 return false;
419 }
420 return update_user_option( $this->_id, 'activitypub_icon', $value );
421 }
422
423 /**
424 * Update the User-Header-Image.
425 *
426 * @param int $value The new value. Should be an attachment ID.
427 * @return bool True if the attribute was updated, false otherwise.
428 */
429 public function update_header( $value ) {
430 if ( ! wp_attachment_is_image( $value ) ) {
431 return false;
432 }
433 return \update_user_option( $this->_id, 'activitypub_header_image', $value );
434 }
435
436 /**
437 * Returns the website hosts allowed to credit this blog.
438 *
439 * @return string[]|null The attribution domains or null if not found.
440 */
441 public function get_attribution_domains() {
442 return get_attribution_domains();
443 }
444
445 /**
446 * Returns the alsoKnownAs.
447 *
448 * @return string[] The alsoKnownAs.
449 */
450 public function get_also_known_as() {
451 $also_known_as = array(
452 \add_query_arg( 'author', $this->_id, \home_url( '/' ) ),
453 $this->get_url(),
454 $this->get_alternate_url(),
455 );
456
457 // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
458 $also_known_as = array_merge( $also_known_as, \get_user_option( 'activitypub_also_known_as', $this->_id ) ?: array() );
459
460 return array_unique( $also_known_as );
461 }
462
463 /**
464 * Returns the movedTo.
465 *
466 * @return string The movedTo.
467 */
468 public function get_moved_to() {
469 $moved_to = \get_user_option( 'activitypub_moved_to', $this->_id );
470
471 return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
472 }
473 }
474