PluginProbe
ActivityPub / 2.6.0
ActivityPub v2.6.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-blog.php

class-blog.php in ActivityPub 2.6.0, at includes/model/class-blog.php

406 lines 7.9 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
7 use Activitypub\Signature;
8 use Activitypub\Activity\Actor;
9 use Activitypub\Collection\Users;
10
11 use function Activitypub\is_single_user;
12 use function Activitypub\is_user_disabled;
13 use function Activitypub\get_rest_url_by_path;
14
15 class Blog extends Actor {
16 /**
17 * The Featured-Posts.
18 *
19 * @see https://docs.joinmastodon.org/spec/activitypub/#featured
20 *
21 * @context {
22 * "@id": "http://joinmastodon.org/ns#featured",
23 * "@type": "@id"
24 * }
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-ID
41 *
42 * @var int
43 */
44 protected $_id = Users::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
45
46 /**
47 * If the User is indexable.
48 *
49 * @context http://joinmastodon.org/ns#indexable
50 *
51 * @var boolean
52 */
53 protected $indexable;
54
55 /**
56 * The WebFinger Resource.
57 *
58 * @var string<url>
59 */
60 protected $webfinger;
61
62 /**
63 * If the User is discoverable.
64 *
65 * @see https://docs.joinmastodon.org/spec/activitypub/#discoverable
66 *
67 * @context http://joinmastodon.org/ns#discoverable
68 *
69 * @var boolean
70 */
71 protected $discoverable;
72
73 /**
74 * Restrict posting to mods
75 *
76 * @see https://join-lemmy.org/docs/contributors/05-federation.html
77 *
78 * @var boolean
79 */
80 protected $posting_restricted_to_mods;
81
82 public function get_manually_approves_followers() {
83 return false;
84 }
85
86 public function get_discoverable() {
87 return true;
88 }
89
90 /**
91 * Get the User-ID.
92 *
93 * @return string The User-ID.
94 */
95 public function get_id() {
96 return $this->get_url();
97 }
98
99 /**
100 * Get the type of the object.
101 *
102 * If the Blog is in "single user" mode, return "Person" insted of "Group".
103 *
104 * @return string The type of the object.
105 */
106 public function get_type() {
107 if ( is_single_user() ) {
108 return 'Person';
109 } else {
110 return 'Group';
111 }
112 }
113
114 /**
115 * Get the User-Name.
116 *
117 * @return string The User-Name.
118 */
119 public function get_name() {
120 return \wp_strip_all_tags(
121 \html_entity_decode(
122 \get_bloginfo( 'name' ),
123 \ENT_QUOTES,
124 'UTF-8'
125 )
126 );
127 }
128
129 /**
130 * Get the User-Description.
131 *
132 * @return string The User-Description.
133 */
134 public function get_summary() {
135 return \wpautop(
136 \wp_kses(
137 \get_bloginfo( 'description' ),
138 'default'
139 )
140 );
141 }
142
143 /**
144 * Get the User-Url.
145 *
146 * @return string The User-Url.
147 */
148 public function get_url() {
149 return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_preferred_username() );
150 }
151
152 /**
153 * Get blog's homepage URL.
154 *
155 * @return string The User-Url.
156 */
157 public function get_alternate_url() {
158 return \esc_url( \trailingslashit( get_home_url() ) );
159 }
160
161 /**
162 * Generate a default Username.
163 *
164 * @return string The auto-generated Username.
165 */
166 public static function get_default_username() {
167 // check if domain host has a subdomain
168 $host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST );
169 $host = \preg_replace( '/^www\./i', '', $host );
170
171 /**
172 * Filter the default blog username.
173 *
174 * @param string $host The default username.
175 */
176 return apply_filters( 'activitypub_default_blog_username', $host );
177 }
178
179 /**
180 * Get the preferred User-Name.
181 *
182 * @return string The User-Name.
183 */
184 public function get_preferred_username() {
185 $username = \get_option( 'activitypub_blog_user_identifier' );
186
187 if ( $username ) {
188 return $username;
189 }
190
191 return self::get_default_username();
192 }
193
194 /**
195 * Get the User-Icon.
196 *
197 * @return array The User-Icon.
198 */
199 public function get_icon() {
200 // try site icon first
201 $icon_id = get_option( 'site_icon' );
202
203 // try custom logo second
204 if ( ! $icon_id ) {
205 $icon_id = get_theme_mod( 'custom_logo' );
206 }
207
208 $icon_url = false;
209
210 if ( $icon_id ) {
211 $icon = wp_get_attachment_image_src( $icon_id, 'full' );
212 if ( $icon ) {
213 $icon_url = $icon[0];
214 }
215 }
216
217 if ( ! $icon_url ) {
218 // fallback to default icon
219 $icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
220 }
221
222 return array(
223 'type' => 'Image',
224 'url' => esc_url( $icon_url ),
225 );
226 }
227
228 /**
229 * Get the User-Header-Image.
230 *
231 * @return array|null The User-Header-Image.
232 */
233 public function get_image() {
234 if ( \has_header_image() ) {
235 return array(
236 'type' => 'Image',
237 'url' => esc_url( \get_header_image() ),
238 );
239 }
240
241 return null;
242 }
243
244 public function get_published() {
245 $first_post = new WP_Query(
246 array(
247 'orderby' => 'date',
248 'order' => 'ASC',
249 'number' => 1,
250 )
251 );
252
253 if ( ! empty( $first_post->posts[0] ) ) {
254 $time = \strtotime( $first_post->posts[0]->post_date_gmt );
255 } else {
256 $time = \time();
257 }
258
259 return \gmdate( 'Y-m-d\TH:i:s\Z', $time );
260 }
261
262 public function get_canonical_url() {
263 return \home_url();
264 }
265
266 public function get_moderators() {
267 if ( is_single_user() || 'Group' !== $this->get_type() ) {
268 return null;
269 }
270
271 return get_rest_url_by_path( 'collections/moderators' );
272 }
273
274 public function get_attributed_to() {
275 if ( is_single_user() || 'Group' !== $this->get_type() ) {
276 return null;
277 }
278
279 return get_rest_url_by_path( 'collections/moderators' );
280 }
281
282 public function get_public_key() {
283 return array(
284 'id' => $this->get_id() . '#main-key',
285 'owner' => $this->get_id(),
286 'publicKeyPem' => Signature::get_public_key_for( $this->get__id() ),
287 );
288 }
289
290 public function get_posting_restricted_to_mods() {
291 if ( 'Group' === $this->get_type() ) {
292 return true;
293 }
294
295 return null;
296 }
297
298 /**
299 * Returns the Inbox-API-Endpoint.
300 *
301 * @return string The Inbox-Endpoint.
302 */
303 public function get_inbox() {
304 return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
305 }
306
307 /**
308 * Returns the Outbox-API-Endpoint.
309 *
310 * @return string The Outbox-Endpoint.
311 */
312 public function get_outbox() {
313 return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
314 }
315
316 /**
317 * Returns the Followers-API-Endpoint.
318 *
319 * @return string The Followers-Endpoint.
320 */
321 public function get_followers() {
322 return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
323 }
324
325 /**
326 * Returns the Following-API-Endpoint.
327 *
328 * @return string The Following-Endpoint.
329 */
330 public function get_following() {
331 return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
332 }
333
334 public function get_endpoints() {
335 $endpoints = null;
336
337 if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) {
338 $endpoints = array(
339 'sharedInbox' => get_rest_url_by_path( 'inbox' ),
340 );
341 }
342
343 return $endpoints;
344 }
345
346 /**
347 * Returns a user@domain type of identifier for the user.
348 *
349 * @return string The Webfinger-Identifier.
350 */
351 public function get_webfinger() {
352 return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
353 }
354
355 /**
356 * Returns the Featured-API-Endpoint.
357 *
358 * @return string The Featured-Endpoint.
359 */
360 public function get_featured() {
361 return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
362 }
363
364 public function get_indexable() {
365 if ( \get_option( 'blog_public', 1 ) ) {
366 return true;
367 } else {
368 return false;
369 }
370 }
371
372 /**
373 * Extend the User-Output with Attachments.
374 *
375 * @return array The extended User-Output.
376 */
377 public function get_attachment() {
378 $array = array();
379
380 $array[] = array(
381 'type' => 'PropertyValue',
382 'name' => \__( 'Blog', 'activitypub' ),
383 'value' => \html_entity_decode(
384 sprintf(
385 '<a rel="me" title="%s" target="_blank" href="%s">%s</a>',
386 \esc_attr( \home_url( '/' ) ),
387 \esc_url( \home_url( '/' ) ),
388 \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST )
389 ),
390 \ENT_QUOTES,
391 'UTF-8'
392 ),
393 );
394
395 // Add support for FEP-fb2a, for more information see FEDERATION.md
396 $array[] = array(
397 'type' => 'Link',
398 'name' => \__( 'Blog', 'activitypub' ),
399 'href' => \esc_url( \home_url( '/' ) ),
400 'rel' => array( 'me' ),
401 );
402
403 return $array;
404 }
405 }
406