PluginProbe
ActivityPub / 1.2.0
ActivityPub v1.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 1.2.0, at includes/model/class-user.php

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