wpseek.com
A WordPress-centric search engine for devs and theme authors



_load_script_textdomain_from_src › WordPress Function

Since7.0.0
Deprecatedn/a
_load_script_textdomain_from_src ( $handle, $src, $domain, $path, $is_module )
Access:
  • private
Parameters: (5)
  • (string) $handle Name of the script or script module identifier to register a translation domain to.
    Required: Yes
  • (string) $src Absolute source URL of the script or script module.
    Required: Yes
  • (string) $domain Text domain.
    Required: Yes
  • (string) $path The full file path to the directory containing translation files, or an empty string to use the default path from the text domain registry.
    Required: Yes
  • (bool) $is_module Whether the source belongs to a script module (true) or a classic script (false).
    Required: Yes
Returns:
  • (string|false) The JSON-encoded translated strings on success, false otherwise.
Defined at:
Codex:

Resolves and loads the translation JSON file for a given script or script module source URL.

This is a shared implementation used by load_script_textdomain and load_script_module_textdomain to avoid duplicating the path resolution and file lookup logic.


Source

function _load_script_textdomain_from_src( string $handle, string $src, string $domain, string $path, bool $is_module ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $wp_textdomain_registry;

	$locale = determine_locale();

	if ( ! $path ) {
		$path = $wp_textdomain_registry->get( $domain, $locale );
	}

	if ( $path ) {
		$path = untrailingslashit( $path );
	}

	// If a path was given and the handle file exists simply return it.
	$file_base       = 'default' === $domain ? $locale : $domain . '-' . $locale;
	$handle_filename = $file_base . '-' . $handle . '.json';

	if ( $path ) {
		$translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain );

		if ( $translations ) {
			return $translations;
		}
	}

	$relative       = false;
	$languages_path = WP_LANG_DIR;

	$src_url = wp_parse_url( $src );
	if ( ! $src_url ) {
		return load_script_translations( false, $handle, $domain );
	}
	$src_url['path'] ??= '';

	$content_url = wp_parse_url( content_url() );
	if ( ! $content_url ) {
		return load_script_translations( false, $handle, $domain );
	}

	$plugins_url = wp_parse_url( plugins_url() );
	$site_url    = wp_parse_url( site_url() );
	$theme_root  = get_theme_root();

	// If the host is the same or it's a relative URL.
	if (
		( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
		( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
	) {
		// Make the src relative the specific plugin or theme.
		if ( isset( $content_url['path'] ) ) {
			$relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
		} else {
			$relative = $src_url['path'];
		}
		$relative = trim( $relative, '/' );
		$relative = explode( '/', $relative );

		/*
		 * Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration,
		 * a custom theme root, and/or using Multisite with subdirectories.
		 * See https://core.trac.wordpress.org/ticket/60891 and https://core.trac.wordpress.org/ticket/62016.
		 */

		$theme_dir = array_slice( explode( '/', $theme_root ), -1 );
		$dirname   = $theme_dir[0] === $relative[0] ? 'themes' : 'plugins';

		$languages_path = WP_LANG_DIR . '/' . $dirname;

		$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
		$relative = implode( '/', $relative );
	} elseif (
		( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
		( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
	) {
		// Make the src relative the specific plugin.
		if ( isset( $plugins_url['path'] ) ) {
			$relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
		} else {
			$relative = $src_url['path'];
		}
		$relative = trim( $relative, '/' );
		$relative = explode( '/', $relative );

		$languages_path = WP_LANG_DIR . '/plugins';

		$relative = array_slice( $relative, 1 ); // Remove <plugin name>.
		$relative = implode( '/', $relative );
	} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
		if ( ! isset( $site_url['path'] ) ) {
			$relative = trim( $src_url['path'], '/' );
		} elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
			// Make the src relative to the WP root.
			$relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
			$relative = trim( $relative, '/' );
		}
	}

	/**
	 * Filters the relative path of scripts used for finding translation files.
	 *
	 * @since 5.0.2
	 * @since 7.0.0 The `$is_module` parameter was added.
	 *
	 * @param string|false $relative  The relative path of the script. False if it could not be determined.
	 * @param string       $src       The full source URL of the script.
	 * @param bool         $is_module Whether the source belongs to a script module (true) or a classic script (false).
	 */
	$relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src, $is_module );

	// If the source is not from WP.
	if ( ! is_string( $relative ) ) {
		return load_script_translations( false, $handle, $domain );
	}

	// Translations are always based on the unminified filename.
	if ( str_ends_with( $relative, '.min.js' ) ) {
		$relative = substr( $relative, 0, -7 ) . '.js';
	}

	$md5_filename = $file_base . '-' . md5( $relative ) . '.json';

	if ( $path ) {
		$translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );

		if ( $translations ) {
			return $translations;
		}
	}

	$translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );

	if ( $translations ) {
		return $translations;
	}

	return load_script_translations( false, $handle, $domain );
}