90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Dynamic editorial block registration and shared rendering helpers.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/**
|
|
* Allowlist a semantic heading level.
|
|
*
|
|
* @param mixed $value Requested level.
|
|
* @param int $fallback_level Default level.
|
|
* @return int Safe level from 2 through 6.
|
|
*/
|
|
function chrnx_get_heading_level( $value, $fallback_level = 2 ) {
|
|
$level = absint( $value );
|
|
return in_array( $level, array( 2, 3, 4, 5, 6 ), true ) ? $level : $fallback_level;
|
|
}
|
|
|
|
/**
|
|
* Render a post's meaningful featured image or decorative topic fallback.
|
|
*
|
|
* @param int $post_id Post ID.
|
|
* @param string $class_name CSS class prefix.
|
|
* @return string Escaped image/fallback markup.
|
|
*/
|
|
function chrnx_get_card_media_markup( $post_id, $class_name ) {
|
|
$thumbnail_id = get_post_thumbnail_id( $post_id );
|
|
if ( $thumbnail_id ) {
|
|
return wp_get_attachment_image(
|
|
$thumbnail_id,
|
|
'chrnx-card',
|
|
false,
|
|
array( 'class' => $class_name . '__image' )
|
|
);
|
|
}
|
|
$topic = chrnx_get_post_topic_slug( $post_id );
|
|
$class = $class_name . '__fallback' . ( '' !== $topic ? ' is-topic-' . sanitize_html_class( $topic ) : '' );
|
|
return '<span class="' . esc_attr( $class ) . '" aria-hidden="true"></span>';
|
|
}
|
|
|
|
/**
|
|
* Render one semantic editorial card.
|
|
*
|
|
* @param int $post_id Post ID.
|
|
* @param int $heading_level Heading level.
|
|
* @param string $class_name Card class.
|
|
* @return string Escaped card markup or empty string.
|
|
*/
|
|
function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_name = 'chrnx-story-card' ) {
|
|
if ( ! chrnx_is_eligible_editorial_post( $post_id ) ) {
|
|
return '';
|
|
}
|
|
$post = get_post( $post_id );
|
|
$heading_level = chrnx_get_heading_level( $heading_level, 3 );
|
|
$topic_terms = get_the_terms( $post_id, 'chrnx_topic' );
|
|
$topic_label = ! is_wp_error( $topic_terms ) && ! empty( $topic_terms ) ? $topic_terms[0]->name : '';
|
|
$excerpt = wp_trim_words( wp_strip_all_tags( get_the_excerpt( $post ) ), 24 );
|
|
|
|
$markup = '<article class="' . esc_attr( $class_name ) . '">';
|
|
$markup .= '<a class="' . esc_attr( $class_name . '__link' ) . '" href="' . esc_url( get_permalink( $post ) ) . '">';
|
|
$markup .= chrnx_get_card_media_markup( $post_id, $class_name );
|
|
$markup .= '<span class="' . esc_attr( $class_name . '__content' ) . '">';
|
|
if ( '' !== $topic_label ) {
|
|
$markup .= '<span class="' . esc_attr( $class_name . '__badge' ) . '">' . esc_html( $topic_label ) . '</span>';
|
|
}
|
|
$markup .= '<h' . $heading_level . ' class="' . esc_attr( $class_name . '__title' ) . '">' . esc_html( get_the_title( $post ) ) . '</h' . $heading_level . '>';
|
|
if ( '' !== $excerpt ) {
|
|
$markup .= '<span class="' . esc_attr( $class_name . '__excerpt' ) . '">' . esc_html( $excerpt ) . '</span>';
|
|
}
|
|
$markup .= '</span></a></article>';
|
|
return $markup;
|
|
}
|
|
|
|
/** Register all block metadata directories that exist. */
|
|
function chrnx_register_editorial_blocks() {
|
|
$blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle' );
|
|
foreach ( $blocks as $block ) {
|
|
$name = 'chrnx/' . $block;
|
|
if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) {
|
|
continue;
|
|
}
|
|
$directory = get_theme_file_path( 'blocks/' . $block );
|
|
if ( is_readable( $directory . '/block.json' ) ) {
|
|
register_block_type( $directory );
|
|
}
|
|
}
|
|
}
|
|
add_action( 'init', 'chrnx_register_editorial_blocks' );
|