140 lines
4.4 KiB
JavaScript
140 lines
4.4 KiB
JavaScript
( 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 };
|
|
} );
|