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/terms.php
<?php

/**
 * Renders the `gtm/terms` 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 terms.
 */
function gtm_render_block_terms( $attr, $content, $block ) {
	if ( ! isset( $attr['term'] ) ) {
		return '';
	}

	// Do not display if the term is not viewable
	if ( ! is_taxonomy_viewable( $attr['term'] ) ) {
		return '';
	}

	// Do not display if set to show sub items only and the current page is not archive
	if ( 'sub' == $attr['itemDisplay'] && ! is_archive() ) {
		return '';
	}

	$image_size = $attr['imageSize'] ?: 'full';

	$wrapper_attributes = get_block_wrapper_attributes();
	$wrapper_markup     = '<div %1$s><ul class="wp-block-gtm-terms__list wp-block-gtm-terms__list-level-0">%2$s</ul></div>';

	$args = [
		'taxonomy'     => $attr['term'],
		'hierarchical' => false,
		'hide_empty'   => true,
	];

	if ( 'parent' == $attr['itemDisplay'] ) {
		$args['parent'] = 0;
	} else if ( 'sub' == $attr['itemDisplay'] && is_archive() ) {
		$args['parent'] = get_queried_object_id();
	} else if ( 'custom' == $attr['itemDisplay'] ) {
		$args['include'] = $attr['customTerms'];
		$args['orderby'] = 'include';
	}

	$terms = get_terms( $args );

	$items_markup = '';
	if ( ! empty( $terms ) ) {
		foreach ( $terms as $term ) {
			$url      = get_term_link( $term );
			$image_id = gtm_get_featured_image_from_term( $term, $attr['useAltImage'] );

			$items_markup .= '<li>';
			if ( $image_id ) {
				$items_markup .= sprintf( '<a class="wp-block-gtm-terms__image" href="%1$s">', esc_url( $url ) );
				$items_markup .= wp_get_attachment_image( $image_id, $image_size );

			} else {
				$items_markup .= sprintf( '<a class="wp-block-gtm-terms__image wp-block-gtm-terms__image--no-image" href="%1$s">', esc_url( $url ) );

			}
			$items_markup .= '</a>';

			$items_markup .= sprintf( '<a class="wp-block-gtm-terms__term" href="%1$s">', esc_url( $url ) );
			$items_markup .= sprintf( '<span class="wp-block-gtm-terms__name">%1$s</span>', esc_html( $term->name ) );

			if ( $attr['enableItemDescription'] && ! empty( $term->description ) ) {
				$items_markup .= sprintf( '<span class="wp-block-gtm-terms__description">%1$s</span>', esc_html( $term->description ) );
			}
			$items_markup .= '</a>';

			$items_markup .= '</li>';
		}

		return sprintf(
			$wrapper_markup,
			$wrapper_attributes,
			$items_markup
		);
	}
}

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

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

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

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

	if ( $attributes['enableItemDescription'] ) {
		$block_style['classes'][] = 'gtm-has-terms-item-description';
	}

	if ( $attributes['imageHoverBlur'] ) {
		$block_style['classes'][] = "gtm-has-terms-item-image-hover-blur";
	}

	if ( $attributes['imageHoverGray'] ) {
		$block_style['classes'][] = "gtm-has-terms-item-image-hover-gray";
	}

	gtm_css_compileCssVar( $block_style, "terms-columns", $attributes['columns'] );
	gtm_css_compileCssVar( $block_style, "terms-gap", gtm_css_parseSpacingValue( $attributes['gap'] ) );

	gtm_css_compileCssVar( $block_style, "terms-name-typography", gtm_css_parseTypographyProps( $attributes['itemNameTypography'] ) );

	if ( $attributes['enableItemDescription'] ) {
		gtm_css_compileCssVar( $block_style, "terms-description-spacing", gtm_css_parseSpacingValue( $attributes['itemDescriptionSpacing'] ) );
		gtm_css_compileCssVar( $block_style, "terms-description-typography", gtm_css_parseTypographyProps( $attributes['itemDescriptionTypography'] ) );
	}

	gtm_css_compileCssVar( $block_style, "terms-image-height", $attributes['imageHeight'] );
	gtm_css_compileCssVar( $block_style, "terms-image-width", $attributes['imageWidth'] );

	gtm_css_compileCssVar( $block_style, "terms-item-image-focal", gtm_css_parseFocalPointValue( $attributes['imageFocalPoint'] ) );

	gtm_css_compileCssVarClass( $block_style, "terms-item-image-hover", $attributes['imageHoverEffect'] );

	gtm_css_compileCssVar( $block_style, "terms-item-overlay", gtm_css_parseColorValue( $attributes['overlayBg'] ) );

	if ( is_numeric( $attributes['overlayOpacity'] ?? null ) ) {
		gtm_css_compileCssVar( $block_style, "terms-item-overlay-opacity", floatval( $attributes['overlayOpacity'] ) / 100 );
	}

	gtm_css_compileCssVar( $block_style, "terms-item-overlay-blend-mode", $attributes['overlayBlendMode'] );

	gtm_css_compileCssVar( $block_style, "terms-item-overlay-hover", gtm_css_parseColorValue( $attributes['overlayHoverBg'] ) );

	if ( is_numeric( $attributes['overlayHoverOpacity'] ?? null ) ) {
		gtm_css_compileCssVar( $block_style, "terms-item-overlay-hover-opacity", floatval( $attributes['overlayHoverOpacity'] ) / 100 );
	}

	gtm_css_compileCssVar( $block_style, "terms-item-overlay-hover-blend-mode", $attributes['overlayHoverBlendMode'] );

	gtm_css_compileCssVar( $block_style, "terms-item-image-radius", gtm_css_parseBorderRadiusValue( $attributes['imageRadius'] ) );
	gtm_css_compileCssVar( $block_style, "terms-item-image-shadow", gtm_css_parseShadowValue( $attributes['imageShadow'] ) );

	/* Colors */

	gtm_css_compileCssVar( $block_style, "terms-item-name-color", gtm_css_parseColorValue( $attributes['itemNameColor'] ) );
	gtm_css_compileCssVar( $block_style, "terms-item-name-hover-color", gtm_css_parseColorValue( $attributes['itemNameHoverColor'] ) );
	gtm_css_compileCssVar( $block_style, "terms-item-description-color", gtm_css_parseColorValue( $attributes['itemDescriptionColor'] ) );
	gtm_css_compileCssVar( $block_style, "terms-item-description-hover-color", gtm_css_parseColorValue( $attributes['itemDescriptionHoverColor'] ) );

	// 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'] ),
	];
}

function gtm_get_featured_image_from_term( $term, $use_alt_image = false ) {
	$posts = get_posts( [
		'posts_per_page' => 1,
		'tax_query'      => [
			[
				'taxonomy' => $term->taxonomy,
				'field'    => 'slug',
				'terms'    => $term->slug,
			],
		],
		'meta_query'     => [
			[
				'key'     => '_thumbnail_id',
				'compare' => 'EXISTS',
			],
		],
	] );

	if ( ! empty( $posts[0] ) ) {
		$alt_image_id = false;

		if ( $use_alt_image ) {
			$alt_image_id = get_post_meta( $posts[0]->ID, 'gtm_alt_featured_image', true );
		}

		return $alt_image_id ?: get_post_thumbnail_id( $posts[0]->ID );
	}

	return false;
}