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

Fix remove from index on update #3937

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions includes/classes/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] = [];
Expand Down
47 changes: 47 additions & 0 deletions tests/cypress/integration/features/protected-content-removal.cy.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
20 changes: 20 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading