Skip to content

Commit 1e396a7

Browse files
pfefferlematticbotobenland
authored
Add: Vary header settings (#1552)
* Add: `Vary` header settings Allow users to easily enable/disable the vary header without having to add constants. /cc @snarfed * Add changelog * add some slashes * fix phpcs * fix phpcs * create options class * load action class and re-order inits * add some slashes * Add phpdoc * ensure to run migration as early as possible * optimize wording props @obenland --------- Co-authored-by: Automattic Bot <[email protected]> Co-authored-by: Konstantin Obenland <[email protected]>
1 parent cb35e4f commit 1e396a7

File tree

8 files changed

+211
-119
lines changed

8 files changed

+211
-119
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Add option to enable/disable the `Vary` Header to the "Advanced Settings".

activitypub.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ function rest_init() {
6767
* Initialize plugin.
6868
*/
6969
function plugin_init() {
70-
\add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) );
7170
\add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
71+
\add_action( 'init', array( __NAMESPACE__ . '\Comment', 'init' ) );
7272
\add_action( 'init', array( __NAMESPACE__ . '\Dispatcher', 'init' ) );
7373
\add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) );
7474
\add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );
75-
\add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) );
76-
\add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) );
77-
\add_action( 'init', array( __NAMESPACE__ . '\Comment', 'init' ) );
7875
\add_action( 'init', array( __NAMESPACE__ . '\Link', 'init' ) );
7976
\add_action( 'init', array( __NAMESPACE__ . '\Mailer', 'init' ) );
77+
\add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) );
78+
\add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ), 1 );
79+
\add_action( 'init', array( __NAMESPACE__ . '\Options', 'init' ) );
80+
\add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) );
8081

8182
if ( site_supports_blocks() ) {
8283
\add_action( 'init', array( __NAMESPACE__ . '\Blocks', 'init' ) );

includes/class-activitypub.php

Lines changed: 33 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ public static function init() {
4949

5050
\add_action( 'updated_postmeta', array( self::class, 'updated_postmeta' ), 10, 4 );
5151
\add_action( 'added_post_meta', array( self::class, 'updated_postmeta' ), 10, 4 );
52-
\add_filter( 'pre_option_activitypub_actor_mode', array( self::class, 'pre_option_activitypub_actor_mode' ) );
53-
\add_filter( 'pre_option_activitypub_authorized_fetch', array( self::class, 'pre_option_activitypub_authorized_fetch' ) );
54-
\add_filter( 'pre_option_activitypub_shared_inbox', array( self::class, 'pre_option_activitypub_shared_inbox' ) );
5552

5653
\add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
5754

58-
\add_filter( 'pre_option_activitypub_allow_likes', array( self::class, 'maybe_disable_interactions' ) );
59-
\add_filter( 'pre_option_activitypub_allow_replies', array( self::class, 'maybe_disable_interactions' ) );
60-
6155
// Register several post_types.
6256
self::register_post_types();
6357

@@ -96,37 +90,38 @@ public static function uninstall() {
9690
\remove_filter( 'pre_wp_update_comment_count_now', array( Comment::class, 'pre_wp_update_comment_count_now' ) );
9791
Migration::update_comment_counts( 2000 );
9892

99-
delete_option( 'activitypub_actor_mode' );
100-
delete_option( 'activitypub_allow_likes' );
101-
delete_option( 'activitypub_allow_replies' );
102-
delete_option( 'activitypub_attribution_domains' );
103-
delete_option( 'activitypub_authorized_fetch' );
104-
delete_option( 'activitypub_application_user_private_key' );
105-
delete_option( 'activitypub_application_user_public_key' );
106-
delete_option( 'activitypub_blog_user_also_known_as' );
107-
delete_option( 'activitypub_blog_user_moved_to' );
108-
delete_option( 'activitypub_blog_user_private_key' );
109-
delete_option( 'activitypub_blog_user_public_key' );
110-
delete_option( 'activitypub_blog_description' );
111-
delete_option( 'activitypub_blog_identifier' );
112-
delete_option( 'activitypub_custom_post_content' );
113-
delete_option( 'activitypub_db_version' );
114-
delete_option( 'activitypub_default_extra_fields' );
115-
delete_option( 'activitypub_enable_blog_user' );
116-
delete_option( 'activitypub_enable_users' );
117-
delete_option( 'activitypub_header_image' );
118-
delete_option( 'activitypub_last_post_with_permalink_as_id' );
119-
delete_option( 'activitypub_mailer_new_follower' );
120-
delete_option( 'activitypub_mailer_new_dm' );
121-
delete_option( 'activitypub_max_image_attachments' );
122-
delete_option( 'activitypub_migration_lock' );
123-
delete_option( 'activitypub_object_type' );
124-
delete_option( 'activitypub_outbox_purge_days' );
125-
delete_option( 'activitypub_shared_inbox' );
126-
delete_option( 'activitypub_support_post_types' );
127-
delete_option( 'activitypub_use_hashtags' );
128-
delete_option( 'activitypub_use_opengraph' );
129-
delete_option( 'activitypub_use_permalink_as_id_for_blog' );
93+
\delete_option( 'activitypub_actor_mode' );
94+
\delete_option( 'activitypub_allow_likes' );
95+
\delete_option( 'activitypub_allow_replies' );
96+
\delete_option( 'activitypub_attribution_domains' );
97+
\delete_option( 'activitypub_authorized_fetch' );
98+
\delete_option( 'activitypub_application_user_private_key' );
99+
\delete_option( 'activitypub_application_user_public_key' );
100+
\delete_option( 'activitypub_blog_user_also_known_as' );
101+
\delete_option( 'activitypub_blog_user_moved_to' );
102+
\delete_option( 'activitypub_blog_user_private_key' );
103+
\delete_option( 'activitypub_blog_user_public_key' );
104+
\delete_option( 'activitypub_blog_description' );
105+
\delete_option( 'activitypub_blog_identifier' );
106+
\delete_option( 'activitypub_custom_post_content' );
107+
\delete_option( 'activitypub_db_version' );
108+
\delete_option( 'activitypub_default_extra_fields' );
109+
\delete_option( 'activitypub_enable_blog_user' );
110+
\delete_option( 'activitypub_enable_users' );
111+
\delete_option( 'activitypub_header_image' );
112+
\delete_option( 'activitypub_last_post_with_permalink_as_id' );
113+
\delete_option( 'activitypub_mailer_new_follower' );
114+
\delete_option( 'activitypub_mailer_new_dm' );
115+
\delete_option( 'activitypub_max_image_attachments' );
116+
\delete_option( 'activitypub_migration_lock' );
117+
\delete_option( 'activitypub_object_type' );
118+
\delete_option( 'activitypub_outbox_purge_days' );
119+
\delete_option( 'activitypub_shared_inbox' );
120+
\delete_option( 'activitypub_support_post_types' );
121+
\delete_option( 'activitypub_use_hashtags' );
122+
\delete_option( 'activitypub_use_opengraph' );
123+
\delete_option( 'activitypub_use_permalink_as_id_for_blog' );
124+
\delete_option( 'activitypub_vary_header' );
130125
}
131126

132127
/**
@@ -209,7 +204,7 @@ public static function add_headers() {
209204
if ( ! headers_sent() ) {
210205
\header( 'Link: <' . esc_url( $id ) . '>; title="ActivityPub (JSON)"; rel="alternate"; type="application/activity+json"', false );
211206

212-
if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
207+
if ( \get_option( 'activitypub_vary_header' ) ) {
213208
// Send Vary header for Accept header.
214209
\header( 'Vary: Accept', false );
215210
}
@@ -396,68 +391,6 @@ public static function pre_get_avatar_data( $args, $id_or_email ) {
396391
return $args;
397392
}
398393

399-
/**
400-
* Pre-get option filter for the Actor-Mode.
401-
*
402-
* @param string|false $pre The pre-get option value.
403-
*
404-
* @return string|false The actor mode or false if it should not be filtered.
405-
*/
406-
public static function pre_option_activitypub_actor_mode( $pre ) {
407-
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) && ACTIVITYPUB_SINGLE_USER_MODE ) {
408-
return ACTIVITYPUB_BLOG_MODE;
409-
}
410-
411-
if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) && ACTIVITYPUB_DISABLE_USER ) {
412-
return ACTIVITYPUB_BLOG_MODE;
413-
}
414-
415-
if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) && ACTIVITYPUB_DISABLE_BLOG_USER ) {
416-
return ACTIVITYPUB_ACTOR_MODE;
417-
}
418-
419-
return $pre;
420-
}
421-
422-
/**
423-
* Pre-get option filter for the Authorized Fetch.
424-
*
425-
* @param string $pre The pre-get option value.
426-
*
427-
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
428-
*/
429-
public static function pre_option_activitypub_authorized_fetch( $pre ) {
430-
if ( ! \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
431-
return $pre;
432-
}
433-
434-
if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
435-
return '1';
436-
}
437-
438-
return '0';
439-
}
440-
441-
/**
442-
* Pre-get option filter for the Shared Inbox.
443-
*
444-
* @param string $pre The pre-get option value.
445-
*
446-
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
447-
*/
448-
public static function pre_option_activitypub_shared_inbox( $pre ) {
449-
if ( ! \defined( 'ACTIVITYPUB_SHARED_INBOX_FEATURE' ) ) {
450-
return $pre;
451-
}
452-
453-
if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) {
454-
return '1';
455-
}
456-
457-
return '0';
458-
}
459-
460-
461394
/**
462395
* Store permalink in meta, to send delete Activity.
463396
*
@@ -902,18 +835,4 @@ public static function register_user_meta() {
902835
)
903836
);
904837
}
905-
906-
/**
907-
* Disallow interactions if the constant is set.
908-
*
909-
* @param bool $pre_option The value of the option.
910-
* @return bool|string The value of the option.
911-
*/
912-
public static function maybe_disable_interactions( $pre_option ) {
913-
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
914-
return '0';
915-
}
916-
917-
return $pre_option;
918-
}
919838
}

includes/class-options.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Options file.
4+
*
5+
* @package ActivityPub
6+
*/
7+
8+
namespace ActivityPub;
9+
10+
/**
11+
* Options class.
12+
*
13+
* @package ActivityPub
14+
*/
15+
class Options {
16+
17+
/**
18+
* Initialize the options.
19+
*/
20+
public static function init() {
21+
\add_filter( 'pre_option_activitypub_actor_mode', array( self::class, 'pre_option_activitypub_actor_mode' ) );
22+
\add_filter( 'pre_option_activitypub_authorized_fetch', array( self::class, 'pre_option_activitypub_authorized_fetch' ) );
23+
\add_filter( 'pre_option_activitypub_shared_inbox', array( self::class, 'pre_option_activitypub_shared_inbox' ) );
24+
\add_filter( 'pre_option_activitypub_vary_header', array( self::class, 'pre_option_activitypub_vary_header' ) );
25+
26+
\add_filter( 'pre_option_activitypub_allow_likes', array( self::class, 'maybe_disable_interactions' ) );
27+
\add_filter( 'pre_option_activitypub_allow_replies', array( self::class, 'maybe_disable_interactions' ) );
28+
}
29+
30+
31+
/**
32+
* Pre-get option filter for the Actor-Mode.
33+
*
34+
* @param string|false $pre The pre-get option value.
35+
*
36+
* @return string|false The actor mode or false if it should not be filtered.
37+
*/
38+
public static function pre_option_activitypub_actor_mode( $pre ) {
39+
if ( \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) && ACTIVITYPUB_SINGLE_USER_MODE ) {
40+
return ACTIVITYPUB_BLOG_MODE;
41+
}
42+
43+
if ( \defined( 'ACTIVITYPUB_DISABLE_USER' ) && ACTIVITYPUB_DISABLE_USER ) {
44+
return ACTIVITYPUB_BLOG_MODE;
45+
}
46+
47+
if ( \defined( 'ACTIVITYPUB_DISABLE_BLOG_USER' ) && ACTIVITYPUB_DISABLE_BLOG_USER ) {
48+
return ACTIVITYPUB_ACTOR_MODE;
49+
}
50+
51+
return $pre;
52+
}
53+
54+
/**
55+
* Pre-get option filter for the Authorized Fetch.
56+
*
57+
* @param string $pre The pre-get option value.
58+
*
59+
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
60+
*/
61+
public static function pre_option_activitypub_authorized_fetch( $pre ) {
62+
if ( ! \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
63+
return $pre;
64+
}
65+
66+
if ( ACTIVITYPUB_AUTHORIZED_FETCH ) {
67+
return '1';
68+
}
69+
70+
return '0';
71+
}
72+
73+
/**
74+
* Pre-get option filter for the Shared Inbox.
75+
*
76+
* @param string $pre The pre-get option value.
77+
*
78+
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
79+
*/
80+
public static function pre_option_activitypub_shared_inbox( $pre ) {
81+
if ( ! \defined( 'ACTIVITYPUB_SHARED_INBOX_FEATURE' ) ) {
82+
return $pre;
83+
}
84+
85+
if ( ACTIVITYPUB_SHARED_INBOX_FEATURE ) {
86+
return '1';
87+
}
88+
89+
return '0';
90+
}
91+
92+
/**
93+
* Pre-get option filter for the Vary Header.
94+
*
95+
* @param string $pre The pre-get option value.
96+
*
97+
* @return string If the constant is defined, return the value, otherwise return the pre-get option value.
98+
*/
99+
public static function pre_option_activitypub_vary_header( $pre ) {
100+
if ( ! \defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) ) {
101+
return $pre;
102+
}
103+
104+
if ( ACTIVITYPUB_SEND_VARY_HEADER ) {
105+
return '1';
106+
}
107+
108+
return '0';
109+
}
110+
111+
/**
112+
* Disallow interactions if the constant is set.
113+
*
114+
* @param bool $pre_option The value of the option.
115+
* @return bool|string The value of the option.
116+
*/
117+
public static function maybe_disable_interactions( $pre_option ) {
118+
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
119+
return '0';
120+
}
121+
122+
return $pre_option;
123+
}
124+
}

includes/constants.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
\defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false );
2020
\defined( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS', false );
2121
\defined( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS', false );
22-
\defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) || \define( 'ACTIVITYPUB_SEND_VARY_HEADER', false );
2322
\defined( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE' ) || \define( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE', 'wordpress-post-format' );
2423
\defined( 'ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE' ) || \define( 'ACTIVITYPUB_OUTBOX_PROCESSING_BATCH_SIZE', 100 );
2524

includes/wp-admin/class-advanced-settings-fields.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public static function register_advanced_fields() {
3939
array( 'label_for' => 'activitypub_outbox_purge_days' )
4040
);
4141

42+
if ( ! defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) ) {
43+
\add_settings_field(
44+
'activitypub_vary_header',
45+
\__( 'Vary Header', 'activitypub' ),
46+
array( self::class, 'render_vary_header_field' ),
47+
'activitypub_advanced_settings',
48+
'activitypub_advanced_settings',
49+
array( 'label_for' => 'activitypub_vary_header' )
50+
);
51+
}
52+
4253
if ( ! defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) ) {
4354
\add_settings_field(
4455
'activitypub_authorized_fetch',
@@ -101,6 +112,24 @@ public static function render_outbox_purge_days_field() {
101112
) . '</p>';
102113
}
103114

115+
/**
116+
* Render vary header field.
117+
*/
118+
public static function render_vary_header_field() {
119+
$value = \get_option( 'activitypub_vary_header', '0' );
120+
?>
121+
<p>
122+
<label>
123+
<input type="checkbox" id="activitypub_vary_header" name="activitypub_vary_header" value="1" <?php checked( '1', $value ); ?> />
124+
<?php echo \wp_kses( \__( 'Help prevent incorrect caching of ActivityPub responses.', 'activitypub' ), array( 'code' => array() ) ); ?>
125+
</label>
126+
</p>
127+
<p class="description">
128+
<?php \esc_html_e( 'Enable this if you notice your site showing technical content instead of normal web pages, or if your ActivityPub connections seem unreliable. This setting helps your site deliver the right format of content to different services automatically.', 'activitypub' ); ?>
129+
</p>
130+
<?php
131+
}
132+
104133
/**
105134
* Render use Authorized Fetch field.
106135
*/

includes/wp-admin/class-health-check.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ public static function debug_information( $info ) {
326326
'private' => false,
327327
);
328328

329+
$info['activitypub']['fields']['vary_header'] = array(
330+
'label' => \__( 'Vary Header', 'activitypub' ),
331+
'value' => \esc_attr( (int) \get_option( 'activitypub_vary_header', '0' ) ),
332+
'private' => false,
333+
);
334+
329335
$info['activitypub']['fields']['shared_inbox'] = array(
330336
'label' => \__( 'Shared Inbox', 'activitypub' ),
331337
'value' => \esc_attr( (int) \get_option( 'activitypub_shared_inbox', '0' ) ),

0 commit comments

Comments
 (0)