File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/enhancement.php
<?php
/**
* Preload an important featured image for individual post page
* For improving page loads
* Ref: https://html.spec.whatwg.org/multipage/semantics.html#attr-link-imagesrcset
*/
// Disable lazy load for blocks
add_filter( 'render_block_core/post-featured-image', 'gtm_preload_render_block', 10, 3 );
add_filter( 'render_block_gtm/post-featured-image', 'gtm_preload_render_block', 10, 3 );
function gtm_preload_render_block( $block_content, $block, $wp_block ) {
if ( ! empty( $wp_block->context['postId'] ) && ! empty( $block['attrs']['imageSize'] ) ) {
$post_ID = $wp_block->context['postId'];
if ( is_singular() && $post_ID === get_queried_object_id() && gtm_should_preload_the_image( $block['attrs']['imageSize'] ) ) {
gtm_preload_important_featured_image( $block['attrs']['imageSize'] );
$block_content = gtm_preload_remove_lazy_load( $block_content );
}
}
return $block_content;
}
function gtm_should_preload_the_image( $image_size ) {
static $img_count = 0;
$img_count++;
// Preload only the first featured image of the current post
if ( $img_count == 1 ) {
$ignore_sizes = ['thumbnail'];
if ( is_singular() && has_post_thumbnail() && ! empty( $image_size ) && ! in_array( $image_size, $ignore_sizes ) ) {
return true;
}
}
}
function gtm_preload_important_featured_image( $image_size ) {
add_action( 'wp_head', function () use ( $image_size ) {
$preload = '<link rel="preload" as="image"';
$img_html = get_the_post_thumbnail( get_queried_object_id(), $image_size );
if ( preg_match( '|src="([^"]+)"|', $img_html, $src_matches ) ) {
$preload .= sprintf( ' href="%s"', esc_url( $src_matches[1] ) );
}
if ( preg_match( '|srcset="([^"]+)"|', $img_html, $srcset_matches ) ) {
$preload .= sprintf( ' imagesrcset="%s"', esc_attr( $srcset_matches[1] ) );
}
if ( preg_match( '|sizes="([^"]+)"|', $img_html, $sizes_matches ) ) {
$preload .= sprintf( ' imagesizes="%s"', esc_attr( $sizes_matches[1] ) );
}
// To make sure there is rendered image
if ( ! empty( $img_html ) ) {
echo trim( $preload ?? '' ) . '>';
}
} );
}
// Remove lazy load since is was preloaded
function gtm_preload_remove_lazy_load( $content ) {
$content = str_replace( 'loading="lazy"', '', $content );
return $content;
}
/**
* Additional allowance for using MIN() and MAX() in theme.json
*/
add_filter( 'safecss_filter_attr_allow_css', 'gtm_safecss_filter_attr_allow_css_functions', 10, 2 );
function gtm_safecss_filter_attr_allow_css_functions( $allow_css, $css_test_string ) {
// The below code is checking the same way as wp-includes\kses.php@2471
// Allow CSS min().
$css_test_string = preg_replace( '/min\(((?:\([^()]*\)?|[^()])*)\)/', '', $css_test_string );
// Allow CSS max().
$css_test_string = preg_replace( '/max\(((?:\([^()]*\)?|[^()])*)\)/', '', $css_test_string );
// Check for any CSS containing \ ( & } = or comments,
// except for url(), calc(), or var() usage checked above.
$allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string );
return $allow_css;
}
/**
* Disable all innerblocks inside comments query loop if comments is not open
*/
add_filter( 'render_block', 'gtm_disable_inner_blocks_in_comments_query_loop', 10, 2 );
function gtm_disable_inner_blocks_in_comments_query_loop( $block_content, $block ) {
if ( $block['blockName'] === 'core/comments-query-loop' ) {
if ( ! comments_open() ) {
return '';
}
}
return $block_content;
}