Skip to content

Commit

Permalink
Merge pull request #2919 from dparker1005/only-save-restricted-levels…
Browse files Browse the repository at this point in the history
…-once

Fixing errors when API call to update post restrictions is called multiple times
  • Loading branch information
ideadude authored Mar 27, 2024
2 parents 9913ed1 + 16c3f30 commit 33dde9a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion blocks/build/sidebar/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-api-fetch', 'wp-data', 'wp-element'), 'version' => '40addcd3480c492aa9bf');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-data', 'wp-element'), 'version' => '3a0536de8f5cec3496a0');
2 changes: 1 addition & 1 deletion blocks/build/sidebar/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion blocks/src/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,42 @@ register( pmproCustomStore() );
} );

// Whenever a post is saved, call the saveRestrictedLevels action.
// Adapted from here to ensure API is only called once: https://github.com/WordPress/gutenberg/issues/17632#issuecomment-819379829
/**
* Consults values to determine whether the editor is busy saving a post.
* Includes checks on whether the save button is busy.
*
* @returns {boolean} Whether the editor is on a busy save state.
*/
function isSavingPost() {

// State data necessary to establish if a save is occuring.
const isSaving = wp.data.select('core/editor').isSavingPost() || wp.data.select('core/editor').isAutosavingPost();
const isSaveable = wp.data.select('core/editor').isEditedPostSaveable();
const isPostSavingLocked = wp.data.select('core/editor').isPostSavingLocked();
const hasNonPostEntityChanges = wp.data.select('core/editor').hasNonPostEntityChanges();
const isAutoSaving = wp.data.select('core/editor').isAutosavingPost();
const isButtonDisabled = isSaving || !isSaveable || isPostSavingLocked;

// Reduces state into checking whether the post is saving and that the save button is disabled.
const isBusy = !isAutoSaving && isSaving;
const isNotInteractable = isButtonDisabled && ! hasNonPostEntityChanges;

return isBusy && isNotInteractable;
}

// Current saving state. isSavingPost is defined above.
var wasSaving = isSavingPost();
wp.data.subscribe( function () {
if ( wp.data.select( 'core/editor' ).isSavingPost() ) {
// New saving state
let isSaving = isSavingPost();

// It is done saving if it was saving and it no longer is.
let isDoneSaving = wasSaving && !isSaving;

// Update value for next use.
wasSaving = isSaving;
if ( isDoneSaving ) {
dispatch( 'pmpro/require-membership' ).saveRestrictedLevels();
}
} );
Expand Down

0 comments on commit 33dde9a

Please sign in to comment.