diff --git a/includes/class-user-factory.php b/includes/class-user-factory.php index faa170e..8ba4521 100644 --- a/includes/class-user-factory.php +++ b/includes/class-user-factory.php @@ -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 ); } diff --git a/includes/model/class-blog-user.php b/includes/model/class-blog-user.php index fd01aa7..492bf76 100644 --- a/includes/model/class-blog-user.php +++ b/includes/model/class-blog-user.php @@ -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() { diff --git a/templates/blog-json.php b/templates/blog-json.php index b87bc94..cfa759c 100644 --- a/templates/blog-json.php +++ b/templates/blog-json.php @@ -1,66 +1,10 @@ {'@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( - '' . \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) . '', - \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() ); diff --git a/templates/settings.php b/templates/settings.php index 819dbb0..08a65a1 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -42,7 +42,7 @@