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( '//', ' ', 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> 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' => '', 'menu' => '', 'theme' => '', ); if ( ! isset( $icons[ $name ] ) ) { return ''; } return sprintf( // The path data is a fixed literal, never user input. '', 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 = ''; 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' );