/', ' ', 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 ''; } /** * 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 = '
'; $markup .= ''; $markup .= chrnx_get_card_media_markup( $post_id, $class_name ); $markup .= ''; if ( '' !== $topic_label ) { $markup .= '' . esc_html( $topic_label ) . ''; } $markup .= '' . esc_html( get_the_title( $post ) ) . ''; if ( '' !== $excerpt ) { $markup .= '' . esc_html( $excerpt ) . ''; } $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' ); 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' );