-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathclass-blog.php
More file actions
656 lines (571 loc) · 14.8 KB
/
Copy pathclass-blog.php
File metadata and controls
656 lines (571 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
/**
* Blog model file.
*
* @package Activitypub
*/
namespace Activitypub\Model;
use Activitypub\Activity\Actor;
use Activitypub\Collection\Actors;
use Activitypub\Collection\Extra_Fields;
use function Activitypub\esc_hashtag;
use function Activitypub\get_attribution_domains;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\is_blog_public;
use function Activitypub\is_single_user;
/**
* Blog class.
*
* @method int get__id() Gets the internal user ID for the blog (always returns BLOG_USER_ID).
*/
class Blog extends Actor {
/**
* The User-ID
*
* @var int
*/
protected $_id = Actors::BLOG_USER_ID; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
/**
* The generator of the object.
*
* @see https://www.w3.org/TR/activitypub/#generator
* @see https://codeberg.org/fediverse/fep/src/branch/main/fep/844e/fep-844e.md#discovery-through-an-actor
*
* @var array
*/
protected $generator = array(
'type' => 'Application',
'implements' => array(
array(
'href' => 'https://datatracker.ietf.org/doc/html/rfc9421',
'name' => 'RFC-9421: HTTP Message Signatures',
),
),
);
/**
* Constructor.
*/
public function __construct() {
/**
* Fires when a model actor is constructed.
*
* @param Blog $this The Blog model.
*/
\do_action( 'activitypub_construct_model_actor', $this );
}
/**
* Whether the User manually approves followers.
*
* @return false
*/
public function get_manually_approves_followers() {
return false;
}
/**
* Whether the User is discoverable.
*
* @return boolean
*/
public function get_discoverable() {
return true;
}
/**
* Get the User ID.
*
* @return string The User ID.
*/
public function get_id() {
$id = parent::get_id();
if ( $id ) {
return $id;
}
$permalink = \get_option( 'activitypub_use_permalink_as_id_for_blog', false );
if ( $permalink ) {
return \esc_url( \home_url( '/@' . $this->get_preferred_username() ) );
}
return \add_query_arg( 'author', $this->_id, \home_url( '/' ) );
}
/**
* Get the type of the object.
*
* If relay mode is enabled, return "Service".
* If the Blog is in "single user" mode, return "Person" instead of "Group".
*
* @return string The type of the object.
*/
public function get_type() {
if ( \get_option( 'activitypub_relay_mode', false ) ) {
return 'Service';
}
if ( is_single_user() ) {
return 'Person';
} else {
return 'Group';
}
}
/**
* Get the Username.
*
* @return string The Username.
*/
public function get_name() {
return \wp_strip_all_tags(
\html_entity_decode(
\get_bloginfo( 'name' ),
\ENT_QUOTES,
'UTF-8'
)
);
}
/**
* Get the User description.
*
* @return string The User description.
*/
public function get_summary() {
$summary = \get_option( 'activitypub_blog_description', null );
if ( ! $summary ) {
$summary = \get_bloginfo( 'description' );
}
return \wpautop(
\wp_kses(
$summary,
'default'
)
);
}
/**
* Get the User url.
*
* @return string The User url.
*/
public function get_url() {
return \get_bloginfo( 'url' );
}
/**
* Get blog's homepage URL.
*
* @return string The User-Url.
*/
public function get_alternate_url() {
return \esc_url( \trailingslashit( get_home_url() ) );
}
/**
* Generate a default Username.
*
* @return string The auto-generated Username.
*/
public static function get_default_username() {
// Check if domain host has a subdomain.
$host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST );
$host = \preg_replace( '/^www\./i', '', $host );
/**
* Filters the default blog username.
*
* This filter allows developers to modify the default username that is
* generated for the blog, which by default is the site's host name
* without the 'www.' prefix.
*
* @param string $host The default username (site's host name).
*/
return apply_filters( 'activitypub_default_blog_username', $host );
}
/**
* Get the preferred Username.
*
* @return string The Username.
*/
public function get_preferred_username() {
$username = \get_option( 'activitypub_blog_identifier' );
if ( $username ) {
return $username;
}
return self::get_default_username();
}
/**
* Get the User icon.
*
* @return string[] The User icon.
*/
public function get_icon() {
// Try site_logo, falling back to site_icon, first.
$icon_id = get_option( 'site_icon' );
// Try custom logo second.
if ( ! $icon_id ) {
$icon_id = get_theme_mod( 'custom_logo' );
}
$icon_url = false;
if ( $icon_id ) {
$icon = wp_get_attachment_image_src( $icon_id, 'full' );
if ( $icon ) {
$icon_url = $icon[0];
}
}
if ( ! $icon_url ) {
// Fallback to default icon.
$icon_url = plugins_url( '/assets/img/wp-logo.png', ACTIVITYPUB_PLUGIN_FILE );
}
return array(
'type' => 'Image',
'url' => esc_url( $icon_url ),
);
}
/**
* Get the User-Header-Image.
*
* @return string[]|null The User-Header-Image.
*/
public function get_image() {
$header_image = get_option( 'activitypub_header_image' );
$image_url = null;
if ( $header_image ) {
$image_url = \wp_get_attachment_url( $header_image );
}
if ( ! $image_url && \has_header_image() ) {
$image_url = \get_header_image();
}
if ( $image_url ) {
return array(
'type' => 'Image',
'url' => esc_url( $image_url ),
);
}
return null;
}
/**
* Get the published date.
*
* @return string The published date.
*/
public function get_published() {
$published = \get_option( 'activitypub_blog_published' );
if ( $published ) {
return $published;
}
// Backfill from the first federated post.
$first_federated = new \WP_Query(
array(
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_status' => 'publish',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => 'activitypub_status',
'compare' => 'EXISTS',
),
),
)
);
if ( ! empty( $first_federated->posts[0] ) ) {
$time = \strtotime( $first_federated->posts[0]->post_date_gmt );
} else {
$time = \time();
}
$published = \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $time );
\update_option( 'activitypub_blog_published', $published, false );
return $published;
}
/**
* Get the canonical URL.
*
* @return string|null The canonical URL.
*/
public function get_canonical_url() {
return \home_url();
}
/**
* Get the Moderators endpoint.
*
* @return string|null The Moderators endpoint.
*/
public function get_moderators() {
if ( is_single_user() || 'Group' !== $this->get_type() ) {
return null;
}
return get_rest_url_by_path( 'collections/moderators' );
}
/**
* Get attributedTo value.
*
* @return string|null The attributedTo value.
*/
public function get_attributed_to() {
if ( is_single_user() || 'Group' !== $this->get_type() ) {
return null;
}
return get_rest_url_by_path( 'collections/moderators' );
}
/**
* Get the public key information.
*
* @return string[] The public key.
*/
public function get_public_key() {
return array(
'id' => $this->get_id() . '#main-key',
'owner' => $this->get_id(),
'publicKeyPem' => Actors::get_public_key( $this->get__id() ),
);
}
/**
* Returns whether posting is restricted to mods.
*
* @return bool|null True if posting is restricted to mods, null if not applicable.
*/
public function get_posting_restricted_to_mods() {
if ( 'Group' === $this->get_type() ) {
return true;
}
return null;
}
/**
* Returns the Inbox-API-Endpoint.
*
* @return string The Inbox-Endpoint.
*/
public function get_inbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/inbox', $this->get__id() ) );
}
/**
* Returns the Outbox-API-Endpoint.
*
* @return string The Outbox-Endpoint.
*/
public function get_outbox() {
return get_rest_url_by_path( sprintf( 'actors/%d/outbox', $this->get__id() ) );
}
/**
* Returns the Followers-API-Endpoint.
*
* @return string The Followers-Endpoint.
*/
public function get_followers() {
return get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->get__id() ) );
}
/**
* Returns the Following-API-Endpoint.
*
* @return string The Following-Endpoint.
*/
public function get_following() {
return get_rest_url_by_path( sprintf( 'actors/%d/following', $this->get__id() ) );
}
/**
* Returns endpoints.
*
* @return string[]|null The endpoints.
*/
public function get_endpoints() {
$endpoints = array(
'sharedInbox' => get_rest_url_by_path( 'inbox' ),
'oauthAuthorizationEndpoint' => get_rest_url_by_path( 'oauth/authorize' ),
'oauthTokenEndpoint' => get_rest_url_by_path( 'oauth/token' ),
'oauthRegistrationEndpoint' => get_rest_url_by_path( 'oauth/clients' ),
'proxyUrl' => get_rest_url_by_path( 'proxy' ),
'proxyEventStream' => get_rest_url_by_path( 'proxy/stream' ),
);
if ( \get_option( 'activitypub_api', false ) ) {
$endpoints['uploadMedia'] = get_rest_url_by_path( sprintf( 'actors/%d/uploadMedia', $this->get__id() ) );
}
return $endpoints;
}
/**
* Returns a user@domain type of identifier for the user.
*
* @return string The Webfinger-Identifier.
*/
public function get_webfinger() {
return $this->get_preferred_username() . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST );
}
/**
* Returns the Liked API endpoint.
*
* @since 8.1.0
*
* @return string The Liked endpoint.
*/
public function get_liked() {
return get_rest_url_by_path( sprintf( 'actors/%d/liked', $this->get__id() ) );
}
/**
* Returns the Featured-API-Endpoint.
*
* @return string The Featured-Endpoint.
*/
public function get_featured() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $this->get__id() ) );
}
/**
* Returns the Featured-Tags-API-Endpoint.
*
* @return string The Featured-Tags-Endpoint.
*/
public function get_featured_tags() {
return get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $this->get__id() ) );
}
/**
* Returns whether the site is indexable.
*
* @return bool Whether the site is indexable.
*/
public function get_indexable() {
if ( is_blog_public() ) {
return true;
} else {
return false;
}
}
/**
* Update the Username.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_name( $value ) {
return \update_option( 'blogname', $value );
}
/**
* Update the User description.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_summary( $value ) {
return \update_option( 'blogdescription', $value );
}
/**
* Update the User icon.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_icon( $value ) {
if ( ! wp_attachment_is_image( $value ) ) {
return false;
}
return \update_option( 'site_icon', $value );
}
/**
* Update the User-Header-Image.
*
* @param mixed $value The new value.
* @return bool True if the attribute was updated, false otherwise.
*/
public function update_header( $value ) {
if ( ! wp_attachment_is_image( $value ) ) {
return false;
}
return \update_option( 'activitypub_header_image', $value );
}
/**
* Get the User - Hashtags.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#Hashtag
*
* @return string[] The User - Hashtags.
*/
public function get_tag() {
$hashtags = array();
$args = array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 10,
);
$tags = get_tags( $args );
foreach ( $tags as $tag ) {
$hashtags[] = array(
'type' => 'Hashtag',
'href' => \get_tag_link( $tag->term_id ),
'name' => esc_hashtag( $tag->name ),
);
}
return $hashtags;
}
/**
* Extend the User-Output with Attachments.
*
* @return array The extended User-Output.
*/
public function get_attachment() {
$extra_fields = Extra_Fields::get_actor_fields( $this->_id );
return Extra_Fields::fields_to_attachments( $extra_fields );
}
/**
* Returns the website hosts allowed to credit this blog.
*
* @return string[]|null The attribution domains or null if not found.
*/
public function get_attribution_domains() {
return get_attribution_domains();
}
/**
* Returns the alsoKnownAs.
*
* @return string[] The alsoKnownAs.
*/
public function get_also_known_as() {
$also_known_as = array(
\add_query_arg( 'author', $this->_id, \home_url( '/' ) ),
$this->get_url(),
$this->get_alternate_url(),
);
$also_known_as = array_merge( $also_known_as, \get_option( 'activitypub_blog_user_also_known_as', array() ) );
return array_unique( $also_known_as );
}
/**
* Returns the movedTo.
*
* @return string The movedTo.
*/
public function get_moved_to() {
$moved_to = \get_option( 'activitypub_blog_user_moved_to' );
return $moved_to && $moved_to !== $this->get_id() ? $moved_to : null;
}
/**
* Get the actor-level interaction policy.
*
* Overrides the magic property accessor on Base_Object so that we always
* compute the policy from the current site setting rather than returning a
* cached property value. Currently only emits `canFeature` (FEP-7aa9).
* Driven by the site option `activitypub_default_feature_policy` and
* defaults to denying all featured-collection requests, in line with
* FEP-7aa9's "absence of policy = no consent" rule.
*
* @see https://w3id.org/fep/7aa9
*
* @since 9.0.0
*
* @return array
*/
public function get_interaction_policy() {
$policy = array( 'canFeature' => $this->build_can_feature_policy() );
// Merge with an explicitly set interaction policy, if any.
if ( $this->interaction_policy ) {
$policy = \array_merge( (array) $this->interaction_policy, $policy );
}
return $policy;
}
/**
* Build the `canFeature` policy array from the site option.
*
* @return array
*/
protected function build_can_feature_policy() {
$policy = \get_option( 'activitypub_default_feature_policy', ACTIVITYPUB_INTERACTION_POLICY_ME );
switch ( $policy ) {
case ACTIVITYPUB_INTERACTION_POLICY_ANYONE:
return array( 'automaticApproval' => array( 'https://www.w3.org/ns/activitystreams#Public' ) );
case ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS:
return array( 'automaticApproval' => array( $this->get_followers() ) );
case ACTIVITYPUB_INTERACTION_POLICY_ME:
default:
return array( 'automaticApproval' => array( $this->get_id() ) );
}
}
}