From f1de5d1f3d3e8adbe87c6c2f199d1d3a9978311d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 30 Apr 2024 14:57:31 +0200 Subject: [PATCH 1/5] Add warnings if loading translations too early --- src/wp-includes/l10n.php | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 9e2a86a7598b2..9e76af44e6ec8 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -997,6 +997,19 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path return false; } + if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: The text domain. 2: 'init'. */ + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + '' . esc_html( $domain ) . '', + 'init' + ), + '6.6.0' + ); + } + /** * Filters a plugin's locale. * @@ -1049,6 +1062,19 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { return false; } + if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: The text domain. 2: 'init'. */ + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + '' . esc_html( $domain ) . '', + 'init' + ), + '6.6.0' + ); + } + /** This filter is documented in wp-includes/l10n.php */ $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); @@ -1092,6 +1118,19 @@ function load_theme_textdomain( $domain, $path = false ) { return false; } + if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: The text domain. 2: 'init'. */ + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + '' . esc_html( $domain ) . '', + 'init' + ), + '6.6.0' + ); + } + /** * Filters a theme's locale. * @@ -1371,6 +1410,20 @@ function _load_textdomain_just_in_time( $domain ) { if ( ! $path ) { return false; } + + if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: The text domain. 2: 'init'. */ + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + '' . esc_html( $domain ) . '', + 'init' + ), + '6.6.0' + ); + } + // Themes with their language directory outside of WP_LANG_DIR have a different file name. $template_directory = trailingslashit( get_template_directory() ); $stylesheet_directory = trailingslashit( get_stylesheet_directory() ); From f882f92eaf4071a0d73297063f9b6c83f240a648 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 1 May 2024 21:33:14 +0200 Subject: [PATCH 2/5] Remove escaping --- src/wp-includes/l10n.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 9e76af44e6ec8..86ec08ef46cf6 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1003,7 +1003,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path sprintf( /* translators: 1: The text domain. 2: 'init'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), - '' . esc_html( $domain ) . '', + '' . $domain . '', 'init' ), '6.6.0' @@ -1068,7 +1068,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { sprintf( /* translators: 1: The text domain. 2: 'init'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), - '' . esc_html( $domain ) . '', + '' . $domain . '', 'init' ), '6.6.0' @@ -1124,7 +1124,7 @@ function load_theme_textdomain( $domain, $path = false ) { sprintf( /* translators: 1: The text domain. 2: 'init'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), - '' . esc_html( $domain ) . '', + '' . $domain . '', 'init' ), '6.6.0' @@ -1417,7 +1417,7 @@ function _load_textdomain_just_in_time( $domain ) { sprintf( /* translators: 1: The text domain. 2: 'init'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), - '' . esc_html( $domain ) . '', + '' . $domain . '', 'init' ), '6.6.0' From 80fff1611fc94e559add222ec44bfd629bf27289 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 22 May 2024 11:10:50 +0200 Subject: [PATCH 3/5] Change hook & wording --- src/wp-includes/l10n.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 9ec0cb3c3e970..6f578ade3c46f 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -999,7 +999,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path return false; } - if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( @@ -1064,7 +1064,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { return false; } - if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( @@ -1120,7 +1120,7 @@ function load_theme_textdomain( $domain, $path = false ) { return false; } - if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( @@ -1413,12 +1413,12 @@ function _load_textdomain_just_in_time( $domain ) { return false; } - if ( ! doing_action( 'init' ) && ! did_action( 'init' ) ) { + if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: The text domain. 2: 'init'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code running too early.' ), '' . $domain . '', 'init' ), From d1ec0632e208caf46866c94257750affb842b61e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 10 Jun 2024 10:41:35 +0200 Subject: [PATCH 4/5] Apply suggestions from code review --- src/wp-includes/l10n.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 6f578ade3c46f..0d969cf78bc82 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1003,12 +1003,12 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: The text domain. 2: 'init'. */ + /* translators: 1: The text domain. 2: 'after_setup_theme'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), '' . $domain . '', - 'init' + 'after_setup_theme' ), - '6.6.0' + '6.7.0' ); } @@ -1068,12 +1068,12 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: The text domain. 2: 'init'. */ + /* translators: 1: The text domain. 2: 'after_setup_theme'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), '' . $domain . '', - 'init' + 'after_setup_theme' ), - '6.6.0' + '6.7.0' ); } @@ -1124,12 +1124,12 @@ function load_theme_textdomain( $domain, $path = false ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: The text domain. 2: 'init'. */ + /* translators: 1: The text domain. 2: 'after_setup_theme'. */ __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), '' . $domain . '', - 'init' + 'after_setup_theme' ), - '6.6.0' + '6.7.0' ); } @@ -1417,12 +1417,12 @@ function _load_textdomain_just_in_time( $domain ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: The text domain. 2: 'init'. */ - __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code running too early.' ), + /* translators: 1: The text domain. 2: 'after_setup_theme'. */ + __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code in the plugin/theme running too early.' ), '' . $domain . '', - 'init' + 'after_setup_theme' ), - '6.6.0' + '6.7.0' ); } From b78807088b0d8ea731b91e9c5feb892a7e8915af Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 3 Sep 2024 15:14:06 +0200 Subject: [PATCH 5/5] Apply suggestions from code review --- src/wp-includes/l10n.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 0d969cf78bc82..a5669f72056c6 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1004,7 +1004,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path __FUNCTION__, sprintf( /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), '' . $domain . '', 'after_setup_theme' ), @@ -1069,7 +1069,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { __FUNCTION__, sprintf( /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), '' . $domain . '', 'after_setup_theme' ), @@ -1125,7 +1125,7 @@ function load_theme_textdomain( $domain, $path = false ) { __FUNCTION__, sprintf( /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user has been already set up.' ), + __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), '' . $domain . '', 'after_setup_theme' ), @@ -1417,10 +1417,9 @@ function _load_textdomain_just_in_time( $domain ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code in the plugin/theme running too early.' ), - '' . $domain . '', - 'after_setup_theme' + /* translators: %s: The text domain. */ + __( 'Translation loading for the %s domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early.' ), + '' . $domain . '' ), '6.7.0' );