Skip to content

Commit 8c31c9a

Browse files
committed
Interactivity API: populate router's state.url in the server
Set the `core/router` state `url` property with the current page URL using `get_self_link()` when the first `data-wp-router-region` directive is processed, so that the stores subscribing to that property already have the correct value on page load. Props westonruter. Fixes #64649. git-svn-id: https://develop.svn.wordpress.org/trunk@61659 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ab93978 commit 8c31c9a

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

src/wp-includes/interactivity-api/class-wp-interactivity-api.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,14 @@ private function data_wp_router_region_processor( WP_Interactivity_API_Directive
13141314
if ( 'enter' === $mode && ! $this->has_processed_router_region ) {
13151315
$this->has_processed_router_region = true;
13161316

1317+
// Initializes the `state.url` property from the server.
1318+
$this->state(
1319+
'core/router',
1320+
array(
1321+
'url' => get_self_link(),
1322+
)
1323+
);
1324+
13171325
// Enqueues as an inline style.
13181326
wp_register_style( 'wp-interactivity-router-animations', false );
13191327
wp_add_inline_style( 'wp-interactivity-router-animations', $this->get_router_animation_styles() );

tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-router-region.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ protected function render_wp_footer() {
7676
return ob_get_clean();
7777
}
7878

79+
/**
80+
* Processes directives while temporarily replacing the global
81+
* WP_Interactivity_API instance so that global functions like
82+
* `wp_interactivity_state` operate on the test instance.
83+
*
84+
* @param string $html The HTML to process.
85+
* @return string The processed HTML.
86+
*/
87+
protected function process_directives( string $html ): string {
88+
global $wp_interactivity;
89+
$prev = $wp_interactivity;
90+
$wp_interactivity = $this->interactivity;
91+
92+
$result = $this->interactivity->process_directives( $html );
93+
94+
$wp_interactivity = $prev;
95+
return $result;
96+
}
97+
7998
/**
8099
* Tests that no elements are added if the `data-wp-router-region` is
81100
* missing.
@@ -126,4 +145,58 @@ public function test_wp_router_region_adds_loading_bar_region_only_once() {
126145
$this->assertTrue( $p->next_tag( $query ) );
127146
$this->assertFalse( $p->next_tag( $query ) );
128147
}
148+
149+
/**
150+
* Tests that the `data-wp-router-region` directive initializes the
151+
* `core/router` state URL from the server.
152+
*
153+
* @ticket 64649
154+
*
155+
* @covers ::process_directives
156+
*/
157+
public function test_wp_router_region_initializes_state_url() {
158+
$_SERVER['REQUEST_URI'] = '/test-page/?query=1';
159+
160+
$html = '<div data-wp-router-region="region A">Interactive region</div>';
161+
$this->process_directives( $html );
162+
163+
$state = $this->interactivity->state( 'core/router' );
164+
$this->assertSame( home_url( '/test-page/?query=1' ), $state['url'] );
165+
}
166+
167+
/**
168+
* Tests that the `core/router` state URL uses HTTPS when SSL is active.
169+
*
170+
* @ticket 64649
171+
*
172+
* @covers ::process_directives
173+
*/
174+
public function test_wp_router_region_initializes_state_url_with_https() {
175+
$_SERVER['REQUEST_URI'] = '/';
176+
$_SERVER['HTTPS'] = 'on';
177+
178+
$html = '<div data-wp-router-region="region A">Interactive region</div>';
179+
$this->process_directives( $html );
180+
181+
$state = $this->interactivity->state( 'core/router' );
182+
$this->assertStringStartsWith( 'https://', $state['url'] );
183+
}
184+
185+
/**
186+
* Tests that the `core/router` state URL is not set when no
187+
* `data-wp-router-region` directive is present.
188+
*
189+
* @ticket 64649
190+
*
191+
* @covers ::process_directives
192+
*/
193+
public function test_wp_router_region_does_not_set_state_url_without_directive() {
194+
$_SERVER['REQUEST_URI'] = '/';
195+
196+
$html = '<div>Nothing here</div>';
197+
$this->process_directives( $html );
198+
199+
$state = $this->interactivity->state( 'core/router' );
200+
$this->assertEmpty( $state );
201+
}
129202
}

0 commit comments

Comments
 (0)