Skip to content

Commit ad1fabe

Browse files
Interactivity API: Move interactivity-router i18n strings to Script Module data.
Moves the 'loading' and 'loaded' i18n strings for the `interactivity-router` to the script module data via the `script_module_data_@wordpress/interactivity-router` filter. Key changes: - Add the `filter_script_module_interactivity_router_data()` method, hooked into the `script_module_data_@wordpress/interactivity-router` filter, to set the `i18n` data with the 'loading' and 'loaded' messages. - Rename the `print_router_loading_and_screen_reader_markup()` method to `print_router_markup()` and remove the screen reader markup from it because it's no longer needed. - Remove the `loading` and `loaded` strings from the `core/router` store state because they're no longer needed. - Initialize the `core/router` store with a minimal navigation object to prevent errors in the interactivity-router script module when the store is not properly initialized. - Update corresponding unit tests to reflect these changes. This change ensures that the `interactivity-router` i18n messages are localized in a single place and removes the need to initialize them in the `core/router` store state. Props jonsurrell, swissspidy, czapla. See #60647. git-svn-id: https://develop.svn.wordpress.org/trunk@59097 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 66afbbd commit ad1fabe

2 files changed

Lines changed: 34 additions & 25 deletions

File tree

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ public function print_client_interactivity_data() {
199199
_deprecated_function( __METHOD__, '6.7.0' );
200200
}
201201

202+
/**
203+
* Set client-side interactivity-router data.
204+
*
205+
* Once in the browser, the state will be parsed and used to hydrate the client-side
206+
* interactivity stores and the configuration will be available using a `getConfig` utility.
207+
*
208+
* @since 6.7.0
209+
*
210+
* @param array $data Data to filter.
211+
* @return array Data for the Interactivity Router script module.
212+
*/
213+
public function filter_script_module_interactivity_router_data( array $data ): array {
214+
if ( ! isset( $data['i18n'] ) ) {
215+
$data['i18n'] = array();
216+
}
217+
$data['i18n']['loading'] = __( 'Loading page, please wait.' );
218+
$data['i18n']['loaded'] = __( 'Page Loaded.' );
219+
return $data;
220+
}
221+
202222
/**
203223
* Set client-side interactivity data.
204224
*
@@ -296,6 +316,7 @@ public function register_script_modules() {
296316
*/
297317
public function add_hooks() {
298318
add_filter( 'script_module_data_@wordpress/interactivity', array( $this, 'filter_script_module_interactivity_data' ) );
319+
add_filter( 'script_module_data_@wordpress/interactivity-router', array( $this, 'filter_script_module_interactivity_router_data' ) );
299320
}
300321

301322
/**
@@ -973,29 +994,21 @@ private function get_router_animation_styles(): string {
973994
}
974995

975996
/**
976-
* Outputs the markup for the top loading indicator and the screen reader
977-
* notifications during client-side navigations.
997+
* Outputs markup for the @wordpress/interactivity-router script module.
978998
*
979999
* This method prints a div element representing a loading bar visible during
980-
* navigation, as well as an aria-live region that can be read by screen
981-
* readers to announce navigation status.
1000+
* navigation.
9821001
*
983-
* @since 6.5.0
1002+
* @since 6.7.0
9841003
*/
985-
public function print_router_loading_and_screen_reader_markup() {
1004+
public function print_router_markup() {
9861005
echo <<<HTML
9871006
<div
9881007
class="wp-interactivity-router-loading-bar"
9891008
data-wp-interactive="core/router"
9901009
data-wp-class--start-animation="state.navigation.hasStarted"
9911010
data-wp-class--finish-animation="state.navigation.hasFinished"
9921011
></div>
993-
<div
994-
class="screen-reader-text"
995-
aria-live="polite"
996-
data-wp-interactive="core/router"
997-
data-wp-text="state.navigation.message"
998-
></div>
9991012
HTML;
10001013
}
10011014

@@ -1016,16 +1029,16 @@ private function data_wp_router_region_processor( WP_Interactivity_API_Directive
10161029
if ( 'enter' === $mode && ! $this->has_processed_router_region ) {
10171030
$this->has_processed_router_region = true;
10181031

1019-
// Initialize the `core/router` store.
1032+
/*
1033+
* Initialize the `core/router` store.
1034+
* If the store is not initialized like this with minimal
1035+
* navigation object, the interactivity-router script module
1036+
* errors.
1037+
*/
10201038
$this->state(
10211039
'core/router',
10221040
array(
1023-
'navigation' => array(
1024-
'texts' => array(
1025-
'loading' => __( 'Loading page, please wait.' ),
1026-
'loaded' => __( 'Page Loaded.' ),
1027-
),
1028-
),
1041+
'navigation' => new stdClass(),
10291042
)
10301043
);
10311044

@@ -1035,7 +1048,7 @@ private function data_wp_router_region_processor( WP_Interactivity_API_Directive
10351048
wp_enqueue_style( 'wp-interactivity-router-animations' );
10361049

10371050
// Adds the necessary markup to the footer.
1038-
add_action( 'wp_footer', array( $this, 'print_router_loading_and_screen_reader_markup' ) );
1051+
add_action( 'wp_footer', array( $this, 'print_router_markup' ) );
10391052
}
10401053
}
10411054

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function test_wp_router_region_missing() {
102102
*
103103
* @covers ::process_directives
104104
*/
105-
public function test_wp_router_region_adds_loading_bar_aria_live_region_only_once() {
105+
public function test_wp_router_region_adds_loading_bar_region_only_once() {
106106
$html = '
107107
<div data-wp-router-region="region A">Interactive region</div>
108108
<div data-wp-router-region="region B">Another interactive region</div>
@@ -125,9 +125,5 @@ public function test_wp_router_region_adds_loading_bar_aria_live_region_only_onc
125125
$p = new WP_HTML_Tag_Processor( $footer );
126126
$this->assertTrue( $p->next_tag( $query ) );
127127
$this->assertFalse( $p->next_tag( $query ) );
128-
$query = array( 'class_name' => 'screen-reader-text' );
129-
$p = new WP_HTML_Tag_Processor( $footer );
130-
$this->assertTrue( $p->next_tag( $query ) );
131-
$this->assertFalse( $p->next_tag( $query ) );
132128
}
133129
}

0 commit comments

Comments
 (0)