Automattic\WooCommerce\Blocks\BlockTypes

AbstractBlock::enqueue_dataprotectedWC 1.0

Data passed through from server to client for block.

Method of the class: AbstractBlock{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->enqueue_data( $attributes );
$attributes(array)
Any attributes that currently are available from the block. Note, this will be empty in the editor context when the block is not in the post content on editor load.
Default: []

AbstractBlock::enqueue_data() code WC 9.8.5

protected function enqueue_data( array $attributes = [] ) {
	$registered_script_data = $this->integration_registry->get_all_registered_script_data();

	foreach ( $registered_script_data as $asset_data_key => $asset_data_value ) {
		if ( ! $this->asset_data_registry->exists( $asset_data_key ) ) {
			$this->asset_data_registry->add( $asset_data_key, $asset_data_value );
		}
	}

	if ( ! $this->asset_data_registry->exists( 'wcBlocksConfig' ) ) {
		$wc_blocks_config = [
			'pluginUrl'     => plugins_url( '/', dirname( __DIR__, 2 ) ),
			'restApiRoutes' => [
				'/wc/store/v1' => array_keys( $this->get_routes_from_namespace( 'wc/store/v1' ) ),
			],
			'defaultAvatar' => get_avatar_url( 0, [ 'force_default' => true ] ),

			/*
			 * translators: If your word count is based on single characters (e.g. East Asian characters),
			 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
			 * Do not translate into your own language.
			 */
			'wordCountType' => _x( 'words', 'Word count type. Do not translate!', 'woocommerce' ),
		];
		if ( is_admin() && ! WC()->is_rest_api_request() ) {
			$product_counts     = wp_count_posts( 'product' );
			$published_products = isset( $product_counts->publish ) ? $product_counts->publish : 0;
			$wc_blocks_config   = array_merge(
				$wc_blocks_config,
				[
					// Note that while we don't have a consolidated way of doing feature-flagging
					// we are borrowing from the WC Admin Features implementation. Also note we cannot
					// use the wcAdminFeatures global because it's not always enqueued in the context of blocks.
					'experimentalBlocksEnabled' => Features::is_enabled( 'experimental-blocks' ),
					'productCount'              => $published_products,
				]
			);
		}
		$this->asset_data_registry->add(
			'wcBlocksConfig',
			$wc_blocks_config
		);
	}
}