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

Patterns, Templates: Try conditional theme attribute injection #5514

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Oct 17, 2023

Not planning to land this for 6.4, it's been too much of a 🎢

WIP, early stages.

Since we had to revert our previous attempt at this, I'm starting a new draft PR based on the notes I posted here.

TODO:

  • Move hooked blocks insertion to the patterns controller so we can remove it from WP_Block_Patterns_Registry
  • Make sure hooked block insertion is run on the frontend. We can probably achieve that through running it in the Pattern block's render_block.
  • Apply the same changes to Templates?
  • Add test coverage to avoid the previous regression. If we manage to move all the REST API specific logic into the respective controllers (and get rid of all REST_REQUEST conditionals), it's probably enough to add unit test coverage for those controllers

When testing, make sure that this doesn't cause this regression (it currently still does; needs more tweaking!)

Trac ticket: TBD


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@ockham ockham self-assigned this Oct 17, 2023
@ockham
Copy link
Contributor Author

ockham commented Oct 17, 2023

cc/ @gziolo @felixarntz

@felixarntz
Copy link
Member

Thanks for the ping @ockham. Do we know why the revert in #5509 fixed the problem? Why did the ( defined( 'REST_REQUEST' ) && REST_REQUEST ) not work? Is it because the relevant REST endpoint that needs this is preloaded via PHP (in which case REST_REQUEST would not be defined)? Or why was that insufficient / unreliable as a solution?

@gziolo
Copy link
Member

gziolo commented Oct 18, 2023

Why did the ( defined( 'REST_REQUEST' ) && REST_REQUEST ) not work? Is it because the relevant REST endpoint that needs this is preloaded via PHP (in which case REST_REQUEST would not be defined)?

I haven't had a chance to have a closer look, but it would be my understanding that this check won't work with the preloading feature. I'm wondering if it would be possible to create a helper function like wp_is_rest_request which would account for preloading requests with some magic global, too.

However, I believe that in this case, @ockham is planning to move the necessary computation to the REST API handler which will remove the need for using this check altogether.

@ockham
Copy link
Contributor Author

ockham commented Oct 18, 2023

Yeah, my best guess is that REST_REQUEST isn't set to true when preloading. I didn't verify 100% but did notice that at the time, the theme attribute was missing for a number of template parts on trunk but was correctly injected on the revert branch.

@ockham
Copy link
Contributor Author

ockham commented Oct 18, 2023

BTW if that's indeed the reason, it raises the question why we're not setting REST_REQUEST to true right before preloading in the first place (and back to false right after -- if that's possible in PHP with define? 🤔 )

It seems like we'd like to mimic a REST API request context as closely as possible when preloading routes (not just in this case, but for all routes really 🤔 )

@ockham
Copy link
Contributor Author

ockham commented Oct 18, 2023

setting REST_REQUEST to true right before preloading in the first place (and back to false right after -- if that's possible in PHP with define? 🤔 )

Ah nevermind, can't redefine a constant. Fair enough 😬

@felixarntz
Copy link
Member

@ockham That's why there is this ticket: https://core.trac.wordpress.org/ticket/42061

If it was moving forward, it would allow us to more properly test REST API specific logic, as there would be a filter to use instead of being limited to the constant.

@ockham
Copy link
Contributor Author

ockham commented Oct 18, 2023

@ockham That's why there is this ticket: https://core.trac.wordpress.org/ticket/42061

Ah, TIL! Thank you for the pointer :)

If it was moving forward, it would allow us to more properly test REST API specific logic, as there would be a filter to use instead of being limited to the constant.

👍

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.

3 participants