Skip to content

Commit

Permalink
Remove post from queue when not indexable
Browse files Browse the repository at this point in the history
This change fixes updates that trigger multiple filters for the same
post. Currently, when any of the filters adds the post to the update
queue the post is indexed, even when other filters determine
that the post should not be indexed. This change makes the last
filter have the final word about this.
  • Loading branch information
dtakken committed Jun 12, 2024
1 parent a989d9e commit 1f22e86
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
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
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 @@ -176,6 +176,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

0 comments on commit 1f22e86

Please sign in to comment.