Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for the lockPostSaving and unlockPostSaving actions #16713

Merged
merged 2 commits into from
Aug 12, 2019

Conversation

adamsilverstein
Copy link
Member

@adamsilverstein adamsilverstein commented Jul 22, 2019

Description

  • Add @example code examples for lockPostSaving and unlockPostSaving.

See #10649 (comment)

How has this been tested?

Test the example for lockPostSaving by pasting code into the browser console. The post will be unpublishable until the publish date is set to the future.

Screenshots

Example code running:
image

Types of changes

  • Add JSDocs for lockPostSaving and unlockPostSaving.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@draganescu
Copy link
Contributor

So I did this:

  • created a new post
  • pasted the code in the console
  • still was able to publish post

@adamsilverstein
Copy link
Member Author

@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?

@draganescu
Copy link
Contributor

Hi, no errors whatsoever, just didn't do what I expected :) Maybe I did something wrong ...

@adamsilverstein
Copy link
Member Author

@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' );
				}
			}
		}
	} );
}

Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example works as described

@draganescu
Copy link
Contributor

draganescu commented Aug 11, 2019

@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.

@adamsilverstein
Copy link
Member Author

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

@adamsilverstein adamsilverstein merged commit 89bd6b1 into master Aug 12, 2019
@adamsilverstein adamsilverstein deleted the add/docs-for-post-save-locking branch August 12, 2019 16:28
gziolo pushed a commit that referenced this pull request Aug 29, 2019
* Add examples for the lockPostSaving and unlockPostSaving actions

* build docs
gziolo pushed a commit that referenced this pull request Aug 29, 2019
* Add examples for the lockPostSaving and unlockPostSaving actions

* build docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants