diff --git a/activitypub.php b/activitypub.php index 691a318..2f74c2f 100644 --- a/activitypub.php +++ b/activitypub.php @@ -26,14 +26,15 @@ function init() { \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_SECURE_MODE' ) || \define( 'ACTIVITYPUB_SECURE_MODE', apply_filters( 'activitypub_secure_mode', $value = false ) ); + \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__ ) ); \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 +42,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 +97,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(); } /** @@ -136,7 +138,6 @@ register_uninstall_hook( ) ); - /** * Only load code that needs BuddyPress to run once BP is loaded and initialized. */ diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 9fed0c0..3b16213 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -227,7 +227,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' ); } @@ -235,12 +235,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' ); } diff --git a/includes/functions.php b/includes/functions.php index 18e5c46..1c37a38 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -219,6 +219,20 @@ 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, '/' ); + $namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path ); + return \get_rest_url( null, $namespaced_path ); +} + /** * Check if a request is for an ActivityPub request. * diff --git a/includes/model/class-activity.php b/includes/model/class-activity.php index 50aada1..bf06bc1 100644 --- a/includes/model/class-activity.php +++ b/includes/model/class-activity.php @@ -1,6 +1,8 @@ published = $object['published']; } - $this->add_to( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $post->get_post_author() ) . '/followers' ) ); + $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 e54c295..ff5ccb8 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -1,6 +1,8 @@ post = \get_post( $post ); - $this->add_to( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $this->get_post_author() ) . '/followers' ) ); + $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 7dd49a8..fc05bdd 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -7,6 +7,8 @@ use WP_REST_Server; use WP_REST_Response; use Activitypub\Collection\Followers as FollowerCollection; +use function Activitypub\get_rest_url_by_path; + /** * ActivityPub Followers REST-Class * @@ -27,7 +29,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 +80,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_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 // phpcs:ignore diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 52a95e7..93ad6a7 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -1,6 +1,8 @@ \d+)/following', array( array( @@ -72,7 +74,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_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 64e4a0c..145cbaf 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -5,6 +5,8 @@ use WP_REST_Response; use Activitypub\Signature; use Activitypub\Model\Activity; +use function Activitypub\get_rest_url_by_path; + /** * ActivityPub Inbox REST-Class * @@ -27,7 +29,7 @@ class Inbox { */ public static function register_routes() { \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/inbox', array( array( @@ -40,7 +42,7 @@ class Inbox { ); \register_rest_route( - 'activitypub/1.0', + ACTIVITYPUB_REST_NAMESPACE, '/users/(?P\d+)/inbox', array( array( @@ -80,7 +82,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_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 980c24b..1d9de67 100644 --- a/includes/rest/class-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -1,6 +1,8 @@ 'http://nodeinfo.diaspora.software/ns/schema/2.0', - 'href' => \get_rest_url( null, 'activitypub/1.0/nodeinfo' ), + 'href' => get_rest_url_by_path( '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..abffbe9 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -1,6 +1,8 @@ \d+)/outbox', array( array( @@ -72,7 +74,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_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 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..7b112ac 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 = \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 4a1efc8..b87bc94 100644 --- a/templates/blog-json.php +++ b/templates/blog-json.php @@ -27,10 +27,10 @@ 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' ); +$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 diff --git a/tests/test-class-activitypub-activity.php b/tests/test-class-activitypub-activity.php index 7fe6551..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( null, '/activitypub/1.0/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' );