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

Support transforming legacy widgets to blocks and hide widgets when the blocks are available #2819

Merged
merged 22 commits into from
Aug 12, 2022

Conversation

JakePT
Copy link
Contributor

@JakePT JakePT commented Jun 2, 2022

Description of the Change

This PR does two things:

  1. Adds support for transforming the legacy Facet and Related Posts widgets into blocks. For these widgets when the block icon is clicked the Transform to menu will include the new block versions of these widgets. The settings of the legacy widget will be carried over to the block.
  2. Hides the Facet and Related Posts widgets from the Legacy Widget block. This means that when the block editor is being used there won't be two Facet or Related Posts blocks, which could cause confusion. This won't affect any existing instances of the legacy widgets, which will still be visible and editable, but it will prevent new instances being added. The legacy widgets will also still be usable if the site is using the Classic Widgets plugin, or similar, to disable the block editor for widgets.

Both changes are implemented as per the guidance in the Block Editor Handbook and the behaviour is consistent with how most core legacy widgets are handled, such as Search.

Closes #2792

Possible Drawbacks

While existing block instances won't be affected, the user will not be able to add new instances of the legacy widget.

However, if the user doesn't want to switch existing instances to the block and needs to add new legacy widget instances, then there are two workarounds:

  1. Use the core Widget Group block, which will wrap the block in the markup defined by the theme for the widget area.
  2. Un-hide the legacy widgets by using the widget_types_to_hide_from_legacy_widget_block filter hook to remove their IDs from the list filtered by the hook.

Verification Process

  1. Before applying this PR, add several Facet and Related Posts legacy widget blocks with a variety of settings.
  2. After applying the PR, go to the Widgets editor and the legacy widget blocks should still be visible.
  3. Attempt to add a new block, when searching for "Facet" or "Related Posts" only one option from ElasticPress should appear for each and it should be the block, not the legacy widget.
  4. Install and activate Classic Widgets. With this activated the legacy widgets should be available in the widgets editor.
  5. Deactivate the plugin.
  6. On the widgets screen, select each legacy widget and click the block's icon in the block toolbar. There should be an option to transform it into the block version of the widget. Clicking to transform the widget should convert it to the block and the settings of the widget should be reflected on the new block. If the widget had a Title this should be added as a heading block before the new block.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Added - Support for transforming instances of the legacy Facet and Related Posts widgets into blocks.
Changed - The legacy Facet and Related Posts widgets are now hidden when using the block editor.

Credits

Props @JakePT

@JakePT JakePT added this to the 4.3.0 milestone Jun 2, 2022
@JakePT JakePT requested a review from felipeelia June 2, 2022 14:19
@JakePT JakePT self-assigned this Jun 2, 2022
@felipeelia felipeelia modified the milestones: 4.3.0, 4.2.1 Jun 7, 2022
@felipeelia felipeelia modified the milestones: 4.2.1, 4.3.0 Jun 21, 2022
@JakePT JakePT changed the base branch from develop to trunk July 25, 2022 06:46
@JakePT JakePT changed the base branch from trunk to develop July 25, 2022 06:46
@JakePT JakePT marked this pull request as draft July 25, 2022 09:46
@JakePT
Copy link
Contributor Author

JakePT commented Jul 25, 2022

I've finally added complete tests for the Related Posts block, but the Facets block still needs new tests as the current tests are based on using the Legacy Widget as a block.

@JakePT JakePT marked this pull request as ready for review August 9, 2022 09:13
@JakePT JakePT removed the needs tests label Aug 9, 2022
@JakePT JakePT assigned felipeelia and unassigned JakePT Aug 9, 2022
Copy link
Member

@felipeelia felipeelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks really good @JakePT. Left some comments about specific items.

I'd like to know your thoughts about testing the "transform" part of those blocks. Isn't that worth testing in Cypress?

/**
* Test that the Related Posts block is functional.
*/
it('Facet block', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakePT do you mind rephrasing this to describe what is being tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeelia Done.

* Test that the Facet widget is functional and can be transformed into the
* Facet block.
*/
it('Facet widget', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakePT same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeelia Done.

/**
* Visit the block-based widgets screen.
*/
cy.wpCli('wp plugin deactivate classic-widgets');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakePT we have a command to activate and deactivate plugins. I'd prefer to stick with that just to be sure any performance improvement we have is applied across the entire test suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeelia Done.

cy.wpCli('elasticpress index --setup --yes');
cy.wpCli('plugin install classic-widgets');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakePT can we install the plugin during the env setup? It would be a matter of adding it to the .wp-env file. Hopefully that gets cached in a good way (if not now, in the future) and it doesn't slow down the test execution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeelia Done.

Comment on lines 274 to 302
Cypress.Commands.add('openBlockSettingsSidebar', () => {
cy.get('body').then(($el) => {
if ($el.hasClass('widgets-php')) {
cy.get('.edit-widgets-header__actions button[aria-label="Settings"]').click();
cy.get('.edit-widgets-sidebar__panel-tab').contains('Block').click();
} else {
cy.get('.edit-post-header__settings button[aria-label="Settings"]').click();
cy.get('.edit-post-sidebar__panel-tab').contains('Block').click();
}
});
});

Cypress.Commands.add('openBlockInserter', () => {
cy.get('body').then(($el) => {
if ($el.hasClass('widgets-php')) {
cy.get('.edit-widgets-header-toolbar__inserter-toggle').click();
} else {
cy.get('.edit-post-header-toolbar__inserter-toggle').click();
}
});
});

Cypress.Commands.add('getBlocksList', () => {
cy.get('.block-editor-inserter__block-list');
});

Cypress.Commands.add('insertBlock', (blockName) => {
cy.get('.block-editor-block-types-list__item').contains(blockName).click();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't start a new file only with blocks. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felipeelia Done.

@felipeelia felipeelia assigned JakePT and unassigned felipeelia Aug 10, 2022
@JakePT JakePT assigned felipeelia and unassigned JakePT Aug 11, 2022
@felipeelia felipeelia merged commit 07b4379 into develop Aug 12, 2022
@felipeelia felipeelia deleted the feature/2792 branch August 12, 2022 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hide legacy widgets when not already in use
2 participants