Automattic\WooCommerce\EmailEditor\Engine

Assets_Manager::load_editor_assetspublicWC 1.0

Load editor assets.

Method of the class: Assets_Manager{}

Returns

null. Nothing (null).

Usage

$Assets_Manager = new Assets_Manager();
$Assets_Manager->load_editor_assets( $edited_item, $script_name ): void;
$edited_item(WP_Post|\WP_Block_Template) (required)
The edited post or template.
$script_name(string) (required)
The name of the registered script.

Assets_Manager::load_editor_assets() code WC 10.5.0

public function load_editor_assets( $edited_item, string $script_name ): void {
	$post_type = $edited_item instanceof \WP_Post ? $edited_item->post_type : 'wp_template';
	$post_id   = $edited_item instanceof \WP_Post ? $edited_item->ID : $edited_item->id;

	$email_editor_assets_path = rtrim( $this->assets_path, '/' ) . '/';
	$email_editor_assets_url  = rtrim( $this->assets_url, '/' ) . '/';

	// Email editor rich text JS - Because the Personalization Tags depend on Gutenberg 19.8.0 and higher
	// the following code replaces used Rich Text for the version containing the necessary changes.
	$rich_text_assets_file = $email_editor_assets_path . 'assets/rich-text.asset.php';
	if ( ! file_exists( $rich_text_assets_file ) ) {
		$this->logger->error( 'Rich Text assets file does not exist.', array( 'path' => $rich_text_assets_file ) );
	} else {
		$rich_text_assets = require $rich_text_assets_file;
		wp_deregister_script( 'wp-rich-text' );
		wp_enqueue_script(
			'wp-rich-text',
			$email_editor_assets_url . 'assets/rich-text.js',
			$rich_text_assets['dependencies'],
			$rich_text_assets['version'],
			true
		);
	}
	// End of replacing Rich Text package.

	$assets_file = $email_editor_assets_path . 'style.asset.php';
	if ( ! file_exists( $assets_file ) ) {
		$this->logger->error( 'Email editor assets file does not exist.', array( 'path' => $assets_file ) );
	} else {
		$assets_file = require $assets_file;
		wp_enqueue_style(
			'wc-admin-email-editor-integration',
			$email_editor_assets_url . 'style.css',
			array(),
			$assets_file['version']
		);
	}

	// The get_block_categories() function expects a WP_Post or WP_Block_Editor_Context object.
	// Therefore, we need to create an instance of WP_Block_Editor_Context when $edited_item is an instance of WP_Block_Template.
	if ( $edited_item instanceof \WP_Block_Template ) {
		$context = new \WP_Block_Editor_Context(
			array(
				'post' => $edited_item,
			)
		);
	} else {
		$context = $edited_item;
	}
	// The email editor needs to load block categories to avoid warning and missing category names.
	// See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L116-L120.
	wp_add_inline_script(
		'wp-blocks',
		sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $context ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ),
		'after'
	);

	// Preload server-registered block schemas to avoid warning about missing block titles.
	// See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L144C1-L148C3.
	wp_add_inline_script(
		'wp-blocks',
		sprintf( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode( get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) )
	);

	$localization_data = array(
		'current_post_type'     => $post_type,
		'current_post_id'       => $post_id,
		'current_wp_user_email' => wp_get_current_user()->user_email,
		'editor_settings'       => $this->settings_controller->get_settings(),
		'editor_theme'          => $this->theme_controller->get_base_theme()->get_raw_data(),
		'user_theme_post_id'    => $this->user_theme->get_user_theme_post()->ID,
		'urls'                  => array(
			'listings'     => admin_url( 'admin.php?page=wc-settings&tab=email' ),
			'send'         => admin_url( 'admin.php?page=wc-settings&tab=email' ),
			'back'         => admin_url( 'admin.php?page=wc-settings&tab=email' ),
			'createCoupon' => admin_url( 'post-new.php?post_type=shop_coupon' ),
		),
	);

	wp_localize_script(
		$script_name,
		'WooCommerceEmailEditor',
		apply_filters( 'woocommerce_email_editor_script_localization_data', $localization_data )
	);

	$this->preload_rest_api_data( $post_id, $post_type );
}