Skip to content

Commit 9907585

Browse files
mattwiebepfefferle
andauthored
Plugin loading refactor (#485)
* Plugin loading refactor * changed load order for REST endpoints --------- Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 7b0fc06 commit 9907585

File tree

11 files changed

+54
-73
lines changed

11 files changed

+54
-73
lines changed

activitypub.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
use function Activitypub\site_supports_blocks;
1919

20-
\defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' );
20+
require_once __DIR__ . '/includes/functions.php';
2121

2222
/**
23-
* Initialize plugin
23+
* Initialize the plugin constants.
2424
*/
25-
function init() {
25+
function define_constants() {
26+
\defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' );
2627
\defined( 'ACTIVITYPUB_EXCERPT_LENGTH' ) || \define( 'ACTIVITYPUB_EXCERPT_LENGTH', 400 );
2728
\defined( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS' ) || \define( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS', true );
2829
\defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 3 );
@@ -35,13 +36,12 @@ function init() {
3536
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
3637
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
3738
\define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
39+
}
3840

39-
Migration::init();
40-
Activitypub::init();
41-
Activity_Dispatcher::init();
42-
Collection\Followers::init();
43-
44-
// Configure the REST API route
41+
/**
42+
* Initialize REST routes.
43+
*/
44+
function rest_init() {
4545
Rest\Users::init();
4646
Rest\Outbox::init();
4747
Rest\Inbox::init();
@@ -51,23 +51,46 @@ function init() {
5151
Rest\Server::init();
5252
Rest\Collection::init();
5353

54-
Admin::init();
55-
Hashtag::init();
56-
Shortcodes::init();
57-
Mention::init();
58-
Health_Check::init();
59-
Scheduler::init();
54+
// load NodeInfo endpoints only if blog is public
55+
if ( \get_option( 'blog_public', 1 ) ) {
56+
Rest\NodeInfo::init();
57+
}
58+
}
59+
\add_action( 'rest_api_init', __NAMESPACE__ . '\rest_init' );
60+
61+
/**
62+
* Initialize plugin.
63+
*/
64+
function plugin_init() {
65+
define_constants();
66+
67+
\add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) );
68+
\add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
69+
\add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) );
70+
\add_action( 'init', array( __NAMESPACE__ . '\Collection\Followers', 'init' ) );
71+
\add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) );
72+
\add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );
73+
\add_action( 'init', array( __NAMESPACE__ . '\Shortcodes', 'init' ) );
74+
\add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) );
75+
\add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) );
76+
\add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) );
6077

6178
if ( site_supports_blocks() ) {
62-
Blocks::init();
79+
\add_action( 'init', array( __NAMESPACE__ . '\Blocks', 'init' ) );
80+
}
81+
82+
$debug_file = __DIR__ . '/includes/debug.php';
83+
if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) {
84+
require_once $debug_file;
85+
Debug::init();
6386
}
6487
}
65-
\add_action( 'init', __NAMESPACE__ . '\init' );
88+
\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' );
6689

6790
/**
6891
* Class Autoloader
6992
*/
70-
spl_autoload_register(
93+
\spl_autoload_register(
7194
function ( $full_class ) {
7295
$base_dir = __DIR__ . '/includes/';
7396
$base = 'Activitypub\\';
@@ -100,19 +123,6 @@ function ( $full_class ) {
100123
}
101124
);
102125

103-
require_once __DIR__ . '/includes/functions.php';
104-
105-
// load NodeInfo endpoints only if blog is public
106-
if ( \get_option( 'blog_public', 1 ) ) {
107-
Rest\NodeInfo::init();
108-
}
109-
110-
$debug_file = __DIR__ . '/includes/debug.php';
111-
if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) {
112-
require_once $debug_file;
113-
Debug::init();
114-
}
115-
116126
/**
117127
* Add plugin settings link
118128
*/

includes/rest/class-collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Collection {
2525
* Initialize the class, registering WordPress hooks
2626
*/
2727
public static function init() {
28-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
28+
self::register_routes();
2929
}
3030

3131
/**

includes/rest/class-followers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Followers {
2222
* Initialize the class, registering WordPress hooks
2323
*/
2424
public static function init() {
25-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
25+
self::register_routes();
2626
}
2727

2828
/**

includes/rest/class-following.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Following {
1818
* Initialize the class, registering WordPress hooks
1919
*/
2020
public static function init() {
21-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
21+
self::register_routes();
22+
2223
\add_filter( 'activitypub_rest_following', array( self::class, 'default_following' ), 10, 2 );
2324
}
2425

includes/rest/class-inbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Inbox {
2424
* Initialize the class, registering WordPress hooks
2525
*/
2626
public static function init() {
27-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
27+
self::register_routes();
2828

2929
\add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 2 );
3030
}

includes/rest/class-nodeinfo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class Nodeinfo {
1717
* Initialize the class, registering WordPress hooks
1818
*/
1919
public static function init() {
20-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
20+
self::register_routes();
21+
2122
\add_filter( 'nodeinfo_data', array( self::class, 'add_nodeinfo_discovery' ), 10, 2 );
2223
\add_filter( 'nodeinfo2_data', array( self::class, 'add_nodeinfo2_discovery' ), 10 );
2324
}

includes/rest/class-ostatus.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

includes/rest/class-outbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Outbox {
2424
* Initialize the class, registering WordPress hooks
2525
*/
2626
public static function init() {
27-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
27+
self::register_routes();
2828
}
2929

3030
/**

includes/rest/class-server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Server {
1818
* Initialize the class, registering WordPress hooks
1919
*/
2020
public static function init() {
21-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
21+
self::register_routes();
22+
2223
\add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 );
2324
}
2425

includes/rest/class-users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Users {
2323
* Initialize the class, registering WordPress hooks
2424
*/
2525
public static function init() {
26-
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
26+
self::register_routes();
2727
}
2828

2929
/**

0 commit comments

Comments
 (0)