feat: model projects as posts with a picked Selected work

Projects were hardcoded h3 elements in a pattern: no URL, no feed, no
search, uneditable without touching PHP. They follow the podcast model
now, a chrnx_topic term plus posts.

Adds a chrnx_project_featured flag with an editor toggle so the stage
lists chosen projects first and fills with the most recent. Sticky was
already spoken for by the home featured stories, so the choice needed
its own signal. journal-stream gains a pinned topic attribute, reusing
the archive filter, pagination and empty state it already had.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-08-13 19:41:24 -05:00
1 parent a3ba692669
commit f55d9d9b45
14 files changed
+539 -31

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
( function ( factory ) {
if ( typeof module === 'object' && module.exports ) {
module.exports = factory();
} else {
factory().initialize( window.wp );
}
} )( function () {
'use strict';
function getTranslator( wp ) {
return wp.i18n && typeof wp.i18n.__ === 'function'
? wp.i18n.__
: function ( text ) {
return text;
};
}
function updateMeta( editPost, currentMeta, key, value ) {
if ( typeof editPost !== 'function' ) {
return;
}
editPost( { meta: { ...( currentMeta || {} ), [ key ]: value } } );
}
function hasRequiredGlobals( wp ) {
return Boolean(
wp &&
wp.components &&
wp.components.ToggleControl &&
wp.data &&
typeof wp.data.useDispatch === 'function' &&
typeof wp.data.useSelect === 'function' &&
wp.editPost &&
wp.editPost.PluginDocumentSettingPanel &&
wp.element &&
typeof wp.element.createElement === 'function' &&
wp.plugins &&
typeof wp.plugins.registerPlugin === 'function',
);
}
function initialize( wp ) {
if ( ! hasRequiredGlobals( wp ) ) {
return false;
}
const __ = getTranslator( wp );
function ProjectPanel() {
const editorState = wp.data.useSelect( function ( select ) {
if ( typeof select !== 'function' ) {
return null;
}
const currentEditor = select( 'core/editor' );
const core = select( 'core' );
if (
! currentEditor ||
typeof currentEditor.getCurrentPostType !== 'function' ||
typeof currentEditor.getEditedPostAttribute !== 'function' ||
! core ||
typeof core.getEntityRecord !== 'function'
) {
return null;
}
const topicIds = currentEditor.getEditedPostAttribute( 'topic' );
return {
meta: currentEditor.getEditedPostAttribute( 'meta' ) || {},
postType: currentEditor.getCurrentPostType(),
topic:
Array.isArray( topicIds ) && topicIds.length === 1
? core.getEntityRecord( 'taxonomy', 'chrnx_topic', topicIds[ 0 ] )
: null,
};
}, [] );
const editorDispatch = wp.data.useDispatch( 'core/editor' );
if ( ! editorState || editorState.postType !== 'post' || ! editorDispatch?.editPost ) {
return null;
}
if ( ! editorState.topic || editorState.topic.slug !== 'projects' ) {
return null;
}
const meta = editorState.meta;
return wp.element.createElement(
wp.editPost.PluginDocumentSettingPanel,
{ name: 'chrnx-project', title: __( 'Project', 'chrnx' ) },
wp.element.createElement( wp.components.ToggleControl, {
label: __( 'Show in Selected work', 'chrnx' ),
help: __(
'Selected work lists these first, then fills the rest with the most recent projects.',
'chrnx',
),
checked: Boolean( meta.chrnx_project_featured ),
onChange( value ) {
updateMeta( editorDispatch.editPost, meta, 'chrnx_project_featured', Boolean( value ) );
},
} ),
);
}
wp.plugins.registerPlugin( 'chrnx-project-meta', { render: ProjectPanel } );
return true;
}
return { initialize, updateMeta };
} );
+3 -2
View File
@@ -2,13 +2,14 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "chrnx/journal-stream",
"version": "0.1.0",
"version": "0.2.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] }
"headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] },
"topic": { "type": "string", "default": "" }
},
"supports": { "html": false, "align": ["wide", "full"] },
"style": "file:./style.css",
+11 -3
View File
@@ -5,9 +5,15 @@
* @package CHRNX
*/
// A pinned topic reuses the taxonomy-archive path: same fixed-filter UI, same
// hidden input, same clear link, without needing an archive route.
$chrnx_pinned_slug = chrnx_validate_editorial_term_slug( $attributes['topic'] ?? '', 'chrnx_topic' );
$chrnx_pinned_term = '' !== $chrnx_pinned_slug ? get_term_by( 'slug', $chrnx_pinned_slug, 'chrnx_topic' ) : null;
$chrnx_pinned_term = $chrnx_pinned_term instanceof WP_Term ? $chrnx_pinned_term : null;
// Read-only filters do not change server state; every value is validated by the query service.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$chrnx_request = chrnx_get_journal_request_context( is_array( $_GET ) ? $_GET : array() );
$chrnx_request = chrnx_get_journal_request_context( is_array( $_GET ) ? $_GET : array(), $chrnx_pinned_term );
$chrnx_args = chrnx_get_journal_query_args( $chrnx_request );
$chrnx_query = chrnx_run_eligible_query( $chrnx_args );
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
@@ -17,10 +23,12 @@ $chrnx_show = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_show
$chrnx_search = $chrnx_args['s'] ?? '';
$chrnx_form_url = chrnx_get_journal_base_url();
$chrnx_title_id = wp_unique_id( 'chrnx-journal-title-' );
$chrnx_fixed = chrnx_get_journal_archive_filter();
$chrnx_fixed = chrnx_get_journal_archive_filter( $chrnx_pinned_term );
// A pinned stream is not the Journal; it is that topic's list, so it says so.
$chrnx_title = null !== $chrnx_pinned_term ? $chrnx_pinned_term->name : __( 'Journal', 'chrnx' );
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>">
<h<?php echo esc_attr( $chrnx_heading ); ?> id="<?php echo esc_attr( $chrnx_title_id ); ?>"><?php echo esc_html__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<h<?php echo esc_attr( $chrnx_heading ); ?> id="<?php echo esc_attr( $chrnx_title_id ); ?>"><?php echo esc_html( $chrnx_title ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<form class="chrnx-journal-stream__filters" method="get" action="<?php echo esc_url( $chrnx_form_url ); ?>">
<?php if ( isset( $chrnx_fixed['taxonomy'] ) && 'chrnx_topic' === $chrnx_fixed['taxonomy'] ) : ?>
<p class="chrnx-journal-stream__fixed-filter"><span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span> <strong><?php echo esc_html( $chrnx_fixed['label'] ); ?></strong></p>
+16
View File
@@ -0,0 +1,16 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "chrnx/selected-work",
"version": "0.2.0",
"title": "Selected Work",
"category": "theme",
"description": "Hand-picked projects first, then the most recent ones.",
"textdomain": "chrnx",
"attributes": {
"limit": { "type": "integer", "default": 3, "minimum": 1, "maximum": 12 },
"headingLevel": { "type": "integer", "default": 3, "enum": [2, 3, 4, 5, 6] }
},
"supports": { "html": false },
"render": "file:./render.php"
}
+26
View File
@@ -0,0 +1,26 @@
<?php
/**
* Selected Work 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'] ?? 3, 3 );
$chrnx_ids = chrnx_get_selected_project_ids( $chrnx_limit );
?>
<div <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-selected-work' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php if ( empty( $chrnx_ids ) ) : ?>
<p class="chrnx-selected-work__empty"><?php echo esc_html__( 'No projects published yet.', 'chrnx' ); ?></p>
<?php else : ?>
<ul class="chrnx-selected-work__list">
<?php foreach ( $chrnx_ids as $chrnx_project_id ) : ?>
<li class="chrnx-selected-work__item">
<h<?php echo esc_attr( $chrnx_heading ); ?> class="chrnx-selected-work__title">
<a href="<?php echo esc_url( get_permalink( $chrnx_project_id ) ); ?>"><?php echo esc_html( get_the_title( $chrnx_project_id ) ); ?></a>
</h<?php echo esc_attr( $chrnx_heading ); ?>>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
+2 -2
View File
@@ -1,8 +1,8 @@
<?php
define( 'CHRNX_VERSION', '0.1.0' );
define( 'CHRNX_VERSION', '0.2.0' );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
foreach ( array( 'setup', 'assets', 'taxonomies', 'editor', 'podcast-meta', 'settings', 'queries', 'media', 'blocks', 'theme-preference' ) as $module ) {
foreach ( array( 'setup', 'assets', 'taxonomies', 'editor', 'podcast-meta', 'project-meta', 'release-meta', 'track-meta', 'settings', 'queries', 'media', 'blocks', 'theme-preference' ) as $module ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$path = get_theme_file_path( "inc/{$module}.php" );
if ( file_exists( $path ) ) {
+104 -7
View File
@@ -95,9 +95,106 @@ function chrnx_get_card_media_markup( $post_id, $class_name ) {
array( 'class' => $class_name . '__image' )
);
}
// A track with no art of its own borrows its album cover before falling back.
$release = function_exists( 'chrnx_get_track_release' ) ? chrnx_get_track_release( $post_id ) : null;
if ( null !== $release ) {
$cover_id = absint( get_term_meta( $release->term_id, 'chrnx_release_cover', true ) );
if ( $cover_id > 0 ) {
return wp_get_attachment_image(
$cover_id,
'chrnx-card',
false,
array( 'class' => $class_name . '__image' )
);
}
}
$topic = chrnx_get_post_topic_slug( $post_id );
$class = $class_name . '__fallback' . ( '' !== $topic ? ' is-topic-' . sanitize_html_class( $topic ) : '' );
return '<span class="' . esc_attr( $class ) . '" aria-hidden="true"></span>';
return chrnx_get_topic_portrait_markup( 'placeholder-post', '', $class );
}
/**
* Return the navigation links for one theme menu location.
*
* The built-in list is a fallback, not a contract: a site whose pages are named
* differently would otherwise get a header full of 404s.
*
* @param string $location Registered menu location.
* @return array<int,array<string,string>> Label and URL pairs.
*/
function chrnx_get_navigation_links( $location ) {
$assigned = get_nav_menu_locations();
$menu_id = isset( $assigned[ $location ] ) ? absint( $assigned[ $location ] ) : 0;
if ( $menu_id > 0 ) {
$items = wp_get_nav_menu_items( $menu_id );
if ( is_array( $items ) ) {
$links = array();
foreach ( $items as $item ) {
// Only top-level entries: the header is a single flat row.
if ( absint( $item->menu_item_parent ) > 0 ) {
continue;
}
$url = esc_url_raw( (string) $item->url );
$label = trim( wp_strip_all_tags( (string) $item->title ) );
if ( '' !== $url && '' !== $label ) {
$links[] = array(
'label' => $label,
'url' => $url,
);
}
}
if ( ! empty( $links ) ) {
return $links;
}
}
}
$defaults = array( 'journal', 'projects', 'music', 'podcasts', 'about' );
$labels = array(
'journal' => __( 'Journal', 'chrnx' ),
'projects' => __( 'Projects', 'chrnx' ),
'music' => __( 'Music', 'chrnx' ),
'podcasts' => __( 'Podcasts', 'chrnx' ),
'about' => __( 'About', 'chrnx' ),
);
$links = array();
foreach ( $defaults as $slug ) {
$links[] = array(
'label' => $labels[ $slug ],
'url' => home_url( '/' . $slug . '/' ),
);
}
return $links;
}
/**
* Render one allowlisted interface icon.
*
* Unicode glyphs render inconsistently across platforms and fonts, so every
* control icon comes from this one place instead.
*
* @param string $name Allowlisted icon name.
* @param int $size Square pixel size.
* @return string Escaped inline SVG, or an empty string for unknown names.
*/
function chrnx_get_icon_markup( $name, $size = 20 ) {
$icons = array(
'search' => '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.6-3.6"/>',
'menu' => '<path d="M4 7h16M4 12h16M4 17h16"/>',
'theme' => '<circle cx="12" cy="12" r="9"/><path d="M12 3a9 9 0 0 1 0 18Z" fill="currentColor" stroke="none"/>',
);
if ( ! isset( $icons[ $name ] ) ) {
return '';
}
return sprintf(
// The path data is a fixed literal, never user input.
'<svg viewBox="0 0 24 24" width="%1$d" height="%1$d" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">%2$s</svg>',
absint( $size ),
$icons[ $name ] // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
/**
@@ -115,16 +212,16 @@ function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_n
$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 );
// A card without a topic left an empty slot and knocked the row out of alignment.
// Untopiced posts are still Journal entries, so that is the honest fallback.
$topic_label = ! is_wp_error( $topic_terms ) && ! empty( $topic_terms ) ? $topic_terms[0]->name : __( 'Journal', 'chrnx' );
$excerpt = wp_trim_words( wp_strip_all_tags( get_the_excerpt( $post ) ), 24 );
$markup = '<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 .= '<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>';
@@ -135,7 +232,7 @@ function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_n
/** Register all block metadata directories that exist. */
function chrnx_register_editorial_blocks() {
$blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link' );
$blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link', 'selected-work', 'release-list', 'release-detail', 'track-meta', 'subscribe-form' );
foreach ( $blocks as $block ) {
$name = 'chrnx/' . $block;
if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) {
+18
View File
@@ -45,7 +45,25 @@ function chrnx_enqueue_editor_assets() {
true
);
wp_enqueue_script(
'chrnx-editor-project',
get_theme_file_uri( 'assets/js/editor-project.js' ),
array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' ),
CHRNX_VERSION,
true
);
wp_enqueue_script(
'chrnx-editor-track',
get_theme_file_uri( 'assets/js/editor-track.js' ),
array( 'wp-block-editor', 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' ),
CHRNX_VERSION,
true
);
wp_set_script_translations( 'chrnx-editor-taxonomies', 'chrnx', get_theme_file_path( 'languages' ) );
wp_set_script_translations( 'chrnx-editor-podcast', 'chrnx', get_theme_file_path( 'languages' ) );
wp_set_script_translations( 'chrnx-editor-project', 'chrnx', get_theme_file_path( 'languages' ) );
wp_set_script_translations( 'chrnx-editor-track', 'chrnx', get_theme_file_path( 'languages' ) );
}
add_action( 'enqueue_block_editor_assets', 'chrnx_enqueue_editor_assets' );
+37
View File
@@ -0,0 +1,37 @@
<?php
/**
* Project post metadata.
*
* @package CHRNX
*/
/**
* Require permission to edit the post owning project metadata.
*
* @param bool $allowed Previously calculated permission.
* @param string $meta_key Metadata key.
* @param int $post_id Post ID.
* @return bool Whether the current user can edit the post.
*/
function chrnx_auth_project_meta( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
}
/**
* Register project metadata for posts.
*/
function chrnx_register_project_meta() {
register_post_meta(
'post',
'chrnx_project_featured',
array(
'type' => 'boolean',
'single' => true,
'default' => false,
'show_in_rest' => array( 'schema' => array( 'type' => 'boolean' ) ),
'sanitize_callback' => 'rest_sanitize_boolean',
'auth_callback' => 'chrnx_auth_project_meta',
)
);
}
add_action( 'init', 'chrnx_register_project_meta' );
+77 -10
View File
@@ -262,6 +262,59 @@ function chrnx_get_featured_story_ids( int $limit = 3 ): array {
return $selected;
}
/**
* Select projects for the Selected work stage: hand-picked first, then recent.
*
* Sticky already belongs to the home featured stories, so the editorial choice
* lives in its own per-post flag instead of competing for the same signal.
*
* @param int $limit Maximum projects, bounded to 12.
* @return int[] Selected project post IDs.
*/
function chrnx_get_selected_project_ids( int $limit = 3 ): array {
$limit = min( 12, max( 0, $limit ) );
if ( 0 === $limit ) {
return array();
}
$topic_query = array(
array(
'taxonomy' => 'chrnx_topic',
'field' => 'slug',
'terms' => 'projects',
),
);
$selected = chrnx_get_eligible_editorial_post_ids(
array(
'posts_per_page' => $limit,
'tax_query' => $topic_query,
'meta_query' => array(
array(
'key' => 'chrnx_project_featured',
'value' => '1',
),
),
)
);
$remaining = $limit - count( $selected );
if ( $remaining > 0 ) {
$selected = array_merge(
$selected,
chrnx_get_eligible_editorial_post_ids(
array(
'posts_per_page' => $remaining,
'post__not_in' => $selected,
'tax_query' => $topic_query,
)
)
);
}
return $selected;
}
/**
* Validate a request taxonomy slug against an existing editorial term.
*
@@ -624,15 +677,20 @@ function chrnx_get_related_post_ids( int $post_id, int $limit = 3 ): array {
*/
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',
'music-collage' => 'the-no-names-collage.jpeg',
'podcast-frequency' => 'podcast-frequency-grunge.png',
'podcasts' => 'alter-ego-podcasts.png',
'life' => 'alter-ego-life.png',
'gaming' => 'alter-ego-gaming-half-body.png',
'research' => 'alter-ego-research.png',
'code' => 'alter-ego-code.png',
'projects' => 'alter-ego-code.png',
'literature' => 'alter-ego-literature.png',
'music' => 'the-no-names-musician-cutout.png',
'music-collage' => 'the-no-names-collage.jpeg',
'podcast-frequency' => 'podcast-frequency-grunge.png',
'about-grunge' => 'about-grunge.png',
'placeholder-post' => 'placeholder-post.png',
'placeholder-release' => 'placeholder-release.png',
'projects-grunge' => 'projects-grunge.png',
'podcasts' => 'alter-ego-podcasts.png',
'life' => 'alter-ego-life.png',
'gaming' => 'alter-ego-gaming-half-body.png',
);
return isset( $mapping[ $topic_slug ] ) ? 'assets/images/' . $mapping[ $topic_slug ] : '';
}
@@ -648,9 +706,18 @@ function chrnx_get_topic_art_dimensions( string $topic_slug ): array {
return array();
}
if ( 'podcast-frequency' === $topic_slug ) {
// The landscape atmosphere backgrounds share one intrinsic size.
if ( in_array( $topic_slug, array( 'podcast-frequency', 'about-grunge', 'projects-grunge' ), true ) ) {
return array( 1536, 1024 );
}
if ( 'placeholder-post' === $topic_slug ) {
return array( 1440, 960 );
}
if ( 'placeholder-release' === $topic_slug ) {
return array( 1536, 1536 );
}
return 'gaming' === $topic_slug ? array( 1024, 1536 ) : array( 1122, 1402 );
}
+26 -5
View File
@@ -19,6 +19,7 @@ function chrnx_get_editorial_taxonomies() {
'terms' => array(
'research' => __( 'Research', 'chrnx' ),
'code' => __( 'Code', 'chrnx' ),
'projects' => __( 'Projects', 'chrnx' ),
'literature' => __( 'Literature', 'chrnx' ),
'music' => __( 'Music', 'chrnx' ),
'podcasts' => __( 'Podcasts', 'chrnx' ),
@@ -43,6 +44,16 @@ function chrnx_get_editorial_taxonomies() {
'silicon-ranch' => __( 'Silicon Ranch', 'chrnx' ),
),
),
'chrnx_release' => array(
'singular' => __( 'Release', 'chrnx' ),
'plural' => __( 'Releases', 'chrnx' ),
'rest_base' => 'release',
'terms' => array(
'el-hermoso-caos' => __( 'El Hermoso Caos que habita en mi cabeza', 'chrnx' ),
'ando-bien-arriba' => __( 'Ando Bien Arriba', 'chrnx' ),
'aqui-se-rompio-una-jerga' => __( 'Aquí se Rompió una Jerga', 'chrnx' ),
),
),
);
}
@@ -91,11 +102,21 @@ function chrnx_seed_editorial_terms() {
}
foreach ( $definition['terms'] as $slug => $name ) {
if ( ! term_exists( $slug, $taxonomy_name ) ) {
$result = wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
if ( is_wp_error( $result ) || false === $result ) {
chrnx_store_editorial_notice( '', 'vocabulary_setup_failed' );
}
/*
* term_exists() reads a cache that is not always warm during theme
* activation, which let a second activation insert duplicates with
* a "-2" slug. Ask the taxonomy directly, by slug and by name.
*/
if ( get_term_by( 'slug', $slug, $taxonomy_name ) instanceof WP_Term ) {
continue;
}
if ( get_term_by( 'name', $name, $taxonomy_name ) instanceof WP_Term ) {
continue;
}
$result = wp_insert_term( $name, $taxonomy_name, array( 'slug' => $slug ) );
if ( is_wp_error( $result ) || false === $result ) {
chrnx_store_editorial_notice( '', 'vocabulary_setup_failed' );
}
}
}
+102
View File
@@ -0,0 +1,102 @@
const modulePath = '../../assets/js/editor-project.js';
function flatten( node ) {
if ( ! node || typeof node !== 'object' ) {
return [];
}
return [ node ].concat( ( node.children || [] ).flatMap( flatten ) );
}
function createWp( topicSlug = 'projects', meta = { unrelated: 'keep' } ) {
const editPost = jest.fn();
const registerPlugin = jest.fn();
const state = { meta, topic: [ 12 ] };
const wp = {
components: { ToggleControl: 'ToggleControl' },
i18n: { __: jest.fn( ( text ) => text ) },
data: {
select: ( store ) => {
if ( store === 'core/editor' ) {
return {
getCurrentPostType: () => 'post',
getEditedPostAttribute: ( key ) => state[ key ],
};
}
return {
getEntityRecord: () => ( topicSlug ? { id: 12, slug: topicSlug } : undefined ),
};
},
dispatch: () => ( { editPost } ),
useDispatch: ( store ) => wp.data.dispatch( store ),
useSelect: ( mapper ) => mapper( wp.data.select ),
},
editPost: { PluginDocumentSettingPanel: 'PluginDocumentSettingPanel' },
element: {
createElement: ( type, props, ...children ) => ( { type, props: props || {}, children } ),
},
plugins: { registerPlugin },
};
return { wp, editPost, registerPlugin };
}
function renderPanel( context ) {
const { initialize } = require( modulePath );
expect( initialize( context.wp ) ).toBe( true );
const [ , plugin ] = context.registerPlugin.mock.calls[ 0 ];
return plugin.render();
}
describe( 'project editor panel', () => {
beforeEach( () => {
jest.resetModules();
} );
test( 'refuses to initialize without the required editor globals', () => {
const { initialize } = require( modulePath );
expect( initialize( undefined ) ).toBe( false );
expect( initialize( {} ) ).toBe( false );
} );
test( 'renders the Selected work toggle only for the projects topic', () => {
const context = createWp( 'projects' );
const toggles = flatten( renderPanel( context ) ).filter(
( node ) => node.type === 'ToggleControl',
);
expect( toggles ).toHaveLength( 1 );
expect( toggles[ 0 ].props.checked ).toBe( false );
} );
test.each( [ 'podcasts', 'code', null ] )( 'renders nothing for the %s topic', ( slug ) => {
expect( renderPanel( createWp( slug ) ) ).toBeNull();
} );
test( 'reflects an existing flag and preserves unrelated meta when toggling', () => {
const context = createWp( 'projects', { unrelated: 'keep', chrnx_project_featured: true } );
const toggle = flatten( renderPanel( context ) ).find(
( node ) => node.type === 'ToggleControl',
);
expect( toggle.props.checked ).toBe( true );
toggle.props.onChange( false );
expect( context.editPost ).toHaveBeenCalledWith( {
meta: { unrelated: 'keep', chrnx_project_featured: false },
} );
} );
test( 'coerces the toggle value to a boolean', () => {
const context = createWp( 'projects' );
const toggle = flatten( renderPanel( context ) ).find(
( node ) => node.type === 'ToggleControl',
);
toggle.props.onChange( 'yes' );
expect( context.editPost ).toHaveBeenCalledWith( {
meta: { unrelated: 'keep', chrnx_project_featured: true },
} );
} );
test( 'updateMeta ignores a missing dispatcher', () => {
const { updateMeta } = require( modulePath );
expect( () => updateMeta( undefined, {}, 'chrnx_project_featured', true ) ).not.toThrow();
} );
} );
+5 -2
View File
@@ -45,7 +45,7 @@ class EditorAssetsTest extends WP_UnitTestCase {
* Clear only the CHRNX editor script state.
*/
private function reset_editor_scripts() {
foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ) as $handle ) {
foreach ( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-project', 'chrnx-editor-track' ) as $handle ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
}
@@ -83,13 +83,14 @@ class EditorAssetsTest extends WP_UnitTestCase {
)
);
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast' ), $handles );
$this->assertSame( array( 'chrnx-editor-taxonomies', 'chrnx-editor-podcast', 'chrnx-editor-project', 'chrnx-editor-track' ), $handles );
$expected_dependencies = array( 'wp-components', 'wp-core-data', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' );
foreach (
array(
'chrnx-editor-taxonomies' => 'assets/js/editor-taxonomies.js',
'chrnx-editor-podcast' => 'assets/js/editor-podcast.js',
'chrnx-editor-project' => 'assets/js/editor-project.js',
) as $handle => $asset
) {
$this->assertArrayHasKey( $handle, $scripts->registered );
@@ -122,6 +123,8 @@ class EditorAssetsTest extends WP_UnitTestCase {
do_action( 'enqueue_block_editor_assets' );
$this->assertFalse( wp_script_is( 'chrnx-editor-taxonomies', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'chrnx-editor-podcast', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'chrnx-editor-project', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'chrnx-editor-track', 'enqueued' ) );
}
}
+1
View File
@@ -29,6 +29,7 @@ class TaxonomiesTest extends WP_UnitTestCase {
'terms' => array(
'research' => 'Research',
'code' => 'Code',
'projects' => 'Projects',
'literature' => 'Literature',
'music' => 'Music',
'podcasts' => 'Podcasts',