From f55d9d9b45d31f96e6759768384061e3c9fe9708 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 13 Aug 2026 19:41:24 -0500 Subject: [PATCH] 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) --- assets/js/editor-project.js | 111 +++++++++++++++++++++++++++++++ blocks/journal-stream/block.json | 5 +- blocks/journal-stream/render.php | 14 +++- blocks/selected-work/block.json | 16 +++++ blocks/selected-work/render.php | 26 ++++++++ functions.php | 4 +- inc/blocks.php | 111 +++++++++++++++++++++++++++++-- inc/editor.php | 18 +++++ inc/project-meta.php | 37 +++++++++++ inc/queries.php | 87 +++++++++++++++++++++--- inc/taxonomies.php | 31 +++++++-- tests/js/editor-project.test.js | 102 ++++++++++++++++++++++++++++ tests/php/EditorAssetsTest.php | 7 +- tests/php/TaxonomiesTest.php | 1 + 14 files changed, 539 insertions(+), 31 deletions(-) create mode 100644 assets/js/editor-project.js create mode 100644 blocks/selected-work/block.json create mode 100644 blocks/selected-work/render.php create mode 100644 inc/project-meta.php create mode 100644 tests/js/editor-project.test.js diff --git a/assets/js/editor-project.js b/assets/js/editor-project.js new file mode 100644 index 0000000..fb14215 --- /dev/null +++ b/assets/js/editor-project.js @@ -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 }; +} ); diff --git a/blocks/journal-stream/block.json b/blocks/journal-stream/block.json index 43ebd18..20731e2 100644 --- a/blocks/journal-stream/block.json +++ b/blocks/journal-stream/block.json @@ -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", diff --git a/blocks/journal-stream/render.php b/blocks/journal-stream/render.php index fe42195..68ae684 100644 --- a/blocks/journal-stream/render.php +++ b/blocks/journal-stream/render.php @@ -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' ); ?>
'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby=""> - id="">> + id="">>

diff --git a/blocks/selected-work/block.json b/blocks/selected-work/block.json new file mode 100644 index 0000000..8cc1fae --- /dev/null +++ b/blocks/selected-work/block.json @@ -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" +} diff --git a/blocks/selected-work/render.php b/blocks/selected-work/render.php new file mode 100644 index 0000000..7291587 --- /dev/null +++ b/blocks/selected-work/render.php @@ -0,0 +1,26 @@ + +
'chrnx-selected-work' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> + +

+ +
    + +
  • + class="chrnx-selected-work__title"> + + > +
  • + +
+ +
diff --git a/functions.php b/functions.php index 17ee7c5..cf3ee9c 100644 --- a/functions.php +++ b/functions.php @@ -1,8 +1,8 @@ $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 ''; + 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> 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' => '', + 'menu' => '', + 'theme' => '', + ); + if ( ! isset( $icons[ $name ] ) ) { + return ''; + } + + return sprintf( + // The path data is a fixed literal, never user input. + '', + 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 = '
'; $markup .= ''; $markup .= chrnx_get_card_media_markup( $post_id, $class_name ); $markup .= ''; - if ( '' !== $topic_label ) { - $markup .= '' . esc_html( $topic_label ) . ''; - } + $markup .= '' . esc_html( $topic_label ) . ''; $markup .= '' . esc_html( get_the_title( $post ) ) . ''; if ( '' !== $excerpt ) { $markup .= '' . esc_html( $excerpt ) . ''; @@ -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 ) ) { diff --git a/inc/editor.php b/inc/editor.php index 4880d24..f185611 100644 --- a/inc/editor.php +++ b/inc/editor.php @@ -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' ); diff --git a/inc/project-meta.php b/inc/project-meta.php new file mode 100644 index 0000000..86c7141 --- /dev/null +++ b/inc/project-meta.php @@ -0,0 +1,37 @@ + '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' ); diff --git a/inc/queries.php b/inc/queries.php index 22fbad3..66eb20a 100644 --- a/inc/queries.php +++ b/inc/queries.php @@ -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 ); } diff --git a/inc/taxonomies.php b/inc/taxonomies.php index bbdf96e..079c320 100644 --- a/inc/taxonomies.php +++ b/inc/taxonomies.php @@ -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' ); } } } diff --git a/tests/js/editor-project.test.js b/tests/js/editor-project.test.js new file mode 100644 index 0000000..590d160 --- /dev/null +++ b/tests/js/editor-project.test.js @@ -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(); + } ); +} ); diff --git a/tests/php/EditorAssetsTest.php b/tests/php/EditorAssetsTest.php index 85e0d9a..2002ca3 100644 --- a/tests/php/EditorAssetsTest.php +++ b/tests/php/EditorAssetsTest.php @@ -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' ) ); } } diff --git a/tests/php/TaxonomiesTest.php b/tests/php/TaxonomiesTest.php index 1b9f486..9161cad 100644 --- a/tests/php/TaxonomiesTest.php +++ b/tests/php/TaxonomiesTest.php @@ -29,6 +29,7 @@ class TaxonomiesTest extends WP_UnitTestCase { 'terms' => array( 'research' => 'Research', 'code' => 'Code', + 'projects' => 'Projects', 'literature' => 'Literature', 'music' => 'Music', 'podcasts' => 'Podcasts',