-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathclass-options.php
More file actions
1045 lines (936 loc) · 29.8 KB
/
Copy pathclass-options.php
File metadata and controls
1045 lines (936 loc) · 29.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Options file.
*
* @package Activitypub
*/
namespace Activitypub;
use Activitypub\Model\Blog;
/**
* Options class.
*/
class Options {
/**
* Initialize the options.
*/
public static function init() {
\add_action( 'admin_init', array( self::class, 'register_settings' ) );
\add_action( 'rest_api_init', array( self::class, 'register_settings' ) );
\add_filter( 'pre_option_activitypub_actor_mode', array( self::class, 'pre_option_activitypub_actor_mode' ) );
\add_filter( 'pre_option_activitypub_authorized_fetch', array( self::class, 'pre_option_activitypub_authorized_fetch' ) );
\add_filter( 'pre_option_activitypub_vary_header', array( self::class, 'pre_option_activitypub_vary_header' ) );
\add_filter( 'pre_option_activitypub_following_ui', array( self::class, 'pre_option_activitypub_following_ui' ) );
\add_filter( 'pre_option_activitypub_create_posts', array( self::class, 'pre_option_activitypub_create_posts' ) );
\add_filter( 'pre_option_activitypub_distribution_mode', array( self::class, 'pre_option_activitypub_distribution_mode' ) );
\add_filter( 'activitypub_dispatcher_batch_size', array( self::class, 'filter_dispatcher_batch_size' ) );
\add_filter( 'activitypub_scheduler_async_batch_pause', array( self::class, 'filter_scheduler_batch_pause' ), 10, 2 );
\add_filter( 'pre_option_activitypub_allow_likes', array( self::class, 'maybe_disable_interactions' ) );
\add_filter( 'pre_option_activitypub_allow_replies', array( self::class, 'maybe_disable_interactions' ) );
\add_filter( 'default_option_activitypub_negotiate_content', array( self::class, 'default_option_activitypub_negotiate_content' ) );
\add_filter( 'option_activitypub_max_image_attachments', array( self::class, 'default_max_image_attachments' ) );
\add_filter( 'option_activitypub_support_post_types', array( self::class, 'support_post_types_ensure_array' ) );
\add_filter( 'option_activitypub_object_type', array( self::class, 'default_object_type' ) );
\add_filter( 'option_activitypub_outbox_purge_days', array( self::class, 'sanitize_purge_days' ) );
\add_filter( 'option_activitypub_inbox_purge_days', array( self::class, 'sanitize_purge_days' ) );
\add_filter( 'option_activitypub_ap_post_purge_days', array( self::class, 'sanitize_purge_days' ) );
\add_action( 'update_option_activitypub_relay_mode', array( self::class, 'relay_mode_changed' ), 10, 2 );
}
/**
* Register ActivityPub settings.
*/
public static function register_settings() {
/*
* Options Group: activitypub
*/
\register_setting(
'activitypub',
'activitypub_post_content_type',
array(
'type' => 'string',
'description' => 'Use title and link, summary, full or custom content',
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'title', 'excerpt', 'content' ),
),
),
'default' => 'content',
)
);
\register_setting(
'activitypub',
'activitypub_custom_post_content',
array(
'type' => 'string',
'description' => 'Define your own custom post template',
'show_in_rest' => true,
'default' => ACTIVITYPUB_CUSTOM_POST_CONTENT,
)
);
\register_setting(
'activitypub',
'activitypub_max_image_attachments',
array(
'type' => 'integer',
'description' => 'Number of images to attach to posts.',
'default' => ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS,
'sanitize_callback' => static function ( $value ) {
return \is_numeric( $value ) ? \absint( $value ) : ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS;
},
)
);
\register_setting(
'activitypub',
'activitypub_use_hashtags',
array(
'type' => 'boolean',
'description' => 'Add hashtags in the content as native tags and replace the #tag with the tag-link',
'default' => '0',
)
);
\register_setting(
'activitypub',
'activitypub_use_opengraph',
array(
'type' => 'boolean',
'description' => 'Automatically add "fediverse:creator" OpenGraph tags for Authors and the Blog-User.',
'default' => '1',
)
);
\register_setting(
'activitypub',
'activitypub_support_post_types',
array(
'type' => 'string',
'description' => 'Enable ActivityPub support for post types',
'show_in_rest' => true,
'default' => array( 'post' ),
)
);
\register_setting(
'activitypub',
'activitypub_actor_mode',
array(
'type' => 'string',
'description' => 'Choose your preferred Actor-Mode.',
'default' => ACTIVITYPUB_ACTOR_MODE,
'show_in_rest' => array(
'schema' => array(
'type' => 'string',
'enum' => array(
ACTIVITYPUB_ACTOR_MODE,
ACTIVITYPUB_BLOG_MODE,
ACTIVITYPUB_ACTOR_AND_BLOG_MODE,
),
),
),
)
);
\register_setting(
'activitypub',
'activitypub_attribution_domains',
array(
'type' => 'string',
'description' => 'Websites allowed to credit you.',
'default' => home_host(),
'sanitize_callback' => array( Sanitize::class, 'host_list' ),
)
);
\register_setting(
'activitypub',
'activitypub_allow_likes',
array(
'type' => 'integer',
'description' => 'Allow likes.',
'default' => '1',
'sanitize_callback' => 'absint',
)
);
\register_setting(
'activitypub',
'activitypub_allow_reposts',
array(
'type' => 'integer',
'description' => 'Allow reposts.',
'default' => '1',
'sanitize_callback' => 'absint',
)
);
\register_setting(
'activitypub',
'activitypub_auto_approve_reactions',
array(
'type' => 'integer',
'description' => 'Auto-approve Reactions.',
'default' => '0',
'sanitize_callback' => 'absint',
)
);
\register_setting(
'activitypub',
'activitypub_default_quote_policy',
array(
'type' => 'string',
'description' => 'Default quote policy for new posts.',
'default' => ACTIVITYPUB_INTERACTION_POLICY_ANYONE,
'sanitize_callback' => static function ( $value ) {
$allowed = array(
ACTIVITYPUB_INTERACTION_POLICY_ANYONE,
ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS,
ACTIVITYPUB_INTERACTION_POLICY_ME,
);
return \in_array( $value, $allowed, true ) ? $value : ACTIVITYPUB_INTERACTION_POLICY_ANYONE;
},
)
);
\register_setting(
'activitypub',
'activitypub_default_feature_policy',
array(
'type' => 'string',
'description' => 'Default policy for who can include this site\'s actors in featured collections (FEP-7aa9).',
'default' => ACTIVITYPUB_INTERACTION_POLICY_ME,
'sanitize_callback' => static function ( $value ) {
$allowed = array(
ACTIVITYPUB_INTERACTION_POLICY_ANYONE,
ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS,
ACTIVITYPUB_INTERACTION_POLICY_ME,
);
return \in_array( $value, $allowed, true ) ? $value : ACTIVITYPUB_INTERACTION_POLICY_ME;
},
)
);
\register_setting(
'activitypub',
'activitypub_relays',
array(
'type' => 'array',
'description' => 'Relays',
'default' => array(),
'sanitize_callback' => array( Sanitize::class, 'url_list' ),
)
);
\register_setting(
'activitypub',
'activitypub_site_blocked_actors',
array(
'type' => 'array',
'description' => 'Site-wide blocked ActivityPub actors.',
'default' => array(),
'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
)
);
/*
* Options Group: activitypub_advanced
*/
\register_setting(
'activitypub_advanced',
'activitypub_outbox_purge_days',
array(
'type' => 'integer',
'description' => 'Number of days to keep items in the Outbox.',
'default' => ACTIVITYPUB_OUTBOX_PURGE_DAYS,
'sanitize_callback' => static function ( $value ) {
return \max( 1, \absint( $value ) );
},
)
);
\register_setting(
'activitypub_advanced',
'activitypub_inbox_purge_days',
array(
'type' => 'integer',
'description' => 'Number of days to keep items in the Inbox.',
'default' => ACTIVITYPUB_INBOX_PURGE_DAYS,
'sanitize_callback' => static function ( $value ) {
return \max( 1, \absint( $value ) );
},
)
);
\register_setting(
'activitypub_advanced',
'activitypub_ap_post_purge_days',
array(
'type' => 'integer',
'description' => 'Number of days to keep remote posts.',
'default' => ACTIVITYPUB_AP_POST_PURGE_DAYS,
'sanitize_callback' => static function ( $value ) {
return \max( 1, \absint( $value ) );
},
)
);
\register_setting(
'activitypub_advanced',
'activitypub_vary_header',
array(
'type' => 'boolean',
'description' => 'Add the Vary header to the ActivityPub response.',
'default' => true,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_content_negotiation',
array(
'type' => 'boolean',
'description' => 'Enable content negotiation.',
'default' => true,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_authorized_fetch',
array(
'type' => 'boolean',
'description' => 'Require HTTP signature authentication.',
'default' => false,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_rfc9421_signature',
array(
'type' => 'boolean',
'description' => 'Use RFC-9421 signature.',
'default' => true,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_following_ui',
array(
'type' => 'boolean',
'description' => 'Show Following UI in admin menus and settings.',
'default' => false,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_reader_ui',
array(
'type' => 'boolean',
'description' => 'Enable the Reader to view posts from accounts you follow.',
'default' => false,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_create_posts',
array(
'type' => 'boolean',
'description' => 'Allow creating posts via ActivityPub.',
'default' => false,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_api',
array(
'type' => 'boolean',
'description' => 'Enable the ActivityPub API to allow third-party clients.',
'default' => false,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_object_type',
array(
'type' => 'string',
'description' => 'The Activity-Object-Type',
'show_in_rest' => array(
'schema' => array(
'enum' => array( 'note', 'wordpress-post-format' ),
),
),
'default' => ACTIVITYPUB_DEFAULT_OBJECT_TYPE,
)
);
\register_setting(
'activitypub_advanced',
'activitypub_relay_mode',
array(
'type' => 'integer',
'description' => 'Enable relay mode to forward public activities to all followers.',
'default' => 0,
'sanitize_callback' => 'absint',
)
);
$default_distribution = self::get_distribution_preset_values()['default'];
\register_setting(
'activitypub_advanced',
'activitypub_distribution_mode',
array(
'type' => 'string',
'description' => \__( 'Distribution mode for federation delivery.', 'activitypub' ),
'default' => 'default',
'sanitize_callback' => array( self::class, 'sanitize_distribution_mode' ),
)
);
\register_setting(
'activitypub_advanced',
'activitypub_custom_batch_size',
array(
'type' => 'integer',
'description' => \__( 'Custom batch size for federation delivery.', 'activitypub' ),
'default' => $default_distribution['batch_size'],
'sanitize_callback' => static function ( $value ) {
return \min( 500, \max( 1, \absint( $value ) ) );
},
)
);
\register_setting(
'activitypub_advanced',
'activitypub_custom_batch_pause',
array(
'type' => 'integer',
'description' => \__( 'Custom pause in seconds between batches.', 'activitypub' ),
'default' => $default_distribution['pause'],
'sanitize_callback' => static function ( $value ) {
return \min( 3600, \absint( $value ) );
},
)
);
/*
* Options Group: activitypub_blog
*/
\register_setting(
'activitypub_blog',
'activitypub_blog_description',
array(
'type' => 'string',
'description' => 'The Description of the Blog-User',
'show_in_rest' => true,
'default' => '',
)
);
\register_setting(
'activitypub_blog',
'activitypub_blog_identifier',
array(
'type' => 'string',
'description' => 'The Identifier of the Blog-User',
'show_in_rest' => true,
'default' => Blog::get_default_username(),
'sanitize_callback' => array( Sanitize::class, 'blog_identifier' ),
)
);
\register_setting(
'activitypub_blog',
'activitypub_header_image',
array(
'type' => 'integer',
'description' => 'The Attachment-ID of the Sites Header-Image',
'default' => null,
)
);
\register_setting(
'activitypub_blog',
'activitypub_blog_user_mailer_new_dm',
array(
'type' => 'integer',
'description' => 'Send a notification when someone sends a user of the blog a direct message.',
'default' => 1,
)
);
\register_setting(
'activitypub_blog',
'activitypub_blog_user_mailer_new_follower',
array(
'type' => 'integer',
'description' => 'Send a notification when someone starts to follow a user of the blog.',
'default' => 1,
)
);
\register_setting(
'activitypub_blog',
'activitypub_blog_user_mailer_new_mention',
array(
'type' => 'integer',
'description' => 'Send a notification when someone mentions a user of the blog.',
'default' => 1,
)
);
\register_setting(
'activitypub_blog',
'activitypub_mailer_annual_report',
array(
'type' => 'integer',
'description' => 'Send the annual Fediverse Year in Review email.',
'default' => 1,
)
);
\register_setting(
'activitypub_blog',
'activitypub_mailer_monthly_report',
array(
'type' => 'integer',
'description' => 'Send a monthly Fediverse stats report email.',
'default' => 0,
)
);
\register_setting(
'activitypub_blog',
'activitypub_blog_user_also_known_as',
array(
'type' => 'array',
'description' => 'An array of URLs that the blog user is known by.',
'default' => array(),
'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
)
);
\register_setting(
'activitypub_blog',
'activitypub_hide_social_graph',
array(
'type' => 'integer',
'description' => 'Hide Followers and Followings on Profile.',
'default' => 0,
'sanitize_callback' => 'absint',
'show_in_rest' => true,
)
);
}
/**
* Delete all options.
*/
public static function delete() {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'activitypub_%'" );
}
/**
* Pre-get option filter for the Actor-Mode.
*
* @param string|false $pre The pre-get option value.
*
* @return string|false The actor mode or false if it should not be filtered.
*/
public static function pre_option_activitypub_actor_mode( $pre ) {
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) && ACTIVITYPUB_SINGLE_USER_MODE ) {
return ACTIVITYPUB_BLOG_MODE;
}
if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) && ACTIVITYPUB_DISABLE_USER ) {
return ACTIVITYPUB_BLOG_MODE;
}
if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) && ACTIVITYPUB_DISABLE_BLOG_USER ) {
return ACTIVITYPUB_ACTOR_MODE;
}
return $pre;
}
/**
* Pre-get option filter for the Authorized Fetch.
*
* @param string $pre The pre-get option value.
*
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
*/
public static function pre_option_activitypub_authorized_fetch( $pre ) {
if ( ! \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
return $pre;
}
if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
return '1';
}
return '0';
}
/**
* Pre-get option filter for the Vary Header.
*
* @param string $pre The pre-get option value.
*
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
*/
public static function pre_option_activitypub_vary_header( $pre ) {
if ( ! \defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) ) {
return $pre;
}
if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
return '1';
}
return '0';
}
/**
* Pre-get option filter for the Following UI.
*
* Forces the Following UI to be enabled when the Reader is enabled.
*
* @param string $pre The pre-get option value.
*
* @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
*/
public static function pre_option_activitypub_following_ui( $pre ) {
/*
* Bypass the filter to get the actual stored value for activitypub_reader_ui.
* This avoids infinite loops if activitypub_reader_ui also had a pre_option filter.
*/
if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
return '1';
}
return $pre;
}
/**
* Pre-get option filter for the Create Posts setting.
*
* Forces the Create Posts setting to be enabled when the Reader is enabled.
*
* @param string $pre The pre-get option value.
*
* @return string If the Reader is enabled, return '1', otherwise return the pre-get option value.
*/
public static function pre_option_activitypub_create_posts( $pre ) {
if ( \get_option( 'activitypub_reader_ui', '0' ) ) {
return '1';
}
return $pre;
}
/**
* Disallow interactions if the constant is set.
*
* @param bool $pre The value of the option.
*
* @return bool|string The value of the option.
*/
public static function maybe_disable_interactions( $pre ) {
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return '0';
}
return $pre;
}
/**
* Default option filter for the Content-Negotiation.
*
* @see https://github.com/Automattic/wordpress-activitypub/wiki/Caching
*
* @param string $default_value The default value of the option.
*
* @return string The default value of the option.
*/
public static function default_option_activitypub_negotiate_content( $default_value ) {
$disable_for_plugins = array(
'wp-optimize/wp-optimize.php',
'wp-rocket/wp-rocket.php',
'w3-total-cache/w3-total-cache.php',
'wp-fastest-cache/wp-fastest-cache.php',
'sg-cachepress/sg-cachepress.php',
);
foreach ( $disable_for_plugins as $plugin ) {
if ( \is_plugin_active( $plugin ) ) {
return '0';
}
}
return $default_value;
}
/**
* Default max image attachments.
*
* @param string $value The value of the option.
*
* @return string|int The value of the option.
*/
public static function default_max_image_attachments( $value ) {
if ( ! \is_numeric( $value ) ) {
$value = ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS;
}
return $value;
}
/**
* Ensure support post types is an array.
*
* @param string[] $value The value of the option.
*
* @return string[] The value of the option.
*/
public static function support_post_types_ensure_array( $value ) {
return (array) $value;
}
/**
* Default object type.
*
* @param string $value The value of the option.
*
* @return string The value of the option.
*/
public static function default_object_type( $value ) {
if ( ! $value ) {
$value = ACTIVITYPUB_DEFAULT_OBJECT_TYPE;
}
return $value;
}
/**
* Pre-get option filter for the Distribution Mode.
*
* @since 9.0.0
*
* @param string|false $pre The pre-get option value.
*
* @return string|false The distribution mode or false if it should not be filtered.
*/
public static function pre_option_activitypub_distribution_mode( $pre ) {
return self::resolve_distribution_mode( $pre, ACTIVITYPUB_DISTRIBUTION_MODE );
}
/**
* Whether the distribution mode is locked to a valid preset by the
* `ACTIVITYPUB_DISTRIBUTION_MODE` constant.
*
* Returns true only when the constant is set to a key recognized by
* `get_distribution_preset_values()`. Invalid constant values fall back
* to `'default'` at runtime (see `resolve_distribution_mode()`) but the
* UI stays visible so admins can spot the misconfiguration.
*
* @since 9.0.0
*
* @return bool True when the constant pins the mode to a valid preset.
*/
public static function is_distribution_mode_locked() {
if ( false === ACTIVITYPUB_DISTRIBUTION_MODE ) {
return false;
}
return \in_array( ACTIVITYPUB_DISTRIBUTION_MODE, \array_keys( self::get_distribution_preset_values() ), true );
}
/**
* Resolve the distribution mode against the wp-config constant.
*
* Extracted from `pre_option_activitypub_distribution_mode()` so the
* constant-lock path can be exercised from tests without redefining
* the real constant.
*
* Only preset modes are honored via the constant. The 'custom' mode
* is excluded because its batch size and pause values are still read
* from the database, which would defeat the purpose of locking the
* mode via wp-config.php.
*
* @since 9.0.0
*
* @param string|false $pre The pre-get option value.
* @param mixed $constant_value The value of `ACTIVITYPUB_DISTRIBUTION_MODE`.
*
* @return string|false Mode if locked, `$pre` otherwise.
*/
public static function resolve_distribution_mode( $pre, $constant_value ) {
if ( false === $constant_value ) {
return $pre;
}
$allowed = \array_keys( self::get_distribution_preset_values() );
if ( \in_array( $constant_value, $allowed, true ) ) {
return $constant_value;
}
\_doing_it_wrong(
__METHOD__,
\sprintf(
/* translators: %s: invalid constant value */
\esc_html__( 'ACTIVITYPUB_DISTRIBUTION_MODE value %s is not a valid preset; falling back to default.', 'activitypub' ),
\esc_html( (string) $constant_value )
),
'9.0.0'
);
return 'default';
}
/**
* Get the raw batch_size/pause values for each distribution preset.
*
* Single source of truth for the preset values, used in the hot path
* (get_distribution_params, sanitize_distribution_mode, resolve_distribution_mode)
* to avoid running translation calls just to check keys or numbers.
*
* @since 9.0.0
*
* @return array Associative array of mode => { batch_size, pause }.
*/
private static function get_distribution_preset_values() {
return array(
'default' => array(
'batch_size' => 100,
'pause' => 15,
),
'balanced' => array(
'batch_size' => 50,
'pause' => 30,
),
'eco' => array(
'batch_size' => 20,
'pause' => 30,
),
);
}
/**
* Get the available distribution mode presets with UI labels.
*
* Decorates `get_distribution_preset_values()` with translated labels
* and descriptions for use in the admin settings page.
*
* @since 9.0.0
*
* @return array Associative array of mode => { batch_size, pause, label, description }.
*/
public static function get_distribution_modes() {
$modes = self::get_distribution_preset_values();
$modes['default']['label'] = \__( 'Default', 'activitypub' );
$modes['default']['description'] = \sprintf(
/* translators: 1: batch size, 2: pause in seconds */
\__( 'Deliver activities as fast as possible (<code>%1$d</code> per batch, <code>%2$ds</code> pause).', 'activitypub' ),
$modes['default']['batch_size'],
$modes['default']['pause']
);
$modes['balanced']['label'] = \__( 'Balanced', 'activitypub' );
$modes['balanced']['description'] = \sprintf(
/* translators: 1: batch size, 2: pause in seconds */
\__( 'Moderate pace with reasonable pauses between batches (<code>%1$d</code> per batch, <code>%2$ds</code> pause).', 'activitypub' ),
$modes['balanced']['batch_size'],
$modes['balanced']['pause']
);
$modes['eco']['label'] = \__( 'Eco Mode', 'activitypub' );
$modes['eco']['description'] = \sprintf(
/* translators: 1: batch size, 2: pause in seconds */
\__( 'Gentle on server resources, ideal for shared hosting (<code>%1$d</code> per batch, <code>%2$ds</code> pause).', 'activitypub' ),
$modes['eco']['batch_size'],
$modes['eco']['pause']
);
return $modes;
}
/**
* Sanitize the distribution mode option.
*
* Restricts the stored value to a known preset (from
* `get_distribution_modes()`) or `'custom'`. Anything else
* falls back to `'default'`.
*
* @since 9.0.0
*
* @param string $value The submitted option value.
*
* @return string A valid distribution mode key.
*/
public static function sanitize_distribution_mode( $value ) {
$allowed = \array_merge( \array_keys( self::get_distribution_preset_values() ), array( 'custom' ) );
return \in_array( $value, $allowed, true ) ? $value : 'default';
}
/**
* Get distribution parameters for the current mode.
*
* @since 9.0.0
*
* @return array { mode: string, batch_size: int, pause: int }
*/
public static function get_distribution_params() {
$mode = \get_option( 'activitypub_distribution_mode', 'default' );
$modes = self::get_distribution_preset_values();
if ( isset( $modes[ $mode ] ) ) {
return array(
'mode' => $mode,
'batch_size' => $modes[ $mode ]['batch_size'],
'pause' => $modes[ $mode ]['pause'],
);
}
// Custom mode reads its values from dedicated options; any other
// unrecognized mode falls back to the default preset so callers
// always receive a valid configuration.
if ( 'custom' !== $mode ) {
return array(
'mode' => 'default',
'batch_size' => $modes['default']['batch_size'],
'pause' => $modes['default']['pause'],
);
}
$default_params = $modes['default'];
return array(
'mode' => 'custom',
'batch_size' => \max( 1, \absint( \get_option( 'activitypub_custom_batch_size', $default_params['batch_size'] ) ) ),
'pause' => \absint( \get_option( 'activitypub_custom_batch_pause', $default_params['pause'] ) ),
);
}
/**
* Filter the dispatcher batch size based on distribution mode.
*
* In `'default'` mode the upstream value is passed through so the
* `ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE` constant and other filters
* still win; any explicit mode imposes its own batch size.
*
* @since 9.0.0
*
* @param int $batch_size The default batch size.
*
* @return int The batch size for the current distribution mode.
*/
public static function filter_dispatcher_batch_size( $batch_size ) {
$params = self::get_distribution_params();
return 'default' === $params['mode'] ? $batch_size : $params['batch_size'];
}
/**
* Filter the scheduler batch pause based on distribution mode.
*
* Only delivery batches (`activitypub_send_activity`) are affected. Every
* mode imposes its own delivery pause: `'default'` is the fast preset, which
* is intentionally shorter than the generic async-batch baseline, so it does
* not pass the upstream value through.
*
* @since 9.0.0
*
* @param int $pause The default pause in seconds.
* @param string|false|null $hook The async batch hook being scheduled.
*
* @return int The pause for the current distribution mode.
*/
public static function filter_scheduler_batch_pause( $pause, $hook = null ) {
if ( 'activitypub_send_activity' !== $hook ) {
return $pause;
}
return self::get_distribution_params()['pause'];
}
/**
* Sanitize purge day values.
*
* Ensures the value is a non-negative integer. Returns the
* registered default when the stored value is empty or false
* (option not properly set), but allows 0 to disable purging.
*
* @since 8.1.0
*
* @param mixed $value The stored option value.
*
* @return int The sanitized value.
*/
public static function sanitize_purge_days( $value ) {
if ( '' === $value || false === $value ) {
$filter = \current_filter();
$defaults = array(
'option_activitypub_outbox_purge_days' => ACTIVITYPUB_OUTBOX_PURGE_DAYS,
'option_activitypub_inbox_purge_days' => ACTIVITYPUB_INBOX_PURGE_DAYS,
'option_activitypub_ap_post_purge_days' => ACTIVITYPUB_AP_POST_PURGE_DAYS,
);