45 lines
2.3 KiB
PHP
45 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Responsive theme-owned media helpers.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/**
|
|
* Render an allowlisted topic portrait using modern derivatives and a PNG fallback.
|
|
*
|
|
* @param string $topic_slug Topic slug.
|
|
* @param string $alt Alternative text. Empty is valid for decorative art.
|
|
* @param string $class_name Optional image class.
|
|
* @param string $loading Either lazy (default) or eager for above-fold art.
|
|
* @return string Safe picture markup or an empty string.
|
|
*/
|
|
function chrnx_get_topic_portrait_markup( string $topic_slug, string $alt = '', string $class_name = '', string $loading = 'lazy' ): string {
|
|
$source = chrnx_get_topic_art_asset( $topic_slug );
|
|
$dimensions = chrnx_get_topic_art_dimensions( $topic_slug );
|
|
if ( '' === $source || 2 !== count( $dimensions ) ) {
|
|
return '';
|
|
}
|
|
|
|
$basename = pathinfo( $source, PATHINFO_FILENAME );
|
|
$base_uri = get_theme_file_uri( 'assets/images/generated/' . $basename );
|
|
$sizes = '(max-width: 599px) 70vw, 384px';
|
|
$split_classes = preg_split( '/\s+/', trim( $class_name ) );
|
|
$classes = is_array( $split_classes ) ? array_filter( $split_classes ) : array();
|
|
$class = implode( ' ', array_map( 'sanitize_html_class', $classes ) );
|
|
$decorative = '' === $alt ? ' aria-hidden="true"' : '';
|
|
$loading = 'eager' === $loading ? 'eager' : 'lazy';
|
|
$priority = 'eager' === $loading ? ' fetchpriority="high"' : '';
|
|
if ( 'music-collage' === $topic_slug ) {
|
|
$sizes = '(max-width: 599px) 100vw, 720px';
|
|
}
|
|
|
|
$markup = '<picture class="chrnx-responsive-portrait"' . $decorative . '>';
|
|
$markup .= '<source type="image/avif" srcset="' . esc_url( $base_uri . '-384.avif' ) . ' 384w, ' . esc_url( $base_uri . '-768.avif' ) . ' 768w" sizes="' . esc_attr( $sizes ) . '">';
|
|
$markup .= '<source type="image/webp" srcset="' . esc_url( $base_uri . '-384.webp' ) . ' 384w, ' . esc_url( $base_uri . '-768.webp' ) . ' 768w" sizes="' . esc_attr( $sizes ) . '">';
|
|
$markup .= '<img' . ( '' !== $class ? ' class="' . esc_attr( $class ) . '"' : '' ) . ' src="' . esc_url( get_theme_file_uri( $source ) ) . '" width="' . esc_attr( $dimensions[0] ) . '" height="' . esc_attr( $dimensions[1] ) . '" loading="' . esc_attr( $loading ) . '"' . $priority . ' decoding="async" sizes="' . esc_attr( $sizes ) . '" alt="' . esc_attr( $alt ) . '">';
|
|
$markup .= '</picture>';
|
|
|
|
return $markup;
|
|
}
|