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

Jetpack's Infinite Scroll not working with lightbox-photoswipe plugin active #45

Open
wnuczek opened this issue Jul 7, 2020 · 5 comments

Comments

@wnuczek
Copy link

wnuczek commented Jul 7, 2020

Hi, I have a problem with getting Jetpack's infinite scroll to work when your plugin is active.

I was able to determine that the response to the request sent by infinite scroll contains some HTML code from site header. It results in following error: Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.xhr.onload (infinity.min.js?ver=8.6.1-is5.0.0:3)

I changed the priority in add_action('wp_head', array($this, 'bufferStart'), 2000); from 2000 to 90 and it works well, however I suppose it's a temporary and dirty hack.

Any thoughts on how this can be solved?

arnowelzel added a commit that referenced this issue Jul 7, 2020
@arnowelzel
Copy link
Owner

My plugin also uses 2000 for the action priority. Since it works, when lowering the priority for JetPack the easiest fix would be to increase the priority in my plugin to some higher value.

Also see here:
4fb9d96

If this works for you, I will include this for the next update.

@wnuczek
Copy link
Author

wnuczek commented Jul 8, 2020

Actually it's the other way round. I changed the priority in your plugin.
Just did another check and 98 seems to be highest value, anything above that value causes the error.

@arnowelzel
Copy link
Owner

Hmm - this is bad news. A priority lower than about 200 will cause conflicts with other plugins - that's the reason why I set it to 2000 to be sure to get the output buffer after all other modifications.

As workaround you can keep your modification with a priority of 90. But I'll have to look into this as lowering the priority below 200 is definetly not an option, as this will break many sites.

@wnuczek
Copy link
Author

wnuczek commented Jul 8, 2020

I tinkered with the infinite scroll code and was able to get it to run with your plugin left at 2000 priority.

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();
	while ( ob_get_length() ) {
		ob_end_clean();
	}
	ob_end_clean(); //forcing to clean and end the buffer seems to resolve the issue
	$results['type'] = 'success';

It seems to be an issue with output buffer as in case with YoastSEO issue.
Forcing to clean and end the buffer seems to resolve the issue.

I am no PHP expert so I look forward to hear your opinion.

@arnowelzel arnowelzel added this to the 3.1.0 milestone Jul 11, 2020
@arnowelzel arnowelzel removed this from the 3.1.0 milestone Sep 2, 2020
@arnowelzel
Copy link
Owner

After reviewing the code you mentioned - this translates to:

  1. start output buffering (ob_start())
  2. output the site header (wp_head())
  3. as long as there is anything in the output buffer, delete the buffer content and then end buffering (while ( ob_get_length() ) { ob_end_clean(); })
  4. delete the buffer and end buffering (ob_end_clean())

So what this code wants to achive is to actually execute the WordPress header code but supress the output. That is already a very crude hack. But when opening a buffer it must be closed as well - even if there is nothing in it. I don't understand, why they call ob_end_clean() only if there is anything in the buffer. Also after having called ob_end_clean() the buffer is empty - there is no reason to this in conjunction with while( ob_get_length() ). Also see https://www.php.net/manual/en/function.ob-end-clean.php.

I think this is a bug with the infinite scroll code which should be solved there.

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

No branches or pull requests

2 participants