Skip to content

Improve interception functions.#4703

Merged
swissspidy merged 2 commits intoWordPress:masterfrom
tfrommen:gutenberg-interception
Feb 8, 2018
Merged

Improve interception functions.#4703
swissspidy merged 2 commits intoWordPress:masterfrom
tfrommen:gutenberg-interception

Conversation

@tfrommen
Copy link
Copy Markdown
Member

Description

Overall improved version of the gutenberg_intercept_* functions.

Types of changes

  • Strict checking (also for in_array).
  • Switch early return logic in case of missing post for edit.
  • Deleted redundant definition of $post_type.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.


if ( empty( $post_id ) ) {
$post_ID = (int) $_GET['post'];
$post_id = $post_ID;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we even do this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ha, I also thought about what the situation might be. Couldn't think of anything but a hand-crafted invalid edit post URL. 🤔 🤷‍♂️

Copy link
Copy Markdown
Member

@swissspidy swissspidy Jan 27, 2018

Choose a reason for hiding this comment

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

Just a bit silly to not assign this variable directly. But well...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh, you mean why there are two variables...? Well, these are actually two different globals. See the import at the top of the function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, I see. WordPress 🤷‍♀️

gutenberg.php Outdated
$post_ID = (int) $_GET['post'];
$post_id = $post_ID;

if ( ! $post_id ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When would this condition be triggered? Looks like dead code to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, this is what @swissspidy and I were talking about. I only can think of incorrect usage of the functionality, for example, hand-crafting an invalid URL without a (valid) post ID.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Empty value, negative number, zero, some string, ... All that would be (int) 0, and thus falsy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I understand, but when would $post_id be assigned zero, since it's indirectly assigned from $_GET['post'] and we have the empty( $_GET['post'] ) condition above to shortcut?

Copy link
Copy Markdown
Member Author

@tfrommen tfrommen Feb 5, 2018

Choose a reason for hiding this comment

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

When I put ?post=foo in the URL, its not empty in terms of the first check empty(), but after casting it to an integer, it is 0, and thus empty.

Copy link
Copy Markdown
Member Author

@tfrommen tfrommen Feb 5, 2018

Choose a reason for hiding this comment

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

But yes, personally, I would have adapted the first check to include this already. This would have been a backward incompatible change, though, as the current code sets the $post_id and $post_ID variables to that invalid value.

Should I adapt this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

if ( empty( $_GET['post'] ) || ! is_numeric( $_GET['post'] ) ) {
	return;
}

$post_ID = (int) $_GET['post'];
$post_id = $post_ID;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, that seems improved to me, since if this conversation is any evidence, what was being accounted for was not entirely clear (though in retrospect makes sense, thanks for clarifying).

Re: Backwards incompatibility, I'd assume post=foo would break miserably in some other manner anyways, or at least not something we'd intend to support.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

}

if ( empty( $post_id ) ) {
if ( empty( $_GET['post'] ) || ! is_numeric( $_GET['post'] ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In retrospect, ?post=0.1 would pass this check and be coerced into a 0 int value, which is what the previous condition was guarding against. However, I'd guess this would make it as far as the $post assignment and fail anyways on the subsequent falsey-ness check. Does that sound right to you, and then a non-concern?

Copy link
Copy Markdown
Member Author

@tfrommen tfrommen Feb 8, 2018

Choose a reason for hiding this comment

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

Yes, but there is no way that 0.1 could come from the database (ID is a BIGINT), so this is invalid data anyway. And also, yes, this would still be some (invalid) edge case. -1 would also pass, but in any case, the check for the post object further down will fail, and all is good, I'd say.

@aduth
Copy link
Copy Markdown
Member

aduth commented Feb 8, 2018

You may need to rebase your branch against master and force push your branch to resolve the build errors.

@tfrommen
Copy link
Copy Markdown
Member Author

tfrommen commented Feb 8, 2018

No, there are no conflicts. You just need to restart the build (the single one that failed). I can't do that as I am not a member/owner of this repository.

@swissspidy
Copy link
Copy Markdown
Member

Restarted it now :-)

@tfrommen
Copy link
Copy Markdown
Member Author

tfrommen commented Feb 8, 2018

All green. Go, go, gooo! 😅

@swissspidy swissspidy merged commit abd2ab8 into WordPress:master Feb 8, 2018
@aduth
Copy link
Copy Markdown
Member

aduth commented Feb 8, 2018

No, there are no conflicts. You just need to restart the build (the single one that failed). I can't do that as I am not a member/owner of this repository.

Hmm, I had restarted the build prior to leaving my comment and it was still failing. Not sure why.

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

Successfully merging this pull request may close these issues.

4 participants