Added settings for blog-wide user

This commit is contained in:
Matthias Pfefferle 2023-05-26 16:08:08 +02:00
parent 3feef1e8cf
commit 503353bcd0
11 changed files with 152 additions and 25 deletions

View file

@ -1,3 +1,8 @@
.activitypub-settings-body {
max-width: 800px;
margin: 0 auto;
}
.settings_page_activitypub .notice { .settings_page_activitypub .notice {
max-width: 800px; max-width: 800px;
margin: auto; margin: auto;
@ -115,7 +120,8 @@ summary {
flex-grow: 1; 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: solid #50575e medium;
border-width: 0 2px 2px 0; border-width: 0 2px 2px 0;
height: .5rem; height: .5rem;
@ -131,7 +137,8 @@ summary {
transform: translateY(-30%) rotate(-135deg); 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; background: #f6f7f7;
} }
@ -143,3 +150,8 @@ summary {
outline: 2px solid #2271b1; outline: 2px solid #2271b1;
background-color: #f6f7f7; background-color: #f6f7f7;
} }
.activitypub-settings-body
input.blog-user-identifier {
text-align: right;
}

View file

@ -100,7 +100,11 @@ class Admin {
'description' => \__( 'Use title and link, summary, full or custom content', 'activitypub' ), 'description' => \__( 'Use title and link, summary, full or custom content', 'activitypub' ),
'show_in_rest' => array( 'show_in_rest' => array(
'schema' => array( 'schema' => array(
'enum' => array( 'title', 'excerpt', 'content' ), 'enum' => array(
'title',
'excerpt',
'content',
),
), ),
), ),
'default' => 'content', 'default' => 'content',
@ -133,7 +137,11 @@ class Admin {
'description' => \__( 'The Activity-Object-Type', 'activitypub' ), 'description' => \__( 'The Activity-Object-Type', 'activitypub' ),
'show_in_rest' => array( 'show_in_rest' => array(
'schema' => array( 'schema' => array(
'enum' => array( 'note', 'article', 'wordpress-post-format' ), 'enum' => array(
'note',
'article',
'wordpress-post-format',
),
), ),
), ),
'default' => 'note', 'default' => 'note',
@ -158,6 +166,27 @@ class Admin {
'default' => array( 'post', 'pages' ), '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() { public static function add_settings_help_tab() {

View file

@ -58,10 +58,15 @@ class User_Factory {
*/ */
public static function get_by_username( $username ) { public static function get_by_username( $username ) {
// check for blog user. // 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 ); 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 // check for 'activitypub_username' meta
$user = new WP_User_Query( $user = new WP_User_Query(
array( array(
@ -71,7 +76,7 @@ class User_Factory {
'meta_query' => array( 'meta_query' => array(
'relation' => 'OR', 'relation' => 'OR',
array( array(
'key' => 'activitypub_identifier', 'key' => 'activitypub_user_identifier',
'value' => $username, 'value' => $username,
'compare' => 'LIKE', 'compare' => 'LIKE',
), ),

View file

@ -32,18 +32,25 @@ class Webfinger {
return $user->user_login . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); 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; return null;
} }
$transient_key = 'activitypub_resolve_' . ltrim( $account, '@' ); $transient_key = 'activitypub_resolve_' . ltrim( $resource, '@' );
$link = \get_transient( $transient_key ); $link = \get_transient( $transient_key );
if ( $link ) { if ( $link ) {
return $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 ) ) { if ( ! \wp_http_validate_url( $url ) ) {
$response = new WP_Error( 'invalid_webfinger_url', null, $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. \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. \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
return $link; return $link;
} }

View file

@ -56,7 +56,7 @@ class Blog_User extends User {
} }
public function get_username() { 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() { public function get_avatar() {

View file

@ -31,7 +31,7 @@ class Followers {
public static function register_routes() { public static function register_routes() {
\register_rest_route( \register_rest_route(
ACTIVITYPUB_REST_NAMESPACE, ACTIVITYPUB_REST_NAMESPACE,
'/users/(?P<user_id>\w+)/followers', '/users/(?P<user_id>[\w\-\.]+)/followers',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,

View file

@ -26,7 +26,7 @@ class Following {
public static function register_routes() { public static function register_routes() {
\register_rest_route( \register_rest_route(
ACTIVITYPUB_REST_NAMESPACE, ACTIVITYPUB_REST_NAMESPACE,
'/users/(?P<user_id>\w+)/following', '/users/(?P<user_id>[\w\-\.]+)/following',
array( array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,

View file

@ -48,7 +48,7 @@ class Inbox {
\register_rest_route( \register_rest_route(
ACTIVITYPUB_REST_NAMESPACE, ACTIVITYPUB_REST_NAMESPACE,
'/users/(?P<user_id>\w+)/inbox', '/users/(?P<user_id>[\w\-\.]+)/inbox',
array( array(
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,

View file

@ -33,7 +33,7 @@ class Outbox {
public static function register_routes() { public static function register_routes() {
\register_rest_route( \register_rest_route(
ACTIVITYPUB_REST_NAMESPACE, ACTIVITYPUB_REST_NAMESPACE,
'/users/(?P<user_id>\w+)/outbox', '/users/(?P<user_id>[\w\-\.]+)/outbox',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,

View file

@ -27,7 +27,7 @@ class Users {
public static function register_routes() { public static function register_routes() {
\register_rest_route( \register_rest_route(
ACTIVITYPUB_REST_NAMESPACE, ACTIVITYPUB_REST_NAMESPACE,
'/users/(?P<user_id>\w+)', '/users/(?P<user_id>[\w\-\.]+)',
array( array(
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,

View file

@ -9,7 +9,7 @@
); );
?> ?>
<div class="privacy-settings-body hide-if-no-js"> <div class="activitypub-settings-body hide-if-no-js">
<div class="notice notice-info"> <div class="notice notice-info">
<p> <p>
<?php <?php
@ -30,6 +30,31 @@
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php \settings_fields( 'activitypub' ); ?> <?php \settings_fields( 'activitypub' ); ?>
<h3><?php \esc_html_e( 'Blog-User', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'All activity related settings.', 'activitypub' ); ?></p>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<?php \esc_html_e( 'User-Identifier', 'activitypub' ); ?>
</th>
<td>
<label for="activitypub_blog_user_identifier">
<input class="blog-user-identifier" name="activitypub_blog_user_identifier" id="activitypub_blog_user_identifier" type="text" value="<?php echo esc_attr( \get_option( 'activitypub_blog_user_identifier', 'feed' ) ); ?>" />
@<?php echo esc_html( \wp_parse_url( \home_url(), PHP_URL_HOST ) ); ?>
</label>
<p class="description">
</p>
</td>
</tr>
</tbody>
</table>
<?php \do_settings_fields( 'activitypub', 'blog-user' ); ?>
<h3><?php \esc_html_e( 'Activities', 'activitypub' ); ?></h3> <h3><?php \esc_html_e( 'Activities', 'activitypub' ); ?></h3>
<p><?php \esc_html_e( 'All activity related settings.', 'activitypub' ); ?></p> <p><?php \esc_html_e( 'All activity related settings.', 'activitypub' ); ?></p>
@ -42,16 +67,44 @@
</th> </th>
<td> <td>
<p> <p>
<label><input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_title_link" value="title" <?php echo \checked( 'title', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> /> <?php \esc_html_e( 'Title and link', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'Only the title and a link.', 'activitypub' ); ?></span> <label for="activitypub_post_content_type_title_link">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_title_link" value="title" <?php echo \checked( 'title', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Title and link', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Only the title and a link.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<label><input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_excerpt" value="excerpt" <?php echo \checked( 'excerpt', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> /> <?php \esc_html_e( 'Excerpt', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'A content summary, shortened to 400 characters and without markup.', 'activitypub' ); ?></span> <label for="activitypub_post_content_type_excerpt">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_excerpt" value="excerpt" <?php echo \checked( 'excerpt', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Excerpt', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'A content summary, shortened to 400 characters and without markup.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<label><input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_content" value="content" <?php echo \checked( 'content', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> /> <?php \esc_html_e( 'Content (default)', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'The full content.', 'activitypub' ); ?></span> <label for="activitypub_post_content_type_content">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_content" value="content" <?php echo \checked( 'content', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Content (default)', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'The full content.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<label><input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_custom" value="custom" <?php echo \checked( 'custom', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> /> <?php \esc_html_e( 'Custom', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'Use the text-area below, to customize your activities.', 'activitypub' ); ?></span> <label for="activitypub_post_content_type_custom">
<input type="radio" name="activitypub_post_content_type" id="activitypub_post_content_type_custom" value="custom" <?php echo \checked( 'custom', \get_option( 'activitypub_post_content_type', 'content' ) ); ?> />
<?php \esc_html_e( 'Custom', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Use the text-area below, to customize your activities.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<textarea name="activitypub_custom_post_content" id="activitypub_custom_post_content" rows="10" cols="50" class="large-text" placeholder="<?php echo wp_kses( ACTIVITYPUB_CUSTOM_POST_CONTENT, 'post' ); ?>"><?php echo wp_kses( \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ), 'post' ); ?></textarea> <textarea name="activitypub_custom_post_content" id="activitypub_custom_post_content" rows="10" cols="50" class="large-text" placeholder="<?php echo wp_kses( ACTIVITYPUB_CUSTOM_POST_CONTENT, 'post' ); ?>"><?php echo wp_kses( \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ), 'post' ); ?></textarea>
@ -98,13 +151,34 @@
</th> </th>
<td> <td>
<p> <p>
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo \checked( 'note', \get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php \esc_html_e( 'Note (default)', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'Should work with most platforms.', 'activitypub' ); ?></span> <label for="activitypub_object_type_note">
<input type="radio" name="activitypub_object_type" id="activitypub_object_type_note" value="note" <?php echo \checked( 'note', \get_option( 'activitypub_object_type', 'note' ) ); ?> />
<?php \esc_html_e( 'Note (default)', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Should work with most platforms.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type_article" value="article" <?php echo \checked( 'article', \get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php \esc_html_e( 'Article', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'The presentation of the "Article" might change on different platforms. Mastodon for example shows the "Article" type as a simple link.', 'activitypub' ); ?></span> <label for="activitypub_object_type_article">
<input type="radio" name="activitypub_object_type" id="activitypub_object_type_article" value="article" <?php echo \checked( 'article', \get_option( 'activitypub_object_type', 'note' ) ); ?> />
<?php \esc_html_e( 'Article', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'The presentation of the "Article" might change on different platforms. Mastodon for example shows the "Article" type as a simple link.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
<p> <p>
<label><input type="radio" name="activitypub_object_type" id="activitypub_object_type" value="wordpress-post-format" <?php echo \checked( 'wordpress-post-format', \get_option( 'activitypub_object_type', 'note' ) ); ?> /> <?php \esc_html_e( 'WordPress Post-Format', 'activitypub' ); ?></label> - <span class="description"><?php \esc_html_e( 'Maps the WordPress Post-Format to the ActivityPub Object Type.', 'activitypub' ); ?></span> <label>
<input type="radio" name="activitypub_object_type" id="activitypub_object_type" value="wordpress-post-format" <?php echo \checked( 'wordpress-post-format', \get_option( 'activitypub_object_type', 'note' ) ); ?> />
<?php \esc_html_e( 'WordPress Post-Format', 'activitypub' ); ?>
-
<span class="description">
<?php \esc_html_e( 'Maps the WordPress Post-Format to the ActivityPub Object Type.', 'activitypub' ); ?>
</span>
</label>
</p> </p>
</td> </td>
</tr> </tr>