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

Add a filter to build_query_vars_from_query_block #43590

Conversation

sunyatasattva
Copy link
Contributor

@sunyatasattva sunyatasattva commented Aug 25, 2022

What?

This PR adds a filter to the build_query_vars_from_query_block which is used to parse a WP_Query and pass it down the Query Loop block context.

Why?

This allows Query Loop block variations to hook into the query and possibly inject custom query parameters, enabling developers to support all kinds of custom queries and create powerful variations on the Query Loop block.

For an example on how this can be used to achieve this goal, see this PR: woocommerce/woocommerce-blocks#6812 (specifically, this filter is used here).

Paging @gziolo and @ntsekouras.

How?

The minimal change to enable this is to wrap the return value of the function in a filter.

Testing Instructions

  1. Add a callback to your PHP code that hooks to this filter, e.g.
add_filter(
  'gutenberg_build_query_vars_from_query_block',
  function ( $query ) {
    $query[ 'offset' ] = 1;

    return $query;
 },
 10,
 1
);
  1. Add multiple Query Loop blocks to your pages with different settings.
  2. Save the page and go to the front-end.
  3. Notice that all the blocks will render with your modified query (e.g. start from an offset of one, if the above is used).

A way to test the practical use of this change would be to use the above-mentioned PR to see how it could work. Or just pull from trunk on WooCommerce Blocks.

The filter allows Query Loop block variation to hook into the query
and possibly inject custom query parameters.
@gziolo gziolo added the [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked. label Aug 25, 2022
@gziolo
Copy link
Member

gziolo commented Aug 25, 2022

@sunyatasattva, thank you for opening this PR. I see this filter applicable in many more cases. It would help to handle custom post types or even custom fields for existing REST API endpoints registered by plugins 👍🏻

The way to move forward with this PR would be to include the documentation for the hook like here in this repo:

/**
* Filters the fallback experience for the Navigation block.
*
* Returning a falsey value will opt out of the fallback and cause the block not to render.
* To customise the blocks provided return an array of blocks - these should be valid
* children of the `core/navigation` block.
*
* @since 5.9.0
*
* @param array[] default fallback blocks provided by the default block mechanic.
*/
return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks );

or like here in WordPress core:

https://github.com/WordPress/wordpress-develop/blob/7f7d616d822b79c952cbd6a3046a6f6a8aa5a35e/src/wp-includes/blocks.php#L262-L269

We also need to settle on the name of the hook that is going to stay forever. It can't be prefixed with gutenberg_ and it can be shorter than the function name. You can check the existing WordPress hooks for inspiration at https://developer.wordpress.org/reference/hooks/.

We will also need to move the updated function from lib/compat/wordpress-6.0/blocks.php to lib/compat/wordpress-6.1/blocks.php to ensure it polyfills the functionality in the Gutenberg plugin correctly. Later, we will need to backport those changes to WordPress Core to land them in WordPress 6.1 (planned for November 1st).

@gziolo gziolo removed the [Type] WP Core Ticket Requires an upstream change from WordPress. Core Trac ticket should be linked. label Aug 25, 2022
@gziolo gziolo requested a review from ntsekouras August 25, 2022 06:07
@gziolo gziolo added Backport from WordPress Core Pull request that needs to be backported to a Gutenberg release from WordPress Core [Feature] Extensibility The ability to extend blocks or the editing experience [Block] Query Loop Affects the Query Loop Block [Type] New API New API to be used by plugin developers or package users. and removed Backport from WordPress Core Pull request that needs to be backported to a Gutenberg release from WordPress Core labels Aug 25, 2022
@ntsekouras
Copy link
Contributor

Thanks for the PR @sunyatasattva! Everything @gziolo said is great 😄

We also need to look into the editor side(related: #34201), but it can happen in a different PR.

@spacedmonkey
Copy link
Member

Php docs are a must have.

@sunyatasattva
Copy link
Contributor Author

Hello @gziolo thank you for your review. I pushed another commit, let's see if I've understood correctly:

  1. The function has been completely moved from compat/wordpress-6.0 to compat/wordpress-6.1, not just duplicated with the new changes. Is that right?
  2. I added the docs to the filter.
  3. I've changed the name of the filter to query_block_query_vars.

The reasoning for my proposal on that name is that:

  1. Filters are not named like verbs, but rather should be nouns.
  2. They should represent what is output by the filter itself.
  3. There are such names for other filters as query_vars or removable_query_args. It seemed to me that such a name could match those naming conventions.

Let me know what you think and if I did everything right.

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Thanks for iterating @sunyatasattva! 💯

if ( ! empty( $block->context['query']['search'] ) ) {
$query['s'] = $block->context['query']['search'];
}
if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interestingly enough this code from here was close to the 6.0 release and didn't land there, so we should have moved this file to compat/6.1 either way.. Good timing for this change 😄

lib/compat/wordpress-6.1/blocks.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/blocks.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/blocks.php Outdated Show resolved Hide resolved
lib/compat/wordpress-6.1/blocks.php Show resolved Hide resolved
lib/compat/wordpress-6.1/blocks.php Outdated Show resolved Hide resolved
sunyatasattva and others added 2 commits August 30, 2022 13:31
* Change filter name to `query_loop_block_query_vars`
* Fix docs typo
* Fix docs version

Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
@sunyatasattva sunyatasattva requested review from ntsekouras and removed request for spacedmonkey August 30, 2022 11:37
@sunyatasattva
Copy link
Contributor Author

@ntsekouras Feedback implemented, let me know if there is anything else that should be done 🙏

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Great work, thanks @sunyatasattva! This looks good to me! 💯

I'd appreciate a final look from @gziolo, but I think it's good to 🚢

lib/compat/wordpress-6.1/blocks.php Outdated Show resolved Hide resolved
Comment on lines +333 to +334
* This can help, for example, to include additional settings or meta queries not
* directly supported by the core Query Loop Block, and extend its capabilities.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is the key part of the comment. Anything already supported by the block should be done by the block. For example we have special handling for offset based the perPage and page attribute, and if someone would alter only the perPage from the filter, would end up with wrong offset in the pagination..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I see that. I guess that's the risk with many filters, right? Each developer needs to be responsible and see what kind of side effects they might be introducing…

@gziolo gziolo added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Aug 30, 2022
@gziolo
Copy link
Member

gziolo commented Aug 30, 2022

Awesome, thank you @sunyatasattva for bringing it to the finish line. Once, we have also the matching API in the editor, let's make sure it is documented in the Block Editor Handbook somewhere in the hooks section:

https://github.com/WordPress/gutenberg/tree/trunk/docs/reference-guides/filters

@gziolo
Copy link
Member

gziolo commented Aug 30, 2022

@sunyatasattva, can you resolve conflicts in this branch. It looks like I landed changes added in exactly the same place and git has some trouble understanding those are different features and both should be there 😅

@gziolo gziolo merged commit bc4ecd4 into WordPress:trunk Aug 31, 2022
@github-actions github-actions bot added this to the Gutenberg 14.1 milestone Aug 31, 2022
sunyatasattva added a commit to sunyatasattva/gutenberg that referenced this pull request Sep 13, 2022
Detail and explain how to make best use of the changes introduced in
the following PRs: WordPress#43590, WordPress#43632 and WordPress#44093
@bph bph mentioned this pull request Sep 14, 2022
89 tasks
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Sep 15, 2022
…enberg repository.

This changeset backports changes from the following Gutenberg pull requests:

- [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block`
- [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter

Props ntsekouras, bernhard-reiter.
See #56467.


git-svn-id: https://develop.svn.wordpress.org/trunk@54175 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 15, 2022
…enberg repository.

This changeset backports changes from the following Gutenberg pull requests:

- [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block`
- [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter

Props ntsekouras, bernhard-reiter.
See #56467.

Built from https://develop.svn.wordpress.org/trunk@54175


git-svn-id: http://core.svn.wordpress.org/trunk@53734 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Sep 15, 2022
…enberg repository.

This changeset backports changes from the following Gutenberg pull requests:

- [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block`
- [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter

Props ntsekouras, bernhard-reiter.
See #56467.

Built from https://develop.svn.wordpress.org/trunk@54175


git-svn-id: https://core.svn.wordpress.org/trunk@53734 1a063a9b-81f0-0310-95a4-ce76da25c4cd
sunyatasattva added a commit to woocommerce/woocommerce-blocks that referenced this pull request Sep 16, 2022
As we worked with Gutenberg folks in WordPress/gutenberg#43590,
WordPress/gutenberg#/43632 and WordPress/gutenberg#/44093 we have
created a standard API that could be used for our use-case. This
PR refactors our WIP experimental work to use that standardized API.
whereiscodedude pushed a commit to whereiscodedude/wpss that referenced this pull request Sep 18, 2022
…enberg repository.

This changeset backports changes from the following Gutenberg pull requests:

- [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block`
- [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter

Props ntsekouras, bernhard-reiter.
See #56467.

Built from https://develop.svn.wordpress.org/trunk@54175
sunyatasattva added a commit to woocommerce/woocommerce-blocks that referenced this pull request Sep 23, 2022
* Refactor Product Query to use the latest Gutenberg APIs

As we worked with Gutenberg folks in WordPress/gutenberg#43590,
WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have
created a standard API that could be used for our use-case. This
PR refactors our WIP experimental work to use that standardized API.
ntsekouras added a commit that referenced this pull request Sep 29, 2022
* Add docs for extending the Query Loop block via variations

Detail and explain how to make best use of the changes introduced in
the following PRs: #43590, #43632 and #44093

* Fix typo

Change enhables to enables

Co-authored-by: Sören Wrede <soerenwrede@gmail.com>

* Address code review feedback

Specifically:

* Added the complete code at the beginning
* Change `isActive` from array to function
* Reworded a few things
* Added information about custom logic and other hints

* Update docs/how-to-guides/block-tutorial/extending-the-query-loop-block.md

* Change wording of opening paragraph for the example

* Add section about innerBlocks and `scope` shortcomings

* rename to `allowedControls`

Co-authored-by: Sören Wrede <soerenwrede@gmail.com>
Co-authored-by: Ryan Welcher <me@ryanwelcher.com>
Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
tarhi-saad pushed a commit to tarhi-saad/woocommerce-gutenberg-products-block that referenced this pull request Oct 31, 2022
* Refactor Product Query to use the latest Gutenberg APIs

As we worked with Gutenberg folks in WordPress/gutenberg#43590,
WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have
created a standard API that could be used for our use-case. This
PR refactors our WIP experimental work to use that standardized API.
ootwch pushed a commit to ootwch/wordpress-develop that referenced this pull request Nov 4, 2022
…enberg repository.

This changeset backports changes from the following Gutenberg pull requests:

- [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block`
- [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter

Props ntsekouras, bernhard-reiter.
See #56467.


git-svn-id: https://develop.svn.wordpress.org/trunk@54175 602fd350-edb4-49c9-b593-d223f7449a82
senadir pushed a commit to senadir/woocommerce-blocks that referenced this pull request Nov 12, 2022
…7169)

* Refactor Product Query to use the latest Gutenberg APIs

As we worked with Gutenberg folks in WordPress/gutenberg#43590,
WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have
created a standard API that could be used for our use-case. This
PR refactors our WIP experimental work to use that standardized API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Extensibility The ability to extend blocks or the editing experience Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] New API New API to be used by plugin developers or package users.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants