diff --git a/includes/classes/Indexable/Post/SyncManager.php b/includes/classes/Indexable/Post/SyncManager.php index a5feafcaa..5f998fa1e 100644 --- a/includes/classes/Indexable/Post/SyncManager.php +++ b/includes/classes/Indexable/Post/SyncManager.php @@ -300,7 +300,7 @@ public function action_delete_post( $post_id ) { Indexables::factory()->get( $this->indexable_slug )->delete( $post_id, false ); /** - * Make sure to remove this post from the sync queue in case an shutdown happens + * Make sure to remove this post from the sync queue in case a shutdown happens * before a redirect when a redirect has already been triggered. */ $this->remove_from_queue( $post_id ); @@ -533,6 +533,7 @@ public function action_set_object_terms( $post_id, $terms, $tt_ids, $taxonomy, $ } if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); return; } @@ -629,6 +630,7 @@ public function action_edited_term( $term_id, $tt_id, $taxonomy ) { // Add all of them to the queue foreach ( $object_ids as $post_id ) { if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); continue; } @@ -693,6 +695,7 @@ public function action_deleted_term_relationships( $post_id, $tt_ids, $taxonomy } if ( ! $this->should_reindex_post( $post_id, $taxonomy ) ) { + $this->remove_from_queue( $post_id ); return; } diff --git a/includes/classes/SyncManager.php b/includes/classes/SyncManager.php index 1f2e54ce3..ea0068767 100644 --- a/includes/classes/SyncManager.php +++ b/includes/classes/SyncManager.php @@ -131,6 +131,10 @@ public function remove_from_queue( $object_id ) { return false; } + if ( ! isset( $this->sync_queue[ $object_id ] ) ) { + return false; + } + $current_blog_id = get_current_blog_id(); if ( ! isset( $this->sync_queue[ $current_blog_id ] ) ) { $this->sync_queue[ $current_blog_id ] = []; diff --git a/tests/cypress/integration/features/protected-content-removal.cy.js b/tests/cypress/integration/features/protected-content-removal.cy.js new file mode 100644 index 000000000..d10261874 --- /dev/null +++ b/tests/cypress/integration/features/protected-content-removal.cy.js @@ -0,0 +1,47 @@ +describe('Set password on post', () => { + it('Removes Post from Index when Password is Set', () => { + cy.login(); + cy.maybeDisableFeature('protected_content'); + + // Delete previous posts, so we can be sure we just expect 1 post. + cy.wpCli('post list --format=ids').then((wpCliResponse) => { + if (wpCliResponse.stdout !== '') { + cy.wpCli(`post delete ${wpCliResponse.stdout}`); + } + }); + + cy.publishPost({ + title: 'Protected Post Removal Test', + }); + + /** + * Give Elasticsearch some time to process the new post. + * + * @todo instead of waiting for an arbitrary time, we should ensure the post is stored. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + // Post is indexed + cy.visit('/?s=Protected+Post+Removal+Test'); + cy.contains('.site-content article h2', 'Protected Post Removal Test').should('exist'); + + cy.wpCli('post list --format=ids').then((wpCliResponse) => { + if (wpCliResponse.stdout !== '') { + cy.postSetPassword(wpCliResponse.stdout, 'enter'); + } + }); + + /** + * Give Elasticsearch some time to process the update. + * + * @todo instead of waiting for an arbitrary time, we should ensure the post is stored. + */ + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.wait(2000); + + // Post is removed from index + cy.visit('/?s=Protected+Post+Removal+Test'); + cy.contains('.site-content article h2', 'Protected Post Removal Test').should('not.exist'); + }); +}); diff --git a/tests/cypress/support/commands.js b/tests/cypress/support/commands.js index c8f415be3..df863039c 100644 --- a/tests/cypress/support/commands.js +++ b/tests/cypress/support/commands.js @@ -197,6 +197,26 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => { cy.wait(2000); }); +Cypress.Commands.add('postSetPassword', (id, password) => { + cy.visitAdminPage(`post.php?post=${id}&action=edit`); + cy.get('h1.editor-post-title__input').click(); + cy.get('body').then(($body) => { + const $button = $body.find('.edit-post-post-visibility__toggle'); + if (!$button.is(':visible')) { + cy.get('.edit-post-header__settings button[aria-label="Settings"]').click(); + } + }); + cy.get('.edit-post-post-visibility__toggle').click(); + cy.get('.editor-post-visibility__dialog-radio, .editor-post-visibility__radio').check( + 'password', + ); + cy.get( + '.editor-post-visibility__dialog-password-input, .editor-post-visibility__password-input', + ).type(password); + + cy.get('.editor-post-publish-button').click(); +}); + Cypress.Commands.add('updateFeatures', (featureName, newValues) => { const escapedNewValues = JSON.stringify(newValues);