From fff4c372404f848fcac2912ea24cf99dd6ab7299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Fri, 17 Oct 2025 13:34:52 +0300 Subject: [PATCH 1/3] TMS-1209: Rework window.s translations inline script --- CHANGELOG.MD | 2 + lib/Assets.php | 110 +++++++++++++++++++++++++++---------------------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index df6d6511..01e91538 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +- TMS-1209: Rework window.s translations inline script + ## [1.66.1] - 2025-10-01 - TMS-1199: Change events-search no-results text diff --git a/lib/Assets.php b/lib/Assets.php index fa9514e4..06e7a061 100644 --- a/lib/Assets.php +++ b/lib/Assets.php @@ -66,12 +66,18 @@ public function hooks() : void { 0 ); - add_filter( + \add_filter( 'script_loader_tag', \Closure::fromCallable( [ $this, 'add_script_attributes' ] ), 10, 2 ); + + // Add localization to footer with data-attribute to be ignored by Cookiebot + \add_action( + 'wp_footer', + \Closure::fromCallable( [ $this, 'add_localizations_to_footer' ] ) + ); } /** @@ -89,18 +95,18 @@ private function add_editor_styles() : void { * @param string $theme Theme file name without prefix 'theme_' or suffix '.js/.css'. */ protected function enqueue_theme( $theme = 'tunnelma' ) : void { - $css = apply_filters( 'tms/theme/theme_css_file', sprintf( 'theme_%s.css', $theme ) ); - $js = apply_filters( 'tms/theme/theme_js_file', sprintf( 'theme_%s.js', $theme ) ); + $css = \apply_filters( 'tms/theme/theme_css_file', sprintf( 'theme_%s.css', $theme ) ); + $js = \apply_filters( 'tms/theme/theme_js_file', sprintf( 'theme_%s.js', $theme ) ); \wp_enqueue_style( 'theme-css', - apply_filters( + \apply_filters( 'tms/theme/theme_css_path', DPT_ASSET_URI . '/' . $css, $css ), [], - apply_filters( + \apply_filters( 'tms/theme/asset_mod_time', static::get_theme_asset_mod_time( $css ), $css @@ -110,13 +116,13 @@ protected function enqueue_theme( $theme = 'tunnelma' ) : void { \wp_enqueue_script( 'theme-js', - apply_filters( + \apply_filters( 'tms/theme/theme_js_path', DPT_ASSET_URI . '/' . $js, $js ), [ 'jquery', 'vendor-js' ], - apply_filters( + \apply_filters( 'tms/theme/asset_mod_time', static::get_theme_asset_mod_time( $js ), $js @@ -131,13 +137,13 @@ protected function enqueue_theme( $theme = 'tunnelma' ) : void { private function enqueue_assets() : void { \wp_enqueue_script( 'vendor-js', - apply_filters( + \apply_filters( 'tms/theme/theme_js_path', DPT_ASSET_URI . '/vendor.js', 'vendor.js' ), [ 'jquery', 'wp-a11y' ], - apply_filters( + \apply_filters( 'tms/theme/asset_mod_time', static::get_theme_asset_mod_time( 'vendor.js' ), 'vendor.js' @@ -145,27 +151,18 @@ private function enqueue_assets() : void { true ); - $theme_default_color = apply_filters( + $theme_default_color = \apply_filters( 'tms/theme/theme_default_color', DEFAULT_THEME_COLOR ); - $selected_theme = apply_filters( + $selected_theme = \apply_filters( 'tms/theme/theme_selected', Settings::get_setting( 'theme_color' ) ?? $theme_default_color ); $this->enqueue_theme( $selected_theme ); - /** - * Add localizations to window.s object. - */ - \wp_localize_script( 'theme-js', 's', ( new \Strings() )->s() ); - - \wp_localize_script( 'theme-js', 'themeData', [ - 'assetsUri' => esc_url( get_template_directory_uri() ), - ] ); - \wp_dequeue_style( 'wp-block-library' ); \wp_enqueue_script( // phpcs:ignore @@ -185,6 +182,19 @@ private function enqueue_assets() : void { ); } + /** + * Add localizations to window.s object. + */ + public static function add_localizations_to_footer() { + $script_strings = ( new \Strings() )->s(); + $script = 'var s = ' . json_encode( $script_strings ) . ';'; + + \wp_print_inline_script_tag( $script, [ + 'id' => 'themeData', + 'data-cookieconsent' => 'ignore', + ] ); + } + /** * Add attributes to enqueued script tags * @@ -397,36 +407,36 @@ protected function get_theme_icons() { */ public static function get_accessibility_icons() { return [ - 'aaniopastus' => __( 'Audio guide', 'tms-theme-base' ), - 'avustaja' => __( 'Assistant', 'tms-theme-base' ), - 'hissi' => __( 'Lift', 'tms-theme-base' ), - 'induktiosilmukka' => __( 'Induction loop', 'tms-theme-base' ), - 'info' => __( 'Information', 'tms-theme-base' ), - 'internet' => __( 'Internet', 'tms-theme-base' ), - 'isa' => __( 'Accessible entrance, Accessible toilet, Accessible parking', 'tms-theme-base' ), - 'kahvila' => __( 'Café', 'tms-theme-base' ), - 'kauppa' => __( 'Shop', 'tms-theme-base' ), - 'kokoustilaa' => __( 'Meeting room', 'tms-theme-base' ), - 'lainattavia-valineita' => __( 'Assistive device lending', 'tms-theme-base' ), - 'lastenhoitotila' => __( 'Baby changing', 'tms-theme-base' ), - 'latauspiste' => __( 'Charging point', 'tms-theme-base' ), - 'lipunmyynti' => __( 'Ticket sales', 'tms-theme-base' ), - 'luiska' => __( 'Ramp', 'tms-theme-base' ), - 'nakovammaisia_helpottavat_palvelut' => __( 'Services for the visually impaired', 'tms-theme-base' ), - 'opaskoirat_sallittu' => __( 'Service dogs allowed', 'tms-theme-base' ), - 'pistekirjoitus' => __( 'Braille', 'tms-theme-base' ), - 'porrashissi' => __( 'Stairlift', 'tms-theme-base' ), - 'portaat' => __( 'Staircase', 'tms-theme-base' ), - 'pyoratuolihissi' => __( 'Platform lift', 'tms-theme-base' ), - 'rollaattori' => __( 'Rollator, walker', 'tms-theme-base' ), - 'sahkomopon_sailytys' => __( 'Electric mobility scooter storage', 'tms-theme-base' ), - 'sailytyslokerot' => __( 'Lockers', 'tms-theme-base' ), - 'suuri_teksti' => __( 'Large print', 'tms-theme-base' ), - 'vaatesailytys' => __( 'Cloakroom', 'tms-theme-base' ), - 'viitomakielinen_palvelu' => __( 'Sign-language services', 'tms-theme-base' ), - 'wc_oikea' => __( 'Accessible toilet: one-sided access', 'tms-theme-base' ), - 'wc_vasen' => __( 'Accessible toilet: one-sided access', 'tms-theme-base' ), - 'wc' => __( 'WC', 'tms-theme-base' ), + 'aaniopastus' => \__( 'Audio guide', 'tms-theme-base' ), + 'avustaja' => \__( 'Assistant', 'tms-theme-base' ), + 'hissi' => \__( 'Lift', 'tms-theme-base' ), + 'induktiosilmukka' => \__( 'Induction loop', 'tms-theme-base' ), + 'info' => \__( 'Information', 'tms-theme-base' ), + 'internet' => \__( 'Internet', 'tms-theme-base' ), + 'isa' => \__( 'Accessible entrance, Accessible toilet, Accessible parking', 'tms-theme-base' ), + 'kahvila' => \__( 'Café', 'tms-theme-base' ), + 'kauppa' => \__( 'Shop', 'tms-theme-base' ), + 'kokoustilaa' => \__( 'Meeting room', 'tms-theme-base' ), + 'lainattavia-valineita' => \__( 'Assistive device lending', 'tms-theme-base' ), + 'lastenhoitotila' => \__( 'Baby changing', 'tms-theme-base' ), + 'latauspiste' => \__( 'Charging point', 'tms-theme-base' ), + 'lipunmyynti' => \__( 'Ticket sales', 'tms-theme-base' ), + 'luiska' => \__( 'Ramp', 'tms-theme-base' ), + 'nakovammaisia_helpottavat_palvelut' => \__( 'Services for the visually impaired', 'tms-theme-base' ), + 'opaskoirat_sallittu' => \__( 'Service dogs allowed', 'tms-theme-base' ), + 'pistekirjoitus' => \__( 'Braille', 'tms-theme-base' ), + 'porrashissi' => \__( 'Stairlift', 'tms-theme-base' ), + 'portaat' => \__( 'Staircase', 'tms-theme-base' ), + 'pyoratuolihissi' => \__( 'Platform lift', 'tms-theme-base' ), + 'rollaattori' => \__( 'Rollator, walker', 'tms-theme-base' ), + 'sahkomopon_sailytys' => \__( 'Electric mobility scooter storage', 'tms-theme-base' ), + 'sailytyslokerot' => \__( 'Lockers', 'tms-theme-base' ), + 'suuri_teksti' => \__( 'Large print', 'tms-theme-base' ), + 'vaatesailytys' => \__( 'Cloakroom', 'tms-theme-base' ), + 'viitomakielinen_palvelu' => \__( 'Sign-language services', 'tms-theme-base' ), + 'wc_oikea' => \__( 'Accessible toilet: one-sided access', 'tms-theme-base' ), + 'wc_vasen' => \__( 'Accessible toilet: one-sided access', 'tms-theme-base' ), + 'wc' => \__( 'WC', 'tms-theme-base' ), ]; } } From b6ebbccdfb52dd47233aa77900f079333c153cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= <31741222+eebbi@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:46:09 +0300 Subject: [PATCH 2/3] TMS-1209: Update function comment Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/Assets.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Assets.php b/lib/Assets.php index 06e7a061..b972910b 100644 --- a/lib/Assets.php +++ b/lib/Assets.php @@ -185,6 +185,12 @@ private function enqueue_assets() : void { /** * Add localizations to window.s object. */ + /** + * Adds localization strings to the footer by printing an inline script that sets + * the global `s` object with theme strings. + * + * @return void + */ public static function add_localizations_to_footer() { $script_strings = ( new \Strings() )->s(); $script = 'var s = ' . json_encode( $script_strings ) . ';'; From a41babe24605ecf96f5297e4d122569ed34ca565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20V=C3=A4nttinen?= Date: Wed, 22 Oct 2025 10:03:37 +0300 Subject: [PATCH 3/3] TMS-1209: Load strings with wp_add_inline_script --- lib/Assets.php | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/Assets.php b/lib/Assets.php index b972910b..9e7bb173 100644 --- a/lib/Assets.php +++ b/lib/Assets.php @@ -72,12 +72,6 @@ public function hooks() : void { 10, 2 ); - - // Add localization to footer with data-attribute to be ignored by Cookiebot - \add_action( - 'wp_footer', - \Closure::fromCallable( [ $this, 'add_localizations_to_footer' ] ) - ); } /** @@ -163,6 +157,23 @@ private function enqueue_assets() : void { $this->enqueue_theme( $selected_theme ); + /** + * Add localizations to window.s object. + */ + \wp_add_inline_script( + 'theme-js', + 'var s = ' . json_encode( ( new \Strings() )->s() ) . ';', + 'before' + ); + + \wp_add_inline_script( + 'theme-js', + 'var themeData = ' . json_encode( [ + 'assetsUri' => \esc_url( \get_template_directory_uri() ), + ] ) . ';', + 'before' + ); + \wp_dequeue_style( 'wp-block-library' ); \wp_enqueue_script( // phpcs:ignore @@ -182,25 +193,6 @@ private function enqueue_assets() : void { ); } - /** - * Add localizations to window.s object. - */ - /** - * Adds localization strings to the footer by printing an inline script that sets - * the global `s` object with theme strings. - * - * @return void - */ - public static function add_localizations_to_footer() { - $script_strings = ( new \Strings() )->s(); - $script = 'var s = ' . json_encode( $script_strings ) . ';'; - - \wp_print_inline_script_tag( $script, [ - 'id' => 'themeData', - 'data-cookieconsent' => 'ignore', - ] ); - } - /** * Add attributes to enqueued script tags *