| 1 |
import './editor.scss'; |
| 2 |
import './style.scss'; |
| 3 |
|
| 4 |
const { __ } = wp.i18n; |
| 5 |
const { PluginPostStatusInfo } = wp.editPost; |
| 6 |
const { registerPlugin } = wp.plugins; |
| 7 |
const { subscribe, dispatch, select, withSelect, withDispatch } = wp.data; |
| 8 |
const { compose } = wp.compose; |
| 9 |
const { SelectControl } = wp.components; |
| 10 |
|
| 11 |
/** |
| 12 |
* Map Custom Statuses as options for SelectControl |
| 13 |
*/ |
| 14 |
const 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 ( |
| 40 |
buttonTextObserver === null && |
| 41 |
window.MutationObserver && |
| 42 |
select( 'core/editor' ).isSavingPost() |
| 43 |
) { |
| 44 |
buttonTextObserver = createButtonObserver( |
| 45 |
document.querySelector( '.edit-post-header__settings' ) |
| 46 |
); |
| 47 |
} |
| 48 |
} ); |
| 49 |
|
| 50 |
/** |
| 51 |
* Create a mutation observer that will update the |
| 52 |
* save button text right away when it's changed/re-added. |
| 53 |
* |
| 54 |
* Ideally there will be better ways to go about this in the future. |
| 55 |
* @see https://github.com/Automattic/Edit-Flow/issues/583 |
| 56 |
*/ |
| 57 |
function createButtonObserver( parentNode ) { |
| 58 |
if ( ! parentNode ) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
const observer = new MutationObserver( mutationsList => { |
| 63 |
for ( const mutation of mutationsList ) { |
| 64 |
for ( const node of mutation.addedNodes ) { |
| 65 |
maybeUpdateButtonText( node ); |
| 66 |
} |
| 67 |
} |
| 68 |
} ); |
| 69 |
|
| 70 |
observer.observe( parentNode, { childList: true } ); |
| 71 |
return observer; |
| 72 |
} |
| 73 |
|
| 74 |
function maybeUpdateButtonText( saveButton ) { |
| 75 |
/* |
| 76 |
* saveButton.children < 1 accounts for when a user hovers over the save button |
| 77 |
* and a tooltip is rendered |
| 78 |
*/ |
| 79 |
if ( |
| 80 |
saveButton && |
| 81 |
saveButton.children < 1 && |
| 82 |
( saveButton.innerText === __( 'Save Draft' ) || |
| 83 |
saveButton.innerText === __( 'Save as Pending' ) ) |
| 84 |
) { |
| 85 |
saveButton.innerText = __( 'Save' ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Custom status component |
| 91 |
* @param object props |
| 92 |
*/ |
| 93 |
const EditFlowCustomPostStati = ( { onUpdate, status } ) => ( |
| 94 |
<PluginPostStatusInfo |
| 95 |
className={ `edit-flow-extended-post-status edit-flow-extended-post-status-${ status }` } |
| 96 |
> |
| 97 |
<h4> |
| 98 |
{ status !== 'publish' |
| 99 |
? __( 'Extended Post Status', 'edit-flow' ) |
| 100 |
: __( 'Extended Post Status Disabled.', 'edit-flow' ) } |
| 101 |
</h4> |
| 102 |
|
| 103 |
{ status !== 'publish' ? ( |
| 104 |
<SelectControl label="" value={ status } options={ statuses } onChange={ onUpdate } /> |
| 105 |
) : null } |
| 106 |
|
| 107 |
<small className="edit-flow-extended-post-status-note"> |
| 108 |
{ status !== 'publish' |
| 109 |
? __( 'Note: this will override all status settings above.', 'edit-flow' ) |
| 110 |
: __( 'To select a custom status, please unpublish the content first.', 'edit-flow' ) } |
| 111 |
</small> |
| 112 |
</PluginPostStatusInfo> |
| 113 |
); |
| 114 |
|
| 115 |
const mapSelectToProps = select => { |
| 116 |
return { |
| 117 |
status: select( 'core/editor' ).getEditedPostAttribute( 'status' ), |
| 118 |
}; |
| 119 |
}; |
| 120 |
|
| 121 |
const mapDispatchToProps = dispatch => { |
| 122 |
return { |
| 123 |
onUpdate( status ) { |
| 124 |
dispatch( 'core/editor' ).editPost( { status } ); |
| 125 |
}, |
| 126 |
}; |
| 127 |
}; |
| 128 |
|
| 129 |
const plugin = compose( |
| 130 |
withSelect( mapSelectToProps ), |
| 131 |
withDispatch( mapDispatchToProps ) |
| 132 |
)( EditFlowCustomPostStati ); |
| 133 |
|
| 134 |
/** |
| 135 |
* Kick it off |
| 136 |
*/ |
| 137 |
registerPlugin( 'edit-flow-custom-status', { |
| 138 |
icon: 'edit-flow', |
| 139 |
render: plugin, |
| 140 |
} ); |
| 141 |
|