Automattic\WooCommerce\Blocks\Assets

Api::register_script()publicWC 2.5.0

Registers a script according to wp_register_script, adding the correct prefix, and additionally loading translations.

When creating script assets, the following rules should be followed:

  1. All asset handles should have a wc- prefix.
  2. If the asset handle is for a Block (in editor context) use the -block suffix.
  3. If the asset handle is for a Block (in frontend context) use the -block-frontend suffix.
  4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the wc-blocks- prefix.

{} Это метод класса: Api{}

Возвращает

null. Ничего.

Использование

$Api = new Api();
$Api->register_script( $handle, $relative_src, $dependencies, $has_i18n );
$handle(строка) (обязательный)
Unique name of the script.
$relative_src(строка) (обязательный)
Relative url for the script to the path from plugin root.
$dependencies(массив)
An array of registered script handles this script depends on.
По умолчанию: empty array
$has_i18n(true|false)
Whether to add a script translation call to this file.
По умолчанию: true

Список изменений

С версии 2.5.0 Введена.

Код Api::register_script() WC 6.4.1

public function register_script( $handle, $relative_src, $dependencies = [], $has_i18n = true ) {
	$script_data = $this->get_script_data( $relative_src, $dependencies );

	if ( in_array( $handle, $script_data['dependencies'], true ) ) {
		if ( $this->package->feature()->is_development_environment() ) {
			$dependencies = array_diff( $script_data['dependencies'], [ $handle ] );
				add_action(
					'admin_notices',
					function() use ( $handle ) {
							echo '<div class="error"><p>';
							/* translators: %s file handle name. */
							printf( esc_html__( 'Script with handle %s had a dependency on itself which has been removed. This is an indicator that your JS code has a circular dependency that can cause bugs.', 'woocommerce' ), esc_html( $handle ) );
							echo '</p></div>';
					}
				);
		} else {
			throw new Exception( sprintf( 'Script with handle %s had a dependency on itself. This is an indicator that your JS code has a circular dependency that can cause bugs.', $handle ) );
		}
	}

	/**
	 * Filters the list of script dependencies.
	 *
	 * @param array $dependencies The list of script dependencies.
	 * @param string $handle The script's handle.
	 * @return array
	 */
	$script_dependencies = apply_filters( 'woocommerce_blocks_register_script_dependencies', $script_data['dependencies'], $handle );

	wp_register_script( $handle, $script_data['src'], $script_dependencies, $script_data['version'], true );

	if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) {
		wp_set_script_translations( $handle, 'woocommerce', $this->package->get_path( 'languages' ) );
	}
}