Automattic / Automattic/jetpack

Possible bug in output buffering for infinite scroll

Open
#17,050 0 comments 0 reactions 0 assignees View on GitHub
[Feature] Infinite Scroll [Pri] Low Bug Good For Community
Dominant language
PHP
Stars
1.8k
Forks
898
Avg merge
1d 18h
Merged PRs (30d)
774

Description

Also see https://github.com/arnowelzel/lightbox-photoswipe/issues/45 about this.

See the following code fragment:

https://github.com/Automattic/jetpack/blob/ec88783ffaf578b05fe0d1f44427d790d69743a4/modules/infinite-scroll/infinity.php#L1335-L1341

What this does:

1. Start a new output buffer
2. Call `wp_head()`
3. As long as there is anything in the output buffer (and only then!) delete the buffer and stop buffering

However this causes a lot of problems if `wp_head()` does not create any output (which might happen in rare cases). Also note that `ob_end_clean()` only needs to be called once - after calling this, there is no buffer any longer (also see https://www.php.net/manual/en/function.ob-end-clean.php). On the other hand it also must be called every time after using `ob_start()` and not only if there is anything in the buffer.

My plugin "Lightbox with PhotoSwipe" won't work at least on one site using infinite scroll and the fix by the user was to add `ob_end_clean()` after the `while()`-loop. The correct code should be:

```
if ( have_posts() ) {
// Fire wp_head to ensure that all necessary scripts are enqueued. Output isn't used, but scripts are extracted in self::action_wp_footer.
ob_start();
wp_head();
ob_end_clean();
```

Contributor guide

Open the contributing guide

Research direction

Start in modules/infinite-scroll/infinity.php around lines 1335-1341 and review how the output buffer is handled around wp_head(). Compare the current loop with the reported ob_end_clean() behavior, then verify that the buffer is consistently closed when wp_head() produces no output and that infinite-scroll output still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.