Skip to content

Commit 28f0666

Browse files
authored
Return proper ID instead of object (#1453)
1 parent e5db776 commit 28f0666

File tree

6 files changed

+78
-34
lines changed

6 files changed

+78
-34
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+
Fixed an issue with the Content Carousel and Blog Posts block: https://github.com/Automattic/wp-calypso/issues/101220

.github/changelog/fix-query

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+
Ensured that Query::get_object_id() returns an ID instead of an Object.
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+
Improved and simplified Query code.

includes/class-activitypub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static function uninstall() {
123123
* @return string The new path to the JSON template.
124124
*/
125125
public static function render_activitypub_template( $template ) {
126-
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
126+
if ( \wp_is_serving_rest_request() || \wp_doing_ajax() ) {
127127
return $template;
128128
}
129129

includes/class-query.php

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,12 @@ public function get_activitypub_object() {
8585
return $this->activitypub_object;
8686
}
8787

88-
$queried_object = $this->get_queried_object();
89-
90-
// Check for Outbox Activity.
91-
if (
92-
$queried_object instanceof \WP_Post &&
93-
Outbox::POST_TYPE === $queried_object->post_type
94-
) {
95-
$activitypub_object = Outbox::maybe_get_activity( $queried_object );
96-
97-
// Check if the Outbox Activity is public.
98-
if ( ! \is_wp_error( $activitypub_object ) ) {
99-
$this->activitypub_object = $activitypub_object;
100-
101-
return $this->activitypub_object;
102-
}
103-
}
104-
105-
if ( ! $queried_object ) {
106-
// If the object is not a valid ActivityPub object, try to get a virtual object.
107-
$activitypub_object = $this->maybe_get_virtual_object();
108-
109-
if ( $activitypub_object ) {
110-
$this->activitypub_object = $activitypub_object;
111-
112-
return $this->activitypub_object;
113-
}
88+
if ( $this->prepare_activitypub_data() ) {
89+
return $this->activitypub_object;
11490
}
11591

116-
$transformer = Factory::get_transformer( $queried_object );
92+
$queried_object = $this->get_queried_object();
93+
$transformer = Factory::get_transformer( $queried_object );
11794

11895
if ( $transformer && ! \is_wp_error( $transformer ) ) {
11996
$this->activitypub_object = $transformer->to_object();
@@ -132,13 +109,55 @@ public function get_activitypub_object_id() {
132109
return $this->activitypub_object_id;
133110
}
134111

135-
if ( $this->activitypub_object ) {
136-
$this->activitypub_object_id = $this->activitypub_object->get_id();
137-
112+
if ( $this->prepare_activitypub_data() ) {
138113
return $this->activitypub_object_id;
139114
}
140115

141-
return $this->get_activitypub_object();
116+
$queried_object = $this->get_queried_object();
117+
$transformer = Factory::get_transformer( $queried_object );
118+
119+
if ( $transformer && ! \is_wp_error( $transformer ) ) {
120+
$this->activitypub_object_id = $transformer->to_id();
121+
}
122+
123+
return $this->activitypub_object_id;
124+
}
125+
126+
/**
127+
* Prepare and set both ActivityPub object and ID for Outbox activities and virtual objects.
128+
*
129+
* @return bool True if an object was found and set, false otherwise.
130+
*/
131+
private function prepare_activitypub_data() {
132+
$queried_object = $this->get_queried_object();
133+
134+
// Check for Outbox Activity.
135+
if (
136+
$queried_object instanceof \WP_Post &&
137+
Outbox::POST_TYPE === $queried_object->post_type
138+
) {
139+
$activitypub_object = Outbox::maybe_get_activity( $queried_object );
140+
141+
// Check if the Outbox Activity is public.
142+
if ( ! \is_wp_error( $activitypub_object ) ) {
143+
$this->activitypub_object = $activitypub_object;
144+
$this->activitypub_object_id = $this->activitypub_object->get_id();
145+
return true;
146+
}
147+
}
148+
149+
if ( ! $queried_object ) {
150+
// If the object is not a valid ActivityPub object, try to get a virtual object.
151+
$activitypub_object = $this->maybe_get_virtual_object();
152+
153+
if ( $activitypub_object ) {
154+
$this->activitypub_object = $activitypub_object;
155+
$this->activitypub_object_id = $this->activitypub_object->get_id();
156+
return true;
157+
}
158+
}
159+
160+
return false;
142161
}
143162

144163
/**
@@ -253,7 +272,7 @@ public function is_activitypub_request() {
253272
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
254273
isset( $_GET['activitypub'] )
255274
) {
256-
defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
275+
\defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
257276
$this->is_activitypub_request = true;
258277

259278
return true;
@@ -275,7 +294,7 @@ public function is_activitypub_request() {
275294
* - application/json
276295
*/
277296
if ( \preg_match( '/(application\/(ld\+json|activity\+json|json))/i', $accept ) ) {
278-
defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
297+
\defined( 'ACTIVITYPUB_REQUEST' ) || \define( 'ACTIVITYPUB_REQUEST', true );
279298
$this->is_activitypub_request = true;
280299

281300
return true;

includes/compat.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,16 @@ function str_contains( $haystack, $needle ) {
9696
return false !== strpos( $haystack, $needle );
9797
}
9898
}
99+
100+
if ( ! function_exists( 'wp_is_serving_rest_request' ) ) {
101+
/**
102+
* Polyfill for `wp_is_serving_rest_request()` function added in WordPress 6.5.
103+
*
104+
* @see https://developer.wordpress.org/reference/functions/wp_is_serving_rest_request/
105+
*
106+
* @return bool True if it's a WordPress REST API request, false otherwise.
107+
*/
108+
function wp_is_serving_rest_request() {
109+
return defined( 'REST_REQUEST' ) && REST_REQUEST;
110+
}
111+
}

0 commit comments

Comments
 (0)