Automattic\WooCommerce\Blocks\BlockTypes

AddToWishlistButton::renderprotectedWC 1.0

Render the block.

Method of the class: AddToWishlistButton{}

No Hooks.

Returns

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

AddToWishlistButton::render() code WC 10.9.1

<?php
protected function render( $attributes, $content, $block ) {
	// Guests can't have a wishlist — bail before enqueuing assets or
	// seeding state.
	if ( ! is_user_logged_in() ) {
		return '';
	}

	$post_id = isset( $block->context['postId'] ) ? absint( $block->context['postId'] ) : 0;
	if ( ! $post_id ) {
		return '';
	}

	$product = wc_get_product( $post_id );
	if ( ! $product instanceof \WC_Product ) {
		return '';
	}

	wp_enqueue_script_module( $this->get_full_block_name() );

	$consent = 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WooCommerce';
	BlocksSharedState::load_store_config( $consent );

	$items = $this->prefetch_items();

	// Seed the shared shopper-lists store the same way the Wishlist
	// block does — restUrl + starter nonce + prefetched items. The
	// two blocks may both render on the same page (e.g. the merchant
	// drops the Wishlist block into a sidebar of single-product); iAPI's
	// deep-merge keeps the first writer's payload, so seeding identical
	// values here is a no-op when Wishlist already ran.
	wp_interactivity_state(
		'woocommerce/shopper-lists',
		array(
			'restUrl' => get_rest_url(),
			'nonce'   => wp_create_nonce( 'wc_store_api' ),
			'lists'   => array(
				self::LIST_SLUG => array(
					'items'     => $items,
					'isLoading' => false,
				),
			),
		)
	);

	// Visible labels flow through `wp_interactivity_config` so the
	// JS-side getter can pick the right one based on
	// `state.isInWishlist`. PHP renders the matching one as the
	// initial server-side label.
	wp_interactivity_config(
		'woocommerce/add-to-wishlist-button',
		array(
			'addLabel'           => $this->get_add_label(),
			'savedLabel'         => $this->get_saved_label(),
			'selectOptionsLabel' => $this->get_select_options_label(),
		)
	);

	$is_variable            = $product->is_type( 'variable' );
	$initial_is_in_wishlist = $this->is_initial_in_wishlist( $items, $product );
	$initial_disabled       = $is_variable;
	$initial_label          = $is_variable
		? $this->get_select_options_label()
		: ( $initial_is_in_wishlist ? $this->get_saved_label() : $this->get_add_label() );

	$wrapper_attributes = array(
		'class'               => 'wc-block-add-to-wishlist-button',
		'data-wp-interactive' => 'woocommerce/add-to-wishlist-button',
		'data-wp-context'     => (string) wp_json_encode(
			array(
				'productId'      => $product->get_id(),
				'isVariableType' => $is_variable,
				'isPending'      => false,
			)
		),
	);

	ob_start();
	?>
	<div <?php echo get_block_wrapper_attributes( $wrapper_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes returns escaped attribute markup. ?>>
		<button
			type="button"
			class="wc-block-add-to-wishlist-button__toggle"
			data-wp-on--click="actions.onClickToggle"
			data-wp-bind--aria-pressed="state.isInWishlist"
			data-wp-bind--disabled="state.isDisabled"
			<?php echo $initial_is_in_wishlist ? 'aria-pressed="true"' : 'aria-pressed="false"'; ?>
			<?php
			if ( $initial_disabled ) {
				echo 'disabled';
			}
			?>
		>
			<span class="wc-block-add-to-wishlist-button__icon wc-block-add-to-wishlist-button__icon--empty" data-wp-bind--hidden="state.isInWishlist"
			<?php
			if ( $initial_is_in_wishlist ) {
				echo ' hidden';
			}
			?>
			>
				<?php echo ShopperListRenderer::get_star_empty_svg(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static SVG markup. ?>
			</span>
			<span class="wc-block-add-to-wishlist-button__icon wc-block-add-to-wishlist-button__icon--filled" data-wp-bind--hidden="!state.isInWishlist"
			<?php
			if ( ! $initial_is_in_wishlist ) {
				echo ' hidden';
			}
			?>
			>
				<?php echo ShopperListRenderer::get_star_filled_svg(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static SVG markup. ?>
			</span>
			<span class="wc-block-add-to-wishlist-button__label" data-wp-text="state.currentLabel"><?php echo esc_html( $initial_label ); ?></span>
		</button>
	</div>
	<?php
	return (string) ob_get_clean();
}