Repository navigation
Ask the main loop at call time for the post the sample renders - #167
Merged
Merged
Conversation
SampleLoopService received a WP_Post from a delegation that ran once, when the service was built at wp. the_content runs for more than the queried post, so other posts on the page, feed items and excerpts all got the queried post's header. The service now receives a MainLoop and asks it inside the filter callback; WordPressMainLoop reads the query functions and throws FailedToFindPost::for_main_query() when the main query has no post. The WP_Post delegation is gone from the composition root.
The pattern for WordPress state, the noGlobalState fix message and its rule documentation now point at MainLoop, and say that a delegation runs once and only suits state that is fixed for the request. Citations into the composition root and the sample service are updated to the moved lines.
schlessera
added a commit
to mwpd/content-freshness
that referenced
this pull request
Oct 23, 2026
Ported from mwpd/basic-scaffold@b72ad56 (mwpd/basic-scaffold#167). The plugin's outdated post notice already worked this way; its WordPressMainLoop takes over the scaffold's notes on secondary queries and the exception's message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #160
What changed
The sample loop service no longer receives the current post when it is built. It receives a
MainLoopand asks it each timethe_contentruns, so it prepends its header to the queried post in the main loop only, and returns the content of other posts on the page, feed items and excerpts unchanged.WordPressMainLoopis the boundary: it reads WordPress's query functions when asked and throwsFailedToFindPost::for_main_query()when the main query did not ask for a post. TheWP_Postdelegation is gone from the composition root, whose delegation example is now the plugin-rootViewConfigurationalone, with a note that a delegation runs when the service is built and only suits state fixed for the request. The "I need WordPress state" pattern, thenoGlobalStatefix message and its rule documentation now teach the call-time interface.Why this shape
The issue chose the approach, following the change already made in mwpd/content-freshness. A delegation cannot work for the current post, whatever it checks, because it runs once when the service is built while
the_contentruns many times afterwards. Comparing the filtered post inside the service would have put WordPress state reads back into the service; the interface keeps them at the boundary and lets tests use a double.for_current_request()was replaced rather than kept next tofor_main_query(), since nothing calls it any more.src/Exception/is not part of the infrastructure API snapshot, and generated plugins carry their own copy.While moving lines in
src/BasicScaffoldPlugin.php, I also updated the citations into it that had already drifted before this change (docs/design-principles.mdanddocs/patterns.md), so every citation into the two changed files points at what it claims.Proof
Before the change: the new composition-root test
test_it_leaves_other_renderings_unchangedwas run againstmain, with a temporaryget_post()stub so the old delegation could build the service. All three cases failed, because the queried post's header was prepended to another post, a feed item and an excerpt:(The 5 errors were the tests that build a
MainLoopdouble, which did not exist yet.)After the change, on PHP 8.3, rebased on current
main:tests/php/Unit/WordPressMainLoopTest.phpcovers each loop state (outside the loop, a secondary query, a feed, an excerpt being built, another post, no post) and theFailedToFindPostfailure for a main query with no post or a non-post queried object.SampleLoopServiceTestcovers the header on the queried post, the three other renderings through the composition root, and aMainLoopdouble that reports another rendering, in which case the post is never asked for.An independent review of the diff found one point, which I addressed by documenting it rather than changing the check: a secondary query run inside the main loop that lists the queried post again leaves
in_the_loop()andis_main_query()true, so that copy of the post also gets the header. WordPress's query functions give no way to tell it apart, and the check is the one the issue specified, soWordPressMainLoopnow says so in its docblock. The review verified the other citations, PHP 7.4 compatibility and that the tests fail when any condition of the check is removed.Checklist
composer testandcomposer lintpass locally, and CI is green on the current head.breakingand a ruling.Anything left open
Nothing. The nested-query limitation above is documented in
WordPressMainLoop.