HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/nutrients.php
<?php

/**
 * Renders the `gtm/nutrients` block on the server.
 *
 * @param array    $attributes Block attributes.
 * @param string   $content    Block default content.
 * @param WP_Block $block      Block instance.
 * @return string Returns the nutrients.
 */
function gtm_render_block_nutrients( $attr, $content, $block ) {

	if ( ! isset( $block->context['gtmNutrition'] ) ) {
		return '';
	}

	$nutrition = $block->context['gtmNutrition'];

	if ( ! is_array( $nutrition ) || empty( $nutrition ) ) {
		return;
	}

	$inner_html = '';

	$nutritionLabels = [
		'energy'      => esc_html__( 'Calories', 'gutenmate' ),
		'carbs'       => esc_html__( 'Carbs', 'gutenmate' ),
		'cholesterol' => esc_html__( 'Chol', 'gutenmate' ),
		'fat'         => esc_html__( 'Fat', 'gutenmate' ),
		'fiber'       => esc_html__( 'Fiber', 'gutenmate' ),
		'protein'     => esc_html__( 'Protein', 'gutenmate' ),
		'sodium'      => esc_html__( 'Sodium', 'gutenmate' ),
		'sugars'      => esc_html__( 'Sugars', 'gutenmate' ),
	];

	foreach ( $nutrition as $slug => $value ) {
		$inner_html .= '<div class="wp-block-gtm-nutrients__item">';

		$inner_html .= '<span class="wp-block-gtm-nutrients__item-label">';
		$inner_html .= $nutritionLabels[$slug];
		$inner_html .= '</span>';

		$inner_html .= '<span class="wp-block-gtm-nutrients__item-value">';
		$inner_html .= $value;
		$inner_html .= '</span>';

		$inner_html .= '</div>';
	}

	$wrapper_attributes = get_block_wrapper_attributes( ['class' => ''] );

	return sprintf(
		'<div %1$s>%2$s</div>',
		$wrapper_attributes,
		$inner_html
	);
}

/**
 * Registers the `gtm/nutrients` block on the server.
 */
function gtm_register_block_nutrients() {
	gtm_register_block_type( 'nutrients', [
		'render_callback' => 'gtm_render_block_nutrients',
	] );
}

add_action( 'init', 'gtm_register_block_nutrients', 20 );

add_filter( 'gtm.BlockStyleCompiler.gtm/nutrients', 'gtm_compile_block_css_nutrients', 10, 3 );
function gtm_compile_block_css_nutrients( $output, $attributes, $blockName ) {
	$block_style = ['classes' => [], 'style' => []];

	$block_style['classes'][] = 'gtm-block';

	// Layout
	gtm_css_compileCssVar( $block_style, "nutrients-item-columns", $attributes['columns'] );
	gtm_css_compileCssVar( $block_style, "nutrients-item-gap", gtm_css_parseSpacingValue( $attributes['gap'] ) );

	// Label
	gtm_css_compileCssVar( $block_style, "nutrients-label-typography", gtm_css_parseTypographyProps( $attributes['labelTypography'] ) );
	gtm_css_compileCssVar( $block_style, "nutrients-label-spacing", gtm_css_parseSpacingValue( $attributes['labelSpacing'] ) );
	gtm_css_compileCssVar( $block_style, "nutrients-label-color", gtm_css_parseColorValue( $attributes['labelColor'] ) );

	// Value
	gtm_css_compileCssVar( $block_style, "nutrients-value-typography", gtm_css_parseTypographyProps( $attributes['valueTypography'] ) );
	gtm_css_compileCssVar( $block_style, "nutrients-value-color", gtm_css_parseColorValue( $attributes['valueColor'] ) );

	// Apply to output
	$block_style['style'] = ['{{BLOCK}}' => $block_style['style']];

	return [
		'classes' => array_merge( $output['classes'], $block_style['classes'] ),
		'style'   => array_replace_recursive( $output['style'], $block_style['style'] ),
	];
}