File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/post-featured-image.php
<?php
/**
* Renders the `gtm/post-featured-image` 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 featured image for the current post.
*/
function gtm_render_block_post_featured_image( $attr, $content, $block ) {
// Require post id
if ( empty( $block->context['postId'] ) ) {
return '';
}
$post_ID = $block->context['postId'];
$isCover = 'cover' == $attr['display'];
$hideWhenNoImage = ! empty( $attr['hideWhenNoImage'] );
$hasLink = ! empty( $attr['linkTo'] );
$hasThumbnail = has_post_thumbnail( $post_ID );
$isEnableMedia = ! empty( $attr['enableMedia'] );
$isHideWhenNoMedia = ! empty( $attr['hideWhenNoMedia'] );
$isInlineMedia = ! empty( $attr['enableInlineMedia'] ) || gtm_is_amp_request(); // Force inline for AMP
$alt_image_id = get_post_meta( $post_ID, 'gtm_alt_featured_image', true );
$class = '';
$video_url = get_post_meta( $post_ID, 'gtm_video_url', true );
$audio_url = get_post_meta( $post_ID, 'gtm_audio_url', true );
$has_video = has_post_format( 'video' ) && ! empty( $video_url );
$has_audio = has_post_format( 'audio' ) && ! empty( $audio_url );
$has_media = $has_video || $has_audio;
$has_inline_media = $isEnableMedia && $has_media && $isInlineMedia;
// Hide block if needed
if ( ! $hasThumbnail ) {
if ( ! $has_inline_media && ( ! $isCover || ! $hideWhenNoImage ) ) {
return '';
}
}
// Hide block if needed
if ( $isEnableMedia && $isHideWhenNoMedia && ! $has_media ) {
return '';
}
if ( $isEnableMedia && $has_media ) {
$class .= ' gtm-has-featured-media ';
}
if ( ! $hasThumbnail ) {
$class .= ' gtm-has-no-featured-image ';
} else if ( is_singular() && $post_ID === get_queried_object_id() ) {
gtm_preload_important_featured_image( $attr['imageSize'] );
}
$inner_html = '';
if ( $isEnableMedia && $has_media && $isInlineMedia ) {
$class .= ' gtm-has-inline-featured-media ';
if ( $has_video ) {
$embed_html = wp_oembed_get( $video_url );
} else if ( $has_audio ) {
$embed_html = wp_oembed_get( $audio_url );
}
$inner_html = gtm_add_featured_media_aspect_ratio( $embed_html ?? '' );
} else {
$inner_html .= '<div class="wp-block-gtm-post-featured-image__img">';
if ( $attr['useAltImage'] && ! empty( $alt_image_id ) ) {
$inner_html .= wp_get_attachment_image( $alt_image_id, $attr['imageSize'] );
} else {
$inner_html .= get_the_post_thumbnail( $post_ID, $attr['imageSize'] );
}
$inner_html .= '<div class="wp-block-gtm-post-featured-image__overlay"></div>';
if ( ! $isCover && $attr['enableFormatIcon'] ) {
$icon = gtm_get_post_format_icon( $post_ID );
if ( $icon ) {
$inner_html .= '<span class="wp-block-gtm-post-featured-image__format-icon">';
$inner_html .= '<i class="gtm-icon ' . esc_attr( $icon ) . '" aria-hidden="true"></i>';
$inner_html .= '</span>';
}
}
// Render inner blocks
if ( $isCover ) {
$inner_html .= '<div class="wp-block-gtm-post-featured-image__content">';
$inner_html .= do_blocks( $content );
$inner_html .= '</div>';
}
if ( $isEnableMedia && $has_media ) {
if ( $has_video ) {
$embed_html = wp_oembed_get( $video_url );
} else if ( $has_audio ) {
$embed_html = wp_oembed_get( $audio_url );
}
$embed_html = gtm_add_featured_media_aspect_ratio( $embed_html );
$inner_html .= sprintf( '<a class="wp-block-gtm-post-featured-image__link" href="%s" data-pswp-type="html" data-pswp-html="%s"></a>', esc_url( $video_url ), esc_attr( $embed_html ) );
} else if ( $hasLink ) {
if ( 'file' == $attr['linkTo'] ) {
$url = get_the_post_thumbnail_url( $post_ID, 'full' );
} else {
$url = get_the_permalink( $post_ID );
}
// Do not display link if no url, event no thumbnails was set.
if ( ! empty( $url ) ) {
$aria = gtm_html_attributes( [
'rel' => 'bookmark',
'aria-label' => esc_html__( "View post content", 'gutenmate' ),
] );
$inner_html .= sprintf( '<a class="wp-block-gtm-post-featured-image__link" href="%s" %2$s></a>', esc_url( $url ), $aria );
}
}
$inner_html .= '</div>'; // .wp-block-gtm-post-featured-image__img
}
$wrapper_attributes = get_block_wrapper_attributes( ['class' => $class] );
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$inner_html
);
}
/**
* Registers the `gtm/post-featured-image` block on the server.
*/
function gtm_register_block_post_featured_image() {
gtm_register_block_type( 'post-featured-image', [
'render_callback' => 'gtm_render_block_post_featured_image',
] );
}
add_action( 'init', 'gtm_register_block_post_featured_image', 20 );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-featured-image', 'gtm_compile_block_css_post_featured_image', 10, 3 );
function gtm_compile_block_css_post_featured_image( $output, $attributes ) {
$block_style = ['classes' => [], 'style' => []];
$isCover = isset( $attributes['display'] ) && $attributes['display'] == "cover";
$block_style['classes'][] = 'gtm-block';
if ( $attributes['imageHoverBlur'] ) {
$block_style['classes'][] = "gtm-has-post-featured-image-hover-blur";
}
if ( $attributes['imageHoverGray'] ) {
$block_style['classes'][] = "gtm-has-post-featured-image-hover-gray";
}
if ( $attributes['enableFormatIcon'] ) {
$block_style['classes'][] = "gtm-has-post-featured-image-format-icon";
}
if ( $attributes['hideWhenNoImage'] ) {
$block_style['classes'][] = "gtm-hide-no-post-featured-image";
}
gtm_css_compileCssVarClass( $block_style, "post-featured-image-display", $attributes['display'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-focal", gtm_css_parseFocalPointValue( $attributes['imageFocalPoint'] ) );
gtm_css_compileCssVarClass( $block_style, "post-featured-image-hover", $attributes['imageHoverEffect'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-height", $attributes['imageHeight'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-width", $attributes['imageWidth'] );
if ( $isCover ) {
gtm_css_compileCssVar( $block_style, "post-featured-image-content-max-width", $attributes['contentMaxWidth'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-content", gtm_css_parseFlexMatrixPositionValue( $attributes['contentPosition'] ) );
gtm_css_compileCssVar( $block_style, "post-featured-image-content-padding", gtm_css_parseResponsiveBoxProps( $attributes['contentPadding'] ) );
}
if ( ! $isCover && ! empty( $attributes['enableFormatIcon'] ) ) {
gtm_css_compileCssVar( $block_style, "post-featured-image-format-icon-size", $attributes['formatIconSize'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-format-icon-width", $attributes['formatIconWidth'] );
gtm_css_compileCssVar( $block_style,
'post-featured-image-format-icon-radius',
gtm_css_parseBorderRadiusValue( $attributes['formatIconRadius'] )
);
gtm_css_compileCssVar( $block_style,
'post-featured-image-format-icon-color',
gtm_css_parseColorValue( $attributes['formatIconColor'] )
);
gtm_css_compileCssVar( $block_style,
'post-featured-image-format-icon-bg',
gtm_css_parseColorValue( $attributes['formatIconBg'] )
);
gtm_css_compileCssVar( $block_style,
'post-featured-image-format-icon-hover-color',
gtm_css_parseColorValue( $attributes['formatIconHoverColor'] )
);
gtm_css_compileCssVar( $block_style,
'post-featured-image-format-icon-hover-bg',
gtm_css_parseColorValue( $attributes['formatIconHoverBg'] )
);
}
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay", gtm_css_parseColorValue( $attributes['overlayBg'] ) );
if ( is_numeric( $attributes['overlayOpacity'] ?? null ) ) {
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay-opacity", floatval( $attributes['overlayOpacity'] ) / 100 );
}
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay-blend-mode", $attributes['overlayBlendMode'] );
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay-hover", gtm_css_parseColorValue( $attributes['overlayHoverBg'] ) );
if ( is_numeric( $attributes['overlayHoverOpacity'] ?? null ) ) {
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay-hover-opacity", floatval( $attributes['overlayHoverOpacity'] ) / 100 );
}
gtm_css_compileCssVar( $block_style, "post-featured-image-overlay-hover-blend-mode", $attributes['overlayHoverBlendMode'] );
// 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_add_featured_media_aspect_ratio( $html ) {
preg_match( '|width="([^"]+)"|', $html, $match_width );
preg_match( '|height="([^"]+)"|', $html, $match_height );
if ( ! empty( $match_width ) && ! empty( $match_height ) ) {
$css = sprintf( '--gtm-featured-media-width:%s;--gtm-featured-media-height:%s;', $match_width[1], $match_height[1] );
if ( stripos( $html, 'style="' ) !== false ) {
$html = str_replace( 'style="', "style=\"$css", $html );
} else {
$html = str_replace( '<iframe ', "<iframe style=\"$css\" ", $html );
}
}
// Add class
$className = 'gtm-featured-media-embed';
if ( stripos( $html, 'class="' ) !== false ) {
$html = str_replace( 'class="', "class=\"$className", $html );
} else {
$html = str_replace( '<iframe ', "<iframe class=\"$className\" ", $html );
}
return $html;
}