From abfa7c796990bf6f09228c0164af2b490ecc3488 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Thu, 11 May 2023 13:25:30 -0500 Subject: [PATCH 01/11] Allow setting the REST namespace with `ACTIVITYPUB_REST_NAMESPACE` --- activitypub.php | 7 ++++--- includes/model/class-activity.php | 3 ++- includes/model/class-post.php | 3 ++- includes/rest/class-followers.php | 4 ++-- includes/rest/class-following.php | 4 ++-- includes/rest/class-inbox.php | 6 +++--- includes/rest/class-nodeinfo.php | 8 ++++---- includes/rest/class-ostatus.php | 2 +- includes/rest/class-outbox.php | 4 ++-- includes/rest/class-webfinger.php | 2 +- templates/author-json.php | 8 ++++---- templates/blog-json.php | 10 ++++++---- tests/test-class-activitypub-activity.php | 2 +- 13 files changed, 34 insertions(+), 29 deletions(-) diff --git a/activitypub.php b/activitypub.php index 2e731f2..6efc3c2 100644 --- a/activitypub.php +++ b/activitypub.php @@ -25,6 +25,7 @@ function init() { \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=

)|(?<=
)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' ); \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); + defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); @@ -120,12 +121,12 @@ function plugin_settings_link( $actions ) { */ function add_rewrite_rules() { if ( ! \class_exists( 'Webfinger' ) ) { - \add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' ); + \add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger', 'top' ); } if ( ! \class_exists( 'Nodeinfo' ) || ! (bool) \get_option( 'blog_public', 1 ) ) { - \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', 'top' ); - \add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/activitypub/1.0/nodeinfo2', 'top' ); + \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery', 'top' ); + \add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2', 'top' ); } \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); diff --git a/includes/model/class-activity.php b/includes/model/class-activity.php index b5372dc..ce99e87 100644 --- a/includes/model/class-activity.php +++ b/includes/model/class-activity.php @@ -148,7 +148,8 @@ class Activity { $this->published = $object['published']; } - $this->add_to( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $post->get_post_author() ) . '/followers' ) ); + $url = sprintf( '/%/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, intval( $post->get_post_author() ) ); + $this->add_to( \get_rest_url( null, $url ) ); 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 ccb161c..022e390 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -142,7 +142,8 @@ class Post { */ public function __construct( $post ) { $this->post = \get_post( $post ); - $this->add_to( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $this->get_post_author() ) . '/followers' ) ); + $url = sprintf( '/%/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, intval( $post->get_post_author() ) ); + $this->add_to( \get_rest_url( null, $url ) ); } /** diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index 9a8b9b6..3abff6e 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -27,7 +27,7 @@ class Followers { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/users/(?P\d+)/followers', array( array( @@ -78,7 +78,7 @@ class Followers { $json->actor = \get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore + $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/followers', ACTIVITYPUB_REST_NAMESPACE, $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 52a95e7..2606df4 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -21,7 +21,7 @@ class Following { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/users/(?P\d+)/following', array( array( @@ -72,7 +72,7 @@ class Following { $json->actor = \get_author_posts_url( $user_id ); $json->type = 'OrderedCollectionPage'; - $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore + $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/following', ACTIVITYPUB_REST_NAMESPACE, $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 5a23d02..16735df 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -26,7 +26,7 @@ class Inbox { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/inbox', array( array( @@ -39,7 +39,7 @@ class Inbox { ); \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/users/(?P\d+)/inbox', array( array( @@ -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, "/activitypub/1.0/users/$user_id/inbox" ); // phpcs:ignore + $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/inbox', ACTIVITYPUB_REST_NAMESPACE, $user_id ) ); // phpcs:ignore $json->totalItems = 0; // phpcs:ignore diff --git a/includes/rest/class-nodeinfo.php b/includes/rest/class-nodeinfo.php index 980c24b..bafa7e4 100644 --- a/includes/rest/class-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -23,7 +23,7 @@ class Nodeinfo { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/nodeinfo/discovery', array( array( @@ -35,7 +35,7 @@ class Nodeinfo { ); \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/nodeinfo', array( array( @@ -47,7 +47,7 @@ class Nodeinfo { ); \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/nodeinfo2', array( array( @@ -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/1.0/nodeinfo' ), + 'href' => \get_rest_url( null, ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo' ), ), ); diff --git a/includes/rest/class-ostatus.php b/includes/rest/class-ostatus.php index 45ff901..415d502 100644 --- a/includes/rest/class-ostatus.php +++ b/includes/rest/class-ostatus.php @@ -14,7 +14,7 @@ class Ostatus { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/ostatus/remote-follow', array( array( diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 905dfd5..5ae8e50 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -21,7 +21,7 @@ class Outbox { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/users/(?P\d+)/outbox', array( array( @@ -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, "/activitypub/1.0/users/$user_id/outbox" ); // phpcs:ignore + $json->partOf = \get_rest_url( null, sprintf( '/%s/users/%d/outbox', ACTIVITYPUB_REST_NAMESPACE, $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 10dcfa4..f75a3f7 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -25,7 +25,7 @@ class Webfinger { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/webfinger', array( array( diff --git a/templates/author-json.php b/templates/author-json.php index 3b355b9..090920d 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, "/activitypub/1.0/users/$author_id/inbox" ); -$json->outbox = \get_rest_url( null, "/activitypub/1.0/users/$author_id/outbox" ); -$json->followers = \get_rest_url( null, "/activitypub/1.0/users/$author_id/followers" ); -$json->following = \get_rest_url( null, "/activitypub/1.0/users/$author_id/following" ); +$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->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 4a1efc8..38f0406 100644 --- a/templates/blog-json.php +++ b/templates/blog-json.php @@ -27,10 +27,12 @@ if ( \has_header_image() ) { ); } -$json->inbox = \get_rest_url( null, '/activitypub/1.0/blog/inbox' ); -$json->outbox = \get_rest_url( null, '/activitypub/1.0/blog/outbox' ); -$json->followers = \get_rest_url( null, '/activitypub/1.0/blog/followers' ); -$json->following = \get_rest_url( null, '/activitypub/1.0/blog/following' ); +$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->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 7fe6551..b18c6ab 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/1.0/users/1/followers' ), $activitypub_activity->get_to() ); + $this->assertContains( \get_rest_url( null, '/' . ACTIVITYPUB_REST_NAMESPACE . '/users/1/followers' ), $activitypub_activity->get_to() ); $this->assertContains( 'https://example.com/alex', $activitypub_activity->get_cc() ); remove_all_filters( 'activitypub_extract_mentions' ); From 314ccf43a6915d87379862383a369be95c2e4bdd Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 14:58:50 -0500 Subject: [PATCH 02/11] 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' ); From 3fa5e4f37ec85ad3ab2eb912caed2d76d24e3186 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 15:31:53 -0500 Subject: [PATCH 03/11] now with more `use` --- includes/model/class-activity.php | 2 ++ includes/model/class-post.php | 2 ++ includes/rest/class-followers.php | 1 + includes/rest/class-following.php | 2 ++ includes/rest/class-inbox.php | 1 + includes/rest/class-nodeinfo.php | 2 ++ includes/rest/class-outbox.php | 2 ++ templates/author-json.php | 2 ++ templates/blog-json.php | 2 ++ tests/test-class-activitypub-activity.php | 2 +- 10 files changed, 17 insertions(+), 1 deletion(-) diff --git a/includes/model/class-activity.php b/includes/model/class-activity.php index 32dd0f5..4bd0d5e 100644 --- a/includes/model/class-activity.php +++ b/includes/model/class-activity.php @@ -1,6 +1,8 @@ {'@context'} = \Activitypub\get_context(); diff --git a/tests/test-class-activitypub-activity.php b/tests/test-class-activitypub-activity.php index 6569652..af0ed55 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_by_path( 'users/1/followers' ), $activitypub_activity->get_to() ); + $this->assertContains( \Activitypub\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' ); From 5a91fdcf0ad73a1555d5928ac57a1db0399df123 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 15:43:04 -0500 Subject: [PATCH 04/11] remove debug log --- includes/rest/class-webfinger.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index b04839b..f75a3f7 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -24,7 +24,6 @@ class Webfinger { * Register routes */ public static function register_routes() { - \l( 'register webfinger' ); \register_rest_route( ACTIVITYPUB_REST_NAMESPACE, '/webfinger', From ec00ace234816cc7d4b4cbad6a7674d69834f508 Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 16:42:30 -0500 Subject: [PATCH 05/11] add a `activitypub_rest_url` filter --- includes/functions.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 39ba262..7d6f86c 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -238,8 +238,11 @@ function is_tombstone( $wp_error ) { 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 ); + $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); + $rest_url = \get_rest_url( null, $namespaced_path ); + // Just in case there are non-default ways of handling REST URLs. + $rest_url = \apply_filters( 'activitypub_rest_url', $rest_url, $path, ACTIVITYPUB_REST_NAMESPACE ); + return $rest_url; } /** From 31e7e44642258cdc7bd5311c426af478d2eea0bd Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Fri, 12 May 2023 18:25:49 -0500 Subject: [PATCH 06/11] remove filter --- includes/functions.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 7d6f86c..8ef0c35 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -239,10 +239,7 @@ function get_rest_url_by_path( $path = '' ) { // we'll handle the leading slash. $path = ltrim( $path, '/' ); $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); - $rest_url = \get_rest_url( null, $namespaced_path ); - // Just in case there are non-default ways of handling REST URLs. - $rest_url = \apply_filters( 'activitypub_rest_url', $rest_url, $path, ACTIVITYPUB_REST_NAMESPACE ); - return $rest_url; + return \get_rest_url( null, $namespaced_path ); } /** From 9cd2a049557b9abd567df8a5b64f0acb767da9d4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 16 May 2023 08:14:04 +0200 Subject: [PATCH 07/11] re-added some namespace consts --- activitypub.php | 2 +- includes/class-activitypub.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/activitypub.php b/activitypub.php index 817f553..71e7261 100644 --- a/activitypub.php +++ b/activitypub.php @@ -25,7 +25,7 @@ function init() { \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=

)|(?<=
)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' ); \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); - defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); + \defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 37bedf4..c5d0e71 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -225,7 +225,7 @@ class Activitypub { if ( ! \class_exists( 'Webfinger' ) ) { \add_rewrite_rule( '^.well-known/webfinger', - 'index.php?rest_route=/activitypub/1.0/webfinger', + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger', 'top' ); } @@ -233,12 +233,12 @@ class Activitypub { if ( ! \class_exists( 'Nodeinfo' ) && true === (bool) \get_option( 'blog_public', 1 ) ) { \add_rewrite_rule( '^.well-known/nodeinfo', - 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo/discovery', 'top' ); \add_rewrite_rule( '^.well-known/x-nodeinfo2', - 'index.php?rest_route=/activitypub/1.0/nodeinfo2', + 'index.php?rest_route=/' . ACTIVITYPUB_REST_NAMESPACE . '/nodeinfo2', 'top' ); } From 3d16b8de1d46f13c6ec2d14cd5e5ee6e678e3d85 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 09:01:28 +0200 Subject: [PATCH 08/11] use full function name in templates --- templates/author-json.php | 10 ++++------ templates/blog-json.php | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/templates/author-json.php b/templates/author-json.php index f132b4b..7b112ac 100644 --- a/templates/author-json.php +++ b/templates/author-json.php @@ -1,6 +1,4 @@ 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->inbox = \Activitypub\get_rest_url_by_path( sprintf( 'users/%d/inbox', $author_id ) ); +$json->outbox = \Activitypub\get_rest_url_by_path( sprintf( 'users/%d/outbox', $author_id ) ); +$json->followers = \Activitypub\get_rest_url_by_path( sprintf( 'users/%d/followers', $author_id ) ); +$json->following = \Activitypub\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 2da9c65..b87bc94 100644 --- a/templates/blog-json.php +++ b/templates/blog-json.php @@ -1,6 +1,4 @@ {'@context'} = \Activitypub\get_context(); @@ -29,10 +27,10 @@ if ( \has_header_image() ) { ); } -$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->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 From d89c05aa49801ad5974179345440c31bc55fd8e0 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 09:02:09 +0200 Subject: [PATCH 09/11] init missing Nodeinfo endpoint --- activitypub.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/activitypub.php b/activitypub.php index 71e7261..3a77944 100644 --- a/activitypub.php +++ b/activitypub.php @@ -32,8 +32,8 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); Migration::init(); - Activity_Dispatcher::init(); Activitypub::init(); + Activity_Dispatcher::init(); Collection\Followers::init(); // Configure the REST API route @@ -41,13 +41,13 @@ function init() { Rest\Inbox::init(); Rest\Followers::init(); Rest\Following::init(); + Rest\Nodeinfo::init(); Rest\Webfinger::init(); Admin::init(); Hashtag::init(); Shortcodes::init(); Mention::init(); - Debug::init(); Health_Check::init(); Scheduler::init(); } @@ -96,6 +96,7 @@ if ( \get_option( 'blog_public', 1 ) ) { $debug_file = \dirname( __FILE__ ) . '/includes/debug.php'; if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) { require_once $debug_file; + Debug::init(); } /** From 60fc581e1de7e6bfba9ab7dc7610102f394359a0 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 09:02:37 +0200 Subject: [PATCH 10/11] coding style --- includes/rest/class-followers.php | 1 + includes/rest/class-inbox.php | 1 + 2 files changed, 2 insertions(+) diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index b2983ae..fc05bdd 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -6,6 +6,7 @@ use stdClass; use WP_REST_Server; use WP_REST_Response; use Activitypub\Collection\Followers as FollowerCollection; + use function Activitypub\get_rest_url_by_path; /** diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 7e2da60..b4765d3 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -2,6 +2,7 @@ namespace Activitypub\Rest; use Activitypub\Model\Activity; + use function Activitypub\get_rest_url_by_path; /** From c34fb74b41b5bc84b38a33b0b9783fb8dd7c7d42 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 09:03:26 +0200 Subject: [PATCH 11/11] coding style --- includes/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/functions.php b/includes/functions.php index 8ef0c35..5bcf97a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -233,6 +233,7 @@ function is_tombstone( $wp_error ) { * 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 = '' ) {