diff --git a/blocks/featured-stories/block.json b/blocks/featured-stories/block.json new file mode 100644 index 0000000..1942d7f --- /dev/null +++ b/blocks/featured-stories/block.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/featured-stories", + "version": "0.1.0", + "title": "Featured Stories", + "category": "theme", + "description": "A sticky-led selection of topic-diverse stories.", + "textdomain": "chrnx", + "attributes": { + "limit": { "type": "integer", "default": 3, "minimum": 1, "maximum": 12 }, + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "style": "file:./style.css", + "render": "file:./render.php" +} diff --git a/blocks/featured-stories/render.php b/blocks/featured-stories/render.php new file mode 100644 index 0000000..bbd0f12 --- /dev/null +++ b/blocks/featured-stories/render.php @@ -0,0 +1,23 @@ + +
'chrnx-featured-stories' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-label=""> + +
diff --git a/blocks/featured-stories/style.css b/blocks/featured-stories/style.css new file mode 100644 index 0000000..a1b3120 --- /dev/null +++ b/blocks/featured-stories/style.css @@ -0,0 +1,27 @@ +.chrnx-featured-stories__grid { + display: grid; + gap: var(--wp--preset--spacing--40, 1.5rem); + grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); +} + +.chrnx-featured-story, +.chrnx-featured-story__link { + height: 100%; +} + +.chrnx-featured-story__link { + display: flex; + flex-direction: column; +} + +.chrnx-featured-story__image, +.chrnx-featured-story__fallback { + aspect-ratio: 3 / 2; + display: block; + object-fit: cover; + width: 100%; +} + +.chrnx-featured-story__fallback { + background: linear-gradient(135deg, var(--wp--preset--color--violet), var(--wp--preset--color--green)); +} diff --git a/blocks/journal-stream/block.json b/blocks/journal-stream/block.json new file mode 100644 index 0000000..43ebd18 --- /dev/null +++ b/blocks/journal-stream/block.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/journal-stream", + "version": "0.1.0", + "title": "Journal Stream", + "category": "theme", + "description": "The filterable chronological journal.", + "textdomain": "chrnx", + "attributes": { + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "style": "file:./style.css", + "render": "file:./render.php" +} diff --git a/blocks/journal-stream/render.php b/blocks/journal-stream/render.php new file mode 100644 index 0000000..988019b --- /dev/null +++ b/blocks/journal-stream/render.php @@ -0,0 +1,94 @@ + +
'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-journal-title"> + id="chrnx-journal-title">> +
+ + + + +
+ have_posts() ) : ?> +
    + posts as $chrnx_post ) : ?> + +
  1. ID, min( 6, $chrnx_heading + 1 ), 'chrnx-journal-card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
  2. + + +
+ max_num_pages > 1 ) : ?> + $chrnx_topic, + 'chrnx_language' => $chrnx_language, + 's' => $chrnx_search, + ), + 'strlen' + ); + $chrnx_links = paginate_links( + array( + 'total' => $chrnx_query->max_num_pages, + 'current' => $chrnx_args['paged'], + 'add_args' => $chrnx_filter_args, + 'type' => 'list', + ) + ); + ?> + + + + + +

+ +
+ diff --git a/blocks/journal-stream/style.css b/blocks/journal-stream/style.css new file mode 100644 index 0000000..4f2fbfb --- /dev/null +++ b/blocks/journal-stream/style.css @@ -0,0 +1,31 @@ +.chrnx-journal-stream__filters { + display: flex; + flex-wrap: wrap; + gap: 1rem; +} + +.chrnx-journal-stream__filters label { + display: grid; + gap: 0.25rem; +} + +.chrnx-journal-stream__list { + display: grid; + gap: 2rem; + list-style: none; + padding: 0; +} + +.chrnx-journal-card__fallback { + background: var(--wp--preset--color--panel); + display: block; + min-height: 8rem; +} + +.chrnx-journal-stream__pagination .page-numbers { + display: flex; + flex-wrap: wrap; + gap: 0.75rem; + list-style: none; + padding: 0; +} diff --git a/blocks/latest-podcast/block.json b/blocks/latest-podcast/block.json new file mode 100644 index 0000000..10f2629 --- /dev/null +++ b/blocks/latest-podcast/block.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/latest-podcast", + "version": "0.1.0", + "title": "Latest Podcast", + "category": "theme", + "description": "A linked card for the latest podcast episode.", + "textdomain": "chrnx", + "attributes": { + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "style": "file:./style.css", + "render": "file:./render.php" +} diff --git a/blocks/latest-podcast/render.php b/blocks/latest-podcast/render.php new file mode 100644 index 0000000..0fe3c2d --- /dev/null +++ b/blocks/latest-podcast/render.php @@ -0,0 +1,16 @@ + +
'chrnx-latest-podcast' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-label=""> + +
diff --git a/blocks/latest-podcast/style.css b/blocks/latest-podcast/style.css new file mode 100644 index 0000000..ae4dac3 --- /dev/null +++ b/blocks/latest-podcast/style.css @@ -0,0 +1,16 @@ +.chrnx-latest-podcast { + border: 1px solid var(--wp--preset--color--border, currentcolor); + padding: var(--wp--preset--spacing--40, 1.5rem); +} + +.chrnx-podcast-card__image, +.chrnx-podcast-card__fallback { + aspect-ratio: 3 / 2; + display: block; + object-fit: cover; + width: 100%; +} + +.chrnx-podcast-card__fallback { + background: linear-gradient(135deg, var(--wp--preset--color--green), var(--wp--preset--color--violet)); +} diff --git a/blocks/podcast-meta/block.json b/blocks/podcast-meta/block.json new file mode 100644 index 0000000..46acf1e --- /dev/null +++ b/blocks/podcast-meta/block.json @@ -0,0 +1,13 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/podcast-meta", + "version": "0.1.0", + "title": "Podcast Metadata", + "category": "theme", + "description": "Safe episode details for the current post.", + "textdomain": "chrnx", + "usesContext": ["postId", "postType"], + "supports": { "html": false }, + "render": "file:./render.php" +} diff --git a/blocks/podcast-meta/render.php b/blocks/podcast-meta/render.php new file mode 100644 index 0000000..4711920 --- /dev/null +++ b/blocks/podcast-meta/render.php @@ -0,0 +1,30 @@ +context['postId'] ) ? absint( $block->context['postId'] ) : get_the_ID(); +$chrnx_episode = chrnx_sanitize_episode_number( get_post_meta( $chrnx_post_id, 'chrnx_episode_number', true ) ); +$chrnx_duration = chrnx_sanitize_episode_duration( get_post_meta( $chrnx_post_id, 'chrnx_episode_duration', true ) ); +$chrnx_participants = chrnx_sanitize_episode_participants( get_post_meta( $chrnx_post_id, 'chrnx_episode_participants', true ) ); +$chrnx_url = chrnx_sanitize_canonical_episode_url( get_post_meta( $chrnx_post_id, 'canonical_episode_url', true ) ); +if ( 0 === $chrnx_episode && '' === $chrnx_duration && empty( $chrnx_participants ) && '' === $chrnx_url ) { + return; +} +?> +
'chrnx-podcast-meta' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> + 0 ) : ?> +
+ + +
+ + +
+ + +
+ +
diff --git a/blocks/related-posts/block.json b/blocks/related-posts/block.json new file mode 100644 index 0000000..53b0fc3 --- /dev/null +++ b/blocks/related-posts/block.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/related-posts", + "version": "0.1.0", + "title": "Related Posts", + "category": "theme", + "description": "Related stories selected by topic, tags, and recency.", + "textdomain": "chrnx", + "usesContext": ["postId", "postType"], + "attributes": { + "limit": { "type": "integer", "default": 3, "minimum": 1, "maximum": 12 }, + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "render": "file:./render.php" +} diff --git a/blocks/related-posts/render.php b/blocks/related-posts/render.php new file mode 100644 index 0000000..f3064c4 --- /dev/null +++ b/blocks/related-posts/render.php @@ -0,0 +1,25 @@ +context['postId'] ) ? absint( $block->context['postId'] ) : get_the_ID(); +$chrnx_limit = isset( $attributes['limit'] ) ? min( 12, max( 1, absint( $attributes['limit'] ) ) ) : 3; +$chrnx_ids = chrnx_get_related_post_ids( $chrnx_post_id, $chrnx_limit ); +if ( empty( $chrnx_ids ) ) { + return; +} +$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 ); +?> +
'chrnx-related-posts' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-related-posts-title-"> + id="chrnx-related-posts-title-">> + +
diff --git a/blocks/subscribe-cta/block.json b/blocks/subscribe-cta/block.json new file mode 100644 index 0000000..df22698 --- /dev/null +++ b/blocks/subscribe-cta/block.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/subscribe-cta", + "version": "0.1.0", + "title": "Subscribe CTA", + "category": "theme", + "description": "A call to action linked to the configured subscribe page.", + "textdomain": "chrnx", + "attributes": { + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "render": "file:./render.php" +} diff --git a/blocks/subscribe-cta/render.php b/blocks/subscribe-cta/render.php new file mode 100644 index 0000000..6ad66e1 --- /dev/null +++ b/blocks/subscribe-cta/render.php @@ -0,0 +1,18 @@ + +
'chrnx-subscribe-cta' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> + class="chrnx-subscribe-cta__title">> +

+ +
diff --git a/blocks/topic-hero/block.json b/blocks/topic-hero/block.json new file mode 100644 index 0000000..94e8e14 --- /dev/null +++ b/blocks/topic-hero/block.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/topic-hero", + "version": "0.1.0", + "title": "Topic Hero", + "category": "theme", + "description": "A fixed-art hero for an editorial topic.", + "textdomain": "chrnx", + "attributes": { + "topic": { "type": "string", "enum": ["research", "code", "literature", "music", "podcasts", "life", "gaming"] }, + "headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] } + }, + "supports": { "html": false, "align": ["wide", "full"] }, + "render": "file:./render.php" +} diff --git a/blocks/topic-hero/render.php b/blocks/topic-hero/render.php new file mode 100644 index 0000000..c510f50 --- /dev/null +++ b/blocks/topic-hero/render.php @@ -0,0 +1,32 @@ +slug : ''; +} +$chrnx_asset = chrnx_get_topic_art_asset( $chrnx_topic ); +if ( '' === $chrnx_asset ) { + return; +} +$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 ); +$chrnx_term = get_term_by( 'slug', $chrnx_topic, 'chrnx_topic' ); +$chrnx_label = $chrnx_term instanceof WP_Term ? $chrnx_term->name : ucfirst( $chrnx_topic ); +?> +
'chrnx-topic-hero is-topic-' . sanitize_html_class( $chrnx_topic ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> +
+ class="chrnx-topic-hero__title">> +
+
+ + <?php echo esc_attr( $chrnx_label ); ?> + + + +
+
diff --git a/inc/blocks.php b/inc/blocks.php new file mode 100644 index 0000000..7aa7894 --- /dev/null +++ b/inc/blocks.php @@ -0,0 +1,89 @@ + $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' ); + 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' ); diff --git a/inc/queries.php b/inc/queries.php new file mode 100644 index 0000000..30afdcd --- /dev/null +++ b/inc/queries.php @@ -0,0 +1,301 @@ +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 : ''; +} + +/** + * 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' => isset( $request['paged'] ) && is_scalar( $request['paged'] ) ? max( 1, intval( $request['paged'] ) ) : 1, + '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; + } + + if ( isset( $request['s'] ) && is_scalar( $request['s'] ) ) { + $search = sanitize_text_field( wp_unslash( (string) $request['s'] ) ); + $search = mb_substr( trim( $search ), 0, 100, 'UTF-8' ); + 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 ] : ''; +} diff --git a/inc/settings.php b/inc/settings.php new file mode 100644 index 0000000..f9d047c --- /dev/null +++ b/inc/settings.php @@ -0,0 +1,69 @@ + Reading. + * + * @package CHRNX + */ + +/** + * Sanitize the configured subscribe Page ID. + * + * @param mixed $value Submitted option value. + * @return int Editable Page ID or zero. + */ +function chrnx_sanitize_subscribe_page_id( $value ) { + $page_id = absint( $value ); + $page = get_post( $page_id ); + if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || ! current_user_can( 'edit_post', $page_id ) ) { + return 0; + } + return $page_id; +} + +/** Render the subscribe Page dropdown. */ +function chrnx_render_subscribe_page_field() { + wp_dropdown_pages( + array( + 'name' => 'chrnx_subscribe_page_id', + 'selected' => absint( get_option( 'chrnx_subscribe_page_id', 0 ) ), + 'show_option_none' => esc_html__( '— Select a page —', 'chrnx' ), + 'option_none_value' => '0', + ) + ); + echo '

' . esc_html__( 'Choose the published page used by subscription calls to action.', 'chrnx' ) . '

'; +} + +/** Register the subscribe Page setting and Reading field. */ +function chrnx_register_subscribe_setting() { + register_setting( + 'reading', + 'chrnx_subscribe_page_id', + array( + 'type' => 'integer', + 'default' => 0, + 'sanitize_callback' => 'chrnx_sanitize_subscribe_page_id', + ) + ); + add_settings_field( + 'chrnx_subscribe_page_id', + esc_html__( 'Subscribe page', 'chrnx' ), + 'chrnx_render_subscribe_page_field', + 'reading' + ); +} +add_action( 'admin_init', 'chrnx_register_subscribe_setting' ); + +/** + * Return the configured public subscription URL. + * + * @return string Published Page permalink or empty string. + */ +function chrnx_get_subscribe_url(): string { + $page_id = absint( get_option( 'chrnx_subscribe_page_id', 0 ) ); + $page = get_post( $page_id ); + if ( ! $page instanceof WP_Post || 'page' !== $page->post_type || 'publish' !== $page->post_status ) { + return ''; + } + $url = get_permalink( $page ); + return is_string( $url ) ? $url : ''; +} diff --git a/tests/php/BlockRenderingTest.php b/tests/php/BlockRenderingTest.php new file mode 100644 index 0000000..e6cad0e --- /dev/null +++ b/tests/php/BlockRenderingTest.php @@ -0,0 +1,196 @@ +get_registered( 'chrnx/' . $name ); + $this->assertInstanceOf( WP_Block_Type::class, $type, $name ); + $this->assertTrue( is_callable( $type->render_callback ), $name ); + } + } + + /** Topic art is an exact fixed mapping and rejects arbitrary paths. */ + public function test_topic_art_uses_only_the_fixed_mapping() { + $expected = 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', + ); + foreach ( $expected as $slug => $filename ) { + $this->assertSame( 'assets/images/' . $filename, chrnx_get_topic_art_asset( $slug ) ); + } + foreach ( array( '', 'unknown', '../secret.png', 'assets/images/anything.png' ) as $value ) { + $this->assertSame( '', chrnx_get_topic_art_asset( $value ) ); + } + } + + /** Podcast metadata renders known text and HTTPS links, never embed HTML. */ + public function test_podcast_meta_renders_only_known_escaped_fields() { + $post_id = self::factory()->post->create( array( 'post_title' => 'Episode' ) ); + update_post_meta( $post_id, 'chrnx_episode_number', 42 ); + update_post_meta( $post_id, 'chrnx_episode_duration', '1:02:03' ); + update_post_meta( $post_id, 'chrnx_episode_participants', array( 'Ada & Bob', '' ) ); + update_post_meta( $post_id, 'canonical_episode_url', 'https://example.test/episode' ); + update_post_meta( $post_id, 'chrnx_buzzsprout_embed', '' ); + + $output = $this->render_for_post( 'chrnx/podcast-meta', $post_id ); + $this->assertStringContainsString( '42', $output ); + $this->assertStringContainsString( '1:02:03', $output ); + $this->assertStringContainsString( 'Ada & Bob', $output ); + $this->assertStringContainsString( 'https://example.test/episode', $output ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( 'assertSame( 1, substr_count( $output, 'https://example.test/episode' ) ); + } + + /** Invalid/empty metadata is omitted rather than guessed. */ + public function test_podcast_meta_omits_unknown_or_empty_values() { + $post_id = self::factory()->post->create(); + update_post_meta( $post_id, 'canonical_episode_url', 'http://example.test/episode' ); + $output = $this->render_for_post( 'chrnx/podcast-meta', $post_id ); + $this->assertStringNotContainsString( 'http://example.test', $output ); + $this->assertStringNotContainsString( 'Episode 0', $output ); + } + + /** Latest podcast is a card/link and never a player. */ + public function test_latest_podcast_renders_a_linked_card_without_player() { + $post_id = self::factory()->post->create( array( 'post_title' => 'Safe Podcast' ) ); + wp_set_object_terms( $post_id, 'podcasts', 'chrnx_topic' ); + wp_set_object_terms( $post_id, 'silicon-ranch', 'chrnx_show' ); + + $output = render_block( + array( + 'blockName' => 'chrnx/latest-podcast', + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + $this->assertStringContainsString( 'assertStringContainsString( 'Safe Podcast', $output ); + $this->assertStringContainsString( esc_url( get_permalink( $post_id ) ), $output ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( ' 'chrnx/topic-hero', + 'attrs' => array( + 'topic' => 'research', + 'headingLevel' => 2, + ), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + $this->assertStringContainsString( 'assertStringContainsString( 'Research', $output ); + $this->assertStringContainsString( 'aria-hidden="true"', $output ); + $this->assertStringNotContainsString( ' 'chrnx/topic-hero', + 'attrs' => array( 'topic' => '../secret.png' ), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + $this->assertSame( '', $unsafe ); + } + + /** Subscribe CTA is exactly empty until a published Page is configured. */ + public function test_subscribe_cta_requires_a_valid_published_page() { + $block = array( + 'blockName' => 'chrnx/subscribe-cta', + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ); + $this->assertSame( '', render_block( $block ) ); + + $page = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Subscribe', + ) + ); + update_option( 'chrnx_subscribe_page_id', $page ); + $output = render_block( $block ); + $this->assertStringContainsString( 'assertStringContainsString( esc_url( get_permalink( $page ) ), $output ); + } + + /** Featured cards render only real stories and use a CSS image fallback. */ + public function test_featured_stories_have_semantic_variable_cards_and_no_placeholder_images() { + $post_id = self::factory()->post->create( array( 'post_title' => 'Only Story' ) ); + wp_set_object_terms( $post_id, 'code', 'chrnx_topic' ); + $output = render_block( + array( + 'blockName' => 'chrnx/featured-stories', + 'attrs' => array( + 'limit' => 3, + 'headingLevel' => 2, + ), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + + $this->assertSame( 1, substr_count( $output, 'assertStringContainsString( 'Only Story', $output ); + $this->assertStringContainsString( 'aria-hidden="true"', $output ); + $this->assertStringNotContainsString( 'placeholder', strtolower( $output ) ); + } + + /** Render one dynamic post-context block for a specific post. */ + private function render_for_post( $block_name, $post_id ) { + global $post; + $previous = $post; + $post = get_post( $post_id ); + setup_postdata( $post ); + try { + return render_block( + array( + 'blockName' => $block_name, + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + } finally { + wp_reset_postdata(); + $post = $previous; + } + } +} diff --git a/tests/php/EditorialQueriesTest.php b/tests/php/EditorialQueriesTest.php new file mode 100644 index 0000000..79eaef9 --- /dev/null +++ b/tests/php/EditorialQueriesTest.php @@ -0,0 +1,195 @@ +post->create( + array_merge( + array( + 'post_title' => $title, + 'post_status' => 'publish', + 'post_type' => 'post', + 'post_date' => $date, + ), + $extra + ) + ); + if ( '' !== $topic ) { + wp_set_object_terms( $post_id, $topic, 'chrnx_topic' ); + } + return $post_id; + } + + /** Newest eligible sticky is primary and older stickies rejoin chronology. */ + public function test_featured_primary_is_newest_eligible_sticky_and_other_stickies_rejoin_chronology() { + $old_sticky = $this->create_post( 'Old sticky', '2026-01-01 10:00:00', 'research' ); + $regular = $this->create_post( 'Regular', '2026-01-03 10:00:00', 'code' ); + $new_sticky = $this->create_post( 'New sticky', '2026-01-02 10:00:00', 'life' ); + update_option( 'sticky_posts', array( $old_sticky, $new_sticky ) ); + + $this->assertSame( array( $new_sticky, $regular, $old_sticky ), chrnx_get_featured_story_ids( 3 ) ); + } + + /** Secondary selection maximizes distinct topics before filling by date. */ + public function test_featured_secondaries_prefer_distinct_topics_then_fill_by_date() { + $primary = $this->create_post( 'Primary', '2026-01-05 10:00:00', 'research' ); + $same_new = $this->create_post( 'Same newest', '2026-01-04 10:00:00', 'research' ); + $distinct_new = $this->create_post( 'Code', '2026-01-03 10:00:00', 'code' ); + $same_distinct = $this->create_post( 'Code again', '2026-01-02 10:00:00', 'code' ); + $distinct_old = $this->create_post( 'Life', '2026-01-01 10:00:00', 'life' ); + update_option( 'sticky_posts', array( $primary ) ); + + $this->assertSame( array( $primary, $distinct_new, $distinct_old, $same_new ), chrnx_get_featured_story_ids( 4 ) ); + $this->assertNotContains( $same_distinct, array_slice( chrnx_get_featured_story_ids( 4 ), 0, 3 ) ); + } + + /** Eligibility excludes anything not suitable for a public story card. */ + public function test_featured_excludes_ineligible_content_and_never_returns_placeholders() { + $eligible = $this->create_post( 'Eligible', '2026-01-08 10:00:00' ); + $this->create_post( 'Draft', '2026-01-09 10:00:00', '', array( 'post_status' => 'draft' ) ); + $this->create_post( 'Private', '2026-01-10 10:00:00', '', array( 'post_status' => 'private' ) ); + $this->create_post( 'Page', '2026-01-11 10:00:00', '', array( 'post_type' => 'page' ) ); + $this->create_post( 'Trash', '2026-01-12 10:00:00', '', array( 'post_status' => 'trash' ) ); + $this->create_post( ' ', '2026-01-13 10:00:00' ); + + $this->assertSame( array( $eligible ), chrnx_get_featured_story_ids( 8 ) ); + } + + /** No-sticky fallback and ties are deterministic; invalid limits are bounded. */ + public function test_featured_falls_back_to_newest_with_deterministic_ties_and_bounded_limits() { + $older = $this->create_post( 'Older', '2026-01-01 10:00:00' ); + $tie_a = $this->create_post( 'Tie A', '2026-01-02 10:00:00' ); + $tie_b = $this->create_post( 'Tie B', '2026-01-02 10:00:00' ); + $actual = chrnx_get_featured_story_ids( 3 ); + + $this->assertSame( array( max( $tie_a, $tie_b ), min( $tie_a, $tie_b ), $older ), $actual ); + $this->assertSame( array(), chrnx_get_featured_story_ids( 0 ) ); + $this->assertLessThanOrEqual( 12, count( chrnx_get_featured_story_ids( 999 ) ) ); + } + + /** Podcast surfaces require both the podcast topic and an assigned show. */ + public function test_latest_podcast_requires_topic_and_any_registered_show() { + $without_show = $this->create_post( 'Podcast without show', '2026-02-03 10:00:00', 'podcasts' ); + $with_show = $this->create_post( 'Podcast with show', '2026-02-02 10:00:00', 'podcasts' ); + $wrong_topic = $this->create_post( 'Show but not podcast', '2026-02-04 10:00:00', 'code' ); + $empty_title = $this->create_post( ' ', '2026-02-05 10:00:00', 'podcasts' ); + wp_set_object_terms( $with_show, 'silicon-ranch', 'chrnx_show' ); + wp_set_object_terms( $wrong_topic, 'silicon-ranch', 'chrnx_show' ); + wp_set_object_terms( $empty_title, 'silicon-ranch', 'chrnx_show' ); + + $this->assertSame( $with_show, chrnx_get_latest_podcast_id() ); + $journal_ids = get_posts( + chrnx_get_journal_query_args( array() ) + array( + 'fields' => 'ids', + 'posts_per_page' => 20, + ) + ); + $this->assertContains( $without_show, $journal_ids ); + $this->assertNotContains( $empty_title, $journal_ids ); + } + + /** Show queries fail closed unless the slug names a registered existing show. */ + public function test_show_query_validates_slug_and_silicon_ranch_membership() { + $member = $this->create_post( 'Member', '2026-02-02 10:00:00', 'podcasts' ); + $outsider = $this->create_post( 'Outsider', '2026-02-03 10:00:00', 'podcasts' ); + $untitled = $this->create_post( ' ', '2026-02-04 10:00:00', 'podcasts' ); + wp_set_object_terms( $member, 'silicon-ranch', 'chrnx_show' ); + wp_set_object_terms( $untitled, 'silicon-ranch', 'chrnx_show' ); + + $this->assertSame( array( $member ), get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) ); + foreach ( array( 'unknown', '../silicon-ranch', '', "silicon-ranch' OR 1=1" ) as $slug ) { + $args = chrnx_get_show_query_args( $slug ); + $this->assertSame( array( 0 ), $args['post__in'] ); + $this->assertSame( array(), get_posts( $args + array( 'fields' => 'ids' ) ) ); + } + $this->assertNotContains( $outsider, get_posts( chrnx_get_show_query_args( 'silicon-ranch' ) + array( 'fields' => 'ids' ) ) ); + } + + /** Journal filters are term-whitelisted, normalized, and safe WP_Query arguments. */ + public function test_journal_query_sanitizes_and_preserves_valid_filters() { + $args = chrnx_get_journal_query_args( + array( + 'chrnx_topic' => 'code', + 'chrnx_language' => 'es', + 'paged' => '3', + 's' => " hello\nworld ", + ) + ); + + $this->assertSame( 'post', $args['post_type'] ); + $this->assertSame( 'publish', $args['post_status'] ); + $this->assertSame( + array( + 'date' => 'DESC', + 'ID' => 'DESC', + ), + $args['orderby'] + ); + $this->assertSame( 3, $args['paged'] ); + $this->assertSame( 'hello world', $args['s'] ); + $this->assertCount( 2, $args['tax_query'] ); + $this->assertSame( 'code', $args['tax_query'][0]['terms'] ); + $this->assertSame( 'es', $args['tax_query'][1]['terms'] ); + } + + /** Malformed journal request values are ignored and never copied into query args. */ + public function test_journal_query_ignores_malformed_filters_and_caps_search() { + $args = chrnx_get_journal_query_args( + array( + 'chrnx_topic' => '../code', + 'chrnx_language' => array( 'es' ), + 'paged' => '-200', + 's' => str_repeat( '', 50 ), + ) + ); + + $this->assertArrayNotHasKey( 'tax_query', $args ); + $this->assertSame( 1, $args['paged'] ); + $search = $args['s'] ?? ''; + $this->assertLessThanOrEqual( 100, mb_strlen( $search, 'UTF-8' ) ); + $this->assertStringNotContainsString( '<', $search ); + } + + /** Related stories tier by topic, then tags, then recency without duplicates. */ + public function test_related_posts_use_ordered_deduplicated_tiers_and_eligibility() { + $current = $this->create_post( 'Current', '2026-03-05 10:00:00', 'research' ); + $same_topic = $this->create_post( 'Same topic', '2026-03-01 10:00:00', 'research' ); + $shared_tag = $this->create_post( 'Shared tag', '2026-03-04 10:00:00', 'code' ); + $recent = $this->create_post( 'Recent', '2026-03-03 10:00:00', 'life' ); + $tag_id = self::factory()->tag->create(); + wp_set_post_tags( $current, array( $tag_id ) ); + wp_set_post_tags( $same_topic, array( $tag_id ) ); + wp_set_post_tags( $shared_tag, array( $tag_id ) ); + $this->create_post( 'Draft related', '2026-03-06 10:00:00', 'research', array( 'post_status' => 'draft' ) ); + + $this->assertSame( array( $same_topic, $shared_tag, $recent ), chrnx_get_related_post_ids( $current, 3 ) ); + $this->assertSame( array(), chrnx_get_related_post_ids( $current, 0 ) ); + $this->assertSame( array(), chrnx_get_related_post_ids( 999999, 3 ) ); + } +} diff --git a/tests/php/SettingsTest.php b/tests/php/SettingsTest.php new file mode 100644 index 0000000..b35c13e --- /dev/null +++ b/tests/php/SettingsTest.php @@ -0,0 +1,106 @@ +assertTrue( function_exists( 'chrnx_register_subscribe_setting' ) ); + $registered = get_registered_settings(); + + $this->assertSame( 'integer', $registered['chrnx_subscribe_page_id']['type'] ); + $this->assertSame( 0, call_user_func( $registered['chrnx_subscribe_page_id']['sanitize_callback'], 12345 ) ); + $this->assertArrayHasKey( 'chrnx_subscribe_page_id', $wp_settings_fields['reading']['default'] ); + } + + /** Sanitizer accepts only a page editable by the current user. */ + public function test_subscribe_setting_sanitizer_requires_an_editable_page() { + $admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $page = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + $post = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + ) + ); + + wp_set_current_user( $admin ); + $this->assertSame( $page, chrnx_sanitize_subscribe_page_id( $page ) ); + $this->assertSame( 0, chrnx_sanitize_subscribe_page_id( $post ) ); + + wp_set_current_user( 0 ); + $this->assertSame( 0, chrnx_sanitize_subscribe_page_id( $page ) ); + } + + /** Public helper returns only a published configured Page permalink. */ + public function test_subscribe_url_requires_a_published_page() { + $admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $page = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + $this->assertSame( '', chrnx_get_subscribe_url() ); + + wp_set_current_user( $admin ); + update_option( 'chrnx_subscribe_page_id', $page ); + $this->assertSame( get_permalink( $page ), chrnx_get_subscribe_url() ); + + wp_update_post( + array( + 'ID' => $page, + 'post_status' => 'draft', + ) + ); + $this->assertSame( '', chrnx_get_subscribe_url() ); + wp_delete_post( $page, true ); + $this->assertSame( '', chrnx_get_subscribe_url() ); + + $post = self::factory()->post->create( + array( + 'post_type' => 'post', + 'post_status' => 'publish', + ) + ); + update_option( 'chrnx_subscribe_page_id', $post ); + $this->assertSame( '', chrnx_get_subscribe_url() ); + } + + /** Field callback uses the core page dropdown and escaped description. */ + public function test_subscribe_field_renders_a_core_page_dropdown() { + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + ob_start(); + chrnx_render_subscribe_page_field(); + $output = ob_get_clean(); + + $this->assertMatchesRegularExpression( '/name=[\'\"]chrnx_subscribe_page_id[\'\"]/', $output ); + $this->assertStringContainsString( 'assertStringContainsString( 'description', $output ); + } +} diff --git a/tests/php/TaxonomiesTest.php b/tests/php/TaxonomiesTest.php index 08e489b..1b9f486 100644 --- a/tests/php/TaxonomiesTest.php +++ b/tests/php/TaxonomiesTest.php @@ -168,6 +168,49 @@ class TaxonomiesTest extends WP_UnitTestCase { $this->assertSame( '', ob_get_clean(), 'The notice should be consumed after one render.' ); } + /** + * Appending a second editorial term still enforces the one-term contract. + */ + public function test_appending_an_editorial_term_normalizes_the_final_assignment() { + $post_id = self::factory()->post->create(); + $first = self::factory()->term->create( + array( + 'taxonomy' => 'chrnx_topic', + 'name' => 'Append first', + ) + ); + $second = self::factory()->term->create( + array( + 'taxonomy' => 'chrnx_topic', + 'name' => 'Append second', + ) + ); + + wp_set_object_terms( $post_id, array( $second ), 'chrnx_topic' ); + wp_set_object_terms( $post_id, array( $first ), 'chrnx_topic', true ); + + $this->assertSame( + array( min( $first, $second ) ), + wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) + ); + } + + /** + * Empty assignment and explicit deletion remain empty without synthetic terms. + */ + public function test_empty_assignment_and_term_deletion_remain_empty() { + $post_id = self::factory()->post->create(); + $term_id = self::factory()->term->create( array( 'taxonomy' => 'chrnx_topic' ) ); + + wp_set_object_terms( $post_id, array( $term_id ), 'chrnx_topic' ); + wp_set_object_terms( $post_id, array(), 'chrnx_topic' ); + $this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) ); + + wp_set_object_terms( $post_id, array( $term_id ), 'chrnx_topic' ); + wp_delete_object_term_relationships( $post_id, 'chrnx_topic' ); + $this->assertSame( array(), wp_get_object_terms( $post_id, 'chrnx_topic', array( 'fields' => 'ids' ) ) ); + } + /** * A nested assignment for another post in the same taxonomy still normalizes. */