159 lines
4.9 KiB
JavaScript
159 lines
4.9 KiB
JavaScript
( function ( factory ) {
|
|
if ( typeof module === 'object' && module.exports ) {
|
|
module.exports = factory();
|
|
} else {
|
|
factory().initialize( window.wp );
|
|
}
|
|
} )( function () {
|
|
'use strict';
|
|
|
|
const maxParticipants = 50;
|
|
const maxParticipantLength = 100;
|
|
|
|
function getTranslator( wp ) {
|
|
return wp.i18n && typeof wp.i18n.__ === 'function'
|
|
? wp.i18n.__
|
|
: function ( text ) {
|
|
return text;
|
|
};
|
|
}
|
|
|
|
function participantsFromText( value ) {
|
|
return String( value || '' )
|
|
.split( /\r?\n/ )
|
|
.map( function ( name ) {
|
|
return Array.from( name.trim() ).slice( 0, maxParticipantLength ).join( '' );
|
|
} )
|
|
.filter( Boolean )
|
|
.slice( 0, maxParticipants );
|
|
}
|
|
|
|
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 __ = getTranslator( wp );
|
|
|
|
function PodcastPanel() {
|
|
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.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', 'chrnx' ) },
|
|
wp.element.createElement( wp.components.TextControl, {
|
|
label: __( 'Episode number', 'chrnx' ),
|
|
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', 'chrnx' ),
|
|
value: meta.chrnx_episode_duration || '',
|
|
onChange( value ) {
|
|
setMeta( 'chrnx_episode_duration', value );
|
|
},
|
|
} ),
|
|
wp.element.createElement( wp.components.TextareaControl, {
|
|
label: __( 'Participants (one per line)', 'chrnx' ),
|
|
value: participantsToText( meta.chrnx_episode_participants ),
|
|
onChange( value ) {
|
|
setMeta( 'chrnx_episode_participants', participantsFromText( value ) );
|
|
},
|
|
} ),
|
|
wp.element.createElement( wp.components.TextControl, {
|
|
label: __( 'Canonical episode URL', 'chrnx' ),
|
|
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 };
|
|
} );
|