| 1 |
import './editor.scss'; |
| 2 |
import './style.scss'; |
| 3 |
|
| 4 |
let { __ } = wp.i18n; |
| 5 |
let { PluginPostStatusInfo } = wp.editPost; |
| 6 |
let { registerPlugin } = wp.plugins; |
| 7 |
let { subscribe, dispatch, select, withSelect, withDispatch } = wp.data; |
| 8 |
let { compose } = wp.compose; |
| 9 |
let { SelectControl } = wp.components; |
| 10 |
|
| 11 |
/** |
| 12 |
* Map Custom Statuses as options for SelectControl |
| 13 |
*/ |
| 14 |
let statuses = window.EditFlowCustomStatuses.map( s => ({ label: s.name, value: s.slug }) ); |
| 15 |
|
| 16 |
/** |
| 17 |
* Subscribe to changes so we can set a default status and update a button's text. |
| 18 |
*/ |
| 19 |
let buttonTextObserver = null; |
| 20 |
subscribe( function () { |
| 21 |
const postId = select( 'core/editor' ).getCurrentPostId(); |
| 22 |
if ( ! postId ) { |
| 23 |
// Post isn't ready yet so don't do anything. |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
// For new posts, we need to force the default custom status. |
| 28 |
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost(); |
| 29 |
if ( isCleanNewPost ) { |
| 30 |
dispatch( 'core/editor' ).editPost( { |
| 31 |
status: ef_default_custom_status |
| 32 |
} ); |
| 33 |
} |
| 34 |
|
| 35 |
// If the save button exists, let's update the text if needed. |
| 36 |
maybeUpdateButtonText( document.querySelector( '.editor-post-save-draft' ) ); |
| 37 |
|
| 38 |
// The post is being saved, so we need to set up an observer to update the button text when it's back. |
| 39 |
if ( buttonTextObserver === null && window.MutationObserver && select( 'core/editor' ).isSavingPost() ) { |
| 40 |
buttonTextObserver = createButtonObserver( document.querySelector( '.edit-post-header__settings' ) ); |
| 41 |
} |
| 42 |
} ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Create a mutation observer that will update the |
| 46 |
* save button text right away when it's changed/re-added. |
| 47 |
* |
| 48 |
* Ideally there will be better ways to go about this in the future. |
| 49 |
* @see https://github.com/Automattic/Edit-Flow/issues/583 |
| 50 |
*/ |
| 51 |
function createButtonObserver( parentNode ) { |
| 52 |
if ( ! parentNode ) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
const observer = new MutationObserver( ( mutationsList ) => { |
| 57 |
for ( const mutation of mutationsList ) { |
| 58 |
for ( const node of mutation.addedNodes ) { |
| 59 |
maybeUpdateButtonText( node ); |
| 60 |
} |
| 61 |
} |
| 62 |
} ); |
| 63 |
|
| 64 |
observer.observe( parentNode, { childList: true } ); |
| 65 |
return observer; |
| 66 |
} |
| 67 |
|
| 68 |
function maybeUpdateButtonText( saveButton ) { |
| 69 |
/* |
| 70 |
* saveButton.children < 1 accounts for when a user hovers over the save button |
| 71 |
* and a tooltip is rendered |
| 72 |
*/ |
| 73 |
if ( saveButton && saveButton.children < 1 && ( saveButton.innerText === __( 'Save Draft' ) || saveButton.innerText === __( 'Save as Pending' ) ) ) { |
| 74 |
saveButton.innerText = __( 'Save' ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Custom status component |
| 80 |
* @param object props |
| 81 |
*/ |
| 82 |
let EditFlowCustomPostStati = ( { onUpdate, status } ) => ( |
| 83 |
<PluginPostStatusInfo |
| 84 |
className={ `edit-flow-extended-post-status edit-flow-extended-post-status-${status}` } |
| 85 |
> |
| 86 |
<h4>{ status !== 'publish' ? __( 'Extended Post Status', 'edit-flow' ) : __( 'Extended Post Status Disabled.', 'edit-flow' ) }</h4> |
| 87 |
|
| 88 |
{ status !== 'publish' ? <SelectControl |
| 89 |
label="" |
| 90 |
value={ status } |
| 91 |
options={ statuses } |
| 92 |
onChange={ onUpdate } |
| 93 |
/> : null } |
| 94 |
|
| 95 |
<small className="edit-flow-extended-post-status-note"> |
| 96 |
{ status !== 'publish' ? __( `Note: this will override all status settings above.`, 'edit-flow' ) : __( 'To select a custom status, please unpublish the content first.', 'edit-flow' ) } |
| 97 |
</small> |
| 98 |
</PluginPostStatusInfo> |
| 99 |
); |
| 100 |
|
| 101 |
const mapSelectToProps = ( select ) => { |
| 102 |
return { |
| 103 |
status: select('core/editor').getEditedPostAttribute('status'), |
| 104 |
}; |
| 105 |
}; |
| 106 |
|
| 107 |
const mapDispatchToProps = ( dispatch ) => { |
| 108 |
return { |
| 109 |
onUpdate( status ) { |
| 110 |
dispatch( 'core/editor' ).editPost( { status } ); |
| 111 |
}, |
| 112 |
}; |
| 113 |
}; |
| 114 |
|
| 115 |
let plugin = compose( |
| 116 |
withSelect( mapSelectToProps ), |
| 117 |
withDispatch( mapDispatchToProps ) |
| 118 |
)( EditFlowCustomPostStati ); |
| 119 |
|
| 120 |
/** |
| 121 |
* Kick it off |
| 122 |
*/ |
| 123 |
registerPlugin( 'edit-flow-custom-status', { |
| 124 |
icon: 'edit-flow', |
| 125 |
render: plugin |
| 126 |
} ); |
| 127 |
|