From 503353bcd0f31f6bca6078e89063c8a75b2043c2 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 26 May 2023 16:08:08 +0200 Subject: [PATCH] Added settings for blog-wide user --- assets/css/activitypub-admin.css | 16 +++++- includes/class-admin.php | 33 ++++++++++- includes/class-user-factory.php | 9 ++- includes/class-webfinger.php | 17 ++++-- includes/model/class-blog-user.php | 2 +- includes/rest/class-followers.php | 2 +- includes/rest/class-following.php | 2 +- includes/rest/class-inbox.php | 2 +- includes/rest/class-outbox.php | 2 +- includes/rest/class-users.php | 2 +- templates/settings.php | 90 +++++++++++++++++++++++++++--- 11 files changed, 152 insertions(+), 25 deletions(-) diff --git a/assets/css/activitypub-admin.css b/assets/css/activitypub-admin.css index 8925bfd..db4546c 100644 --- a/assets/css/activitypub-admin.css +++ b/assets/css/activitypub-admin.css @@ -1,3 +1,8 @@ +.activitypub-settings-body { + max-width: 800px; + margin: 0 auto; +} + .settings_page_activitypub .notice { max-width: 800px; margin: auto; @@ -115,7 +120,8 @@ summary { flex-grow: 1; } -.activitypub-settings-accordion-trigger .icon, .activitypub-settings-accordion-viewed .icon { +.activitypub-settings-accordion-trigger .icon, +.activitypub-settings-accordion-viewed .icon { border: solid #50575e medium; border-width: 0 2px 2px 0; height: .5rem; @@ -131,7 +137,8 @@ summary { transform: translateY(-30%) rotate(-135deg); } -.activitypub-settings-accordion-trigger:active, .activitypub-settings-accordion-trigger:hover { +.activitypub-settings-accordion-trigger:active, +.activitypub-settings-accordion-trigger:hover { background: #f6f7f7; } @@ -143,3 +150,8 @@ summary { outline: 2px solid #2271b1; background-color: #f6f7f7; } + +.activitypub-settings-body +input.blog-user-identifier { + text-align: right; +} diff --git a/includes/class-admin.php b/includes/class-admin.php index b1b5bc7..b88a8de 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -100,7 +100,11 @@ class Admin { 'description' => \__( 'Use title and link, summary, full or custom content', 'activitypub' ), 'show_in_rest' => array( 'schema' => array( - 'enum' => array( 'title', 'excerpt', 'content' ), + 'enum' => array( + 'title', + 'excerpt', + 'content', + ), ), ), 'default' => 'content', @@ -133,7 +137,11 @@ class Admin { 'description' => \__( 'The Activity-Object-Type', 'activitypub' ), 'show_in_rest' => array( 'schema' => array( - 'enum' => array( 'note', 'article', 'wordpress-post-format' ), + 'enum' => array( + 'note', + 'article', + 'wordpress-post-format', + ), ), ), 'default' => 'note', @@ -158,6 +166,27 @@ class Admin { 'default' => array( 'post', 'pages' ), ) ); + \register_setting( + 'activitypub', + 'activitypub_blog_user_identifier', + array( + 'type' => 'string', + 'description' => \esc_html__( 'The Identifier of th Blog-User', 'activitypub' ), + 'show_in_rest' => true, + 'default' => 'feed', + 'sanitize_callback' => function( $value ) { + // hack to allow dots in the username + $parts = explode( '.', $value ); + $sanitized = array(); + + foreach ( $parts as $part ) { + $sanitized[] = \sanitize_title( $part ); + } + + return implode( '.', $sanitized ); + }, + ) + ); } public static function add_settings_help_tab() { diff --git a/includes/class-user-factory.php b/includes/class-user-factory.php index 3ae3237..faa170e 100644 --- a/includes/class-user-factory.php +++ b/includes/class-user-factory.php @@ -58,10 +58,15 @@ class User_Factory { */ public static function get_by_username( $username ) { // check for blog user. - if ( get_option( 'activitypub_blog_identifier', null ) === $username ) { + if ( get_option( 'activitypub_blog_user_identifier', null ) === $username ) { return self::get_by_id( self::BLOG_USER_ID ); } + // check for application user. + if ( get_option( 'activitypub_application_user_identifier', null ) === $username ) { + return self::get_by_id( self::APPLICATION_USER_ID ); + } + // check for 'activitypub_username' meta $user = new WP_User_Query( array( @@ -71,7 +76,7 @@ class User_Factory { 'meta_query' => array( 'relation' => 'OR', array( - 'key' => 'activitypub_identifier', + 'key' => 'activitypub_user_identifier', 'value' => $username, 'compare' => 'LIKE', ), diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index 1581853..44a4a27 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -32,18 +32,25 @@ class Webfinger { return $user->user_login . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); } - public static function resolve( $account ) { - if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) { + /** + * Resolve a WebFinger resource + * + * @param string $resource The WebFinger resource + * + * @return string|WP_Error The URL or WP_Error + */ + public static function resolve( $resource ) { + if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $resource, $m ) ) { return null; } - $transient_key = 'activitypub_resolve_' . ltrim( $account, '@' ); + $transient_key = 'activitypub_resolve_' . ltrim( $resource, '@' ); $link = \get_transient( $transient_key ); if ( $link ) { return $link; } - $url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[2] . '/.well-known/webfinger' ); + $url = \add_query_arg( 'resource', 'acct:' . ltrim( $resource, '@' ), 'https://' . $m[2] . '/.well-known/webfinger' ); if ( ! \wp_http_validate_url( $url ) ) { $response = new WP_Error( 'invalid_webfinger_url', null, $url ); \set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period. @@ -82,7 +89,7 @@ class Webfinger { } } - $link = new WP_Error( 'webfinger_url_no_activity_pub', null, $body ); + $link = new WP_Error( 'webfinger_url_no_activitypub', null, $body ); \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $link; } diff --git a/includes/model/class-blog-user.php b/includes/model/class-blog-user.php index 944fe6d..2183efb 100644 --- a/includes/model/class-blog-user.php +++ b/includes/model/class-blog-user.php @@ -56,7 +56,7 @@ class Blog_User extends User { } public function get_username() { - return \esc_html( \get_option( 'activitypub_blog_identifier', 'feed' ) ); + return \esc_html( \get_option( 'activitypub_blog_user_identifier', 'feed' ) ); } public function get_avatar() { diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index 233c4ed..792cdfb 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -31,7 +31,7 @@ class Followers { public static function register_routes() { \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, - '/users/(?P\w+)/followers', + '/users/(?P[\w\-\.]+)/followers', array( array( 'methods' => WP_REST_Server::READABLE, diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 416d3a4..0d1de61 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -26,7 +26,7 @@ class Following { public static function register_routes() { \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, - '/users/(?P\w+)/following', + '/users/(?P[\w\-\.]+)/following', array( array( 'methods' => \WP_REST_Server::READABLE, diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 8e77edb..54e2894 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -48,7 +48,7 @@ class Inbox { \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, - '/users/(?P\w+)/inbox', + '/users/(?P[\w\-\.]+)/inbox', array( array( 'methods' => WP_REST_Server::EDITABLE, diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index d7e730f..cb09cfd 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -33,7 +33,7 @@ class Outbox { public static function register_routes() { \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, - '/users/(?P\w+)/outbox', + '/users/(?P[\w\-\.]+)/outbox', array( array( 'methods' => WP_REST_Server::READABLE, diff --git a/includes/rest/class-users.php b/includes/rest/class-users.php index 56a10bc..c6d92ee 100644 --- a/includes/rest/class-users.php +++ b/includes/rest/class-users.php @@ -27,7 +27,7 @@ class Users { public static function register_routes() { \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, - '/users/(?P\w+)', + '/users/(?P[\w\-\.]+)', array( array( 'methods' => WP_REST_Server::READABLE, diff --git a/templates/settings.php b/templates/settings.php index 0daca17..819dbb0 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -9,7 +9,7 @@ ); ?> -
+

+

+ +

+ + + + + + + + +
+ + + +

+ +

+
+ + +

@@ -42,16 +67,44 @@

- - +

- - +

- - +

- - +

@@ -98,13 +151,34 @@

- - +

- - +

- - +