post_type && 'publish' === $post->post_status && '' !== trim( wp_strip_all_tags( $post->post_title ) ); } /** * Return all eligible post IDs in deterministic publication order. * * @param array $extra_args Additional WP_Query arguments. * @return int[] Eligible post IDs. */ function chrnx_get_eligible_editorial_post_ids( $extra_args = array() ) { $args = array_merge( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => array( 'date' => 'DESC', 'ID' => 'DESC', ), 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'fields' => 'ids', ), $extra_args ); $ids = get_posts( $args ); return array_values( array_map( 'absint', array_filter( $ids, 'chrnx_is_eligible_editorial_post' ) ) ); } /** * Return the first assigned topic slug for a post. * * @param int $post_id Post ID. * @return string Topic slug or empty string. */ function chrnx_get_post_topic_slug( $post_id ) { $terms = wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'slugs' ) ); return ! is_wp_error( $terms ) && ! empty( $terms ) ? (string) reset( $terms ) : ''; } /** * Select featured story IDs using sticky primary and topic-diverse secondaries. * * @param int $limit Maximum cards, bounded to 12. * @return int[] Selected post IDs. */ function chrnx_get_featured_story_ids( int $limit = 3 ): array { $limit = min( 12, max( 0, $limit ) ); if ( 0 === $limit ) { return array(); } $eligible = chrnx_get_eligible_editorial_post_ids(); if ( empty( $eligible ) ) { return array(); } $sticky_ids = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) ); $primary = 0; foreach ( $eligible as $post_id ) { if ( in_array( $post_id, $sticky_ids, true ) ) { $primary = $post_id; break; } } if ( 0 === $primary ) { $primary = $eligible[0]; } $selected = array( $primary ); $pool = array_values( array_diff( $eligible, array( $primary ) ) ); $used = array(); $topic = chrnx_get_post_topic_slug( $primary ); if ( '' !== $topic ) { $used[] = $topic; } foreach ( $pool as $post_id ) { if ( count( $selected ) >= $limit ) { break; } $topic = chrnx_get_post_topic_slug( $post_id ); if ( '' !== $topic && ! in_array( $topic, $used, true ) ) { $selected[] = $post_id; $used[] = $topic; } } foreach ( $pool as $post_id ) { if ( count( $selected ) >= $limit ) { break; } if ( ! in_array( $post_id, $selected, true ) ) { $selected[] = $post_id; } } return $selected; } /** * Validate a request taxonomy slug against an existing editorial term. * * @param mixed $value Requested value. * @param string $taxonomy Editorial taxonomy. * @return string Valid slug or empty string. */ function chrnx_validate_editorial_term_slug( $value, $taxonomy ) { if ( ! is_string( $value ) || ! taxonomy_exists( $taxonomy ) ) { return ''; } $value = trim( wp_unslash( $value ) ); if ( '' === $value || sanitize_title( $value ) !== $value ) { return ''; } $term = get_term_by( 'slug', $value, $taxonomy ); return $term instanceof WP_Term ? $term->slug : ''; } /** * Parse a canonical public Journal page number. * * @param mixed $value Request-shaped page value. * @return int A page from 1 through 10000, or 1 for invalid input. */ function chrnx_parse_journal_page( $value ) { if ( is_int( $value ) ) { $page = $value; } elseif ( is_string( $value ) && preg_match( '/^[1-9][0-9]*$/', $value ) ) { $page = (int) $value; } else { return 1; } return $page >= 1 && $page <= 10000 ? $page : 1; } /** * Sanitize and bound public Journal search text. * * @param mixed $value Request-shaped search value. * @return string Safe search text or an empty string. */ function chrnx_sanitize_journal_search( $value ) { if ( ! is_scalar( $value ) ) { return ''; } return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' ); } /** * Build a Journal page URL from only validated public filters. * * @param int $page Requested page number. * @param array $filters Request-shaped Journal filters. * @return string Safe Journal URL. */ function chrnx_get_journal_page_url( int $page, array $filters ) { $base_url = strtok( get_pagenum_link( 1, false ), '?' ); $args = array(); foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) { $slug = chrnx_validate_editorial_term_slug( $filters[ $taxonomy ] ?? '', $taxonomy ); if ( '' !== $slug ) { $args[ $taxonomy ] = $slug; } } $search = chrnx_sanitize_journal_search( $filters['s'] ?? '' ); if ( '' !== $search ) { $args['s'] = $search; } $page = chrnx_parse_journal_page( $page ); if ( $page > 1 ) { $args['paged'] = $page; } return add_query_arg( $args, $base_url ); } /** * Build safe Journal WP_Query arguments from request-shaped data. * * @param array $request Request values. * @return array Safe WP_Query arguments. */ function chrnx_get_journal_query_args( array $request ): array { $eligible_ids = chrnx_get_eligible_editorial_post_ids(); $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'post__in' => ! empty( $eligible_ids ) ? $eligible_ids : array( 0 ), 'posts_per_page' => 10, 'paged' => chrnx_parse_journal_page( $request['paged'] ?? null ), 'orderby' => array( 'date' => 'DESC', 'ID' => 'DESC', ), 'ignore_sticky_posts' => true, ); $tax_query = array(); foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) { $value = $request[ $taxonomy ] ?? ''; $slug = chrnx_validate_editorial_term_slug( $value, $taxonomy ); if ( '' !== $slug ) { $tax_query[] = array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $slug, ); } } if ( ! empty( $tax_query ) ) { $args['tax_query'] = $tax_query; } $search = chrnx_sanitize_journal_search( $request['s'] ?? '' ); if ( '' !== $search ) { $args['s'] = $search; } return $args; } /** * Return the latest eligible podcast assigned to any show. * * @return int Podcast post ID or zero. */ function chrnx_get_latest_podcast_id(): int { $ids = chrnx_get_eligible_editorial_post_ids( array( 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'chrnx_topic', 'field' => 'slug', 'terms' => 'podcasts', ), array( 'taxonomy' => 'chrnx_show', 'operator' => 'EXISTS', ), ), ) ); return ! empty( $ids ) ? $ids[0] : 0; } /** * Build query arguments for one validated show. * * @param string $show_slug Existing show slug. * @param int $page Positive page number. * @return array Safe WP_Query arguments. */ function chrnx_get_show_query_args( string $show_slug, int $page = 1 ): array { $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10, 'paged' => max( 1, $page ), 'orderby' => array( 'date' => 'DESC', 'ID' => 'DESC', ), 'ignore_sticky_posts' => true, ); $slug = chrnx_validate_editorial_term_slug( $show_slug, 'chrnx_show' ); if ( '' === $slug ) { $args['post__in'] = array( 0 ); return $args; } $args['tax_query'] = array( array( 'taxonomy' => 'chrnx_show', 'field' => 'slug', 'terms' => $slug, ), ); $eligible_ids = chrnx_get_eligible_editorial_post_ids( array( 'tax_query' => $args['tax_query'] ) ); $args['post__in'] = ! empty( $eligible_ids ) ? $eligible_ids : array( 0 ); return $args; } /** * Return related posts tiered by topic, shared tags, then recency. * * @param int $post_id Current post ID. * @param int $limit Maximum related stories, bounded to 12. * @return int[] Related post IDs. */ function chrnx_get_related_post_ids( int $post_id, int $limit = 3 ): array { $limit = min( 12, max( 0, $limit ) ); if ( 0 === $limit || ! chrnx_is_eligible_editorial_post( $post_id ) ) { return array(); } $candidates = chrnx_get_eligible_editorial_post_ids( array( 'post__not_in' => array( $post_id ) ) ); $topic = chrnx_get_post_topic_slug( $post_id ); $tag_ids = wp_get_post_tags( $post_id, array( 'fields' => 'ids' ) ); $tag_ids = is_wp_error( $tag_ids ) ? array() : array_map( 'absint', $tag_ids ); $tiers = array( array(), array(), array() ); foreach ( $candidates as $candidate_id ) { if ( '' !== $topic && chrnx_get_post_topic_slug( $candidate_id ) === $topic ) { $tiers[0][] = $candidate_id; continue; } $candidate_tags = wp_get_post_tags( $candidate_id, array( 'fields' => 'ids' ) ); $candidate_tags = is_wp_error( $candidate_tags ) ? array() : array_map( 'absint', $candidate_tags ); if ( ! empty( array_intersect( $tag_ids, $candidate_tags ) ) ) { $tiers[1][] = $candidate_id; } else { $tiers[2][] = $candidate_id; } } return array_slice( array_merge( $tiers[0], $tiers[1], $tiers[2] ), 0, $limit ); } /** * Return the fixed theme-relative topic art asset path. * * @param string $topic_slug Supported topic slug. * @return string Theme-relative asset path or empty string. */ function chrnx_get_topic_art_asset( string $topic_slug ): string { $mapping = array( 'research' => 'alter-ego-research.png', 'code' => 'alter-ego-code.png', 'literature' => 'alter-ego-literature.png', 'music' => 'the-no-names-musician-cutout.png', 'podcasts' => 'alter-ego-podcasts.png', 'life' => 'alter-ego-life.png', 'gaming' => 'alter-ego-gaming.png', ); return isset( $mapping[ $topic_slug ] ) ? 'assets/images/' . $mapping[ $topic_slug ] : ''; }