Skip to content

Commit c3380cc

Browse files
committed
I18N: Extract shared helper for loading script translation files.
Refactor load_script_textdomain() and load_script_module_textdomain() to share a private helper, _load_script_textdomain_from_src(), that handles the path resolution and file lookup logic. Each public function now only resolves the source URL for its respective registry and delegates to the helper with the appropriate filter name. Reduces duplication by ~88 lines while preserving behavior and the existing load_script_textdomain_relative_path filter.
1 parent cee8f0c commit c3380cc

1 file changed

Lines changed: 45 additions & 134 deletions

File tree

src/wp-includes/l10n.php

Lines changed: 45 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,24 +1134,52 @@ function load_child_theme_textdomain( $domain, $path = false ) {
11341134
*
11351135
* @see WP_Scripts::set_translations()
11361136
*
1137-
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1138-
*
11391137
* @param string $handle Name of the script to register a translation domain to.
11401138
* @param string $domain Optional. Text domain. Default 'default'.
11411139
* @param string $path Optional. The full file path to the directory containing translation files.
11421140
* @return string|false The translated strings in JSON encoding on success,
11431141
* false if the script textdomain could not be loaded.
11441142
*/
11451143
function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
1146-
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
1147-
global $wp_textdomain_registry;
1148-
11491144
$wp_scripts = wp_scripts();
11501145

11511146
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
11521147
return false;
11531148
}
11541149

1150+
$src = $wp_scripts->registered[ $handle ]->src;
1151+
1152+
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
1153+
$src = $wp_scripts->base_url . $src;
1154+
}
1155+
1156+
return _load_script_textdomain_from_src( $handle, $src, $domain, $path, 'load_script_textdomain_relative_path' );
1157+
}
1158+
1159+
/**
1160+
* Resolves and loads the translation JSON file for a given script or script module source URL.
1161+
*
1162+
* This is a shared implementation used by {@see load_script_textdomain()} and
1163+
* {@see load_script_module_textdomain()} to avoid duplicating the path
1164+
* resolution and file lookup logic.
1165+
*
1166+
* @since 7.0.0
1167+
* @access private
1168+
*
1169+
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1170+
*
1171+
* @param string $handle Name of the script or script module identifier to register a translation domain to.
1172+
* @param string $src Absolute source URL of the script or script module.
1173+
* @param string $domain Text domain.
1174+
* @param string $path The full file path to the directory containing translation files,
1175+
* or an empty string to use the default path from the text domain registry.
1176+
* @param string $filter_name Name of the filter to apply to the resolved relative path
1177+
* ('load_script_textdomain_relative_path' or 'load_script_module_textdomain_relative_path').
1178+
* @return string|false The JSON-encoded translated strings on success, false otherwise.
1179+
*/
1180+
function _load_script_textdomain_from_src( $handle, $src, $domain, $path, $filter_name ) {
1181+
global $wp_textdomain_registry;
1182+
11551183
$locale = determine_locale();
11561184

11571185
if ( ! $path ) {
@@ -1172,12 +1200,6 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
11721200
}
11731201
}
11741202

1175-
$src = $wp_scripts->registered[ $handle ]->src;
1176-
1177-
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
1178-
$src = $wp_scripts->base_url . $src;
1179-
}
1180-
11811203
$relative = false;
11821204
$languages_path = WP_LANG_DIR;
11831205

@@ -1242,14 +1264,20 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
12421264
}
12431265

12441266
/**
1245-
* Filters the relative path of scripts used for finding translation files.
1267+
* Filters the relative path used for finding translation files.
12461268
*
1247-
* @since 5.0.2
1269+
* The filter name is either `load_script_textdomain_relative_path` for
1270+
* classic scripts or `load_script_module_textdomain_relative_path` for
1271+
* script modules.
12481272
*
1249-
* @param string|false $relative The relative path of the script. False if it could not be determined.
1250-
* @param string $src The full source URL of the script.
1273+
* @since 5.0.2 The `load_script_textdomain_relative_path` filter was added.
1274+
* @since 7.0.0 The `load_script_module_textdomain_relative_path` filter was added.
1275+
*
1276+
* @param string|false $relative The relative path of the script or script module source.
1277+
* False if it could not be determined.
1278+
* @param string $src The full source URL of the script or script module.
12511279
*/
1252-
$relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
1280+
$relative = apply_filters( $filter_name, $relative, $src );
12531281

12541282
// If the source is not from WP.
12551283
if ( false === $relative ) {
@@ -1288,142 +1316,25 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
12881316
*
12891317
* @since 7.0.0
12901318
*
1291-
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1292-
*
12931319
* @param string $id The script module identifier.
12941320
* @param string $domain Optional. Text domain. Default 'default'.
12951321
* @param string $path Optional. The full file path to the directory containing translation files.
12961322
* @return string|false The JSON-encoded translated strings for the given script module and text domain.
12971323
* False if there are none.
12981324
*/
12991325
function load_script_module_textdomain( string $id, string $domain = 'default', string $path = '' ) {
1300-
global $wp_textdomain_registry;
1301-
13021326
$src = wp_script_modules()->get_registered_src( $id );
13031327

13041328
if ( null === $src ) {
13051329
return false;
13061330
}
13071331

1308-
$locale = determine_locale();
1309-
1310-
if ( ! $path ) {
1311-
$path = $wp_textdomain_registry->get( $domain, $locale );
1312-
}
1313-
1314-
$path = untrailingslashit( $path );
1315-
1316-
// If a path was given and the handle file exists simply return it.
1317-
$file_base = 'default' === $domain ? $locale : $domain . '-' . $locale;
1318-
$handle_filename = $file_base . '-' . $id . '.json';
1319-
1320-
if ( $path ) {
1321-
$translations = load_script_translations( $path . '/' . $handle_filename, $id, $domain );
1322-
1323-
if ( $translations ) {
1324-
return $translations;
1325-
}
1326-
}
1327-
13281332
// Ensure src is an absolute URL for path resolution.
13291333
if ( ! preg_match( '|^(https?:)?//|', $src ) ) {
13301334
$src = site_url( $src );
13311335
}
13321336

1333-
$relative = false;
1334-
$languages_path = WP_LANG_DIR;
1335-
1336-
$src_url = wp_parse_url( $src );
1337-
$content_url = wp_parse_url( content_url() );
1338-
$plugins_url = wp_parse_url( plugins_url() );
1339-
$site_url = wp_parse_url( site_url() );
1340-
$theme_root = get_theme_root();
1341-
1342-
// If the host is the same or it's a relative URL.
1343-
if (
1344-
( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
1345-
( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
1346-
) {
1347-
// Make the src relative the specific plugin or theme.
1348-
if ( isset( $content_url['path'] ) ) {
1349-
$relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
1350-
} else {
1351-
$relative = $src_url['path'];
1352-
}
1353-
$relative = trim( $relative, '/' );
1354-
$relative = explode( '/', $relative );
1355-
1356-
$theme_dir = array_slice( explode( '/', $theme_root ), -1 );
1357-
$dirname = $theme_dir[0] === $relative[0] ? 'themes' : 'plugins';
1358-
1359-
$languages_path = WP_LANG_DIR . '/' . $dirname;
1360-
1361-
$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
1362-
$relative = implode( '/', $relative );
1363-
} elseif (
1364-
( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
1365-
( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
1366-
) {
1367-
// Make the src relative the specific plugin.
1368-
if ( isset( $plugins_url['path'] ) ) {
1369-
$relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
1370-
} else {
1371-
$relative = $src_url['path'];
1372-
}
1373-
$relative = trim( $relative, '/' );
1374-
$relative = explode( '/', $relative );
1375-
1376-
$languages_path = WP_LANG_DIR . '/plugins';
1377-
1378-
$relative = array_slice( $relative, 1 ); // Remove <plugin name>.
1379-
$relative = implode( '/', $relative );
1380-
} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
1381-
if ( ! isset( $site_url['path'] ) ) {
1382-
$relative = trim( $src_url['path'], '/' );
1383-
} elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
1384-
// Make the src relative to the WP root.
1385-
$relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
1386-
$relative = trim( $relative, '/' );
1387-
}
1388-
}
1389-
1390-
/**
1391-
* Filters the relative path of script module source used for finding translation files.
1392-
*
1393-
* @since 7.0.0
1394-
*
1395-
* @param string|false $relative The relative path of the script module source. False if it could not be determined.
1396-
* @param string $src The full source URL of the script module.
1397-
*/
1398-
$relative = apply_filters( 'load_script_module_textdomain_relative_path', $relative, $src );
1399-
1400-
// If the source is not from WP.
1401-
if ( false === $relative ) {
1402-
return load_script_translations( false, $id, $domain );
1403-
}
1404-
1405-
// Translations are always based on the unminified filename.
1406-
if ( str_ends_with( $relative, '.min.js' ) ) {
1407-
$relative = substr( $relative, 0, -7 ) . '.js';
1408-
}
1409-
1410-
$md5_filename = $file_base . '-' . md5( $relative ) . '.json';
1411-
1412-
if ( $path ) {
1413-
$translations = load_script_translations( $path . '/' . $md5_filename, $id, $domain );
1414-
1415-
if ( $translations ) {
1416-
return $translations;
1417-
}
1418-
}
1419-
1420-
$translations = load_script_translations( $languages_path . '/' . $md5_filename, $id, $domain );
1421-
1422-
if ( $translations ) {
1423-
return $translations;
1424-
}
1425-
1426-
return load_script_translations( false, $id, $domain );
1337+
return _load_script_textdomain_from_src( $id, $src, $domain, $path, 'load_script_module_textdomain_relative_path' );
14271338
}
14281339

14291340
/**

0 commit comments

Comments
 (0)