Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make username configurable #915

Draft
wants to merge 9 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 67 additions & 21 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,9 @@ public static function register_settings() {
'show_in_rest' => true,
'default' => Blog::get_default_username(),
'sanitize_callback' => function ( $value ) {
// hack to allow dots in the username
$parts = explode( '.', $value );
$sanitized = array();
$sanatized = self::sanatize_identifier( $value );

foreach ( $parts as $part ) {
$sanitized[] = \sanitize_title( $part );
}

$sanitized = implode( '.', $sanitized );

// check for login or nicename.
$user = new WP_User_Query(
array(
'search' => $sanitized,
'search_columns' => array( 'user_login', 'user_nicename' ),
'number' => 1,
'hide_empty' => true,
'fields' => 'ID',
)
);

if ( $user->results ) {
if ( \is_wp_error( $sanatized ) ) {
add_settings_error(
'activitypub_blog_identifier',
'activitypub_blog_identifier',
Expand Down Expand Up @@ -367,6 +348,7 @@ public static function add_followers_list_help_tab() {

public static function add_profile( $user ) {
$description = \get_user_option( 'activitypub_description', $user->ID );
$identifier = \get_user_option( 'activitypub_identifier', $user->ID );

wp_enqueue_media();
wp_enqueue_script( 'activitypub-header-image' );
Expand All @@ -376,6 +358,7 @@ public static function add_profile( $user ) {
true,
array(
'description' => $description,
'identifier' => $identifier,
)
);
}
Expand Down Expand Up @@ -413,6 +396,26 @@ public static function save_user_settings( $user_id ) {
} else {
\delete_user_option( $user_id, 'activitypub_header_image' );
}

$identifier = ! empty( $_POST['activitypub_identifier'] ) ? sanitize_text_field( wp_unslash( $_POST['activitypub_identifier'] ) ) : false;
$identifier = self::sanatize_identifier( $identifier );

if ( ! \is_wp_error( $identifier ) ) {
\update_user_option( $user_id, 'activitypub_identifier', $identifier );
} else {
if ( \is_wp_error( $identifier ) ) {
// show error message on user settings page
add_action(
'user_profile_update_errors',
function ( $errors ) use ( $identifier ) {
$errors->add( 'activitypub_identifier', $identifier->get_error_message() );
},
10,
3
);
}
\delete_user_option( $user_id, 'activitypub_identifier' );
}
}

public static function enqueue_scripts( $hook_suffix ) {
Expand Down Expand Up @@ -758,4 +761,47 @@ public static function dashboard_glance_items( $items ) {

return $items;
}

/**
* Sanatize the identifier
*
* @param string $id The identifier.
*
* @return false|string The sanatized identifier or false if it is already in use.
*/
private static function sanatize_identifier( $id ) {
if ( empty( $id ) ) {
return false;
}

// hack to allow dots in the username
$parts = explode( '.', $id );
$sanitized = array();

foreach ( $parts as $part ) {
$sanitized[] = \sanitize_title( $part );
}

$sanitized = implode( '.', $sanitized );

// check for login or nicename.
$user = new WP_User_Query(
array(
'search' => $sanitized,
'search_columns' => array( 'user_login', 'user_nicename' ),
'number' => 1,
'hide_empty' => true,
'fields' => 'ID',
)
);

if ( $user->get_results() ) {
return new \WP_Error(
'identifier_exists',
\__( 'This identifier is already in use.', 'activitypub' )
);
}
pfefferle marked this conversation as resolved.
Show resolved Hide resolved

return $sanitized;
}
}
1 change: 1 addition & 0 deletions includes/class-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ public static function user_meta_update( $meta_id, $user_id, $meta_key ) {
$fields = array(
'activitypub_description',
'activitypub_header_image',
'activitypub_identifier',
'description',
'user_url',
'display_name',
Expand Down
13 changes: 10 additions & 3 deletions includes/collection/class-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public static function get_by_username( $username ) {
return new Application();
}

global $wpdb;

// check for 'activitypub_username' meta
$user = new WP_User_Query(
array(
Expand All @@ -96,9 +98,14 @@ public static function get_by_username( $username ) {
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'activitypub_user_identifier',
'key' => 'activitypub_identifier',
'value' => $username,
'compare' => '=',
),
array(
Comment on lines +101 to +105
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not worth addressing but it could happen that you assign the identifier to a non-existent user at the time, and then register that username afterwards. In this scenario I believe the result here is undefined since you just pick the first result in the code below.

'key' => $wpdb->prefix . 'activitypub_identifier',
'value' => $username,
'compare' => 'LIKE',
'compare' => '=',
),
),
)
Expand Down Expand Up @@ -248,7 +255,7 @@ public static function get_by_various( $id ) {
$user = self::get_by_resource( $id );
}

if ( $user && ! is_wp_error( $user ) ) {
if ( $user && ! \is_wp_error( $user ) ) {
return $user;
}

Expand Down
9 changes: 7 additions & 2 deletions includes/model/class-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public function get_name() {
* @return string The User-Description.
*/
public function get_summary() {
$description = get_user_option( 'activitypub_description', $this->_id );
$description = \get_user_option( 'activitypub_description', $this->_id );
if ( empty( $description ) ) {
$description = get_user_meta( $this->_id, 'description', true );
$description = \get_user_meta( $this->_id, 'description', true );
}
return \wpautop( \wp_kses( $description, 'default' ) );
}
Expand All @@ -132,6 +132,11 @@ public function get_alternate_url() {
}

public function get_preferred_username() {
$custom_user_identifier = get_user_option( 'activitypub_identifier', $this->_id );
if ( $custom_user_identifier ) {
return \esc_attr( $custom_user_identifier );
}

return \esc_attr( \get_the_author_meta( 'login', $this->_id ) );
}

Expand Down
7 changes: 6 additions & 1 deletion templates/user-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
<tbody>
<tr>
<th scope="row">
<label><?php \esc_html_e( 'Profile URL', 'activitypub' ); ?></label>
<label><?php \esc_html_e( 'Profile URI', 'activitypub' ); ?></label>
</th>
<td>
<p>
<label>
<input name="activitypub_identifier" type="text" value="<?php echo \esc_attr( $args['identifier'] ); ?>" placeholder="<?php echo esc_attr( $user->get_preferred_username() ); ?>" style="text-align: right;" />@<?php echo esc_html( \wp_parse_url( \home_url(), PHP_URL_HOST ) ); ?>
</label>
</p>
<p class="description">
<code><?php echo \esc_html( $user->get_webfinger() ); ?></code> or
<code><?php echo \esc_url( $user->get_url() ); ?></code>
</p>
Expand Down
2 changes: 1 addition & 1 deletion tests/test-class-activitypub-users-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public function set_up() {
parent::set_up();

add_option( 'activitypub_blog_identifier', 'blog' );
add_user_meta( 1, 'activitypub_user_identifier', 'admin' );
add_user_meta( 1, 'activitypub_identifier', 'admin' );
}
/**
* @dataProvider the_resource_provider
Expand Down
Loading