PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
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.3.1, at includes/model/class-user.php

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