Attach your add-on’s work to a new episode once it has been saved
A new episode has no id until its first save, so a fill that lets the podcaster pick things for it (such as one in BenecasterEpisodeGuests, see Replace the episode editor’s free guest rows with your own selector) has to hold them and attach them once the id exists. After that first save succeeds, Benecaster dispatches benecaster:episode-created on window with detail: { episodeId, showId }, once, before the editor moves to the saved episode.
Code
const pending = new Map(); // showId => what the podcaster picked on the new episode
window.addEventListener( 'benecaster:episode-created', async ( event ) => {
const { episodeId, showId } = event.detail;
const picks = pending.get( showId );
if ( ! picks ) {
return;
}
pending.delete( showId );
await window.wp.apiFetch( {
path: `/my-addon/v1/episodes/${ episodeId }/links`,
method: 'POST',
data: picks,
} );
myClearDirty(); // the setDirty( false ) your fill was given
} );
Need this built rather than just documented? See our services →