From 080f197d11cd2566e8352013ff7380484860fa28 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Thu, 13 Apr 2023 17:28:03 -0500 Subject: [PATCH 1/5] Support custom validation by allowing disabling of the save itself I've searched high and low for code that would allow me to keep the Save / Publish button active initially so that upon interacting with the button it would trigger some additional validations before bailing on the save and then subsequently enabling save locking. Ultimately, the only way around this is to listen to the click event on the button itself and then bail there. I believe this could be handled much more directly within the component through a new filter which would allow a better approach towards running any custom validations that need to happen before allowing the save to continue. --- .../editor/src/components/post-publish-button/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index b4ea3a07258455..ac8fcf112ce3b2 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -10,6 +10,7 @@ import { Button } from '@wordpress/components'; import { Component, createRef } from '@wordpress/element'; import { withSelect, withDispatch } from '@wordpress/data'; import { compose } from '@wordpress/compose'; +import { applyFilters } from '@wordpress/hooks'; import { __ } from '@wordpress/i18n'; /** @@ -153,6 +154,12 @@ export class PostPublishButton extends Component { if ( isButtonDisabled ) { return; } + + // Allow for overriding the ability to save for custom validations and other customizations. + if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props ) ) { + return; + } + onSubmit(); onStatusChange( publishStatus ); onSave(); From 5d7560e8bac994777c51c6b6681c98ae9a801bbe Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Thu, 13 Apr 2023 17:38:09 -0500 Subject: [PATCH 2/5] Move filter above the disabled button check --- packages/editor/src/components/post-publish-button/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index ac8fcf112ce3b2..0f60db1bc06867 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -151,12 +151,12 @@ export class PostPublishButton extends Component { } const onClickButton = () => { - if ( isButtonDisabled ) { + // Allow for overriding the ability to save for custom validations and other customizations. + if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props ) ) { return; } - // Allow for overriding the ability to save for custom validations and other customizations. - if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props ) ) { + if ( isButtonDisabled ) { return; } From 742002b54fbd324a1c23acf29db66b87a2cf970c Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Thu, 13 Apr 2023 17:45:28 -0500 Subject: [PATCH 3/5] Update text --- packages/editor/src/components/post-publish-button/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index 0f60db1bc06867..02e48561877d4e 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -151,7 +151,7 @@ export class PostPublishButton extends Component { } const onClickButton = () => { - // Allow for overriding the ability to save for custom validations and other customizations. + // Allow for overriding saving for custom validations. if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props ) ) { return; } From 77e3ef970726523b2ce68c1f314456d8cead243a Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Tue, 18 Jun 2024 09:37:09 -0500 Subject: [PATCH 4/5] Fix for code styling --- packages/editor/src/components/post-publish-button/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index 0a8f7db727816e..a77abae618a368 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -159,14 +159,14 @@ export class PostPublishButton extends Component { const onClickButton = () => { // Allow for overriding saving for custom validations. - if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props ) ) { + if (!applyFilters('editor.PostPublishButton.shouldSubmit',true,this.props)) { return; } if ( isButtonDisabled ) { return; } - + onSubmit(); savePostStatus( publishStatus ); }; From f329d5544be7f2d4fcc4763690e64663bd1989c9 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Tue, 18 Jun 2024 09:57:42 -0500 Subject: [PATCH 5/5] Update code styling and add publishStatus to filter args --- packages/editor/src/components/post-publish-button/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index a77abae618a368..334826c058f415 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -159,7 +159,7 @@ export class PostPublishButton extends Component { const onClickButton = () => { // Allow for overriding saving for custom validations. - if (!applyFilters('editor.PostPublishButton.shouldSubmit',true,this.props)) { + if ( ! applyFilters( 'editor.PostPublishButton.shouldSubmit', true, this.props, publishStatus ) ) { return; }