/** * Header display conditions editor. * * Builds the `header.display.rules` list: an ordered set of include/exclude * rules that gate where the header renders. Empty list = show on entire site. * Mirrors the resolver in Display_Conditions.php (exclude wins over include). */ import { __, sprintf } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { Plus, X } from '../../icons'; import SelectField from '../../controls/select-field'; import SpecificPostsField from './specific-posts-field'; import type { SetPath } from '../../types'; export interface DisplayRule { mode: 'include' | 'exclude'; type: string; value?: string | number[]; } interface PostTypeChoice { slug: string; label: string; } interface DisplayConditionsProps { rules: DisplayRule[]; postTypes: PostTypeChoice[]; setPath: SetPath; /** Settings path the rules array is written to (per active template). */ path: string; } const TYPE_OPTIONS = [ { value: 'entire_site', label: __( 'Entire site', 'aureon' ) }, { value: 'front_page', label: __( 'Front page', 'aureon' ) }, { value: 'blog_home', label: __( 'Blog / posts page', 'aureon' ) }, { value: 'archive', label: __( 'All archives', 'aureon' ) }, { value: 'search', label: __( 'Search results', 'aureon' ) }, { value: 'not_found', label: __( '404 page', 'aureon' ) }, { value: 'post_type', label: __( 'All of a post type', 'aureon' ) }, { value: 'post_type_archive', label: __( 'Post type archive', 'aureon' ), }, { value: 'singular', label: __( 'Specific pages / posts', 'aureon' ) }, ]; const NEEDS_POST_TYPE = [ 'post_type', 'post_type_archive' ]; /** * A short human summary of a template's display rules, for the manager bar. * @param rules Display rules (empty ⇒ shown everywhere). */ export const describeConditions = ( rules: DisplayRule[] = [] ): string => { if ( ! rules.length ) { return __( 'Shown everywhere', 'aureon' ); } const labelFor = ( type: string ) => TYPE_OPTIONS.find( ( o ) => o.value === type )?.label ?? type; const include = rules .filter( ( r ) => 'include' === r.mode ) .map( ( r ) => labelFor( r.type ) ); const exclude = rules .filter( ( r ) => 'exclude' === r.mode ) .map( ( r ) => labelFor( r.type ) ); const parts: string[] = []; parts.push( include.length ? /* translators: %s: comma-separated condition names. */ sprintf( __( 'Show on %s', 'aureon' ), include.join( ', ' ) ) : __( 'Shown everywhere', 'aureon' ) ); if ( exclude.length ) { parts.push( /* translators: %s: comma-separated condition names. */ sprintf( __( 'except %s', 'aureon' ), exclude.join( ', ' ) ) ); } return parts.join( ' ' ); }; export default function DisplayConditions( { rules, postTypes, setPath, path, }: DisplayConditionsProps ) { const commit = ( next: DisplayRule[] ) => setPath( path, next ); const updateRule = ( index: number, patch: Partial< DisplayRule > ) => commit( rules.map( ( rule, i ) => i === index ? { ...rule, ...patch } : rule ) ); const changeType = ( index: number, type: string ) => { const patch: Partial< DisplayRule > = { type }; if ( NEEDS_POST_TYPE.includes( type ) ) { patch.value = postTypes[ 0 ]?.slug ?? ''; } else if ( 'singular' === type ) { patch.value = []; } else { patch.value = undefined; } updateRule( index, patch ); }; const addRule = () => commit( [ ...rules, { mode: 'include', type: 'entire_site' } ] ); const removeRule = ( index: number ) => commit( rules.filter( ( _, i ) => i !== index ) ); return (
{ __( 'Control where this header appears. With no conditions it shows on the entire site. Exclude rules always win over include rules.', 'aureon' ) }
{ rules.map( ( rule, index ) => (