feat: add CHRNX editorial metadata
This commit is contained in:
10 files changed
+1127
-1
No files matched your search
@@ -0,0 +1,139 @@
|
||||
( function ( factory ) {
|
||||
if ( typeof module === 'object' && module.exports ) {
|
||||
module.exports = factory();
|
||||
} else {
|
||||
factory().initialize( window.wp );
|
||||
}
|
||||
} )( function () {
|
||||
'use strict';
|
||||
|
||||
function participantsFromText( value ) {
|
||||
return String( value || '' )
|
||||
.split( /\r?\n/ )
|
||||
.map( function ( name ) {
|
||||
return name.trim();
|
||||
} )
|
||||
.filter( Boolean );
|
||||
}
|
||||
|
||||
function participantsToText( value ) {
|
||||
return Array.isArray( value ) ? value.join( '\n' ) : '';
|
||||
}
|
||||
|
||||
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.TextControl &&
|
||||
wp.components.TextareaControl &&
|
||||
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;
|
||||
}
|
||||
|
||||
function PodcastPanel() {
|
||||
const editorState = wp.data.useSelect( function ( select ) {
|
||||
const currentEditor = select( 'core/editor' );
|
||||
const core = select( 'core' );
|
||||
if ( ! currentEditor || ! 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 !== 'podcasts' ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const meta = editorState.meta;
|
||||
const setMeta = function ( key, value ) {
|
||||
updateMeta( editorDispatch.editPost, meta, key, value );
|
||||
};
|
||||
|
||||
return wp.element.createElement(
|
||||
wp.editPost.PluginDocumentSettingPanel,
|
||||
{ name: 'chrnx-podcast', title: 'Podcast episode' },
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Episode number',
|
||||
type: 'number',
|
||||
min: 0,
|
||||
value: meta.chrnx_episode_number ?? '',
|
||||
onChange( value ) {
|
||||
const number = Number.parseInt( value, 10 );
|
||||
setMeta(
|
||||
'chrnx_episode_number',
|
||||
Number.isInteger( number ) ? Math.max( 0, number ) : 0,
|
||||
);
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Duration',
|
||||
value: meta.chrnx_episode_duration || '',
|
||||
onChange( value ) {
|
||||
setMeta( 'chrnx_episode_duration', value );
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextareaControl, {
|
||||
label: 'Participants (one per line)',
|
||||
value: participantsToText( meta.chrnx_episode_participants ),
|
||||
onChange( value ) {
|
||||
setMeta( 'chrnx_episode_participants', participantsFromText( value ) );
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Canonical episode URL',
|
||||
type: 'url',
|
||||
value: meta.canonical_episode_url || '',
|
||||
onChange( value ) {
|
||||
setMeta( 'canonical_episode_url', value );
|
||||
},
|
||||
} ),
|
||||
);
|
||||
}
|
||||
|
||||
wp.plugins.registerPlugin( 'chrnx-podcast-meta', { render: PodcastPanel } );
|
||||
return true;
|
||||
}
|
||||
|
||||
return { initialize, participantsFromText, participantsToText, updateMeta };
|
||||
} );
|
||||
@@ -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