Projects were hardcoded h3 elements in a pattern: no URL, no feed, no search, uneditable without touching PHP. They follow the podcast model now, a chrnx_topic term plus posts. Adds a chrnx_project_featured flag with an editor toggle so the stage lists chosen projects first and fills with the most recent. Sticky was already spoken for by the home featured stories, so the choice needed its own signal. journal-stream gains a pinned topic attribute, reusing the archive filter, pagination and empty state it already had. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
248 lines
8.6 KiB
PHP
248 lines
8.6 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;
|
||
}
|
||
|
||
/**
|
||
* Return the canonical primary-navigation URL for the current main query.
|
||
*
|
||
* @return string Current navigation destination URL, or an empty string.
|
||
*/
|
||
function chrnx_get_current_navigation_destination() {
|
||
$journal_url = home_url( '/journal/' );
|
||
|
||
if ( is_404() ) {
|
||
return '';
|
||
}
|
||
|
||
if ( is_home() || is_search() || is_singular( 'post' ) || is_tax( array( 'chrnx_topic', 'chrnx_language', 'chrnx_show' ) ) ) {
|
||
return $journal_url;
|
||
}
|
||
|
||
if ( is_front_page() || ! is_page() ) {
|
||
return '';
|
||
}
|
||
|
||
$page = get_queried_object();
|
||
if ( ! $page instanceof WP_Post || 'page' !== $page->post_type ) {
|
||
return '';
|
||
}
|
||
|
||
$page_uri = get_page_uri( $page );
|
||
if ( ! in_array( $page_uri, array( 'journal', 'projects', 'music', 'podcasts', 'about' ), true ) ) {
|
||
return '';
|
||
}
|
||
|
||
return home_url( '/' . $page_uri . '/' );
|
||
}
|
||
|
||
/**
|
||
* 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' )
|
||
);
|
||
}
|
||
|
||
// A track with no art of its own borrows its album cover before falling back.
|
||
$release = function_exists( 'chrnx_get_track_release' ) ? chrnx_get_track_release( $post_id ) : null;
|
||
if ( null !== $release ) {
|
||
$cover_id = absint( get_term_meta( $release->term_id, 'chrnx_release_cover', true ) );
|
||
if ( $cover_id > 0 ) {
|
||
return wp_get_attachment_image(
|
||
$cover_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 chrnx_get_topic_portrait_markup( 'placeholder-post', '', $class );
|
||
}
|
||
|
||
/**
|
||
* Return the navigation links for one theme menu location.
|
||
*
|
||
* The built-in list is a fallback, not a contract: a site whose pages are named
|
||
* differently would otherwise get a header full of 404s.
|
||
*
|
||
* @param string $location Registered menu location.
|
||
* @return array<int,array<string,string>> Label and URL pairs.
|
||
*/
|
||
function chrnx_get_navigation_links( $location ) {
|
||
$assigned = get_nav_menu_locations();
|
||
$menu_id = isset( $assigned[ $location ] ) ? absint( $assigned[ $location ] ) : 0;
|
||
if ( $menu_id > 0 ) {
|
||
$items = wp_get_nav_menu_items( $menu_id );
|
||
if ( is_array( $items ) ) {
|
||
$links = array();
|
||
foreach ( $items as $item ) {
|
||
// Only top-level entries: the header is a single flat row.
|
||
if ( absint( $item->menu_item_parent ) > 0 ) {
|
||
continue;
|
||
}
|
||
$url = esc_url_raw( (string) $item->url );
|
||
$label = trim( wp_strip_all_tags( (string) $item->title ) );
|
||
if ( '' !== $url && '' !== $label ) {
|
||
$links[] = array(
|
||
'label' => $label,
|
||
'url' => $url,
|
||
);
|
||
}
|
||
}
|
||
if ( ! empty( $links ) ) {
|
||
return $links;
|
||
}
|
||
}
|
||
}
|
||
|
||
$defaults = array( 'journal', 'projects', 'music', 'podcasts', 'about' );
|
||
$labels = array(
|
||
'journal' => __( 'Journal', 'chrnx' ),
|
||
'projects' => __( 'Projects', 'chrnx' ),
|
||
'music' => __( 'Music', 'chrnx' ),
|
||
'podcasts' => __( 'Podcasts', 'chrnx' ),
|
||
'about' => __( 'About', 'chrnx' ),
|
||
);
|
||
$links = array();
|
||
foreach ( $defaults as $slug ) {
|
||
$links[] = array(
|
||
'label' => $labels[ $slug ],
|
||
'url' => home_url( '/' . $slug . '/' ),
|
||
);
|
||
}
|
||
return $links;
|
||
}
|
||
|
||
/**
|
||
* Render one allowlisted interface icon.
|
||
*
|
||
* Unicode glyphs render inconsistently across platforms and fonts, so every
|
||
* control icon comes from this one place instead.
|
||
*
|
||
* @param string $name Allowlisted icon name.
|
||
* @param int $size Square pixel size.
|
||
* @return string Escaped inline SVG, or an empty string for unknown names.
|
||
*/
|
||
function chrnx_get_icon_markup( $name, $size = 20 ) {
|
||
$icons = array(
|
||
'search' => '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.6-3.6"/>',
|
||
'menu' => '<path d="M4 7h16M4 12h16M4 17h16"/>',
|
||
'theme' => '<circle cx="12" cy="12" r="9"/><path d="M12 3a9 9 0 0 1 0 18Z" fill="currentColor" stroke="none"/>',
|
||
);
|
||
if ( ! isset( $icons[ $name ] ) ) {
|
||
return '';
|
||
}
|
||
|
||
return sprintf(
|
||
// The path data is a fixed literal, never user input.
|
||
'<svg viewBox="0 0 24 24" width="%1$d" height="%1$d" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">%2$s</svg>',
|
||
absint( $size ),
|
||
$icons[ $name ] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 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' );
|
||
// A card without a topic left an empty slot and knocked the row out of alignment.
|
||
// Untopiced posts are still Journal entries, so that is the honest fallback.
|
||
$topic_label = ! is_wp_error( $topic_terms ) && ! empty( $topic_terms ) ? $topic_terms[0]->name : __( 'Journal', 'chrnx' );
|
||
$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' ) . '">';
|
||
$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', 'selected-work', 'release-list', 'release-detail', 'track-meta', 'subscribe-form' );
|
||
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' );
|