File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/block-library/nutrition.php
<?php
/**
* Registers the `gtm/nutrition` block on the server.
*/
function gtm_register_block_nutrition() {
gtm_register_block_type( 'nutrition', [
'render_callback' => 'gtm_render_block_nutrition',
] );
}
add_action( 'init', 'gtm_register_block_nutrition', 20 );
/**
* Renders the `gtm/nutrition` 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 nutrition.
*/
function gtm_render_block_nutrition( $attr, $content, $block ) {
$wrapper_attributes = get_block_wrapper_attributes();
$inner_html = '';
$template_slug = 'nutrition'; // Default template slug
$template_name = null;
if ( ! empty( $attr['templateId'] ) ) {
list( $template_name, $template_slug ) = explode( '//', $attr['templateId'] );
}
GTM_Block_Context::begin( $block );
$inner_html = gtm_do_template_part( $template_slug, $template_name );
GTM_Block_Context::end();
// Render only when enable schema
$nutrition = $attr['nutrition'] ?? [];
if ( get_option( 'gtm_seo_enable_post_rich_snippets', true ) ) {
add_filter( 'gtm_nutrition_block_schema', function ( $schema = [] ) use ( $nutrition ) {
$schema = [
"@type" => "NutritionInformation",
];
// Mapping for attributes and schema props
$fields = [
'energy' => 'calories',
'carbs' => 'carbohydrateContent',
'cholesterol' => 'cholesterolContent',
'fat' => 'fatContent',
'fiber' => 'fiberContent',
'protein' => 'proteinContent',
'sodium' => 'sodiumContent',
'sugars' => 'sugarContent',
];
foreach ( $fields as $field => $prop ) {
if ( ! empty( $nutrition[$field] ) ) {
$schema[$prop] = $nutrition[$field];
}
}
return $schema;
} );
}
if ( empty( $inner_html ) ) {
return gtm_get_admin_warning( sprintf( esc_html__( 'No template available. Please create a template part named "%s" in the full site editor.', 'gutenmate' ), esc_html( $template_slug ) ) ) . $content;
} else {
return $inner_html;
}
}