From 99075855706355d870b2a78c1ca9469938c5d40c Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Wed, 4 Oct 2023 11:15:53 -0500 Subject: [PATCH] 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 ); }