Files
chrnx-dev-wp/inc/blocks.php
T

118 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
/**
* Estimate localized reading time from stored post content without rendering it.
*
* @param string $content Raw post content.
* @return int Estimated whole minutes, or zero for empty content.
*/
function chrnx_get_reading_time_minutes( $content ) {
if ( ! is_string( $content ) || '' === trim( $content ) ) {
return 0;
}
$text = preg_replace( '/<!--(?:.|\s)*?-->/', ' ', strip_shortcodes( $content ) );
$text = html_entity_decode( wp_strip_all_tags( (string) $text, true ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
$text = wp_check_invalid_utf8( $text );
if ( '' === trim( $text ) ) {
return 0;
}
if ( str_starts_with( wp_get_word_count_type(), 'characters' ) ) {
$characters = preg_replace( '/\s+/u', '', $text );
$count = mb_strlen( (string) $characters, 'UTF-8' );
return max( 1, (int) ceil( $count / 500 ) );
}
$count = preg_match_all( "/[\p{L}\p{N}]+(?:['’\x{2010}-\x{2015}-][\p{L}\p{N}]+)*/u", $text );
return false === $count || 0 === $count ? 0 : max( 1, (int) ceil( $count / 200 ) );
}
/**
* 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', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link' );
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' );