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

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