Skip to content

Commit 4becaeb

Browse files
authored
Normalize Account-Aliases to always use the ID (#1974)
1 parent 34bc0d9 commit 4becaeb

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Improved Account-Aliases handling by internally normalizing input formats.

includes/class-activitypub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public static function register_user_meta() {
762762
'description' => 'An array of URLs that the user is known by.',
763763
'single' => true,
764764
'default' => array(),
765-
'sanitize_callback' => array( Sanitize::class, 'url_list' ),
765+
'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
766766
)
767767
);
768768

includes/class-sanitize.php

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

88
namespace Activitypub;
99

10+
use Activitypub\Collection\Actors;
1011
use Activitypub\Model\Blog;
1112

1213
/**
@@ -32,6 +33,50 @@ public static function url_list( $value ) {
3233
return \array_values( $value );
3334
}
3435

36+
/**
37+
* Sanitize and normalize a list of account identifiers to ActivityPub IDs.
38+
*
39+
* This function processes various identifier formats, such as URLs and
40+
* webfinger identifiers, and normalizes them into a consistent format.
41+
*
42+
* @param string|array $value The value to sanitize.
43+
*
44+
* @return array The sanitized and normalized list of account identifiers.
45+
*/
46+
public static function identifier_list( $value ) {
47+
if ( ! \is_array( $value ) ) {
48+
$value = \explode( PHP_EOL, $value );
49+
}
50+
51+
$value = \array_filter( $value );
52+
$uris = array();
53+
54+
foreach ( $value as $uri ) {
55+
$uri = \trim( $uri );
56+
$uri = \ltrim( $uri, '@' );
57+
58+
if ( \is_email( $uri ) ) {
59+
$_uri = Webfinger::resolve( $uri );
60+
if ( \is_wp_error( $_uri ) ) {
61+
$uris[] = $uri;
62+
continue;
63+
}
64+
65+
$uri = $_uri;
66+
}
67+
68+
$uri = \sanitize_url( $uri );
69+
$actor = Actors::fetch_remote_by_uri( $uri );
70+
if ( \is_wp_error( $actor ) ) {
71+
$uris[] = $uri;
72+
} else {
73+
$uris[] = \sanitize_url( $actor->guid );
74+
}
75+
}
76+
77+
return \array_values( \array_unique( $uris ) );
78+
}
79+
3580
/**
3681
* Sanitize a list of hosts.
3782
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class="large-text"
298298
<?php esc_html_e( 'If you’re moving from another account to this one, you’ll need to create an alias here first before transferring your followers. This step is safe, reversible, and doesn’t affect anything on its own. The migration itself is initiated from your old account.', 'activitypub' ); ?>
299299
</p>
300300
<p class="description">
301-
<?php esc_html_e( 'Enter one URL per line.', 'activitypub' ); ?>
301+
<?php echo \wp_kses_post( \__( 'Enter one account per line. Profile links or usernames like <code>@[email protected]</code> are accepted and will be automatically normalized to the correct format.', 'activitypub' ) ); ?>
302302
</p>
303303
<?php
304304
}

includes/wp-admin/class-settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static function register_settings() {
311311
'type' => 'array',
312312
'description' => 'An array of URLs that the blog user is known by.',
313313
'default' => array(),
314-
'sanitize_callback' => array( Sanitize::class, 'url_list' ),
314+
'sanitize_callback' => array( Sanitize::class, 'identifier_list' ),
315315
)
316316
);
317317
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class="large-text"
270270
<?php \esc_html_e( 'If you&#8217;re moving from another account to this one, you&#8217;ll need to create an alias here first before transferring your followers. This step is safe, reversible, and doesn&#8217;t affect anything on its own. The migration itself is initiated from your old account.', 'activitypub' ); ?>
271271
</p>
272272
<p class="description">
273-
<?php \esc_html_e( 'Enter one URL per line.', 'activitypub' ); ?>
273+
<?php echo \wp_kses_post( \__( 'Enter one account per line. Profile links or usernames like <code>@[email protected]</code> are accepted and will be automatically normalized to the correct format.', 'activitypub' ) ); ?>
274274
</p>
275275
<?php
276276
}

0 commit comments

Comments
 (0)