Skip to content

Commit deb6c7f

Browse files
committed
Use rewrite_rules_array filter for better performance
Instead of calling add_rewrite_rule() on every init hook, use the rewrite_rules_array filter which only fires during flush operations. This is more efficient as rules are cached in the database.
1 parent 35e31eb commit deb6c7f

2 files changed

Lines changed: 25 additions & 49 deletions

File tree

includes/class-nodeinfo.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public function register_hooks() {
9898
\add_filter( 'webfinger_post_data', array( Controller_Nodeinfo::class, 'jrd' ), 10, 3 );
9999
\add_filter( 'host_meta', array( Controller_Nodeinfo::class, 'jrd' ) );
100100

101-
// Add rewrite rules for well-known endpoints.
102-
\add_action( 'init', array( $this, 'add_rewrite_rules' ), 1 );
101+
// Add rewrite rules for well-known endpoints (only during flush).
102+
\add_filter( 'rewrite_rules_array', array( $this, 'add_rewrite_rules' ) );
103103

104104
// Register deprecated filter handlers.
105105
\add_filter( 'nodeinfo_discovery', array( $this, 'deprecated_wellknown_nodeinfo_data' ), 99 );
@@ -148,25 +148,27 @@ public function register_routes() {
148148

149149
/**
150150
* Add rewrite rules for well-known endpoints.
151+
*
152+
* @param array $rules The existing rewrite rules.
153+
* @return array The modified rewrite rules.
151154
*/
152-
public function add_rewrite_rules() {
153-
\add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/nodeinfo/discovery', 'top' );
154-
\add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/nodeinfo2/1.0', 'top' );
155+
public function add_rewrite_rules( $rules ) {
156+
$new_rules = array(
157+
'^.well-known/nodeinfo' => 'index.php?rest_route=/nodeinfo/discovery',
158+
'^.well-known/x-nodeinfo2' => 'index.php?rest_route=/nodeinfo2/1.0',
159+
);
160+
161+
return \array_merge( $new_rules, $rules );
155162
}
156163

157164
/**
158165
* Handle plugin activation.
159166
*
160-
* Initializes the plugin and flushes rewrite rules.
161-
*
162-
* Note: We call init() to register all hooks. However, during activation
163-
* the 'init' hook may have already fired, so we also call add_rewrite_rules()
164-
* directly to ensure rules are registered.
167+
* Initializes the plugin and flushes rewrite rules. The rewrite_rules_array
168+
* filter will add our rules during the flush.
165169
*/
166170
public static function activate() {
167-
$instance = self::get_instance();
168-
$instance->init();
169-
$instance->add_rewrite_rules();
171+
self::get_instance()->init();
170172
\flush_rewrite_rules();
171173
}
172174

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

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,16 @@ public function test_integrations_register_versions() {
8181
* @covers ::add_rewrite_rules
8282
*/
8383
public function test_rewrite_rules_added() {
84-
global $wp_rewrite;
85-
86-
// Save original permalink structure.
87-
$original_structure = $wp_rewrite->permalink_structure;
88-
89-
// Enable permalinks for testing.
90-
$wp_rewrite->set_permalink_structure( '/%postname%/' );
91-
92-
// Add rewrite rules.
93-
\Nodeinfo\Nodeinfo::get_instance()->add_rewrite_rules();
94-
$wp_rewrite->flush_rules();
84+
$instance = \Nodeinfo\Nodeinfo::get_instance();
9585

96-
$rules = $wp_rewrite->wp_rewrite_rules();
86+
// Test the filter directly.
87+
$rules = $instance->add_rewrite_rules( array() );
9788

98-
// Ensure rules is an array.
9989
$this->assertIsArray( $rules );
10090
$this->assertArrayHasKey( '^.well-known/nodeinfo', $rules );
10191
$this->assertArrayHasKey( '^.well-known/x-nodeinfo2', $rules );
102-
103-
// Restore original permalink structure.
104-
$wp_rewrite->set_permalink_structure( $original_structure );
105-
$wp_rewrite->flush_rules();
92+
$this->assertEquals( 'index.php?rest_route=/nodeinfo/discovery', $rules['^.well-known/nodeinfo'] );
93+
$this->assertEquals( 'index.php?rest_route=/nodeinfo2/1.0', $rules['^.well-known/x-nodeinfo2'] );
10694
}
10795

10896
/**
@@ -175,30 +163,16 @@ public function test_init_guard_prevents_double_initialization() {
175163
}
176164

177165
/**
178-
* Test activate() method registers rewrite rules.
166+
* Test activate() method initializes plugin and flushes rewrite rules.
179167
*
180168
* @covers ::activate
181169
*/
182-
public function test_activate_registers_rewrite_rules() {
183-
global $wp_rewrite;
184-
185-
// Save original permalink structure.
186-
$original_structure = $wp_rewrite->permalink_structure;
187-
188-
// Enable permalinks for testing.
189-
$wp_rewrite->set_permalink_structure( '/%postname%/' );
190-
191-
// Call activate.
170+
public function test_activate_initializes_and_flushes() {
171+
// Verify activate() runs without errors and initializes the plugin.
192172
\Nodeinfo\Nodeinfo::activate();
193173

194-
$rules = $wp_rewrite->wp_rewrite_rules();
195-
196-
$this->assertIsArray( $rules );
197-
$this->assertArrayHasKey( '^.well-known/nodeinfo', $rules );
198-
199-
// Restore original permalink structure.
200-
$wp_rewrite->set_permalink_structure( $original_structure );
201-
$wp_rewrite->flush_rules();
174+
// Verify the rewrite_rules_array filter is registered.
175+
$this->assertNotFalse( has_filter( 'rewrite_rules_array' ) );
202176
}
203177

204178
/**

0 commit comments

Comments
 (0)