PluginProbe
ActivityPub / 8.0.0
ActivityPub v8.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 / class-options.php

class-options.php in ActivityPub 8.0.0, at includes/class-options.php

671 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Options file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use Activitypub\Model\Blog;
11
12 /**
13 * Options class.
14 */
15 class Options {
16
17 /**
18 * Initialize the options.
19 */
20 public static function init() {
21 \add_action( 'admin_init', array( self::class, 'register_settings' ) );
22 \add_action( 'rest_api_init', array( self::class, 'register_settings' ) );
23
24 \add_filter( 'pre_option_activitypub_actor_mode', array( self::class, 'pre_option_activitypub_actor_mode' ) );
25 \add_filter( 'pre_option_activitypub_authorized_fetch', array( self::class, 'pre_option_activitypub_authorized_fetch' ) );
26 \add_filter( 'pre_option_activitypub_vary_header', array( self::class, 'pre_option_activitypub_vary_header' ) );
27 \add_filter( 'pre_option_activitypub_following_ui', array( self::class, 'pre_option_activitypub_following_ui' ) );
28 \add_filter( 'pre_option_activitypub_create_posts', array( self::class, 'pre_option_activitypub_create_posts' ) );
29
30 \add_filter( 'pre_option_activitypub_allow_likes', array( self::class, 'maybe_disable_interactions' ) );
31 \add_filter( 'pre_option_activitypub_allow_replies', array( self::class, 'maybe_disable_interactions' ) );
32
33 \add_filter( 'default_option_activitypub_negotiate_content', array( self::class, 'default_option_activitypub_negotiate_content' ) );
34 \add_filter( 'option_activitypub_max_image_attachments', array( self::class, 'default_max_image_attachments' ) );
35 \add_filter( 'option_activitypub_support_post_types', array( self::class, 'support_post_types_ensure_array' ) );
36 \add_filter( 'option_activitypub_object_type', array( self::class, 'default_object_type' ) );
37
38 \add_action( 'update_option_activitypub_relay_mode', array( self::class, 'relay_mode_changed' ), 10, 2 );
39 }
40
41 /**
42 * Register ActivityPub settings.
43 */
44 public static function register_settings() {
45 /*
46 * Options Group: activitypub
47 */
48 \register_setting(
49 'activitypub',
50 'activitypub_post_content_type',
51 array(
52 'type' => 'string',
53 'description' => 'Use title and link, summary, full or custom content',
54 'show_in_rest' => array(
55 'schema' => array(
56 'enum' => array( 'title', 'excerpt', 'content' ),
57 ),
58 ),
59 'default' => 'content',
60 )
61 );
62
63 \register_setting(
64 'activitypub',
65 'activitypub_custom_post_content',
66 array(
67 'type' => 'string',
68 'description' => 'Define your own custom post template',
69 'show_in_rest' => true,
70 'default' => ACTIVITYPUB_CUSTOM_POST_CONTENT,
71 )
72 );
73
74 \register_setting(
75 'activitypub',
76 'activitypub_max_image_attachments',
77 array(
78 'type' => 'integer',
79 'description' => 'Number of images to attach to posts.',
80 'default' => ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS,
81 'sanitize_callback' => static function ( $value ) {
82 return \is_numeric( $value ) ? \absint( $value ) : ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS;
83 },
84 )
85 );
86
87 \register_setting(
88 'activitypub',
89 'activitypub_use_hashtags',
90 array(
91 'type' => 'boolean',
92 'description' => 'Add hashtags in the content as native tags and replace the #tag with the tag-link',
93 'default' => '0',
94 )
95 );
96
97 \register_setting(
98 'activitypub',
99 'activitypub_use_opengraph',
100 array(
101 'type' => 'boolean',
102 'description' => 'Automatically add "fediverse:creator" OpenGraph tags for Authors and the Blog-User.',
103 'default' => '1',
104 )
105 );
106
107 \register_setting(
108 'activitypub',
109 'activitypub_support_post_types',
110 array(
111 'type' => 'string',
112 'description' => 'Enable ActivityPub support for post types',
113 'show_in_rest' => true,
114 'default' => array( 'post' ),
115 )
116 );
117
118 \register_setting(
119 'activitypub',
120 'activitypub_actor_mode',
121 array(
122 'type' => 'string',
123 'description' => 'Choose your preferred Actor-Mode.',
124 'default' => ACTIVITYPUB_ACTOR_MODE,
125 'show_in_rest' => array(
126 'schema' => array(
127 'type' => 'string',
128 'enum' => array(
129 ACTIVITYPUB_ACTOR_MODE,
130 ACTIVITYPUB_BLOG_MODE,
131 ACTIVITYPUB_ACTOR_AND_BLOG_MODE,
132 ),
133 ),
134 ),
135 )
136 );
137
138 \register_setting(
139 'activitypub',
140 'activitypub_attribution_domains',
141 array(
142 'type' => 'string',
143 'description' => 'Websites allowed to credit you.',
144 'default' => home_host(),
145 'sanitize_callback' => array( Sanitize::class, 'host_list' ),
146 )
147 );
148
149 \register_setting(
150 'activitypub',
151 'activitypub_allow_likes',
152 array(
153 'type' => 'integer',
154 'description' => 'Allow likes.',
155 'default' => '1',
156 'sanitize_callback' => 'absint',
157 )
158 );
159
160 \register_setting(
161 'activitypub',
162 'activitypub_allow_reposts',
163 array(
164 'type' => 'integer',
165 'description' => 'Allow reposts.',
166 'default' => '1',
167 'sanitize_callback' => 'absint',
168 )
169 );
170
171 \register_setting(
172 'activitypub',
173 'activitypub_auto_approve_reactions',
174 array(
175 'type' => 'integer',
176 'description' => 'Auto-approve Reactions.',
177 'default' => '0',
178 'sanitize_callback' => 'absint',
179 )
180 );
181
182 \register_setting(
183 'activitypub',
184 'activitypub_default_quote_policy',
185 array(
186 'type' => 'string',
187 'description' => 'Default quote policy for new posts.',
188 'default' => ACTIVITYPUB_INTERACTION_POLICY_ANYONE,
189 'sanitize_callback' => static function ( $value ) {
190 $allowed = array(
191 ACTIVITYPUB_INTERACTION_POLICY_ANYONE,
192 ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS,
193 ACTIVITYPUB_INTERACTION_POLICY_ME,
194 );
195 return \in_array( $value, $allowed, true ) ? $value : ACTIVITYPUB_INTERACTION_POLICY_ANYONE;
196 },
197 )
198 );
199
200 \register_setting(
201 'activitypub',
202 'activitypub_relays',
203 array(
204 'type' => 'array',
205 'description' => 'Relays',
206 'default' => array(),
207 'sanitize_callback' => array( Sanitize::class, 'url_list' ),
208 )
209 );
210
211 \register_setting(
212 'activitypub',
213 'activitypub_site_blocked_actors',
214 array(
215 'type' => 'array',
216 'description' => 'Site-wide blocked ActivityPub actors.',
217 'default' => array(),
218 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
219 )
220 );
221
222 /*
223 * Options Group: activitypub_advanced
224 */
225 \register_setting(
226 'activitypub_advanced',
227 'activitypub_outbox_purge_days',
228 array(
229 'type' => 'integer',
230 'description' => 'Number of days to keep items in the Outbox.',
231 'default' => 180,
232 )
233 );
234
235 \register_setting(
236 'activitypub_advanced',
237 'activitypub_inbox_purge_days',
238 array(
239 'type' => 'integer',
240 'description' => 'Number of days to keep items in the Inbox.',
241 'default' => 180,
242 )
243 );
244
245 \register_setting(
246 'activitypub_advanced',
247 'activitypub_ap_post_purge_days',
248 array(
249 'type' => 'integer',
250 'description' => 'Number of days to keep remote posts.',
251 'default' => 30,
252 )
253 );
254
255 \register_setting(
256 'activitypub_advanced',
257 'activitypub_vary_header',
258 array(
259 'type' => 'boolean',
260 'description' => 'Add the Vary header to the ActivityPub response.',
261 'default' => true,
262 )
263 );
264
265 \register_setting(
266 'activitypub_advanced',
267 'activitypub_content_negotiation',
268 array(
269 'type' => 'boolean',
270 'description' => 'Enable content negotiation.',
271 'default' => true,
272 )
273 );
274
275 \register_setting(
276 'activitypub_advanced',
277 'activitypub_authorized_fetch',
278 array(
279 'type' => 'boolean',
280 'description' => 'Require HTTP signature authentication.',
281 'default' => false,
282 )
283 );
284
285 \register_setting(
286 'activitypub_advanced',
287 'activitypub_rfc9421_signature',
288 array(
289 'type' => 'boolean',
290 'description' => 'Use RFC-9421 signature.',
291 'default' => false,
292 )
293 );
294
295 \register_setting(
296 'activitypub_advanced',
297 'activitypub_following_ui',
298 array(
299 'type' => 'boolean',
300 'description' => 'Show Following UI in admin menus and settings.',
301 'default' => false,
302 )
303 );
304
305 \register_setting(
306 'activitypub_advanced',
307 'activitypub_reader_ui',
308 array(
309 'type' => 'boolean',
310 'description' => 'Enable the Reader to view posts from accounts you follow.',
311 'default' => false,
312 )
313 );
314
315 \register_setting(
316 'activitypub_advanced',
317 'activitypub_create_posts',
318 array(
319 'type' => 'boolean',
320 'description' => 'Allow creating posts via ActivityPub.',
321 'default' => false,
322 )
323 );
324
325 \register_setting(
326 'activitypub_advanced',
327 'activitypub_object_type',
328 array(
329 'type' => 'string',
330 'description' => 'The Activity-Object-Type',
331 'show_in_rest' => array(
332 'schema' => array(
333 'enum' => array( 'note', 'wordpress-post-format' ),
334 ),
335 ),
336 'default' => ACTIVITYPUB_DEFAULT_OBJECT_TYPE,
337 )
338 );
339
340 \register_setting(
341 'activitypub_advanced',
342 'activitypub_relay_mode',
343 array(
344 'type' => 'integer',
345 'description' => 'Enable relay mode to forward public activities to all followers.',
346 'default' => 0,
347 'sanitize_callback' => 'absint',
348 )
349 );
350
351 /*
352 * Options Group: activitypub_blog
353 */
354 \register_setting(
355 'activitypub_blog',
356 'activitypub_blog_description',
357 array(
358 'type' => 'string',
359 'description' => 'The Description of the Blog-User',
360 'show_in_rest' => true,
361 'default' => '',
362 )
363 );
364
365 \register_setting(
366 'activitypub_blog',
367 'activitypub_blog_identifier',
368 array(
369 'type' => 'string',
370 'description' => 'The Identifier of the Blog-User',
371 'show_in_rest' => true,
372 'default' => Blog::get_default_username(),
373 'sanitize_callback' => array( Sanitize::class, 'blog_identifier' ),
374 )
375 );
376
377 \register_setting(
378 'activitypub_blog',
379 'activitypub_header_image',
380 array(
381 'type' => 'integer',
382 'description' => 'The Attachment-ID of the Sites Header-Image',
383 'default' => null,
384 )
385 );
386
387 \register_setting(
388 'activitypub_blog',
389 'activitypub_blog_user_mailer_new_dm',
390 array(
391 'type' => 'integer',
392 'description' => 'Send a notification when someone sends a user of the blog a direct message.',
393 'default' => 1,
394 )
395 );
396
397 \register_setting(
398 'activitypub_blog',
399 'activitypub_blog_user_mailer_new_follower',
400 array(
401 'type' => 'integer',
402 'description' => 'Send a notification when someone starts to follow a user of the blog.',
403 'default' => 1,
404 )
405 );
406
407 \register_setting(
408 'activitypub_blog',
409 'activitypub_blog_user_mailer_new_mention',
410 array(
411 'type' => 'integer',
412 'description' => 'Send a notification when someone mentions a user of the blog.',
413 'default' => 1,
414 )
415 );
416
417 \register_setting(
418 'activitypub_blog',
419 'activitypub_blog_user_also_known_as',
420 array(
421 'type' => 'array',
422 'description' => 'An array of URLs that the blog user is known by.',
423 'default' => array(),
424 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
425 )
426 );
427
428 \register_setting(
429 'activitypub_blog',
430 'activitypub_hide_social_graph',
431 array(
432 'type' => 'integer',
433 'description' => 'Hide Followers and Followings on Profile.',
434 'default' => 0,
435 'sanitize_callback' => 'absint',
436 'show_in_rest' => true,
437 )
438 );
439 }
440
441 /**
442 * Delete all options.
443 */
444 public static function delete() {
445 global $wpdb;
446
447 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
448 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'activitypub_%'" );
449 }
450
451 /**
452 * Pre-get option filter for the Actor-Mode.
453 *
454 * @param string|false $pre The pre-get option value.
455 *
456 * @return string|false The actor mode or false if it should not be filtered.
457 */
458 public static function pre_option_activitypub_actor_mode( $pre ) {
459 if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) && ACTIVITYPUB_SINGLE_USER_MODE ) {
460 return ACTIVITYPUB_BLOG_MODE;
461 }
462
463 if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) && ACTIVITYPUB_DISABLE_USER ) {
464 return ACTIVITYPUB_BLOG_MODE;
465 }
466
467 if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) && ACTIVITYPUB_DISABLE_BLOG_USER ) {
468 return ACTIVITYPUB_ACTOR_MODE;
469 }
470
471 return $pre;
472 }
473
474 /**
475 * Pre-get option filter for the Authorized Fetch.
476 *
477 * @param string $pre The pre-get option value.
478 *
479 * @return string If the constant is defined, return the value, otherwise return the pre-get option value.
480 */
481 public static function pre_option_activitypub_authorized_fetch( $pre ) {
482 if ( ! \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
483 return $pre;
484 }
485
486 if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
487 return '1';
488 }
489
490 return '0';
491 }
492
493 /**
494 * Pre-get option filter for the Vary Header.
495 *
496 * @param string $pre The pre-get option value.
497 *
498 * @return string If the constant is defined, return the value, otherwise return the pre-get option value.
499 */
500 public static function pre_option_activitypub_vary_header( $pre ) {
501 if ( ! \defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) ) {
502 return $pre;
503 }
504
505 if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
506 return '1';
507 }
508
509 return '0';
510 }
511
512 /**
513 * Pre-get option filter for the Following UI.
514 *
515 * Forces the Following UI to be enabled when the Reader is enabled.
516 *
517 * @param string $pre The pre-get option value.
518 *
519 * @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
520 */
521 public static function pre_option_activitypub_following_ui( $pre ) {
522 /*
523 * Bypass the filter to get the actual stored value for activitypub_reader_ui.
524 * This avoids infinite loops if activitypub_reader_ui also had a pre_option filter.
525 */
526 if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
527 return '1';
528 }
529
530 return $pre;
531 }
532
533 /**
534 * Pre-get option filter for the Create Posts setting.
535 *
536 * Forces the Create Posts setting to be enabled when the Reader is enabled.
537 *
538 * @param string $pre The pre-get option value.
539 *
540 * @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
541 */
542 public static function pre_option_activitypub_create_posts( $pre ) {
543 if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
544 return '1';
545 }
546
547 return $pre;
548 }
549
550 /**
551 * Disallow interactions if the constant is set.
552 *
553 * @param bool $pre The value of the option.
554 *
555 * @return bool|string The value of the option.
556 */
557 public static function maybe_disable_interactions( $pre ) {
558 if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
559 return '0';
560 }
561
562 return $pre;
563 }
564
565 /**
566 * Default option filter for the Content-Negotiation.
567 *
568 * @see https://github.com/Automattic/wordpress-activitypub/wiki/Caching
569 *
570 * @param string $default_value The default value of the option.
571 *
572 * @return string The default value of the option.
573 */
574 public static function default_option_activitypub_negotiate_content( $default_value ) {
575 $disable_for_plugins = array(
576 'wp-optimize/wp-optimize.php',
577 'wp-rocket/wp-rocket.php',
578 'w3-total-cache/w3-total-cache.php',
579 'wp-fastest-cache/wp-fastest-cache.php',
580 'sg-cachepress/sg-cachepress.php',
581 );
582
583 foreach ( $disable_for_plugins as $plugin ) {
584 if ( \is_plugin_active( $plugin ) ) {
585 return '0';
586 }
587 }
588
589 return $default_value;
590 }
591
592 /**
593 * Default max image attachments.
594 *
595 * @param string $value The value of the option.
596 *
597 * @return string|int The value of the option.
598 */
599 public static function default_max_image_attachments( $value ) {
600 if ( ! \is_numeric( $value ) ) {
601 $value = ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS;
602 }
603
604 return $value;
605 }
606
607 /**
608 * Ensure support post types is an array.
609 *
610 * @param string[] $value The value of the option.
611 *
612 * @return string[] The value of the option.
613 */
614 public static function support_post_types_ensure_array( $value ) {
615 return (array) $value;
616 }
617
618 /**
619 * Default object type.
620 *
621 * @param string $value The value of the option.
622 *
623 * @return string The value of the option.
624 */
625 public static function default_object_type( $value ) {
626 if ( ! $value ) {
627 $value = ACTIVITYPUB_DEFAULT_OBJECT_TYPE;
628 }
629
630 return $value;
631 }
632
633 /**
634 * Handle relay mode option changes.
635 *
636 * When relay mode is enabled, switch to blog-only mode and set username to "relay".
637 * When disabled, restore previous settings.
638 *
639 * @param mixed $old_value The old option value.
640 * @param mixed $new_value The new option value.
641 */
642 public static function relay_mode_changed( $old_value, $new_value ) {
643 if ( $new_value && ! $old_value ) {
644 // Enabling relay mode.
645 // Store previous username and actor mode for restoration.
646 \update_option( 'activitypub_relay_previous_blog_identifier', \get_option( 'activitypub_blog_identifier' ) );
647 \update_option( 'activitypub_relay_previous_actor_mode', \get_option( 'activitypub_actor_mode' ) );
648
649 // Set blog username to "relay".
650 \update_option( 'activitypub_blog_identifier', 'relay' );
651
652 // Switch to blog-only mode.
653 \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
654 } elseif ( ! $new_value && $old_value ) {
655 // Disabling relay mode - restore previous settings.
656 $previous_identifier = \get_option( 'activitypub_relay_previous_blog_identifier' );
657 $previous_actor_mode = \get_option( 'activitypub_relay_previous_actor_mode' );
658
659 if ( $previous_identifier ) {
660 \update_option( 'activitypub_blog_identifier', $previous_identifier );
661 \delete_option( 'activitypub_relay_previous_blog_identifier' );
662 }
663
664 if ( $previous_actor_mode ) {
665 \update_option( 'activitypub_actor_mode', $previous_actor_mode );
666 \delete_option( 'activitypub_relay_previous_actor_mode' );
667 }
668 }
669 }
670 }
671