Skip to content

Commit 6183d87

Browse files
authored
Blocks: Don't show contents in feeds (#1794)
1 parent 16f5746 commit 6183d87

File tree

9 files changed

+66
-36
lines changed

9 files changed

+66
-36
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: fixed
3+
4+
Blocks updated in 6.0.0 are back to not showing up in feeds and federated posts.

build/follow-me/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use Activitypub\Blocks;
99
use Activitypub\Collection\Actors;
1010
use Activitypub\Collection\Followers;
11+
use function Activitypub\is_activitypub_request;
12+
13+
if ( is_activitypub_request() || is_feed() ) {
14+
return;
15+
}
1116

1217
/* @var array $attributes Block attributes. */
1318
$attributes = wp_parse_args( $attributes );

build/followers/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
use Activitypub\Blocks;
99
use Activitypub\Collection\Actors;
1010
use Activitypub\Collection\Followers;
11+
use function Activitypub\is_activitypub_request;
1112
use function Activitypub\object_to_uri;
1213

14+
if ( is_activitypub_request() || is_feed() ) {
15+
return;
16+
}
17+
1318
/* @var array $attributes Block attributes. */
1419
$attributes = wp_parse_args( $attributes );
1520

build/reactions/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
use Activitypub\Comment;
99
use Activitypub\Blocks;
10+
use function Activitypub\is_activitypub_request;
11+
12+
if ( is_activitypub_request() || is_feed() ) {
13+
return;
14+
}
1015

1116
/* @var array $attributes Block attributes. */
1217
$attributes = wp_parse_args( $attributes, array( 'align' => null ) );

includes/class-query.php

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -267,50 +267,41 @@ protected function get_request_url() {
267267
* @return bool True if the request is an ActivityPub request, false otherwise.
268268
*/
269269
public function is_activitypub_request() {
270-
if ( isset( $this->is_activitypub_request ) ) {
271-
return $this->is_activitypub_request;
272-
}
273-
274-
global $wp_query;
270+
if ( ! isset( $this->is_activitypub_request ) ) {
271+
global $wp_query;
275272

276-
// One can trigger an ActivityPub request by adding `?activitypub` to the URL.
277-
if (
278-
isset( $wp_query->query_vars['activitypub'] ) ||
279-
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
280-
isset( $_GET['activitypub'] )
281-
) {
282-
\defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
283-
$this->is_activitypub_request = true;
273+
$this->is_activitypub_request = false;
284274

285-
return true;
286-
}
287-
288-
/*
289-
* The other (more common) option to make an ActivityPub request
290-
* is to send an Accept header.
291-
*/
292-
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
293-
$accept = \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_ACCEPT'] ) );
294-
295-
/*
296-
* $accept can be a single value, or a comma separated list of values.
297-
* We want to support both scenarios,
298-
* and return true when the header includes at least one of the following:
299-
* - application/activity+json
300-
* - application/ld+json
301-
* - application/json
302-
*/
303-
if ( \preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) {
275+
// One can trigger an ActivityPub request by adding `?activitypub` to the URL.
276+
if ( isset( $wp_query->query_vars['activitypub'] ) || isset( $_GET['activitypub'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
304277
\defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
305278
$this->is_activitypub_request = true;
306279

307-
return true;
280+
// The other (more common) option to make an ActivityPub request is to send an Accept header.
281+
} elseif ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
282+
$accept = \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_ACCEPT'] ) );
283+
284+
/*
285+
* $accept can be a single value, or a comma separated list of values.
286+
* We want to support both scenarios,
287+
* and return true when the header includes at least one of the following:
288+
* - application/activity+json
289+
* - application/ld+json
290+
* - application/json
291+
*/
292+
if ( \preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) {
293+
\defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
294+
$this->is_activitypub_request = true;
295+
}
308296
}
309297
}
310298

311-
$this->is_activitypub_request = false;
312-
313-
return false;
299+
/**
300+
* Filters whether the current request is an ActivityPub request.
301+
*
302+
* @param bool $is_activitypub_request True if the request is an ActivityPub request, false otherwise.
303+
*/
304+
return \apply_filters( 'activitypub_is_activitypub_request', $this->is_activitypub_request );
314305
}
315306

316307
/**

includes/collection/class-outbox.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public static function add( Activity $activity, $user_id, $visibility = ACTIVITY
4040
$activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
4141
}
4242

43+
// Save activity in the context of an activitypub request.
44+
\add_filter( 'activitypub_is_activitypub_request', '__return_true' );
45+
4346
$outbox_item = array(
4447
'post_type' => self::POST_TYPE,
4548
'post_title' => sprintf(
@@ -60,6 +63,8 @@ public static function add( Activity $activity, $user_id, $visibility = ACTIVITY
6063
),
6164
);
6265

66+
\remove_filter( 'activitypub_is_activitypub_request', '__return_true' );
67+
6368
$has_kses = false !== \has_filter( 'content_save_pre', 'wp_filter_post_kses' );
6469
if ( $has_kses ) {
6570
// Prevent KSES from corrupting JSON in post_content.

src/follow-me/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use Activitypub\Blocks;
99
use Activitypub\Collection\Actors;
1010
use Activitypub\Collection\Followers;
11+
use function Activitypub\is_activitypub_request;
12+
13+
if ( is_activitypub_request() || is_feed() ) {
14+
return;
15+
}
1116

1217
/* @var array $attributes Block attributes. */
1318
$attributes = wp_parse_args( $attributes );

src/followers/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
use Activitypub\Blocks;
99
use Activitypub\Collection\Actors;
1010
use Activitypub\Collection\Followers;
11+
use function Activitypub\is_activitypub_request;
1112
use function Activitypub\object_to_uri;
1213

14+
if ( is_activitypub_request() || is_feed() ) {
15+
return;
16+
}
17+
1318
/* @var array $attributes Block attributes. */
1419
$attributes = wp_parse_args( $attributes );
1520

src/reactions/render.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
use Activitypub\Comment;
99
use Activitypub\Blocks;
10+
use function Activitypub\is_activitypub_request;
11+
12+
if ( is_activitypub_request() || is_feed() ) {
13+
return;
14+
}
1015

1116
/* @var array $attributes Block attributes. */
1217
$attributes = wp_parse_args( $attributes, array( 'align' => null ) );

0 commit comments

Comments
 (0)