feat: add editorial query blocks

This commit is contained in:
chrnx committed 2026-08-11 15:27:44 -05:00
1 parent 835fb74ebf
commit 89dcc53e02
24 files changed
+1421

No files matched your search

+17
View File
@@ -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"
}
+23
View File
@@ -0,0 +1,23 @@
<?php
/**
* Featured Stories block render.
*
* @package CHRNX
*/
$chrnx_limit = isset( $attributes['limit'] ) ? min( 12, max( 1, absint( $attributes['limit'] ) ) ) : 3;
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
$chrnx_ids = chrnx_get_featured_story_ids( $chrnx_limit );
if ( empty( $chrnx_ids ) ) {
return;
}
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-featured-stories' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-label="<?php echo esc_attr__( 'Featured stories', 'chrnx' ); ?>">
<div class="chrnx-featured-stories__grid">
<?php
foreach ( $chrnx_ids as $chrnx_post_id ) {
echo chrnx_get_editorial_card_markup( $chrnx_post_id, $chrnx_heading, 'chrnx-featured-story' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
</div>
</section>
+27
View File
@@ -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));
}
+16
View File
@@ -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"
}
+94
View File
@@ -0,0 +1,94 @@
<?php
/**
* Journal Stream block render.
*
* @package CHRNX
*/
// Read-only filters do not change server state; every value is validated by the query service.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$chrnx_request = is_array( $_GET ) ? $_GET : array();
$chrnx_args = chrnx_get_journal_query_args( $chrnx_request );
$chrnx_query = new WP_Query( $chrnx_args );
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
$chrnx_topic = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_topic'] ?? '', 'chrnx_topic' );
$chrnx_language = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_language'] ?? '', 'chrnx_language' );
$chrnx_search = $chrnx_args['s'] ?? '';
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-journal-title">
<h<?php echo esc_attr( $chrnx_heading ); ?> id="chrnx-journal-title"><?php echo esc_html__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<form class="chrnx-journal-stream__filters" method="get" action="<?php echo esc_url( remove_query_arg( array( 'chrnx_topic', 'chrnx_language', 's', 'paged' ) ) ); ?>">
<label>
<span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span>
<select name="chrnx_topic">
<option value=""><?php echo esc_html__( 'All topics', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_topic',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_topic, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<label>
<span><?php echo esc_html__( 'Language', 'chrnx' ); ?></span>
<select name="chrnx_language">
<option value=""><?php echo esc_html__( 'All languages', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_language',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_language, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<label>
<span><?php echo esc_html__( 'Search', 'chrnx' ); ?></span>
<input type="search" name="s" value="<?php echo esc_attr( $chrnx_search ); ?>">
</label>
<button type="submit"><?php echo esc_html__( 'Filter', 'chrnx' ); ?></button>
</form>
<?php if ( $chrnx_query->have_posts() ) : ?>
<ol class="chrnx-journal-stream__list">
<?php foreach ( $chrnx_query->posts as $chrnx_post ) : ?>
<?php if ( chrnx_is_eligible_editorial_post( $chrnx_post ) ) : ?>
<li><?php echo chrnx_get_editorial_card_markup( $chrnx_post->ID, min( 6, $chrnx_heading + 1 ), 'chrnx-journal-card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
<?php if ( $chrnx_query->max_num_pages > 1 ) : ?>
<?php
$chrnx_filter_args = array_filter(
array(
'chrnx_topic' => $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',
)
);
?>
<?php if ( is_string( $chrnx_links ) ) : ?>
<nav class="chrnx-journal-stream__pagination" aria-label="<?php echo esc_attr__( 'Journal pages', 'chrnx' ); ?>"><?php echo wp_kses_post( $chrnx_links ); ?></nav>
<?php endif; ?>
<?php endif; ?>
<?php else : ?>
<p class="chrnx-journal-stream__empty"><?php echo esc_html__( 'No journal entries match these filters yet.', 'chrnx' ); ?></p>
<?php endif; ?>
</section>
<?php wp_reset_postdata(); ?>
+31
View File
@@ -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;
}
+16
View File
@@ -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"
}
+16
View File
@@ -0,0 +1,16 @@
<?php
/**
* Latest Podcast block render.
*
* @package CHRNX
*/
$chrnx_post_id = chrnx_get_latest_podcast_id();
if ( 0 === $chrnx_post_id ) {
return;
}
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-latest-podcast' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-label="<?php echo esc_attr__( 'Latest podcast', 'chrnx' ); ?>">
<?php echo chrnx_get_editorial_card_markup( $chrnx_post_id, $chrnx_heading, 'chrnx-podcast-card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</section>
+16
View File
@@ -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));
}
+13
View File
@@ -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"
}
+30
View File
@@ -0,0 +1,30 @@
<?php
/**
* Podcast Metadata block render.
*
* @package CHRNX
*/
$chrnx_post_id = isset( $block->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;
}
?>
<dl <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-podcast-meta' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php if ( $chrnx_episode > 0 ) : ?>
<div class="chrnx-podcast-meta__item"><dt><?php echo esc_html__( 'Episode', 'chrnx' ); ?></dt><dd><?php echo esc_html( number_format_i18n( $chrnx_episode ) ); ?></dd></div>
<?php endif; ?>
<?php if ( '' !== $chrnx_duration ) : ?>
<div class="chrnx-podcast-meta__item"><dt><?php echo esc_html__( 'Duration', 'chrnx' ); ?></dt><dd><?php echo esc_html( $chrnx_duration ); ?></dd></div>
<?php endif; ?>
<?php if ( ! empty( $chrnx_participants ) ) : ?>
<div class="chrnx-podcast-meta__item"><dt><?php echo esc_html__( 'Participants', 'chrnx' ); ?></dt><dd><?php echo esc_html( implode( ', ', $chrnx_participants ) ); ?></dd></div>
<?php endif; ?>
<?php if ( '' !== $chrnx_url ) : ?>
<div class="chrnx-podcast-meta__item"><dt><?php echo esc_html__( 'Episode page', 'chrnx' ); ?></dt><dd><a href="<?php echo esc_url( $chrnx_url ); ?>"><?php echo esc_html__( 'Open canonical episode page', 'chrnx' ); ?></a></dd></div>
<?php endif; ?>
</dl>
+17
View File
@@ -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"
}
+25
View File
@@ -0,0 +1,25 @@
<?php
/**
* Related Posts block render.
*
* @package CHRNX
*/
$chrnx_post_id = isset( $block->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 );
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-related-posts' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-related-posts-title-<?php echo esc_attr( $chrnx_post_id ); ?>">
<h<?php echo esc_attr( $chrnx_heading ); ?> id="chrnx-related-posts-title-<?php echo esc_attr( $chrnx_post_id ); ?>"><?php echo esc_html__( 'Related stories', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<div class="chrnx-related-posts__grid">
<?php
foreach ( $chrnx_ids as $chrnx_related_id ) {
echo chrnx_get_editorial_card_markup( $chrnx_related_id, min( 6, $chrnx_heading + 1 ), 'chrnx-related-card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
</div>
</section>
+15
View File
@@ -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"
}
+18
View File
@@ -0,0 +1,18 @@
<?php
/**
* Subscribe CTA block render.
*
* @package CHRNX
*/
$chrnx_url = chrnx_get_subscribe_url();
if ( '' === $chrnx_url ) {
return;
}
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-subscribe-cta' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<h<?php echo esc_attr( $chrnx_heading ); ?> class="chrnx-subscribe-cta__title"><?php echo esc_html__( 'Stay in the loop', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<p><?php echo esc_html__( 'Get new stories and episodes when they are published.', 'chrnx' ); ?></p>
<a class="chrnx-subscribe-cta__link" href="<?php echo esc_url( $chrnx_url ); ?>"><?php echo esc_html__( 'Subscribe', 'chrnx' ); ?></a>
</section>
+16
View File
@@ -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"
}
+32
View File
@@ -0,0 +1,32 @@
<?php
/**
* Topic Hero block render.
*
* @package CHRNX
*/
$chrnx_topic = isset( $attributes['topic'] ) && is_string( $attributes['topic'] ) ? $attributes['topic'] : '';
if ( '' === $chrnx_topic && is_tax( 'chrnx_topic' ) ) {
$chrnx_queried = get_queried_object();
$chrnx_topic = $chrnx_queried instanceof WP_Term ? $chrnx_queried->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 );
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-topic-hero is-topic-' . sanitize_html_class( $chrnx_topic ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<div class="chrnx-topic-hero__content">
<h<?php echo esc_attr( $chrnx_heading ); ?> class="chrnx-topic-hero__title"><?php echo esc_html( $chrnx_label ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
</div>
<div class="chrnx-topic-hero__art">
<?php if ( is_readable( get_theme_file_path( $chrnx_asset ) ) ) : ?>
<img src="<?php echo esc_url( get_theme_file_uri( $chrnx_asset ) ); ?>" alt="<?php echo esc_attr( $chrnx_label ); ?>">
<?php else : ?>
<span class="chrnx-topic-hero__fallback" aria-hidden="true"></span>
<?php endif; ?>
</div>
</section>
+89
View File
@@ -0,0 +1,89 @@
<?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;
}
/**
* 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 '<span class="' . esc_attr( $class ) . '" aria-hidden="true"></span>';
}
/**
* 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 = '<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' ) . '">';
if ( '' !== $topic_label ) {
$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' );
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' );
+301
View File
@@ -0,0 +1,301 @@
<?php
/**
* Editorial query services.
*
* @package CHRNX
*/
/**
* Whether a post is eligible for public editorial surfaces.
*
* @param int|WP_Post $post Post object or ID.
* @return bool Whether the post is eligible.
*/
function chrnx_is_eligible_editorial_post( $post ) {
$post = get_post( $post );
return $post instanceof WP_Post
&& 'post' === $post->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<string,mixed> $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<string,mixed> 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<string,mixed> 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 ] : '';
}
+69
View File
@@ -0,0 +1,69 @@
<?php
/**
* Theme settings on Settings > 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 '<p class="description">' . esc_html__( 'Choose the published page used by subscription calls to action.', 'chrnx' ) . '</p>';
}
/** 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 : '';
}
+196
View File
@@ -0,0 +1,196 @@
<?php
/**
* Tests for server-rendered editorial blocks.
*
* @package CHRNX
*/
/** Verify block registration, fixed art, and safe dynamic rendering. */
class BlockRenderingTest extends WP_UnitTestCase {
/** Register dependencies and blocks. */
public function set_up() {
parent::set_up();
do_action( 'init' );
chrnx_seed_editorial_terms();
delete_option( 'chrnx_subscribe_page_id' );
}
/** All seven block types register from metadata. */
public function test_all_editorial_blocks_are_registered() {
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta' );
foreach ( $names as $name ) {
$type = WP_Block_Type_Registry::get_instance()->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', '<script>alert(1)</script>' ) );
update_post_meta( $post_id, 'canonical_episode_url', 'https://example.test/episode' );
update_post_meta( $post_id, 'chrnx_buzzsprout_embed', '<iframe src="https://evil.test"></iframe>' );
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
$this->assertStringContainsString( '42', $output );
$this->assertStringContainsString( '1:02:03', $output );
$this->assertStringContainsString( 'Ada &amp; Bob', $output );
$this->assertStringContainsString( 'https://example.test/episode', $output );
$this->assertStringNotContainsString( '<script', $output );
$this->assertStringNotContainsString( '<iframe', $output );
$this->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( '<article', $output );
$this->assertStringContainsString( 'Safe Podcast', $output );
$this->assertStringContainsString( esc_url( get_permalink( $post_id ) ), $output );
$this->assertStringNotContainsString( '<audio', $output );
$this->assertStringNotContainsString( '<iframe', $output );
}
/** Topic hero uses a mapped explicit topic and falls back accessibly if art is absent. */
public function test_topic_hero_rejects_paths_and_uses_css_fallback_for_missing_art() {
$output = render_block(
array(
'blockName' => 'chrnx/topic-hero',
'attrs' => array(
'topic' => 'research',
'headingLevel' => 2,
),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringContainsString( '<h2', $output );
$this->assertStringContainsString( 'Research', $output );
$this->assertStringContainsString( 'aria-hidden="true"', $output );
$this->assertStringNotContainsString( '<img', $output );
$unsafe = render_block(
array(
'blockName' => '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( '<section', $output );
$this->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, '<article' ) );
$this->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;
}
}
}
+195
View File
@@ -0,0 +1,195 @@
<?php
/**
* Tests for editorial query services.
*
* @package CHRNX
*/
/**
* Verify editorial selection and query argument contracts.
*/
class EditorialQueriesTest extends WP_UnitTestCase {
/**
* Register and seed editorial taxonomies.
*/
public function set_up() {
parent::set_up();
do_action( 'init' );
chrnx_seed_editorial_terms();
update_option( 'sticky_posts', array() );
}
/**
* Create a dated post and optionally assign one topic.
*
* @param string $title Post title.
* @param string $date GMT-compatible post date.
* @param string $topic Topic slug.
* @param array $extra Extra factory arguments.
* @return int Post ID.
*/
private function create_post( $title, $date, $topic = '', $extra = array() ) {
$post_id = self::factory()->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( '<script>x</script>', 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 ) );
}
}
+106
View File
@@ -0,0 +1,106 @@
<?php
/**
* Tests for Reading settings.
*
* @package CHRNX
*/
/** Verify the subscribe page setting and public URL helper. */
class SettingsTest extends WP_UnitTestCase {
/** Reset state before each test. */
public function set_up() {
parent::set_up();
delete_option( 'chrnx_subscribe_page_id' );
wp_set_current_user( 0 );
if ( function_exists( 'chrnx_register_subscribe_setting' ) ) {
chrnx_register_subscribe_setting();
}
}
/** Setting registration uses an integer sanitizer and a Reading field. */
public function test_subscribe_setting_is_registered_on_reading_screen() {
global $wp_settings_fields;
$this->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( '<select', $output );
$this->assertStringContainsString( 'description', $output );
}
}
+43
View File
@@ -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.
*/