Created
June 25, 2023 20:17
-
-
Save erikyo/b0fdb60440bb99ff33e6609a72d468c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Registers a custom block variation for the `core/post-featured-image` block. | |
*/ | |
function register_custom_post_featured_image_variation() { | |
// Define the variation attributes | |
$variation_attributes = array( | |
'defaultMediaUrl' => array( | |
'type' => 'string', | |
'default' => wc_placeholder_img(), | |
), | |
); | |
/** | |
* Renders the `core/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 or a custom placeholder if the image is missing. | |
*/ | |
$variation_render_callback = function ( $attributes, $content, $block ) { | |
if ( ! isset( $block->context['postId'] ) ) { | |
return ''; | |
} | |
$post_ID = $block->context['postId']; | |
// Check if the featured image exists | |
if ( has_post_thumbnail( $post_ID ) ) { | |
// the following code is a copy of the render callback of the featured post image block | |
if ( ! isset( $block->context['postId'] ) ) { | |
return ''; | |
} | |
$post_ID = $block->context['postId']; | |
// Check is needed for backward compatibility with third-party plugins | |
// that might rely on the `in_the_loop` check; calling `the_post` sets it to true. | |
if ( ! in_the_loop() && have_posts() ) { | |
the_post(); | |
} | |
$is_link = isset( $attributes['isLink'] ) && $attributes['isLink']; | |
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail'; | |
$attr = get_block_core_post_featured_image_border_attributes( $attributes ); | |
$overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes ); | |
if ( $is_link ) { | |
if ( get_the_title( $post_ID ) ) { | |
$attr['alt'] = trim( strip_tags( get_the_title( $post_ID ) ) ); | |
} else { | |
$attr['alt'] = sprintf( | |
// translators: %d is the post ID. | |
__( 'Untitled post %d' ), | |
$post_ID | |
); | |
} | |
} | |
$extra_styles = ''; | |
// Aspect ratio with a height set needs to override the default width/height. | |
if ( ! empty( $attributes['aspectRatio'] ) ) { | |
$extra_styles .= 'width:100%;height:100%;'; | |
} elseif ( ! empty( $attributes['height'] ) ) { | |
$extra_styles .= "height:{$attributes['height']};"; | |
} | |
if ( ! empty( $attributes['scale'] ) ) { | |
$extra_styles .= "object-fit:{$attributes['scale']};"; | |
} | |
if ( ! empty( $extra_styles ) ) { | |
$attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles; | |
} | |
$featured_image = get_the_post_thumbnail( $post_ID, $size_slug, $attr ); | |
if ( ! $featured_image ) { | |
return ''; | |
} | |
if ( $is_link ) { | |
$link_target = $attributes['linkTarget']; | |
$rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : ''; | |
$height = ! empty( $attributes['height'] ) ? 'style="' . esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . '"' : ''; | |
$featured_image = sprintf( | |
'<a href="%1$s" target="%2$s" %3$s %4$s>%5$s%6$s</a>', | |
get_the_permalink( $post_ID ), | |
esc_attr( $link_target ), | |
$rel, | |
$height, | |
$featured_image, | |
$overlay_markup | |
); | |
} else { | |
$featured_image = $featured_image . $overlay_markup; | |
} | |
$aspect_ratio = ! empty( $attributes['aspectRatio'] ) | |
? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';' | |
: ''; | |
$width = ! empty( $attributes['width'] ) | |
? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';' | |
: ''; | |
$height = ! empty( $attributes['height'] ) | |
? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';' | |
: ''; | |
if ( ! $height && ! $width && ! $aspect_ratio ) { | |
$wrapper_attributes = get_block_wrapper_attributes(); | |
} else { | |
$wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) ); | |
} | |
return "<figure {$wrapper_attributes}>{$featured_image}</figure>"; | |
// the original featured post image ends here | |
} else { | |
// Featured image is missing, render a custom placeholder | |
$placeholder = sprintf( '<figure class="custom-placeholder">%s</figure>', $attributes['defaultMediaUrl'] ); | |
// Return the custom placeholder | |
return $placeholder; | |
} | |
}; | |
// Get the existing block type | |
$cloned_block = WP_Block_Type_Registry::get_instance()->get_registered( 'core/post-featured-image' ); | |
// Add the variation attributes | |
$cloned_block->attributes = array_merge( $cloned_block->attributes, $variation_attributes ); | |
$cloned_block->name = 'shop-featured-image'; | |
$cloned_block->title = __( 'Shop featured image', 'modulr' ); | |
$cloned_block->category = 'media'; | |
// Add the variation rendering callback | |
$cloned_block->render_callback = $variation_render_callback; | |
$namespace = 'modulr'; | |
$block_title = 'shop-featured-image'; | |
// Register the new block type. | |
register_block_type( $namespace . '/' . $block_title, $cloned_block ); | |
} | |
add_action( 'init', 'register_custom_post_featured_image_variation' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment