Add examples for the lockPostSaving and unlockPostSaving actions#16713
Conversation
|
So I did this:
|
|
@draganescu Thanks for testing. Hmmm, let me re-test that, it worked when I tested earlier. Did you see any errors in the browser console? |
|
Hi, no errors whatsoever, just didn't do what I expected :) Maybe I did something wrong ... |
|
@draganescu did you complete the publish process or only notice that the publish button was active? If you click it you sill see the next publish button should be disabled: To confirm here is the code snippet I am testing with const { subscribe } = wp.data;
const initialPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
// Only allow publishing posts that are set to a future date.
if ( 'publish' !== initialPostStatus ) {
// Track locking.
let locked = false;
// Watch for the publish event.
let unssubscribe = subscribe( () => {
const currentPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
if ( 'publish' !== currentPostStatus ) {
// Compare the post date to the current date, lock the post if the date isn't in the future.
const postDate = new Date( wp.data.select( 'core/editor' ).getEditedPostAttribute( 'date' ) );
const currentDate = new Date();
if ( postDate.getTime() <= currentDate.getTime() ) {
if ( ! locked ) {
locked = true;
wp.data.dispatch( 'core/editor' ).lockPostSaving( 'futurelock' );
}
} else {
if ( locked ) {
locked = false;
wp.data.dispatch( 'core/editor' ).unlockPostSaving( 'futurelock' );
}
}
}
} );
} |
draganescu
left a comment
There was a problem hiding this comment.
Example works as described
|
@adamsilverstein yes, that was my issue, I expected that publish would become inactive immediately not after I clicked :) Thank you for the explanation! You should also build the docs and commit the changes I think for the tests to pass. |
Ah right, thanks for the reminder. Built in 786c6a7 |
* Add examples for the lockPostSaving and unlockPostSaving actions * build docs
* Add examples for the lockPostSaving and unlockPostSaving actions * build docs

Description
@examplecode examples forlockPostSavingandunlockPostSaving.See #10649 (comment)
How has this been tested?
Test the example for
lockPostSavingby pasting code into the browser console. The post will be unpublishable until the publish date is set to the future.Screenshots
Example code running:

Types of changes
Checklist: