From c95e501f98a5708f09258f9592c80c686d33742a Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 30 May 2023 10:22:01 +0200 Subject: [PATCH] redirect to canonical URL if it is not an ActivityPub request --- includes/class-activitypub.php | 2 +- includes/functions.php | 2 +- includes/model/class-blog-user.php | 4 ++++ includes/model/class-user.php | 4 ++++ includes/rest/class-users.php | 10 +++++++++- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 72cea52..2da2dce 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -227,7 +227,7 @@ class Activitypub { ); \add_rewrite_rule( '^@([\w\-\.]+)', - 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]&redirect=true', + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/users/$matches[1]', 'top' ); } diff --git a/includes/functions.php b/includes/functions.php index 1f25e04..dfbdf37 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -236,7 +236,7 @@ function is_activitypub_request() { * ActivityPub requests are currently only made for * author archives, singular posts, and the homepage. */ - if ( ! \is_author() && ! \is_singular() && ! \is_home() ) { + if ( ! \is_author() && ! \is_singular() && ! \is_home() && ! defined( 'REST_REQUEST' ) && ! REST_REQUEST ) { return false; } diff --git a/includes/model/class-blog-user.php b/includes/model/class-blog-user.php index 2183efb..fd01aa7 100644 --- a/includes/model/class-blog-user.php +++ b/includes/model/class-blog-user.php @@ -55,6 +55,10 @@ class Blog_User extends User { return \esc_url( \trailingslashit( get_home_url() ) . '@' . $this->get_username() ); } + public function get_canonical_url() { + return \get_home_url(); + } + public function get_username() { return \esc_html( \get_option( 'activitypub_blog_user_identifier', 'feed' ) ); } diff --git a/includes/model/class-user.php b/includes/model/class-user.php index 29b65bf..5bb1e77 100644 --- a/includes/model/class-user.php +++ b/includes/model/class-user.php @@ -96,6 +96,10 @@ class User { return \esc_url( \get_author_posts_url( $this->user_id ) ); } + public function get_canonical_url() { + return $this->get_url(); + } + public function get_username() { return \esc_attr( \get_the_author_meta( 'login', $this->user_id ) ); } diff --git a/includes/rest/class-users.php b/includes/rest/class-users.php index c6d92ee..8c0452d 100644 --- a/includes/rest/class-users.php +++ b/includes/rest/class-users.php @@ -6,6 +6,8 @@ use WP_REST_Server; use WP_REST_Response; use Activitypub\User_Factory; +use function Activitypub\is_activitypub_request; + /** * ActivityPub Followers REST-Class * @@ -54,6 +56,12 @@ class Users { return $user; } + // redirect to canonical URL if it is not an ActivityPub request + if ( ! is_activitypub_request() ) { + header( 'Location: ' . $user->get_canonical_url() ); + exit; + } + /* * Action triggerd prior to the ActivityPub profile being created and sent to the client */ @@ -81,7 +89,7 @@ class Users { $params['user_id'] = array( 'required' => true, - 'type' => 'string', + 'type' => 'string', ); return $params;