PluginProbe
ActivityPub / 4.2.0
ActivityPub v4.2.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 4.2.0, at includes/model/class-user.php

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