From 99075855706355d870b2a78c1ca9469938c5d40c Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 4 Oct 2023 11:15:53 -0500 Subject: [PATCH 1/3] Plugin loading refactor (#485) * Plugin loading refactor * changed load order for REST endpoints --------- Co-authored-by: Matthias Pfefferle --- activitypub.php | 76 +++++++++++++++++------------- includes/rest/class-collection.php | 2 +- includes/rest/class-followers.php | 2 +- includes/rest/class-following.php | 3 +- includes/rest/class-inbox.php | 2 +- includes/rest/class-nodeinfo.php | 3 +- includes/rest/class-ostatus.php | 33 ------------- includes/rest/class-outbox.php | 2 +- includes/rest/class-server.php | 3 +- includes/rest/class-users.php | 2 +- includes/rest/class-webfinger.php | 3 +- 11 files changed, 56 insertions(+), 75 deletions(-) delete mode 100644 includes/rest/class-ostatus.php diff --git a/activitypub.php b/activitypub.php index 266d630..91d9e0e 100644 --- a/activitypub.php +++ b/activitypub.php @@ -17,12 +17,13 @@ namespace Activitypub; use function Activitypub\site_supports_blocks; -\defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); +require_once __DIR__ . '/includes/functions.php'; /** - * Initialize plugin + * Initialize the plugin constants. */ -function init() { +function define_constants() { + \defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); \defined( 'ACTIVITYPUB_EXCERPT_LENGTH' ) || \define( 'ACTIVITYPUB_EXCERPT_LENGTH', 400 ); \defined( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS' ) || \define( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS', true ); \defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 3 ); @@ -35,13 +36,12 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); +} - Migration::init(); - Activitypub::init(); - Activity_Dispatcher::init(); - Collection\Followers::init(); - - // Configure the REST API route +/** + * Initialize REST routes. + */ +function rest_init() { Rest\Users::init(); Rest\Outbox::init(); Rest\Inbox::init(); @@ -51,23 +51,46 @@ function init() { Rest\Server::init(); Rest\Collection::init(); - Admin::init(); - Hashtag::init(); - Shortcodes::init(); - Mention::init(); - Health_Check::init(); - Scheduler::init(); - - if ( site_supports_blocks() ) { - Blocks::init(); + // load NodeInfo endpoints only if blog is public + if ( \get_option( 'blog_public', 1 ) ) { + Rest\NodeInfo::init(); } } -\add_action( 'init', __NAMESPACE__ . '\init' ); +\add_action( 'rest_api_init', __NAMESPACE__ . '\rest_init' ); + +/** + * Initialize plugin. + */ +function plugin_init() { + define_constants(); + + \add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Collection\Followers', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Shortcodes', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) ); + \add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) ); + + if ( site_supports_blocks() ) { + \add_action( 'init', array( __NAMESPACE__ . '\Blocks', 'init' ) ); + } + + $debug_file = __DIR__ . '/includes/debug.php'; + if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) { + require_once $debug_file; + Debug::init(); + } +} +\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' ); /** * Class Autoloader */ -spl_autoload_register( +\spl_autoload_register( function ( $full_class ) { $base_dir = __DIR__ . '/includes/'; $base = 'Activitypub\\'; @@ -100,19 +123,6 @@ spl_autoload_register( } ); -require_once __DIR__ . '/includes/functions.php'; - -// load NodeInfo endpoints only if blog is public -if ( \get_option( 'blog_public', 1 ) ) { - Rest\NodeInfo::init(); -} - -$debug_file = __DIR__ . '/includes/debug.php'; -if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) { - require_once $debug_file; - Debug::init(); -} - /** * Add plugin settings link */ diff --git a/includes/rest/class-collection.php b/includes/rest/class-collection.php index 9e50835..5fa585d 100644 --- a/includes/rest/class-collection.php +++ b/includes/rest/class-collection.php @@ -25,7 +25,7 @@ class Collection { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); } /** diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index c125fcd..b7be9d0 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -22,7 +22,7 @@ class Followers { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); } /** diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 48e3ca0..33bdf65 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -18,7 +18,8 @@ class Following { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); + \add_filter( 'activitypub_rest_following', array( self::class, 'default_following' ), 10, 2 ); } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index b64ff04..4ac2b7f 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -24,7 +24,7 @@ class Inbox { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); \add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 2 ); } diff --git a/includes/rest/class-nodeinfo.php b/includes/rest/class-nodeinfo.php index 711d4fd..8e62874 100644 --- a/includes/rest/class-nodeinfo.php +++ b/includes/rest/class-nodeinfo.php @@ -17,7 +17,8 @@ class Nodeinfo { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); + \add_filter( 'nodeinfo_data', array( self::class, 'add_nodeinfo_discovery' ), 10, 2 ); \add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_discovery' ), 10 ); } diff --git a/includes/rest/class-ostatus.php b/includes/rest/class-ostatus.php deleted file mode 100644 index 415d502..0000000 --- a/includes/rest/class-ostatus.php +++ /dev/null @@ -1,33 +0,0 @@ - \WP_REST_Server::READABLE, - 'callback' => array( '\Activitypub\Rest\Ostatus', 'get' ), - // 'args' => self::request_parameters(), - 'permission_callback' => '__return_true', - ), - ) - ); - } - - public static function get() { - // @todo implement - } -} diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 2f50bd7..eb35e86 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -24,7 +24,7 @@ class Outbox { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); } /** diff --git a/includes/rest/class-server.php b/includes/rest/class-server.php index 01d9a9a..d15831a 100644 --- a/includes/rest/class-server.php +++ b/includes/rest/class-server.php @@ -18,7 +18,8 @@ class Server { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); + \add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 ); } diff --git a/includes/rest/class-users.php b/includes/rest/class-users.php index 820f653..31a92dd 100644 --- a/includes/rest/class-users.php +++ b/includes/rest/class-users.php @@ -23,7 +23,7 @@ class Users { * Initialize the class, registering WordPress hooks */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); } /** diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index 4504e0d..315fee1 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -19,7 +19,8 @@ class Webfinger { * @return void */ public static function init() { - \add_action( 'rest_api_init', array( self::class, 'register_routes' ) ); + self::register_routes(); + \add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 10, 3 ); \add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 99, 2 ); } From e05176cea54684b95abc652984ddf81390e3726e Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 4 Oct 2023 23:55:13 -0500 Subject: [PATCH 2/3] Add a `ACTIVITYPUB_DISABLE_REWRITES` constant (#490) --- activitypub.php | 1 + includes/class-activitypub.php | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/activitypub.php b/activitypub.php index 91d9e0e..38a3d07 100644 --- a/activitypub.php +++ b/activitypub.php @@ -31,6 +31,7 @@ function define_constants() { \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_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false ); + \defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false ); \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 6ac860e..24228f4 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -227,6 +227,12 @@ class Activitypub { * Add rewrite rules */ public static function add_rewrite_rules() { + // If another system needs to take precedence over the ActivityPub rewrite rules, + // they can define their own and will manually call the appropriate functions as required. + if ( ACTIVITYPUB_DISABLE_REWRITES ) { + return; + } + if ( ! \class_exists( 'Webfinger' ) ) { \add_rewrite_rule( '^.well-known/webfinger', From b956f5b088a9b0e44777c77ec7a778b96940d6b5 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Thu, 5 Oct 2023 08:14:32 +0200 Subject: [PATCH 3/3] Posts: add pre-fetch hook to allow plugins to hang filters on (#487) Co-authored-by: Matthias Pfefferle --- includes/transformer/class-post.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index d22304f..dbe9693 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -452,6 +452,15 @@ class Post { protected function get_content() { global $post; + /** + * Provides an action hook so plugins can add their own hooks/filters before AP content is generated. + * + * Example: if a plugin adds a filter to `the_content` to add a button to the end of posts, it can also remove that filter here. + * + * @param WP_Post $post The post object. + */ + do_action( 'activitypub_before_get_content', $post ); + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = $this->wp_post; $content = $this->get_post_content_template();