Skip to content

Commit

Permalink
Merge pull request #2861 from rebeccahum/add/ep_disable_sync_update-f…
Browse files Browse the repository at this point in the history
…ilter

Add new filter ep_skip_autosave_sync and enable autosave sync for protected_content feature
  • Loading branch information
felipeelia authored Aug 5, 2022
2 parents 2915993 + a3d4fc5 commit e74e84a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 19 deletions.
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"wp-content/plugins/fake-new-activation.php": "./tests/cypress/wordpress-files/test-plugins/fake-new-activation.php",
"wp-content/plugins/unsupported-server-software.php": "./tests/cypress/wordpress-files/test-plugins/unsupported-server-software.php",
"wp-content/plugins/unsupported-elasticsearch-version.php": "./tests/cypress/wordpress-files/test-plugins/unsupported-elasticsearch-version.php",
"wp-content/plugins/shorten-autosave.php": "./tests/cypress/wordpress-files/test-plugins/shorten-autosave.php",
"wp-content/uploads/content-example.xml": "./tests/cypress/wordpress-files/test-docs/content-example.xml"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function setup() {
add_filter( 'ep_post_sync_args', [ $this, 'include_post_password' ], 10, 2 );
add_filter( 'ep_post_sync_args', [ $this, 'remove_fields_from_password_protected' ], 11, 2 );
add_filter( 'ep_search_post_return_args', [ $this, 'return_post_password' ] );
add_filter( 'ep_skip_autosave_sync', '__return_false' );

if ( is_admin() ) {
add_filter( 'ep_admin_wp_query_integration', '__return_true' );
Expand Down
99 changes: 80 additions & 19 deletions includes/classes/Indexable/Post/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,22 @@ public function action_queue_meta_sync( $meta_id, $object_id, $meta_key, $meta_v

$indexable = Indexables::factory()->get( $this->indexable_slug );

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
/**
* Filter to whether skip a sync during autosave, defaults to true
*
* @hook ep_skip_autosave_sync
* @since 4.3.0
* @param {bool} $skip True means to disable sync for autosaves
* @param {string} $function Function applying filter
* @return {boolean} New value
*/
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
}

$post = get_post( $object_id );
Expand Down Expand Up @@ -281,11 +292,22 @@ public function action_sync_on_update( $post_id ) {
$indexable = Indexables::factory()->get( $this->indexable_slug );
$post_type = get_post_type( $post_id );

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
/**
* Filter to whether skip a sync during autosave, defaults to true
*
* @hook ep_skip_autosave_sync
* @since 4.3.0
* @param {bool} $skip True means to disable sync for autosaves
* @param {string} $function Function applying filter
* @return {boolean} New value
*/
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
}

/**
Expand Down Expand Up @@ -359,9 +381,22 @@ public function action_set_object_terms( $post_id, $terms, $tt_ids, $taxonomy, $
return;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
return;
/**
* Filter to whether skip a sync during autosave, defaults to true
*
* @hook ep_skip_autosave_sync
* @since 4.3.0
* @param {bool} $skip True means to disable sync for autosaves
* @param {string} $function Function applying filter
* @return {boolean} New value
*/
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
}

/**
Expand Down Expand Up @@ -413,9 +448,22 @@ public function action_set_object_terms( $post_id, $terms, $tt_ids, $taxonomy, $
public function action_edited_term( $term_id, $tt_id, $taxonomy ) {
global $wpdb;

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
return;
/**
* Filter to whether skip a sync during autosave, defaults to true
*
* @hook ep_skip_autosave_sync
* @since 4.3.0
* @param {bool} $skip True means to disable sync for autosaves
* @param {string} $function Function applying filter
* @return {boolean} New value
*/
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
}

// Find ID of all attached posts (query lifted from wp_delete_term())
Expand Down Expand Up @@ -476,9 +524,22 @@ public function action_deleted_term_relationships( $post_id, $tt_ids, $taxonomy
return;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
return;
/**
* Filter to whether skip a sync during autosave, defaults to true
*
* @hook ep_skip_autosave_sync
* @since 4.3.0
* @param {bool} $skip True means to disable sync for autosaves
* @param {string} $function Function applying filter
* @return {boolean} New value
*/
if ( apply_filters( 'ep_skip_autosave_sync', true, __FUNCTION__ ) ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// Bypass saving if doing autosave
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/cypress/integration/features/protected-content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ describe('Protected Content Feature', () => {
cy.visitAdminPage('edit.php?post_status=draft&post_type=post');
cy.getTotal(1);
});

it('Can sync autosaved drafts', () => {
cy.login();

cy.maybeEnableFeature('protected_content');

// Delete previous drafts, so we can be sure we just expect 1 draft post.
cy.wpCli('post list --post_status=draft --format=ids').then((wpCliResponse) => {
if (wpCliResponse.stdout !== '') {
cy.wpCli(`post delete ${wpCliResponse.stdout}`);
}
});

cy.wpCli('elasticpress index --setup --yes');

cy.createAutosavePost();

cy.visitAdminPage('edit.php?post_status=draft&post_type=post');
cy.getTotal(1);
});
});
28 changes: 28 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,31 @@ Cypress.Commands.add('deactivatePlugin', (slug, method = 'dashboard', mode = 'si
}
cy.wpCli(command);
});

Cypress.Commands.add('createAutosavePost', (postData) => {
cy.activatePlugin('shorten-autosave', 'wpCli');
const newPostData = { title: 'Test Post', content: 'Test content.', ...postData };

cy.visitAdminPage('post-new.php');
cy.get('h1.editor-post-title__input, #post-title-0').should('exist');
cy.get('body').then(($body) => {
const welcomeGuide = $body.find(
'.edit-post-welcome-guide .components-modal__header button',
);
cy.log(welcomeGuide);
if (welcomeGuide.length) {
welcomeGuide.click();
}
});

cy.get('h1.editor-post-title__input, #post-title-0').clearThenType(newPostData.title);
cy.get('.block-editor-default-block-appender__content').type(newPostData.content);

/**
* Wait for autosave to complete.
*
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(5000);
cy.deactivatePlugin('shorten-autosave', 'wpCli');
});
10 changes: 10 additions & 0 deletions tests/cypress/wordpress-files/test-plugins/shorten-autosave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Plugin Name: Shorten autosave
* Description: Shorten default autosave period to test protected content feature.
* Version: 1.0.0
* Author: Rebecca Hum
* License: GPLv2 or later
*/

define( 'AUTOSAVE_INTERVAL', 2 );

0 comments on commit e74e84a

Please sign in to comment.