Skip to content

Ask the main loop at call time for the post the sample renders - #167

Merged
schlessera merged 3 commits into
mainfrom
fix/160-main-loop-boundary
Oct 23, 2026
Merged

schlessera merged 3 commits into
mainfrom
fix/160-main-loop-boundary

Conversation

@schlessera

Copy link
Copy Markdown
Member

Closes #160

What changed

The sample loop service no longer receives the current post when it is built. It receives a MainLoop and asks it each time the_content runs, 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. WordPressMainLoop is the boundary: it reads WordPress's query functions when asked and throws FailedToFindPost::for_main_query() when the main query did not ask for a post. The WP_Post delegation is gone from the composition root, whose delegation example is now the plugin-root ViewConfiguration alone, 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, the noGlobalState fix 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_content runs 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 to for_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.md and docs/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_unchanged was run against main, with a temporary get_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:

3) ...SampleLoopServiceTest::test_it_leaves_other_renderings_unchanged with data set "an excerpt being built"
Failed asserting that two strings are identical.
-'<p>Other content.</p>'
+'<pre>
+	Post title: A &lt;em&gt;title&lt;/em&gt;	<br>
...
Tests: 9, Assertions: 15, Errors: 5, Failures: 3.

(The 5 errors were the tests that build a MainLoop double, which did not exist yet.)

After the change, on PHP 8.3, rebased on current main:

composer test   # OK (592 tests, 3414 assertions)
composer lint   # phpcs clean, PHPStan no errors, Psalm no errors, PHPStan rule tests OK (129 tests)

tests/php/Unit/WordPressMainLoopTest.php covers each loop state (outside the loop, a secondary query, a feed, an excerpt being built, another post, no post) and the FailedToFindPost failure for a main query with no post or a non-post queried object. SampleLoopServiceTest covers the header on the queried post, the three other renderings through the composition root, and a MainLoop double 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() and is_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, so WordPressMainLoop now 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 test and composer lint pass locally, and CI is green on the current head.
  • Every acceptance criterion from the issue is met, or the gap is named below.
  • Nothing a generated plugin relies on changed, or the issue carries breaking and a ruling.
  • Docs and inline comments reflect the change, or nothing documented moved.

Anything left open

Nothing. The nested-query limitation above is documented in WordPressMainLoop.

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 schlessera added type: fix Something is broken and must behave the way it is documented. area: bootstrap basic-scaffold.php, the plugin factory and the BasicScaffoldPlugin composition root. area: sample src/SampleSubsystem and views/: example code a generated plugin replaces. area: docs README, AGENTS.md, docs/ and inline documentation meant for plugin authors. labels Oct 23, 2026
@schlessera
schlessera merged commit b72ad56 into main Oct 23, 2026
5 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: bootstrap basic-scaffold.php, the plugin factory and the BasicScaffoldPlugin composition root. area: docs README, AGENTS.md, docs/ and inline documentation meant for plugin authors. area: sample src/SampleSubsystem and views/: example code a generated plugin replaces. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sample: the injected WP_Post is the wrong post for most the_content calls

1 participant