Skip to content

Commit e751086

Browse files
authored
Add URI transformer (#1292)
* Add URI transformer * Migrate a URL to an object * Simplify code * Fix tests * Simplify code * Add changelog * use WPError assertion props @obenland * phpcs fixes * removed unused transformer * update changelog
1 parent 22c8d1e commit e751086

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
* The Outbox purging routine no longer is limited to deleting 5 items at a time.
1717
* Undo API for Outbox items.
18+
* Allow Activities on URLs instead of requiring Activity-Objects. This is useful especially for sending Announces and Likes.
1819

1920
## [5.2.0] - 2025-02-13
2021

includes/class-http.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,7 @@ public static function generate_cache_key( $url ) {
235235
* @return array|WP_Error The Object data as array or WP_Error on failure.
236236
*/
237237
public static function get_remote_object( $url_or_object, $cached = true ) {
238-
if ( is_array( $url_or_object ) ) {
239-
if ( array_key_exists( 'id', $url_or_object ) ) {
240-
$url = $url_or_object['id'];
241-
} elseif ( array_key_exists( 'url', $url_or_object ) ) {
242-
$url = $url_or_object['url'];
243-
} else {
244-
return new WP_Error(
245-
'activitypub_no_valid_actor_identifier',
246-
\__( 'The "actor" identifier is not valid', 'activitypub' ),
247-
array(
248-
'status' => 404,
249-
'object' => $url_or_object,
250-
)
251-
);
252-
}
253-
} else {
254-
$url = $url_or_object;
255-
}
238+
$url = object_to_uri( $url_or_object );
256239

257240
if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $url ) ) {
258241
$url = Webfinger::resolve( $url );

includes/functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,10 @@ function object_to_uri( $data ) {
797797
return $data;
798798
}
799799

800+
if ( is_object( $data ) ) {
801+
$data = $data->to_array();
802+
}
803+
800804
/*
801805
* Check if it is a list, then take first item.
802806
* This plugin does not support collections.

includes/transformer/class-factory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Activitypub\Transformer;
99

1010
use WP_Error;
11+
use Activitypub\Http;
1112
use Activitypub\Comment as Comment_Helper;
1213

1314
use function Activitypub\is_user_disabled;
@@ -25,7 +26,16 @@ class Factory {
2526
* @return Base|WP_Error The transformer to use, or an error.
2627
*/
2728
public static function get_transformer( $data ) {
28-
if ( \is_array( $data ) || \is_string( $data ) ) {
29+
if ( \is_string( $data ) && \filter_var( $data, FILTER_VALIDATE_URL ) ) {
30+
$response = Http::get_remote_object( $data );
31+
32+
if ( \is_wp_error( $response ) ) {
33+
return $response;
34+
}
35+
36+
$class = 'json';
37+
$data = $response;
38+
} elseif ( \is_array( $data ) || \is_string( $data ) ) {
2939
$class = 'json';
3040
} elseif ( \is_object( $data ) ) {
3141
$class = \get_class( $data );

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ For reasons of data protection, it is not possible to see the followers of other
131131

132132
= Unreleased =
133133

134+
* Added: Allow Activities on URLs instead of requiring Activity-Objects. This is useful especially for sending Announces and Likes.
134135
* Added: Undo API for Outbox items.
135136
* Added: Setting to adjust the number of days Outbox items are kept before being purged.
136137
* Fixed: The Outbox purging routine no longer is limited to deleting 5 items at a time.

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,53 @@ function () {
214214

215215
remove_all_filters( 'activitypub_transformer' );
216216
}
217+
218+
/**
219+
* Test successful URI transformation.
220+
*/
221+
public function test_successful_uri_transformation() {
222+
// Mock-Daten für die HTTP-Antwort.
223+
$fake_request = function () {
224+
return array(
225+
'response' => array( 'code' => 200 ),
226+
'body' => wp_json_encode(
227+
array(
228+
'id' => 'https://example.com/activity/1',
229+
'type' => 'Note',
230+
'content' => 'Test Content',
231+
)
232+
),
233+
);
234+
};
235+
236+
add_filter( 'pre_http_request', $fake_request, 10 );
237+
238+
$uri_transformer = Factory::get_transformer( 'https://example.com/activity/1' );
239+
$result = $uri_transformer->to_object();
240+
241+
$this->assertIsObject( $result );
242+
$this->assertEquals( 'https://example.com/activity/1', $result->get_id() );
243+
$this->assertEquals( 'Note', $result->get_type() );
244+
$this->assertEquals( 'Test Content', $result->get_content() );
245+
246+
remove_filter( 'pre_http_request', $fake_request, 10 );
247+
}
248+
249+
/**
250+
* Test URI transformation with error.
251+
*/
252+
public function test_uri_transformation_error() {
253+
// WP_Error für fehlgeschlagene Anfrage erstellen.
254+
$fake_request = function () {
255+
return new \WP_Error( 'fetch_error', 'Failed to fetch remote object' );
256+
};
257+
258+
add_filter( 'pre_http_request', $fake_request, 10 );
259+
260+
$uri_transformer = Factory::get_transformer( 'https://example.com/invalid' );
261+
262+
$this->assertWPError( $uri_transformer );
263+
264+
remove_filter( 'pre_http_request', $fake_request, 10 );
265+
}
217266
}

0 commit comments

Comments
 (0)