Skip to content

Commit 103d532

Browse files
obenlandpfefferle
andauthored
Fix: Handle WP_Error in transformer Factory gracefully (#2429)
Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 13d70b2 commit 103d532

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
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 fatal error when transformer Factory receives WP_Error objects.

includes/transformer/class-factory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Factory {
2727
* @return Base|\WP_Error The transformer to use, or an error.
2828
*/
2929
public static function get_transformer( $data ) {
30+
// Early return for WP_Error objects.
31+
if ( \is_wp_error( $data ) ) {
32+
return $data;
33+
}
34+
3035
if ( \is_string( $data ) && \filter_var( $data, FILTER_VALIDATE_URL ) ) {
3136
$response = Http::get_remote_object( $data );
3237

tests/phpunit/tests/includes/class-test-query.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ public function test_get_activitypub_object_id_for_author() {
122122
$this->assertEquals( $author_url, $query->get_activitypub_object_id() );
123123
}
124124

125+
/**
126+
* Test get_activitypub_object_id doesn't fatal when queried object filter returns WP_Error.
127+
*
128+
* @covers ::get_activitypub_object_id
129+
*/
130+
public function test_get_activitypub_object_id_with_wp_error_from_queried_object_filter() {
131+
// Mock a scenario where activitypub_queried_object filter returns WP_Error.
132+
$filter_callback = function ( $queried_object ) {
133+
// Return WP_Error to simulate an error condition.
134+
if ( $queried_object instanceof \WP_Post ) {
135+
return new \WP_Error( 'queried_object_error', 'Failed to process queried object' );
136+
}
137+
return $queried_object;
138+
};
139+
\add_filter( 'activitypub_queried_object', $filter_callback, 10, 1 );
140+
141+
Query::get_instance()->__destruct();
142+
$this->go_to( \get_permalink( self::$post_id ) );
143+
$query = Query::get_instance();
144+
145+
// This should not cause a fatal error.
146+
$result = $query->get_activitypub_object_id();
147+
148+
// Result should be null when queried object is error.
149+
$this->assertNull( $result );
150+
151+
// Clean up filter.
152+
\remove_filter( 'activitypub_queried_object', $filter_callback );
153+
}
154+
125155
/**
126156
* Test get_queried_object method.
127157
*

tests/phpunit/tests/includes/transformer/class-test-factory.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,19 @@ public function test_uri_transformation_error() {
267267

268268
\remove_filter( 'pre_http_request', $fake_request );
269269
}
270+
271+
/**
272+
* Test get_transformer with WP_Error input.
273+
*
274+
* @covers ::get_transformer
275+
*/
276+
public function test_get_transformer_with_wp_error() {
277+
$wp_error = new \WP_Error( 'test_error', 'Test error message' );
278+
$result = Factory::get_transformer( $wp_error );
279+
280+
// Should return the same WP_Error object.
281+
$this->assertWPError( $result );
282+
$this->assertEquals( 'test_error', $result->get_error_code() );
283+
$this->assertEquals( 'Test error message', $result->get_error_message() );
284+
}
270285
}

0 commit comments

Comments
 (0)