generate default username

This commit is contained in:
Matthias Pfefferle 2023-05-30 11:22:20 +02:00
parent daf228fd44
commit 2feca1388a
4 changed files with 55 additions and 64 deletions

View file

@ -58,7 +58,7 @@ class User_Factory {
*/
public static function get_by_username( $username ) {
// check for blog user.
if ( get_option( 'activitypub_blog_user_identifier', null ) === $username ) {
if ( Blog_User::get_default_username() === $username ) {
return self::get_by_id( self::BLOG_USER_ID );
}

View file

@ -59,8 +59,55 @@ class Blog_User extends User {
return \get_home_url();
}
/**
* Generate and save a default Username.
*
* @return string The auto-generated Username.
*/
public static function get_default_username() {
$username = \get_option( 'activitypub_blog_user_identifier' );
if ( $username ) {
return $username;
}
// check if domain host has a subdomain
$host = \wp_parse_url( \get_home_url(), \PHP_URL_HOST );
$host = \str_replace( 'www.', '', $host );
$host_parts = \explode( '.', $host );
if ( \count( $host_parts ) <= 2 && strlen( $host ) <= 15 ) {
\update_option( 'activitypub_blog_user_identifier', $host );
return $host;
}
// check blog title
$blog_title = \get_bloginfo( 'name' );
$blog_title = \sanitize_title( $blog_title );
if ( strlen( $blog_title ) <= 15 ) {
\update_option( 'activitypub_blog_user_identifier', $blog_title );
return $blog_title;
}
$default_identifier = array(
'feed',
'all',
'everyone',
'authors',
'follow',
'posts',
);
// get random item of $default_identifier
$default = $default_identifier[ \array_rand( $default_identifier ) ];
\update_option( 'activitypub_blog_user_identifier', $default );
return $default;
}
public function get_username() {
return \esc_html( \get_option( 'activitypub_blog_user_identifier', 'feed' ) );
return self::get_default_username();
}
public function get_avatar() {

View file

@ -1,66 +1,10 @@
<?php
$json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context();
$json->id = \get_home_url( '/' );
$json->type = 'Organization';
$json->name = \get_bloginfo( 'name' );
$json->summary = \html_entity_decode(
\get_bloginfo( 'description' ),
\ENT_QUOTES,
'UTF-8'
);
$json->preferredUsername = \get_bloginfo( 'name' ); // phpcs:ignore
$json->url = \get_home_url( '/' );
if ( \has_site_icon() ) {
$json->icon = array(
'type' => 'Image',
'url' => \get_site_icon_url( 120 ),
);
}
if ( \has_header_image() ) {
$json->image = array(
'type' => 'Image',
'url' => \get_header_image(),
);
}
$json->inbox = \Activitypub\get_rest_url_by_path( 'blog/inbox' );
$json->outbox = \Activitypub\get_rest_url_by_path( 'blog/outbox' );
$json->followers = \Activitypub\get_rest_url_by_path( 'blog/followers' );
$json->following = \Activitypub\get_rest_url_by_path( 'blog/following' );
$json->manuallyApprovesFollowers = \apply_filters( 'activitypub_json_manually_approves_followers', \__return_false() ); // phpcs:ignore
// phpcs:ignore
$json->publicKey = array(
'id' => \get_home_url( '/' ) . '#main-key',
'owner' => \get_home_url( '/' ),
'publicKeyPem' => '',
);
$json->tag = array();
$json->attachment = array();
$json->attachment[] = array(
'type' => 'PropertyValue',
'name' => \__( 'Blog', 'activitypub' ),
'value' => \html_entity_decode(
'<a rel="me" title="' . \esc_attr( \home_url( '/' ) ) . '" target="_blank" href="' . \home_url( '/' ) . '">' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '</a>',
\ENT_QUOTES,
'UTF-8'
),
);
// filter output
$json = \apply_filters( 'activitypub_json_blog_array', $json );
$user = \Activitypub\User_Factory::get_by_id( 0 );
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
\do_action( 'activitypub_json_blog_pre' );
\do_action( 'activitypub_json_author_pre', $user->get_user_id() );
$options = 0;
// JSON_PRETTY_PRINT added in PHP 5.4
@ -75,12 +19,12 @@ $options |= \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
*
* @param int $options The current options flags
*/
$options = \apply_filters( 'activitypub_json_blog_options', $options );
$options = \apply_filters( 'activitypub_json_author_options', $options, $user->get_user_id() );
\header( 'Content-Type: application/activity+json' );
echo \wp_json_encode( $json, $options );
echo \wp_json_encode( $user->to_array(), $options );
/*
* Action triggerd after the ActivityPub profile has been created and sent to the client
*/
\do_action( 'activitypub_json_blog_post' );
\do_action( 'activitypub_json_author_post', $user->get_user_id() );

View file

@ -42,7 +42,7 @@
</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' ) ); ?>" />
<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', \Activitypub\Model\Blog_User::get_default_username() ) ); ?>" />
@<?php echo esc_html( \wp_parse_url( \home_url(), PHP_URL_HOST ) ); ?>
</label>
<p class="description">