From 314ccf43a6915d87379862383a369be95c2e4bdd Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 14:58:50 -0500 Subject: [PATCH] add a `get_rest_url_by_path` helper function, and use it --- includes/functions.php | 13 +++++++++++++ includes/model/class-activity.php | 4 ++-- includes/model/class-post.php | 4 ++-- includes/rest/class-followers.php | 2 +- includes/rest/class-following.php | 2 +- includes/rest/class-inbox.php | 2 +- includes/rest/class-nodeinfo.php | 2 +- includes/rest/class-outbox.php | 2 +- includes/rest/class-webfinger.php | 1 + templates/author-json.php | 8 ++++---- templates/blog-json.php | 10 ++++------ tests/test-class-activitypub-activity.php | 2 +- 12 files changed, 32 insertions(+), 20 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 19a3512..41f0d46 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -228,3 +228,16 @@ function is_tombstone( $wp_error ) { return false; } + +/** + * Get the REST URL relative to this plugin's namespace. + * + * @param string $path Optional. REST route path. Otherwise this plugin's namespaced root. + * @return string REST URL relative to this plugin's namespace. + */ +function get_rest_url_by_path( $path = '' ) { + // we'll handle the leading slash. + $path = ltrim( $path, '/' ); + $url = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); + return \get_rest_url( null, $url ); +} \ No newline at end of file diff --git a/includes/model/class-activity.php b/includes/model/class-activity.php index ce99e87..32dd0f5 100644 --- a/includes/model/class-activity.php +++ b/includes/model/class-activity.php @@ -148,8 +148,8 @@ class Activity { $this->published = $object['published']; } - $url = sprintf( '/%/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, intval( $post->get_post_author() ) ); - $this->add_to( \get_rest_url( null, $url ) ); + $path = sprintf( 'users/%d/followers', intval( $post->get_post_author() ) ); + $this->add_to( get_rest_url_by_path( $path ) ); if ( isset( $this->object['attributedTo'] ) ) { $this->actor = $this->object['attributedTo']; diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 022e390..528182c 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -142,8 +142,8 @@ class Post { */ public function __construct( $post ) { $this->post = \get_post( $post ); - $url = sprintf( '/%/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, intval( $post->get_post_author() ) ); - $this->add_to( \get_rest_url( null, $url ) ); + $path = sprintf( 'users/%d/followers', intval( $this->get_post_author() ) ); + $this->add_to( get_rest_url_by_path( $path ) ); } /** diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index 3abff6e..1ec2c78 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -78,7 +78,7 @@ class Followers { $json->actor = \get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, $user_id ) ); // phpcs:ignore + $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/followers', $user_id ) ); // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore $json->totalItems = FollowerCollection::count_followers( $user_id ); // phpcs:ignore $json->orderedItems = FollowerCollection::get_followers( $user_id, ARRAY_N ); // phpcs:ignore diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 2606df4..0c685d5 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -72,7 +72,7 @@ class Following { $json->actor = \get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/following', ACTIVITYPUB_REST_NAMESPACE, $user_id ) ); // phpcs:ignore + $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/following', $user_id ) ); // phpcs:ignore $json->totalItems = 0; // phpcs:ignore $json->orderedItems = apply_filters( 'activitypub_following', array(), $user ); // phpcs:ignore diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 16735df..a73a783 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -108,7 +108,7 @@ class Inbox { $json->id = \home_url( \add_query_arg( null, null ) ); $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/inbox', ACTIVITYPUB_REST_NAMESPACE, $user_id ) ); // phpcs:ignore + $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user_id ) ); // phpcs:ignore $json->totalItems = 0; // phpcs:ignore diff --git a/includes/rest/class-nodeinfo.php b/includes/rest/class-nodeinfo.php index bafa7e4..3b3c6dc 100644 --- a/includes/rest/class-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -173,7 +173,7 @@ class Nodeinfo { $discovery['links'] = array( array( 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', - 'href' => \get_rest_url( null, ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo' ), + 'href' => get_rest_url_by_path( 'nodeinfo' ), ), ); diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 5ae8e50..1a52750 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -72,7 +72,7 @@ class Outbox { $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); $json->actor = \get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/outbox', ACTIVITYPUB_REST_NAMESPACE, $user_id ) ); // phpcs:ignore + $json->partOf = get_rest_url_by_path( sprintf( 'users/%d/outbox', $user_id ) ); // phpcs:ignore $json->totalItems = 0; // phpcs:ignore // phpcs:ignore diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index f75a3f7..b04839b 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -24,6 +24,7 @@ class Webfinger { * Register routes */ public static function register_routes() { + \l( 'register webfinger' ); \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, '/webfinger', diff --git a/templates/author-json.php b/templates/author-json.php index 090920d..cf4b85e 100644 --- a/templates/author-json.php +++ b/templates/author-json.php @@ -28,10 +28,10 @@ if ( \has_header_image() ) { ); } -$json->inbox = \get_rest_url( null, sprintf( '/%s/users/%d/inbox', ACTIVITYPUB_REST_NAMESPACE, $author_id ) ); -$json->outbox = \get_rest_url( null, sprintf( '/%s/users/%d/outbox', ACTIVITYPUB_REST_NAMESPACE, $author_id ) ); -$json->followers = \get_rest_url( null, sprintf( '/%s/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, $author_id ) ); -$json->following = \get_rest_url( null, sprintf( '/%s/users/%d/following', ACTIVITYPUB_REST_NAMESPACE, $author_id ) ); +$json->inbox = get_rest_url_by_path( sprintf( 'users/%d/inbox', $author_id ) ); +$json->outbox = get_rest_url_by_path( sprintf( 'users/%d/outbox', $author_id ) ); +$json->followers = get_rest_url_by_path( sprintf( 'users/%d/followers', $author_id ) ); +$json->following = get_rest_url_by_path( sprintf( 'users/%d/following', $author_id ) ); $json->manuallyApprovesFollowers = \apply_filters( 'activitypub_json_manually_approves_followers', \__return_false() ); // phpcs:ignore diff --git a/templates/blog-json.php b/templates/blog-json.php index 38f0406..a988385 100644 --- a/templates/blog-json.php +++ b/templates/blog-json.php @@ -27,12 +27,10 @@ if ( \has_header_image() ) { ); } -$blog_base = sprintf( '/%s/blog/', ACTIVITYPUB_REST_NAMESPACE ); - -$json->inbox = \get_rest_url( null, $blog_base . 'inbox' ); -$json->outbox = \get_rest_url( null, $blog_base . 'outbox' ); -$json->followers = \get_rest_url( null, $blog_base . 'followers' ); -$json->following = \get_rest_url( null, $blog_base . 'following' ); +$json->inbox = get_rest_url_by_path( 'blog/inbox' ); +$json->outbox = get_rest_url_by_path( 'blog/outbox' ); +$json->followers = get_rest_url_by_path( 'blog/followers' ); +$json->following = get_rest_url_by_path( 'blog/following' ); $json->manuallyApprovesFollowers = \apply_filters( 'activitypub_json_manually_approves_followers', \__return_false() ); // phpcs:ignore diff --git a/tests/test-class-activitypub-activity.php b/tests/test-class-activitypub-activity.php index b18c6ab..6569652 100644 --- a/tests/test-class-activitypub-activity.php +++ b/tests/test-class-activitypub-activity.php @@ -22,7 +22,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase { $activitypub_activity = new \Activitypub\Model\Activity( 'Create' ); $activitypub_activity->from_post( $activitypub_post ); - $this->assertContains( \get_rest_url( null, '/' . ACTIVITYPUB_REST_NAMESPACE . '/users/1/followers' ), $activitypub_activity->get_to() ); + $this->assertContains( get_rest_url_by_path( 'users/1/followers' ), $activitypub_activity->get_to() ); $this->assertContains( 'https://example.com/alex', $activitypub_activity->get_cc() ); remove_all_filters( 'activitypub_extract_mentions' );