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/breadly/block-library/review.php
<?php

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

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

/**
 * Renders the `gtm/review` 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 review.
 */
function gtm_render_block_review( $attr, $content, $block ) {
	$wrapper_attributes = get_block_wrapper_attributes();
	$inner_html         = '';

	$template_slug = 'review'; // 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
	if ( get_option( 'gtm_seo_enable_post_rich_snippets', true ) ) {
		add_action( 'wp_footer', function () use ( $attr ) {
			gtm_render_block_review_schema( [
				'title'   => $attr['title'],
				'tagline' => $attr['tagline'],
				'rating'  => $attr['totalScore'],
			] );
		} );
	}

	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;
	}
}

function gtm_render_block_review_schema( $attr = [] ) {
	$attr = wp_parse_args( $attr, [
		'title'   => '',
		'tagline' => '',
		'body'    => '',
		'rating'  => 0,
	] );

	if ( empty( $attr['body'] ) && is_single() ) {
		// Link to the main content on the single post page
		$attr['body'] = [
			"@id" => "#wp--skip-link--target",
		];
	}

	$schema = [
		"@context"    => "https://schema.org/",
		"@type"       => "Product",
		"name"        => $attr['title'],
		"description" => $attr['tagline'],
		'review'      => [
			"@type"        => "Review",
			'reviewRating' => [
				"@type"       => "Rating",
				"ratingValue" => $attr['rating'] . "%",
			],
			"reviewBody"   => $attr['body'],
			"author"       => [
				"@id" => "#Author",
			],
		],
	];

	if ( apply_filters( 'gtm_has_review_note_type_pros', false ) ) {
		$schema['review']['positiveNotes'] = [
			"@id" => "#gtm-review-note-pros",
		];
	}

	if ( apply_filters( 'gtm_has_review_note_type_cons', false ) ) {
		$schema['review']['negativeNotes'] = [
			"@id" => "#gtm-review-note-cons",
		];
	}

	// Render
	gtm_the_json_ld( $schema );
}