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

Table::extract_table_and_caption_from_figureprivateWC 1.0

Extract table content and caption from figure wrapper if present.

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

Хуков нет.

Возвращает

Массив. Array with 'table' and 'caption' keys.

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

// private - только в коде основоного (родительского) класса
$result = $this->extract_table_and_caption_from_figure( $block_content ): array;
$block_content(строка) (обязательный)
Block content.

Код Table::extract_table_and_caption_from_figure() WC 10.4.3

private function extract_table_and_caption_from_figure( string $block_content ): array {
	$dom_helper = new Dom_Document_Helper( $block_content );

	// Look for figure element with wp-block-table class.
	$figure_tag = $dom_helper->find_element( 'figure' );
	if ( ! $figure_tag ) {
		// If no figure wrapper found, return original content as table.
		return array(
			'table'   => $block_content,
			'caption' => '',
		);
	}

	$figure_class_attr = $dom_helper->get_attribute_value( $figure_tag, 'class' );
	$figure_class      = (string) ( $figure_class_attr ? $figure_class_attr : '' );
	if ( false === strpos( $figure_class, 'wp-block-table' ) ) {
		// If figure doesn't have wp-block-table class, return original content as table.
		return array(
			'table'   => $block_content,
			'caption' => '',
		);
	}

	// Extract table element from within the matched figure only.
	$figure_html = $dom_helper->get_outer_html( $figure_tag );

	// Use regex to extract table from within the figure to avoid document conflicts.
	if ( ! preg_match( '/<table[^>]*>.*?<\/table>/is', $figure_html, $table_matches ) ) {
		return array(
			'table'   => $block_content,
			'caption' => '',
		);
	}
	$table_html = $table_matches[0];

	// Extract figcaption if present (scoped to the figure).
	$caption = '';
	if ( preg_match( '/<figcaption[^>]*>(.*?)<\/figcaption>/is', $figure_html, $figcaption_matches ) ) {
		$caption = $figcaption_matches[1];
	}

	return array(
		'table'   => $table_html,
		'caption' => $caption,
	);
}