| 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 |
* Hack :( |
| 18 |
* |
| 19 |
* @see https://github.com/WordPress/gutenberg/issues/3144 |
| 20 |
* |
| 21 |
* Gutenberg overrides the label of the Save button after save (i.e. "Save Draft"). But there's no way to subscribe to a "post save" message. |
| 22 |
* |
| 23 |
* So instead, we're keeping the button label generic ("Save"). There's a brief period where it still flips to "Save Draft" but that's something we need to work upstream to find a good fix for. |
| 24 |
*/ |
| 25 |
let sideEffectL10nManipulation = () => { |
| 26 |
let node = document.querySelector('.editor-post-save-draft'); |
| 27 |
if ( node ) { |
| 28 |
document.querySelector( '.editor-post-save-draft' ).innerText = `${ __( 'Save' ) }` |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Set the status to the default custom status. |
| 33 |
subscribe( function () { |
| 34 |
const postId = select( 'core/editor' ).getCurrentPostId(); |
| 35 |
// Post isn't ready yet so don't do anything. |
| 36 |
if ( ! postId ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// For new posts, we need to force the our default custom status. |
| 41 |
// Otherwise WordPress will force it to "Draft". |
| 42 |
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost(); |
| 43 |
if ( isCleanNewPost ) { |
| 44 |
dispatch( 'core/editor' ).editPost( { |
| 45 |
status: ef_default_custom_status |
| 46 |
} ); |
| 47 |
|
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Update the "Save" button. |
| 52 |
var status = select( 'core/editor' ).getEditedPostAttribute( 'status' ); |
| 53 |
if ( typeof status !== 'undefined' && status !== 'publish' ) { |
| 54 |
sideEffectL10nManipulation(); |
| 55 |
} |
| 56 |
} ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Custom status component |
| 60 |
* @param object props |
| 61 |
*/ |
| 62 |
let EditFlowCustomPostStati = ( { onUpdate, status } ) => ( |
| 63 |
<PluginPostStatusInfo |
| 64 |
className={ `edit-flow-extended-post-status edit-flow-extended-post-status-${status}` } |
| 65 |
> |
| 66 |
<h4>{ status !== 'publish' ? __( 'Extended Post Status', 'edit-flow' ) : __( 'Extended Post Status Disabled.', 'edit-flow' ) }</h4> |
| 67 |
|
| 68 |
{ status !== 'publish' ? <SelectControl |
| 69 |
label="" |
| 70 |
value={ status } |
| 71 |
options={ statuses } |
| 72 |
onChange={ onUpdate } |
| 73 |
/> : null } |
| 74 |
|
| 75 |
<small className="edit-flow-extended-post-status-note"> |
| 76 |
{ status !== 'publish' ? __( `Note: this will override all status settings above.`, 'edit-flow' ) : __( 'To select a custom status, please unpublish the content first.', 'edit-flow' ) } |
| 77 |
</small> |
| 78 |
</PluginPostStatusInfo> |
| 79 |
); |
| 80 |
|
| 81 |
const mapSelectToProps = ( select ) => { |
| 82 |
return { |
| 83 |
status: select('core/editor').getEditedPostAttribute('status'), |
| 84 |
}; |
| 85 |
}; |
| 86 |
|
| 87 |
const mapDispatchToProps = ( dispatch ) => { |
| 88 |
return { |
| 89 |
onUpdate( status ) { |
| 90 |
dispatch( 'core/editor' ).editPost( { status } ); |
| 91 |
sideEffectL10nManipulation(); |
| 92 |
}, |
| 93 |
}; |
| 94 |
}; |
| 95 |
|
| 96 |
let plugin = compose( |
| 97 |
withSelect( mapSelectToProps ), |
| 98 |
withDispatch( mapDispatchToProps ) |
| 99 |
)( EditFlowCustomPostStati ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Kick it off |
| 103 |
*/ |
| 104 |
registerPlugin( 'edit-flow-custom-status', { |
| 105 |
icon: 'edit-flow', |
| 106 |
render: plugin |
| 107 |
} ); |
| 108 |
|