PluginProbe
ActivityPub / 7.8.4
ActivityPub v7.8.4
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 7.8.4, at includes/class-options.php

653 lines 17.0 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_relays',
185 array(
186 'type' => 'array',
187 'description' => 'Relays',
188 'default' => array(),
189 'sanitize_callback' => array( Sanitize::class, 'url_list' ),
190 )
191 );
192
193 \register_setting(
194 'activitypub',
195 'activitypub_site_blocked_actors',
196 array(
197 'type' => 'array',
198 'description' => 'Site-wide blocked ActivityPub actors.',
199 'default' => array(),
200 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
201 )
202 );
203
204 /*
205 * Options Group: activitypub_advanced
206 */
207 \register_setting(
208 'activitypub_advanced',
209 'activitypub_outbox_purge_days',
210 array(
211 'type' => 'integer',
212 'description' => 'Number of days to keep items in the Outbox.',
213 'default' => 180,
214 )
215 );
216
217 \register_setting(
218 'activitypub_advanced',
219 'activitypub_inbox_purge_days',
220 array(
221 'type' => 'integer',
222 'description' => 'Number of days to keep items in the Inbox.',
223 'default' => 180,
224 )
225 );
226
227 \register_setting(
228 'activitypub_advanced',
229 'activitypub_ap_post_purge_days',
230 array(
231 'type' => 'integer',
232 'description' => 'Number of days to keep remote posts.',
233 'default' => 30,
234 )
235 );
236
237 \register_setting(
238 'activitypub_advanced',
239 'activitypub_vary_header',
240 array(
241 'type' => 'boolean',
242 'description' => 'Add the Vary header to the ActivityPub response.',
243 'default' => true,
244 )
245 );
246
247 \register_setting(
248 'activitypub_advanced',
249 'activitypub_content_negotiation',
250 array(
251 'type' => 'boolean',
252 'description' => 'Enable content negotiation.',
253 'default' => true,
254 )
255 );
256
257 \register_setting(
258 'activitypub_advanced',
259 'activitypub_authorized_fetch',
260 array(
261 'type' => 'boolean',
262 'description' => 'Require HTTP signature authentication.',
263 'default' => false,
264 )
265 );
266
267 \register_setting(
268 'activitypub_advanced',
269 'activitypub_rfc9421_signature',
270 array(
271 'type' => 'boolean',
272 'description' => 'Use RFC-9421 signature.',
273 'default' => false,
274 )
275 );
276
277 \register_setting(
278 'activitypub_advanced',
279 'activitypub_following_ui',
280 array(
281 'type' => 'boolean',
282 'description' => 'Show Following UI in admin menus and settings.',
283 'default' => false,
284 )
285 );
286
287 \register_setting(
288 'activitypub_advanced',
289 'activitypub_reader_ui',
290 array(
291 'type' => 'boolean',
292 'description' => 'Enable the Reader to view posts from accounts you follow.',
293 'default' => false,
294 )
295 );
296
297 \register_setting(
298 'activitypub_advanced',
299 'activitypub_create_posts',
300 array(
301 'type' => 'boolean',
302 'description' => 'Allow creating posts via ActivityPub.',
303 'default' => false,
304 )
305 );
306
307 \register_setting(
308 'activitypub_advanced',
309 'activitypub_object_type',
310 array(
311 'type' => 'string',
312 'description' => 'The Activity-Object-Type',
313 'show_in_rest' => array(
314 'schema' => array(
315 'enum' => array( 'note', 'wordpress-post-format' ),
316 ),
317 ),
318 'default' => ACTIVITYPUB_DEFAULT_OBJECT_TYPE,
319 )
320 );
321
322 \register_setting(
323 'activitypub_advanced',
324 'activitypub_relay_mode',
325 array(
326 'type' => 'integer',
327 'description' => 'Enable relay mode to forward public activities to all followers.',
328 'default' => 0,
329 'sanitize_callback' => 'absint',
330 )
331 );
332
333 /*
334 * Options Group: activitypub_blog
335 */
336 \register_setting(
337 'activitypub_blog',
338 'activitypub_blog_description',
339 array(
340 'type' => 'string',
341 'description' => 'The Description of the Blog-User',
342 'show_in_rest' => true,
343 'default' => '',
344 )
345 );
346
347 \register_setting(
348 'activitypub_blog',
349 'activitypub_blog_identifier',
350 array(
351 'type' => 'string',
352 'description' => 'The Identifier of the Blog-User',
353 'show_in_rest' => true,
354 'default' => Blog::get_default_username(),
355 'sanitize_callback' => array( Sanitize::class, 'blog_identifier' ),
356 )
357 );
358
359 \register_setting(
360 'activitypub_blog',
361 'activitypub_header_image',
362 array(
363 'type' => 'integer',
364 'description' => 'The Attachment-ID of the Sites Header-Image',
365 'default' => null,
366 )
367 );
368
369 \register_setting(
370 'activitypub_blog',
371 'activitypub_blog_user_mailer_new_dm',
372 array(
373 'type' => 'integer',
374 'description' => 'Send a notification when someone sends a user of the blog a direct message.',
375 'default' => 1,
376 )
377 );
378
379 \register_setting(
380 'activitypub_blog',
381 'activitypub_blog_user_mailer_new_follower',
382 array(
383 'type' => 'integer',
384 'description' => 'Send a notification when someone starts to follow a user of the blog.',
385 'default' => 1,
386 )
387 );
388
389 \register_setting(
390 'activitypub_blog',
391 'activitypub_blog_user_mailer_new_mention',
392 array(
393 'type' => 'integer',
394 'description' => 'Send a notification when someone mentions a user of the blog.',
395 'default' => 1,
396 )
397 );
398
399 \register_setting(
400 'activitypub_blog',
401 'activitypub_blog_user_also_known_as',
402 array(
403 'type' => 'array',
404 'description' => 'An array of URLs that the blog user is known by.',
405 'default' => array(),
406 'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
407 )
408 );
409
410 \register_setting(
411 'activitypub_blog',
412 'activitypub_hide_social_graph',
413 array(
414 'type' => 'integer',
415 'description' => 'Hide Followers and Followings on Profile.',
416 'default' => 0,
417 'sanitize_callback' => 'absint',
418 'show_in_rest' => true,
419 )
420 );
421 }
422
423 /**
424 * Delete all options.
425 */
426 public static function delete() {
427 global $wpdb;
428
429 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
430 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'activitypub_%'" );
431 }
432
433 /**
434 * Pre-get option filter for the Actor-Mode.
435 *
436 * @param string|false $pre The pre-get option value.
437 *
438 * @return string|false The actor mode or false if it should not be filtered.
439 */
440 public static function pre_option_activitypub_actor_mode( $pre ) {
441 if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) && ACTIVITYPUB_SINGLE_USER_MODE ) {
442 return ACTIVITYPUB_BLOG_MODE;
443 }
444
445 if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) && ACTIVITYPUB_DISABLE_USER ) {
446 return ACTIVITYPUB_BLOG_MODE;
447 }
448
449 if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) && ACTIVITYPUB_DISABLE_BLOG_USER ) {
450 return ACTIVITYPUB_ACTOR_MODE;
451 }
452
453 return $pre;
454 }
455
456 /**
457 * Pre-get option filter for the Authorized Fetch.
458 *
459 * @param string $pre The pre-get option value.
460 *
461 * @return string If the constant is defined, return the value, otherwise return the pre-get option value.
462 */
463 public static function pre_option_activitypub_authorized_fetch( $pre ) {
464 if ( ! \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
465 return $pre;
466 }
467
468 if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
469 return '1';
470 }
471
472 return '0';
473 }
474
475 /**
476 * Pre-get option filter for the Vary Header.
477 *
478 * @param string $pre The pre-get option value.
479 *
480 * @return string If the constant is defined, return the value, otherwise return the pre-get option value.
481 */
482 public static function pre_option_activitypub_vary_header( $pre ) {
483 if ( ! \defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) ) {
484 return $pre;
485 }
486
487 if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
488 return '1';
489 }
490
491 return '0';
492 }
493
494 /**
495 * Pre-get option filter for the Following UI.
496 *
497 * Forces the Following UI to be enabled when the Reader is enabled.
498 *
499 * @param string $pre The pre-get option value.
500 *
501 * @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
502 */
503 public static function pre_option_activitypub_following_ui( $pre ) {
504 /*
505 * Bypass the filter to get the actual stored value for activitypub_reader_ui.
506 * This avoids infinite loops if activitypub_reader_ui also had a pre_option filter.
507 */
508 if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
509 return '1';
510 }
511
512 return $pre;
513 }
514
515 /**
516 * Pre-get option filter for the Create Posts setting.
517 *
518 * Forces the Create Posts setting to be enabled when the Reader is enabled.
519 *
520 * @param string $pre The pre-get option value.
521 *
522 * @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
523 */
524 public static function pre_option_activitypub_create_posts( $pre ) {
525 if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
526 return '1';
527 }
528
529 return $pre;
530 }
531
532 /**
533 * Disallow interactions if the constant is set.
534 *
535 * @param bool $pre The value of the option.
536 *
537 * @return bool|string The value of the option.
538 */
539 public static function maybe_disable_interactions( $pre ) {
540 if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
541 return '0';
542 }
543
544 return $pre;
545 }
546
547 /**
548 * Default option filter for the Content-Negotiation.
549 *
550 * @see https://github.com/Automattic/wordpress-activitypub/wiki/Caching
551 *
552 * @param string $default_value The default value of the option.
553 *
554 * @return string The default value of the option.
555 */
556 public static function default_option_activitypub_negotiate_content( $default_value ) {
557 $disable_for_plugins = array(
558 'wp-optimize/wp-optimize.php',
559 'wp-rocket/wp-rocket.php',
560 'w3-total-cache/w3-total-cache.php',
561 'wp-fastest-cache/wp-fastest-cache.php',
562 'sg-cachepress/sg-cachepress.php',
563 );
564
565 foreach ( $disable_for_plugins as $plugin ) {
566 if ( \is_plugin_active( $plugin ) ) {
567 return '0';
568 }
569 }
570
571 return $default_value;
572 }
573
574 /**
575 * Default max image attachments.
576 *
577 * @param string $value The value of the option.
578 *
579 * @return string|int The value of the option.
580 */
581 public static function default_max_image_attachments( $value ) {
582 if ( ! \is_numeric( $value ) ) {
583 $value = ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS;
584 }
585
586 return $value;
587 }
588
589 /**
590 * Ensure support post types is an array.
591 *
592 * @param string[] $value The value of the option.
593 *
594 * @return string[] The value of the option.
595 */
596 public static function support_post_types_ensure_array( $value ) {
597 return (array) $value;
598 }
599
600 /**
601 * Default object type.
602 *
603 * @param string $value The value of the option.
604 *
605 * @return string The value of the option.
606 */
607 public static function default_object_type( $value ) {
608 if ( ! $value ) {
609 $value = ACTIVITYPUB_DEFAULT_OBJECT_TYPE;
610 }
611
612 return $value;
613 }
614
615 /**
616 * Handle relay mode option changes.
617 *
618 * When relay mode is enabled, switch to blog-only mode and set username to "relay".
619 * When disabled, restore previous settings.
620 *
621 * @param mixed $old_value The old option value.
622 * @param mixed $new_value The new option value.
623 */
624 public static function relay_mode_changed( $old_value, $new_value ) {
625 if ( $new_value && ! $old_value ) {
626 // Enabling relay mode.
627 // Store previous username and actor mode for restoration.
628 \update_option( 'activitypub_relay_previous_blog_identifier', \get_option( 'activitypub_blog_identifier' ) );
629 \update_option( 'activitypub_relay_previous_actor_mode', \get_option( 'activitypub_actor_mode' ) );
630
631 // Set blog username to "relay".
632 \update_option( 'activitypub_blog_identifier', 'relay' );
633
634 // Switch to blog-only mode.
635 \update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
636 } elseif ( ! $new_value && $old_value ) {
637 // Disabling relay mode - restore previous settings.
638 $previous_identifier = \get_option( 'activitypub_relay_previous_blog_identifier' );
639 $previous_actor_mode = \get_option( 'activitypub_relay_previous_actor_mode' );
640
641 if ( $previous_identifier ) {
642 \update_option( 'activitypub_blog_identifier', $previous_identifier );
643 \delete_option( 'activitypub_relay_previous_blog_identifier' );
644 }
645
646 if ( $previous_actor_mode ) {
647 \update_option( 'activitypub_actor_mode', $previous_actor_mode );
648 \delete_option( 'activitypub_relay_previous_actor_mode' );
649 }
650 }
651 }
652 }
653