Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks

Text::render_contentprotectedWC 1.0

Renders the block content.

Method of the class: Text{}

No Hooks.

Returns

String.

Usage

// protected - for code of main (parent) or child class
$result = $this->render_content( $block_content, $parsed_block, $rendering_context ): string;
$block_content(string) (required)
Block content.
$parsed_block(array) (required)
Parsed block.
$rendering_context(Rendering_Context) (required)
Rendering context.

Text::render_content() code WC 10.5.0

protected function render_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
	// Do not render empty blocks.
	if ( empty( trim( wp_strip_all_tags( $block_content ) ) ) ) {
		return '';
	}

	$block_content        = $this->adjustStyleAttribute( $block_content );
	$block_attributes     = wp_parse_args(
		$parsed_block['attrs'] ?? array(),
		array(
			'textAlign' => 'left',
			'style'     => array(),
		)
	);
	$html                 = new \WP_HTML_Tag_Processor( $block_content );
	$classes              = 'email-text-block';
	$alignment_from_class = null;
	if ( $html->next_tag() ) {
		/** @var string $block_classes */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
		$block_classes = $html->get_attribute( 'class' ) ?? '';
		$classes      .= ' ' . $block_classes;

		// Extract text alignment from has-text-align-* classes before they're potentially modified.
		$class_attr = (string) $block_classes;
		if ( false !== strpos( $class_attr, 'has-text-align-center' ) ) {
			$alignment_from_class = 'center';
		} elseif ( false !== strpos( $class_attr, 'has-text-align-right' ) ) {
			$alignment_from_class = 'right';
		} elseif ( false !== strpos( $class_attr, 'has-text-align-left' ) ) {
			$alignment_from_class = 'left';
		}

		// remove has-background to prevent double padding applied for wrapper and inner element.
		$block_classes = str_replace( 'has-background', '', $block_classes );
		// remove border related classes because we handle border on wrapping table cell.
		$block_classes = preg_replace( '/[a-z-]+-border-[a-z-]+/', '', $block_classes );
		/** @var string $block_classes */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
		$html->set_attribute( 'class', trim( $block_classes ) );
		$block_content = $html->get_updated_html();
	}

	$block_styles      = Styles_Helper::get_block_styles( $block_attributes, $rendering_context, array( 'spacing', 'border', 'background-color', 'color', 'typography' ) );
	$additional_styles = array(
		'min-width' => '100%', // prevent Gmail App from shrinking the table on mobile devices.
	);

	// Add fallback text color when no custom text color or preset text color is set.
	if ( empty( $block_styles['declarations']['color'] ) ) {
		$email_styles               = $rendering_context->get_theme_styles();
		$additional_styles['color'] = $parsed_block['email_attrs']['color'] ?? $email_styles['color']['text'] ?? '#000000'; // Fallback for the text color.
	}

	$additional_styles['text-align'] = 'left';
	if ( ! empty( $parsed_block['attrs']['textAlign'] ) ) { // in this case, textAlign needs to be one of 'left', 'center', 'right'.
		$additional_styles['text-align'] = $parsed_block['attrs']['textAlign'];
	} elseif ( in_array( $parsed_block['attrs']['align'] ?? null, array( 'left', 'center', 'right' ), true ) ) {
		$additional_styles['text-align'] = $parsed_block['attrs']['align'];
	} elseif ( null !== $alignment_from_class ) {
		$additional_styles['text-align'] = $alignment_from_class;
	}

	$block_styles = Styles_Helper::extend_block_styles( $block_styles, $additional_styles );

	$table_attrs = array(
		'style' => 'border-collapse: separate;', // Needed because of border radius.
		'width' => '100%',
	);

	$cell_attrs = array(
		'class' => $classes,
		'style' => $block_styles['css'],
		'align' => $additional_styles['text-align'],
	);

	return Table_Wrapper_Helper::render_table_wrapper( $block_content, $table_attrs, $cell_attrs );
}