Skip to content

Commit aae2d65

Browse files
committed
Address Copilot review feedback
- Hook init to plugins_loaded action for proper timing - Add is_admin() check before registering admin hooks - Call init() in activate() to ensure integrations are loaded - Update activate() docblock to better describe its purpose - Add proper test cleanup for global variables - Clarify changelog wording about deprecated filter
1 parent 0a57ee3 commit aae2d65

4 files changed

Lines changed: 33 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Project and support maintained on github at [pfefferle/wordpress-nodeinfo](https
8484
### 3.1.0
8585

8686
* Added singleton-based plugin loading mechanism for better extensibility
87-
* Deprecated `wellknown_nodeinfo_data` filter in favor of `nodeinfo_discovery`
87+
* Added backwards compatibility handler for deprecated `wellknown_nodeinfo_data` filter
8888

8989
### 3.0.0
9090

includes/class-nodeinfo.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public function init() {
6565

6666
$this->register_integrations();
6767
$this->register_hooks();
68-
$this->register_admin_hooks();
68+
69+
if ( \is_admin() ) {
70+
$this->register_admin_hooks();
71+
}
6972

7073
$this->initialized = true;
7174
}
@@ -152,12 +155,14 @@ public function add_rewrite_rules() {
152155
}
153156

154157
/**
155-
* Flush rewrite rules.
158+
* Handle plugin activation.
156159
*
157-
* Should be called on plugin activation.
160+
* Initializes the plugin and flushes rewrite rules.
158161
*/
159162
public static function activate() {
160-
self::get_instance()->add_rewrite_rules();
163+
$instance = self::get_instance();
164+
$instance->init();
165+
$instance->add_rewrite_rules();
161166
\flush_rewrite_rules();
162167
}
163168

nodeinfo.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ function nodeinfo_plugin() {
3939
return Nodeinfo\Nodeinfo::get_instance();
4040
}
4141

42-
// Initialize the plugin.
43-
nodeinfo_plugin()->init();
42+
// Initialize the plugin after all plugins are loaded.
43+
add_action(
44+
'plugins_loaded',
45+
function () {
46+
nodeinfo_plugin()->init();
47+
}
48+
);
4449

4550
// Register activation and deactivation hooks.
4651
register_activation_hook( __FILE__, array( Nodeinfo\Nodeinfo::class, 'activate' ) );

tests/phpunit/tests/class-test-nodeinfo.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function test_get_instance_returns_nodeinfo() {
4545
public function test_rest_routes_registered() {
4646
global $wp_rest_server;
4747

48+
// Save original state.
49+
$original_server = $wp_rest_server;
50+
4851
$wp_rest_server = new \WP_REST_Server();
4952
do_action( 'rest_api_init' );
5053

@@ -53,7 +56,8 @@ public function test_rest_routes_registered() {
5356
$this->assertArrayHasKey( '/nodeinfo/discovery', $routes );
5457
$this->assertArrayHasKey( '/nodeinfo2/(?P<version>\\d\\.\\d)', $routes );
5558

56-
$wp_rest_server = null;
59+
// Restore original state.
60+
$wp_rest_server = $original_server;
5761
}
5862

5963
/**
@@ -79,6 +83,9 @@ public function test_integrations_register_versions() {
7983
public function test_rewrite_rules_added() {
8084
global $wp_rewrite;
8185

86+
// Save original permalink structure.
87+
$original_structure = $wp_rewrite->permalink_structure;
88+
8289
// Enable permalinks for testing.
8390
$wp_rewrite->set_permalink_structure( '/%postname%/' );
8491

@@ -93,8 +100,9 @@ public function test_rewrite_rules_added() {
93100
$this->assertArrayHasKey( '^.well-known/nodeinfo', $rules );
94101
$this->assertArrayHasKey( '^.well-known/x-nodeinfo2', $rules );
95102

96-
// Reset permalink structure.
97-
$wp_rewrite->set_permalink_structure( '' );
103+
// Restore original permalink structure.
104+
$wp_rewrite->set_permalink_structure( $original_structure );
105+
$wp_rewrite->flush_rules();
98106
}
99107

100108
/**
@@ -135,11 +143,14 @@ public function test_webfinger_filter_registered() {
135143
}
136144

137145
/**
138-
* Test admin hooks are registered.
146+
* Test admin hooks can be registered.
139147
*
140148
* @covers ::register_admin_hooks
141149
*/
142150
public function test_admin_hooks_registered() {
143-
$this->assertTrue( has_action( 'admin_init' ) !== false );
151+
// Call register_admin_hooks directly to test it works.
152+
\Nodeinfo\Nodeinfo::get_instance()->register_admin_hooks();
153+
154+
$this->assertNotFalse( has_action( 'admin_init' ) );
144155
}
145156
}

0 commit comments

Comments
 (0)