Automattic\WooCommerce\Internal\Admin

WCAdminAssets::register_scripts()publicWC 1.0

Registers all the necessary scripts and styles to show the admin experience.

Метод класса: WCAdminAssets{}

Хуков нет.

Возвращает

null. Ничего (null).

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

$WCAdminAssets = new WCAdminAssets();
$WCAdminAssets->register_scripts();

Код WCAdminAssets::register_scripts() WC 8.7.0

public function register_scripts() {
	if ( ! function_exists( 'wp_set_script_translations' ) ) {
		return;
	}

	$js_file_version  = self::get_file_version( 'js' );
	$css_file_version = self::get_file_version( 'css' );

	$scripts = array(
		'wc-admin-layout',
		'wc-explat',
		'wc-experimental',
		'wc-customer-effort-score',
		// NOTE: This should be removed when Gutenberg is updated and the notices package is removed from WooCommerce Admin.
		'wc-notices',
		'wc-number',
		'wc-tracks',
		'wc-date',
		'wc-components',
		WC_ADMIN_APP,
		'wc-csv',
		'wc-store-data',
		'wc-currency',
		'wc-navigation',
		'wc-block-templates',
		'wc-product-editor',
	);

	$scripts_map = array(
		WC_ADMIN_APP    => 'app',
		'wc-csv'        => 'csv-export',
		'wc-store-data' => 'data',
	);

	$translated_scripts = array(
		'wc-currency',
		'wc-date',
		'wc-components',
		'wc-customer-effort-score',
		'wc-experimental',
		'wc-navigation',
		'wc-product-editor',
		WC_ADMIN_APP,
	);

	foreach ( $scripts as $script ) {
		$script_path_name = isset( $scripts_map[ $script ] ) ? $scripts_map[ $script ] : str_replace( 'wc-', '', $script );

		try {
			$script_assets_filename = self::get_script_asset_filename( $script_path_name, 'index' );
			$script_assets          = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . $script_path_name . '/' . $script_assets_filename;

			global $wp_version;
			if ( 'app' === $script_path_name && version_compare( $wp_version, '6.3', '<' ) ) {
				// Remove wp-router dependency for WordPress versions < 6.3 because wp-router is not included in those versions. We only use wp-router in customize store pages and the feature is only available in WordPress 6.3+.
				// We can remove this once our minimum support is WP 6.3.
				$script_assets['dependencies'] = array_diff( $script_assets['dependencies'], array( 'wp-router' ) );
			}

			// Remove wp-editor dependency if we're not on a customize store page since we don't use wp-editor in other pages.
			$is_customize_store_page = (
				PageController::is_admin_page() &&
				isset( $_GET['path'] ) &&
				str_starts_with( wc_clean( wp_unslash( $_GET['path'] ) ), '/customize-store' )
			);
			if ( ! $is_customize_store_page && WC_ADMIN_APP === $script ) {
				$script_assets['dependencies'] = array_diff( $script_assets['dependencies'], array( 'wp-editor' ) );
			}

			wp_register_script(
				$script,
				self::get_url( $script_path_name . '/index', 'js' ),
				$script_assets ['dependencies'],
				$js_file_version,
				true
			);

			if ( in_array( $script, $translated_scripts, true ) ) {
				wp_set_script_translations( $script, 'woocommerce' );
			}
		} catch ( \Exception $e ) {
			// Avoid crashing WordPress if an asset file could not be loaded.
			wc_caught_exception( $e, __CLASS__ . '::' . __FUNCTION__, $script_path_name );
		}
	}

	wp_register_style(
		'wc-admin-layout',
		self::get_url( 'admin-layout/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-admin-layout', 'rtl', 'replace' );

	wp_register_style(
		'wc-components',
		self::get_url( 'components/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-components', 'rtl', 'replace' );

	wp_register_style(
		'wc-block-templates',
		self::get_url( 'block-templates/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-block-templates', 'rtl', 'replace' );

	wp_register_style(
		'wc-product-editor',
		self::get_url( 'product-editor/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-product-editor', 'rtl', 'replace' );

	wp_register_style(
		'wc-customer-effort-score',
		self::get_url( 'customer-effort-score/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-customer-effort-score', 'rtl', 'replace' );

	wp_register_style(
		'wc-experimental',
		self::get_url( 'experimental/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-experimental', 'rtl', 'replace' );

	wp_localize_script(
		WC_ADMIN_APP,
		'wcAdminAssets',
		array(
			'path'    => plugins_url( self::get_path( 'js' ), WC_ADMIN_PLUGIN_FILE ),
			'version' => $js_file_version,
		)
	);

	wp_register_style(
		WC_ADMIN_APP,
		self::get_url( 'app/style', 'css' ),
		array( 'wc-components', 'wc-admin-layout', 'wc-customer-effort-score', 'wc-product-editor', 'wp-components', 'wc-experimental' ),
		$css_file_version
	);
	wp_style_add_data( WC_ADMIN_APP, 'rtl', 'replace' );

	wp_register_style(
		'wc-onboarding',
		self::get_url( 'onboarding/style', 'css' ),
		array(),
		$css_file_version
	);
	wp_style_add_data( 'wc-onboarding', 'rtl', 'replace' );
}