Albums were three hardcoded cards. An album is a chrnx_release term now, with cover, status and listening links; its archive is the album page with an ordered tracklist. A song is a post carrying lyrics and notes, its track number, and the audio. The player is a native audio element, no library and no JavaScript. Platform icons render only when a URL exists, so nothing points at a service the release is not on yet. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
166 lines
5.0 KiB
JavaScript
166 lines
5.0 KiB
JavaScript
( function ( factory ) {
|
|
if ( typeof module === 'object' && module.exports ) {
|
|
module.exports = factory();
|
|
} else {
|
|
factory().initialize( window.wp );
|
|
}
|
|
} )( function () {
|
|
'use strict';
|
|
|
|
function platformFields( __ ) {
|
|
return [
|
|
{ key: 'chrnx_track_url_suno', label: __( 'Suno URL', 'chrnx' ) },
|
|
{ key: 'chrnx_track_url_spotify', label: __( 'Spotify URL', 'chrnx' ) },
|
|
{ key: 'chrnx_track_url_apple', label: __( 'Apple Music URL', 'chrnx' ) },
|
|
];
|
|
}
|
|
|
|
function getTranslator( wp ) {
|
|
return wp.i18n && typeof wp.i18n.__ === 'function'
|
|
? wp.i18n.__
|
|
: function ( text ) {
|
|
return text;
|
|
};
|
|
}
|
|
|
|
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.Button &&
|
|
wp.blockEditor &&
|
|
wp.blockEditor.MediaUpload &&
|
|
wp.data &&
|
|
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 el = wp.element.createElement;
|
|
|
|
function TrackPanel() {
|
|
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 !== 'music' ) {
|
|
return null;
|
|
}
|
|
|
|
const meta = editorState.meta;
|
|
const setMeta = function ( key, value ) {
|
|
updateMeta( editorDispatch.editPost, meta, key, value );
|
|
};
|
|
|
|
return el(
|
|
wp.editPost.PluginDocumentSettingPanel,
|
|
{ name: 'chrnx-track', title: __( 'Track', 'chrnx' ) },
|
|
el( wp.components.TextControl, {
|
|
label: __( 'Track number', 'chrnx' ),
|
|
type: 'number',
|
|
min: 0,
|
|
value: meta.chrnx_track_number ?? '',
|
|
onChange( value ) {
|
|
const number = Number.parseInt( value, 10 );
|
|
setMeta( 'chrnx_track_number', Number.isInteger( number ) ? Math.max( 0, number ) : 0 );
|
|
},
|
|
} ),
|
|
el( wp.blockEditor.MediaUpload, {
|
|
allowedTypes: [ 'audio' ],
|
|
value: meta.chrnx_track_audio || 0,
|
|
onSelect( attachment ) {
|
|
setMeta( 'chrnx_track_audio', attachment?.id ? Number( attachment.id ) : 0 );
|
|
},
|
|
render( { open } ) {
|
|
return el(
|
|
wp.components.Button,
|
|
{ variant: 'secondary', onClick: open },
|
|
meta.chrnx_track_audio
|
|
? __( 'Replace audio file', 'chrnx' )
|
|
: __( 'Select audio file', 'chrnx' ),
|
|
);
|
|
},
|
|
} ),
|
|
meta.chrnx_track_audio
|
|
? el(
|
|
wp.components.Button,
|
|
{
|
|
variant: 'link',
|
|
isDestructive: true,
|
|
onClick() {
|
|
setMeta( 'chrnx_track_audio', 0 );
|
|
},
|
|
},
|
|
__( 'Remove audio file', 'chrnx' ),
|
|
)
|
|
: null,
|
|
...platformFields( __ ).map( function ( platform ) {
|
|
return el( wp.components.TextControl, {
|
|
key: platform.key,
|
|
label: platform.label,
|
|
type: 'url',
|
|
value: meta[ platform.key ] || '',
|
|
onChange( value ) {
|
|
setMeta( platform.key, value );
|
|
},
|
|
} );
|
|
} ),
|
|
);
|
|
}
|
|
|
|
wp.plugins.registerPlugin( 'chrnx-track-meta', { render: TrackPanel } );
|
|
return true;
|
|
}
|
|
|
|
return { initialize, updateMeta, platformFields };
|
|
} );
|