Skip to content

Commit 289c6c9

Browse files
authored
Refactor hook registration and init order in ActivityPub (#2087)
1 parent c992cb6 commit 289c6c9

File tree

3 files changed

+20
-46
lines changed

3 files changed

+20
-46
lines changed

activitypub.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
Autoloader::register_path( __NAMESPACE__, __DIR__ . '/includes' );
3535

36+
\register_activation_hook( __FILE__, array( Activitypub::class, 'activate' ) );
37+
\register_deactivation_hook( __FILE__, array( Activitypub::class, 'deactivate' ) );
38+
\register_uninstall_hook( __FILE__, array( Activitypub::class, 'uninstall' ) );
39+
3640
/**
3741
* Initialize REST routes.
3842
*/
@@ -68,6 +72,7 @@ function plugin_init() {
6872
\add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
6973
\add_action( 'init', array( __NAMESPACE__ . '\Comment', 'init' ) );
7074
\add_action( 'init', array( __NAMESPACE__ . '\Dispatcher', 'init' ) );
75+
\add_action( 'init', array( __NAMESPACE__ . '\Embed', 'init' ) );
7176
\add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) );
7277
\add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );
7378
\add_action( 'init', array( __NAMESPACE__ . '\Link', 'init' ) );
@@ -127,14 +132,6 @@ function plugin_admin_init() {
127132
}
128133
\add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_admin_init' );
129134

130-
\register_activation_hook(
131-
__FILE__,
132-
array(
133-
__NAMESPACE__ . '\Activitypub',
134-
'activate',
135-
)
136-
);
137-
138135
/**
139136
* Redirect to the welcome page after plugin activation.
140137
*
@@ -148,22 +145,6 @@ function activation_redirect( $plugin ) {
148145
}
149146
\add_action( 'activated_plugin', __NAMESPACE__ . '\activation_redirect' );
150147

151-
\register_deactivation_hook(
152-
__FILE__,
153-
array(
154-
__NAMESPACE__ . '\Activitypub',
155-
'deactivate',
156-
)
157-
);
158-
159-
\register_uninstall_hook(
160-
__FILE__,
161-
array(
162-
__NAMESPACE__ . '\Activitypub',
163-
'uninstall',
164-
)
165-
);
166-
167148
// Check for CLI env, to add the CLI commands.Add commentMore actions.
168149
if ( defined( 'WP_CLI' ) && WP_CLI ) {
169150
\WP_CLI::add_command(

includes/class-activitypub.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
use Activitypub\Activity\Activity;
1111
use Activitypub\Collection\Actors;
12+
use Activitypub\Collection\Extra_Fields;
13+
use Activitypub\Collection\Followers;
1214
use Activitypub\Collection\Inbox;
1315
use Activitypub\Collection\Outbox;
14-
use Activitypub\Collection\Followers;
15-
use Activitypub\Collection\Extra_Fields;
1616

1717
/**
1818
* ActivityPub Class.
@@ -24,41 +24,34 @@ class Activitypub {
2424
* Initialize the class, registering WordPress hooks.
2525
*/
2626
public static function init() {
27+
\add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
28+
\add_action( 'init', array( self::class, 'theme_compat' ), 11 );
29+
\add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
30+
\add_action( 'init', array( self::class, 'register_post_types' ), 11 );
31+
\add_action( 'init', array( self::class, 'register_oembed_providers' ), 11 );
32+
2733
\add_filter( 'template_include', array( self::class, 'render_activitypub_template' ), 99 );
2834
\add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
2935
\add_filter( 'redirect_canonical', array( self::class, 'redirect_canonical' ), 10, 2 );
3036
\add_filter( 'redirect_canonical', array( self::class, 'no_trailing_redirect' ), 10, 2 );
3137
\add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
3238
\add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
3339

34-
// Add support for ActivityPub to custom post types.
35-
$post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
36-
37-
foreach ( $post_types as $post_type ) {
38-
\add_post_type_support( $post_type, 'activitypub' );
39-
}
40-
4140
\add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 );
4241
\add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 );
4342

44-
\add_action( 'init', array( self::class, 'add_rewrite_rules' ), 11 );
45-
\add_action( 'init', array( self::class, 'theme_compat' ), 11 );
46-
4743
\add_action( 'user_register', array( self::class, 'user_register' ) );
4844

49-
\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
50-
5145
\add_filter( 'add_post_metadata', array( self::class, 'prevent_empty_post_meta' ), 10, 4 );
5246
\add_filter( 'update_post_metadata', array( self::class, 'prevent_empty_post_meta' ), 10, 4 );
5347
\add_filter( 'default_post_metadata', array( self::class, 'default_post_metadata' ), 10, 3 );
5448

55-
\add_action( 'init', array( self::class, 'register_user_meta' ), 11 );
56-
57-
// Register several post_types.
58-
self::register_post_types();
49+
\add_filter( 'activitypub_get_actor_extra_fields', array( Extra_Fields::class, 'default_actor_extra_fields' ), 10, 2 );
5950

60-
self::register_oembed_providers();
61-
Embed::init();
51+
// Add support for ActivityPub to custom post types.
52+
foreach ( \get_option( 'activitypub_support_post_types', array( 'post' ) ) as $post_type ) {
53+
\add_post_type_support( $post_type, 'activitypub' );
54+
}
6255
}
6356

6457
/**
@@ -449,7 +442,7 @@ public static function theme_compat() {
449442
/**
450443
* Register Custom Post Types.
451444
*/
452-
private static function register_post_types() {
445+
public static function register_post_types() {
453446
\register_post_type(
454447
Actors::POST_TYPE,
455448
array(

tests/includes/collection/class-test-inbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function test_duplicate_activity_prevention() {
215215
* Test post meta registration exists.
216216
*/
217217
public function test_post_meta_registration() {
218-
Activitypub::init();
218+
Activitypub::register_post_types();
219219

220220
// Verify that post meta is registered for inbox post type.
221221
$registered_meta = \get_registered_meta_keys( 'post', Inbox::POST_TYPE );

0 commit comments

Comments
 (0)