Skip to content

General: Connection: Only force register on build_connect_url max one time#7745

Merged
ebinnion merged 1 commit intomasterfrom
update/connect-url-force-register
Sep 22, 2017
Merged

General: Connection: Only force register on build_connect_url max one time#7745
ebinnion merged 1 commit intomasterfrom
update/connect-url-force-register

Conversation

@ebinnion
Copy link
Copy Markdown
Contributor

@ebinnion ebinnion commented Sep 4, 2017

Today, while testing Jetpack Onboarding, with latest master for Jetpack, I cleared out the connection on my site with:

  • wp option delete jetpack_options
  • wp option delete jetpack_private_options

Then, when I tried to reconnect, I got an ERR_TOO_MANY_REDIRECTS error in Chrome. Upon debugging, it looks like build_connect_url got into some loop where it kept trying to register. Here's some debug.

[04-Sep-2017 21:04:54 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url

[04-Sep-2017 21:04:54 UTC] {"register":true,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:54 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url, Jetpack->build_connect_url

[04-Sep-2017 21:04:55 UTC] {"register":false,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:55 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url

[04-Sep-2017 21:04:55 UTC] {"register":true,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:55 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url, Jetpack->build_connect_url

[04-Sep-2017 21:04:56 UTC] {"register":false,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:56 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url

[04-Sep-2017 21:04:56 UTC] {"register":true,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:56 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url, Jetpack->build_connect_url

[04-Sep-2017 21:04:57 UTC] {"register":false,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:57 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url

[04-Sep-2017 21:04:57 UTC] {"register":true,"token":"*****************","site_id":14310116}
[04-Sep-2017 21:04:57 UTC] do_action('load-toplevel_page_jetpack'), WP_Hook->do_action, WP_Hook->apply_filters, Jetpack_Admin_Page->admin_page_load, Jetpack->admin_page_load, Jetpack->build_
connect_url, Jetpack->build_connect_url

Note how the calls are back-to-back and build_connect_url is calling itself. This is actually Jetpack redirecting back to itself. I was able to isolate the issue back to #6964.

How to reproduce

It's not foolproof, but I was often able to reproduce by connecting, and then running the WP CLI calls to delete local Jetpack options.

How to test

Get your site in a state where you get the redirect, then apply this patch.

@ebinnion ebinnion added General Bug When a feature is broken and / or not performing as intended labels Sep 4, 2017
@ebinnion ebinnion added this to the 5.4 milestone Sep 4, 2017
@ebinnion ebinnion self-assigned this Sep 4, 2017
@ebinnion ebinnion requested a review from a team as a code owner September 4, 2017 22:17
@ebinnion ebinnion added the [Status] Needs Review This PR is ready for review. label Sep 4, 2017
Copy link
Copy Markdown
Contributor

@gravityrail gravityrail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reasons of expediency, I think it's ok to merge this - it will work. But I think the code could use a cleanup (as per my comment) which will make it much more readable and eliminate recursion entirely, as well as possibly ditching one of the function arguments.

sprintf( '/sites/%d', $site_id ) .'?force=wpcom',
'1.1'
);
// Let's check the existing blog token to see if we need to re-register. We only check once per minute
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe all this logic should be encapsulated into a function, as part of the condition for entering the first part of the if-statement. That way no recursion is necessary:

if ( $register || ! $token || ! $site_id || ! $this->check_tokens_are_valid() ) {
  // render register URL
} else {
  // render authorize URL
}

In fact, if this works out you may be able to drop the $register arg completely from the function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I agree that we should probably refactor a bit. I'm not sure that we can get rid of the raw option which ensure we only call out once, but it could at least make things a bit more readable. 👍

@gravityrail gravityrail added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Sep 6, 2017
@ebinnion ebinnion merged commit a4c1a46 into master Sep 22, 2017
@ebinnion ebinnion deleted the update/connect-url-force-register branch September 22, 2017 15:19
@zinigor
Copy link
Copy Markdown
Contributor

zinigor commented Sep 27, 2017

Oh, I totally missed that one! Thanks for fixing, this fixes #7694 .

@kraftbj kraftbj removed the [Status] Ready to Merge Go ahead, you can push that green button! label Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended General

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants