PluginProbe
ActivityPub / 2.1.1
ActivityPub v2.1.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 2.1.1, at includes/model/class-user.php

323 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Model;
3
4 use WP_Query;
5 use WP_Error;
6 use Activitypub\Signature;
7 use Activitypub\Collection\Users;
8 use Activitypub\Activity\Actor;
9
10 use function Activitypub\is_user_disabled;
11 use function Activitypub\get_rest_url_by_path;
12
13 class User extends Actor {
14 /**
15 * The local User-ID (WP_User).
16 *
17 * @var int
18 */
19 protected $_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
20
21 /**
22 * The Featured-Posts.
23 *
24 * @see https://docs.joinmastodon.org/spec/activitypub/#featured
25 *
26 * @context {
27 * "@id": "http://joinmastodon.org/ns#featured",
28 * "@type": "@id"
29 * }
30 *
31 * @var string
32 */
33 protected $featured;
34
35 /**
36 * Moderators endpoint.
37 *
38 * @see https://join-lemmy.org/docs/contributors/05-federation.html
39 *
40 * @var string
41 */
42 protected $moderators;
43
44 public function get_type() {
45 return 'Person';
46 }
47
48 /**
49 * If the User is discoverable.
50 *
51 * @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
52 *
53 * @context http://joinmastodon.org/ns#discoverable
54 *
55 * @var boolean
56 */
57 protected $discoverable = true;
58
59 /**
60 * If the User is indexable.
61 *
62 * @context http://joinmastodon.org/ns#indexable
63 *
64 * @var boolean
65 */
66 protected $indexable;
67
68 /**
69 * The WebFinger Resource.
70 *
71 * @var string<url>
72 */
73 protected $webfinger;
74
75 /**
76 * Restrict posting to mods
77 *
78 * @see https://join-lemmy.org/docs/contributors/05-federation.html
79 *
80 * @var boolean
81 */
82 protected $posting_restricted_to_mods = null;
83
84 public static function from_wp_user( $user_id ) {
85 if ( is_user_disabled( $user_id ) ) {
86 return new WP_Error(
87 'activitypub_user_not_found',
88 \__( 'User not found', 'activitypub' ),
89 array( 'status' => 404 )
90 );
91 }
92
93 $object = new static();
94 $object->_id = $user_id;
95
96 return $object;
97 }
98
99 /**
100 * Get the User-ID.
101 *
102 * @return string The User-ID.
103 */
104 public function get_id() {
105 return $this->get_url();
106 }
107
108 /**
109 * Get the User-Name.
110 *
111 * @return string The User-Name.
112 */
113 public function get_name() {
114 return \esc_attr( \get_the_author_meta( 'display_name', $this->_id ) );
115 }
116
117 /**
118 * Get the User-Description.
119 *
120 * @return string The User-Description.
121 */
122 public function get_summary() {
123 $description = get_user_meta( $this->_id, 'activitypub_user_description', true );
124 if ( empty( $description ) ) {
125 $description = get_user_meta( $this->_id, 'description', true );
126 }
127 return \wpautop( \wp_kses( $description, 'default' ) );
128 }
129
130 /**
131 * Get the User-Url.
132 *
133 * @return string The User-Url.
134 */
135 public function get_url() {
136 return \esc_url( \get_author_posts_url( $this->_id ) );
137 }
138
139 /**
140 * Returns the User-URL with @-Prefix for the username.
141 *
142 * @return string The User-URL with @-Prefix for the username.
143 */
144 public function get_alternate_url() {
145 return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
146 }
147
148 public function get_preferred_username() {
149 return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
150 }
151
152 public function get_icon() {
153 $icon = \esc_url(
154 \get_avatar_url(
155 $this->_id,
156 array( 'size' => 120 )
157 )
158 );
159
160 return array(
161 'type' => 'Image',
162 'url' => $icon,
163 );
164 }
165
166 public function get_image() {
167 if ( \has_header_image() ) {
168 $image = \esc_url( \get_header_image() );
169 return array(
170 'type' => 'Image',
171 'url' => $image,
172 );
173 }
174
175 return null;
176 }
177
178 public function get_published() {
179 return \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( \get_the_author_meta( 'registered', $this->_id ) ) );
180 }
181
182 public function get_public_key() {
183 return array(
184 'id' => $this->get_id() . '#main-key',
185 'owner' => $this->get_id(),
186 'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
187 );
188 }
189
190 /**
191 * Returns the Inbox-API-Endpoint.
192 *
193 * @return string The Inbox-Endpoint.
194 */
195 public function get_inbox() {
196 return get_rest_url_by_path( sprintf( 'users/%d/inbox', $this->get__id() ) );
197 }
198
199 /**
200 * Returns the Outbox-API-Endpoint.
201 *
202 * @return string The Outbox-Endpoint.
203 */
204 public function get_outbox() {
205 return get_rest_url_by_path( sprintf( 'users/%d/outbox', $this->get__id() ) );
206 }
207
208 /**
209 * Returns the Followers-API-Endpoint.
210 *
211 * @return string The Followers-Endpoint.
212 */
213 public function get_followers() {
214 return get_rest_url_by_path( sprintf( 'users/%d/followers', $this->get__id() ) );
215 }
216
217 /**
218 * Returns the Following-API-Endpoint.
219 *
220 * @return string The Following-Endpoint.
221 */
222 public function get_following() {
223 return get_rest_url_by_path( sprintf( 'users/%d/following', $this->get__id() ) );
224 }
225
226 /**
227 * Returns the Featured-API-Endpoint.
228 *
229 * @return string The Featured-Endpoint.
230 */
231 public function get_featured() {
232 return get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $this->get__id() ) );
233 }
234
235 public function get_endpoints() {
236 $endpoints = null;
237
238 if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) {
239 $endpoints = array(
240 'sharedInbox' => get_rest_url_by_path( 'inbox' ),
241 );
242 }
243
244 return $endpoints;
245 }
246
247 /**
248 * Extend the User-Output with Attachments.
249 *
250 * @return array The extended User-Output.
251 */
252 public function get_attachment() {
253 $array = array();
254
255 $array[] = array(
256 'type' => 'PropertyValue',
257 'name' => \__( 'Blog', 'activitypub' ),
258 'value' => \html_entity_decode(
259 '<a rel="me" title="' . \esc_attr( \home_url( '/' ) ) . '" target="_blank" href="' . \home_url( '/' ) . '">' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '</a>',
260 \ENT_QUOTES,
261 'UTF-8'
262 ),
263 );
264
265 $array[] = array(
266 'type' => 'PropertyValue',
267 'name' => \__( 'Profile', 'activitypub' ),
268 'value' => \html_entity_decode(
269 '<a rel="me" title="' . \esc_attr( \get_author_posts_url( $this->get__id() ) ) . '" target="_blank" href="' . \get_author_posts_url( $this->get__id() ) . '">' . \wp_parse_url( \get_author_posts_url( $this->get__id() ), \PHP_URL_HOST ) . '</a>',
270 \ENT_QUOTES,
271 'UTF-8'
272 ),
273 );
274
275 if ( \get_the_author_meta( 'user_url', $this->get__id() ) ) {
276 $array[] = array(
277 'type' => 'PropertyValue',
278 'name' => \__( 'Website', 'activitypub' ),
279 'value' => \html_entity_decode(
280 '<a rel="me" title="' . \esc_attr( \get_the_author_meta( 'user_url', $this->get__id() ) ) . '" target="_blank" href="' . \get_the_author_meta( 'user_url', $this->get__id() ) . '">' . \wp_parse_url( \get_the_author_meta( 'user_url', $this->get__id() ), \PHP_URL_HOST ) . '</a>',
281 \ENT_QUOTES,
282 'UTF-8'
283 ),
284 );
285 }
286
287 return $array;
288 }
289
290 /**
291 * Returns a user@domain type of identifier for the user.
292 *
293 * @return string The Webfinger-Identifier.
294 */
295 public function get_webfinger() {
296 return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
297 }
298
299 public function get_resource() {
300 return $this->get_webfinger();
301 }
302
303 public function get_canonical_url() {
304 return $this->get_url();
305 }
306
307 public function get_streams() {
308 return null;
309 }
310
311 public function get_tag() {
312 return array();
313 }
314
315 public function get_indexable() {
316 if ( \get_option( 'blog_public', 1 ) ) {
317 return true;
318 } else {
319 return false;
320 }
321 }
322 }
323