( function ( factory ) { if ( typeof module === 'object' && module.exports ) { module.exports = factory(); } else { factory().initialize( window.wp ); } } )( function () { 'use strict'; const taxonomies = [ { name: 'chrnx_topic', restBase: 'topic', title: 'Topic' }, { name: 'chrnx_language', restBase: 'language', title: 'Language' }, { name: 'chrnx_show', restBase: 'show', title: 'Show' }, ]; function getTranslator( wp ) { return wp.i18n && typeof wp.i18n.__ === 'function' ? wp.i18n.__ : function ( text ) { return text; }; } function orderTerms( terms ) { if ( ! Array.isArray( terms ) ) { return []; } return terms.slice().sort( function ( left, right ) { const byName = String( left.name || '' ).localeCompare( String( right.name || '' ) ); return byName || Number( left.id ) - Number( right.id ); } ); } function writeSelection( editPost, restBase, value ) { if ( typeof editPost !== 'function' ) { return; } const termId = Number.parseInt( value, 10 ); editPost( { [ restBase ]: Number.isInteger( termId ) ? [ termId ] : [] } ); } function hasRequiredGlobals( wp ) { return Boolean( wp && wp.components && wp.components.RadioControl && wp.data && typeof wp.data.select === 'function' && typeof wp.data.dispatch === 'function' && 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 ); const titles = { chrnx_topic: __( 'Topic', 'chrnx' ), chrnx_language: __( 'Language', 'chrnx' ), chrnx_show: __( 'Show', 'chrnx' ), }; const panelDispatch = wp.data.dispatch( 'core/edit-post' ); if ( panelDispatch && typeof panelDispatch.removeEditorPanel === 'function' ) { taxonomies.forEach( function ( taxonomy ) { panelDispatch.removeEditorPanel( 'taxonomy-panel-' + taxonomy.name ); } ); } function EditorialTaxonomyPanels() { 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.getEntityRecords !== 'function' ) { return null; } return { postType: currentEditor.getCurrentPostType(), taxonomies: taxonomies.map( function ( taxonomy ) { return { selected: currentEditor.getEditedPostAttribute( taxonomy.restBase ), terms: core.getEntityRecords( 'taxonomy', taxonomy.name, { per_page: 100, orderby: 'name', order: 'asc', } ), }; } ), }; }, [] ); const editorDispatch = wp.data.useDispatch( 'core/editor' ); if ( ! editorState || editorState.postType !== 'post' || ! editorDispatch?.editPost ) { return null; } const panels = taxonomies.map( function ( taxonomy, index ) { const terms = orderTerms( editorState.taxonomies[ index ].terms ); if ( ! terms.length ) { return null; } const selected = editorState.taxonomies[ index ].selected; const options = [ { label: __( 'Not set', 'chrnx' ), value: '' } ].concat( terms.map( function ( term ) { return { label: term.name, value: String( term.id ) }; } ), ); return wp.element.createElement( wp.editPost.PluginDocumentSettingPanel, { key: taxonomy.name, name: 'chrnx-' + taxonomy.restBase, title: titles[ taxonomy.name ], }, wp.element.createElement( wp.components.RadioControl, { label: titles[ taxonomy.name ], selected: Array.isArray( selected ) && selected.length ? String( selected[ 0 ] ) : '', options, onChange( value ) { writeSelection( editorDispatch.editPost, taxonomy.restBase, value ); }, } ), ); } ); return wp.element.createElement( 'div', null, ...panels.filter( Boolean ) ); } wp.plugins.registerPlugin( 'chrnx-editorial-taxonomies', { render: EditorialTaxonomyPanels } ); return true; } return { initialize, orderTerms, writeSelection }; } );