/** * WordPress dependencies */ import type { select as _select, dispatch as _dispatch, } from '@safe-wordpress/data'; import { _x, sprintf } from '@safe-wordpress/i18n'; type Select = typeof _select; type Dispatch = typeof _dispatch; /** * External dependencies */ import { store as NC_DATA } from '@nelio-content/data'; import { store as NC_EDIT_POST } from '@nelio-content/edit-post'; import { PostStatus } from '@nelio-content/types'; export function manageCustomStatuses(): void { const selector = document.getElementById( 'post_status' ) as HTMLSelectElement | null; if ( ! selector ) { return; } const customStatuses = getCustomStatuses(); appendCustomStatusesInSelector( selector, customStatuses ); manageCustomStatusLabels( customStatuses ); } const appendCustomStatusesInSelector = ( selector: HTMLSelectElement, customStatuses: ReadonlyArray< PostStatus > ) => { customStatuses.forEach( ( status ) => { const option = document.createElement( 'option' ); option.setAttribute( 'value', status.slug ); option.append( status.name ); selector.append( option ); } ); const status = getCurrentPostStatus(); const currentCustomStatus = customStatuses.find( ( s ) => s.slug === status ); if ( currentCustomStatus ) { document .getElementById( 'post-status-select' ) ?.setAttribute( 'value', currentCustomStatus.slug ); document .getElementById( 'post-status-display' ) ?.append( currentCustomStatus.name ); document .querySelector( `#post_status option[value="${ currentCustomStatus.slug }"]` ) ?.setAttribute( 'selected', 'selected' ); updateCustomStatusLabels( currentCustomStatus ); } }; const manageCustomStatusLabels = ( customStatuses: ReadonlyArray< PostStatus > ) => { [ ...document.querySelectorAll( 'a.save-post-status, #save-post' ) ].map( ( e ) => e?.addEventListener( 'click', () => setInterval( () => { const selectedStatus = document.querySelector( '#post_status option:checked' ); const selectedCustomStatus = customStatuses.find( ( s ) => s.slug === ( selectedStatus as HTMLOptionElement | null ) ?.value ); if ( ! selectedCustomStatus ) { return; } updateCustomStatusLabels( selectedCustomStatus ); }, 100 ) ) ); }; const updateCustomStatusLabels = ( customStatus: PostStatus ) => { document .getElementById( 'original_publish' ) ?.setAttribute( 'value', _x( 'Update', 'command', 'nelio-content' ) ); document.getElementById( 'save-post' )?.setAttribute( 'value', sprintf( /* translators: %s: Custom status name. */ _x( 'Save %s', 'command', 'nelio-content' ), customStatus.name ) ); }; function getCustomStatuses() { if ( ! hasWordPressData( window ) ) { return []; } return window.wp.data .select( NC_DATA ) .getPostStatuses() .filter( ( s ) => ! [ 'nelio-content-unscheduled', 'draft', 'pending', 'future', 'auto-draft', 'publish', 'private', 'trash', ].includes( s.slug ) ) .filter( ( s ) => s.available ); } function getCurrentPostStatus() { if ( ! hasWordPressData( window ) ) { return; } return window.wp.data.select( NC_EDIT_POST ).getPost()?.status; } type WordPressData = { readonly wp: { readonly data: { readonly select: Select; readonly dispatch: Dispatch; }; }; }; function hasWordPressData( win: unknown ): win is WordPressData { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access return !! ( win as any ).wp.data; }