feat: add CHRNX editorial metadata
This commit is contained in:
10 files changed
+1127
-1
No files matched your search
@@ -0,0 +1,138 @@
|
||||
( 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 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 editor = wp.data.select( 'core/editor' );
|
||||
if ( ! editor || editor.getCurrentPostType() !== 'post' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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 ) {
|
||||
const currentEditor = select( 'core/editor' );
|
||||
const core = select( 'core' );
|
||||
if ( ! currentEditor || ! 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: -1,
|
||||
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', 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: taxonomy.title,
|
||||
},
|
||||
wp.element.createElement( wp.components.RadioControl, {
|
||||
label: taxonomy.title,
|
||||
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 };
|
||||
} );
|
||||
Reference in new issue
Block a user