Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,14 @@ private function data_wp_router_region_processor( WP_Interactivity_API_Directive
if ( 'enter' === $mode && ! $this->has_processed_router_region ) {
$this->has_processed_router_region = true;

// Initializes the `state.url` property from the server.
$this->state(
'core/router',
array(
'url' => get_self_link(),
)
);

// Enqueues as an inline style.
wp_register_style( 'wp-interactivity-router-animations', false );
wp_add_inline_style( 'wp-interactivity-router-animations', $this->get_router_animation_styles() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ protected function render_wp_footer() {
return ob_get_clean();
}

/**
* Processes directives while temporarily replacing the global
* WP_Interactivity_API instance so that global functions like
* `wp_interactivity_state` operate on the test instance.
*
* @param string $html The HTML to process.
* @return string The processed HTML.
*/
protected function process_directives( string $html ): string {
global $wp_interactivity;
$prev = $wp_interactivity;
$wp_interactivity = $this->interactivity;

$result = $this->interactivity->process_directives( $html );

$wp_interactivity = $prev;
return $result;
}

/**
* Tests that no elements are added if the `data-wp-router-region` is
* missing.
Expand Down Expand Up @@ -126,4 +145,58 @@ public function test_wp_router_region_adds_loading_bar_region_only_once() {
$this->assertTrue( $p->next_tag( $query ) );
$this->assertFalse( $p->next_tag( $query ) );
}

/**
* Tests that the `data-wp-router-region` directive initializes the
* `core/router` state URL from the server.
*
* @ticket 64649
*
* @covers ::process_directives
*/
public function test_wp_router_region_initializes_state_url() {
$_SERVER['REQUEST_URI'] = '/test-page/?query=1';

$html = '<div data-wp-router-region="region A">Interactive region</div>';
$this->process_directives( $html );

$state = $this->interactivity->state( 'core/router' );
$this->assertSame( home_url( '/test-page/?query=1' ), $state['url'] );
}

/**
* Tests that the `core/router` state URL uses HTTPS when SSL is active.
*
* @ticket 64649
*
* @covers ::process_directives
*/
public function test_wp_router_region_initializes_state_url_with_https() {
$_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTPS'] = 'on';

$html = '<div data-wp-router-region="region A">Interactive region</div>';
$this->process_directives( $html );

$state = $this->interactivity->state( 'core/router' );
$this->assertStringStartsWith( 'https://', $state['url'] );
}

/**
* Tests that the `core/router` state URL is not set when no
* `data-wp-router-region` directive is present.
*
* @ticket 64649
*
* @covers ::process_directives
*/
public function test_wp_router_region_does_not_set_state_url_without_directive() {
$_SERVER['REQUEST_URI'] = '/';

$html = '<div>Nothing here</div>';
$this->process_directives( $html );

$state = $this->interactivity->state( 'core/router' );
$this->assertEmpty( $state );
}
}
Loading