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

Fix error when $terms[0] does not exist in open-graph #10122

Merged
merged 2 commits into from Jul 3, 2018

Conversation

mikeschinkel
Copy link
Contributor

@mikeschinkel mikeschinkel commented Jun 23, 2018

Summary

This PR can be summarized in the following changelog entry:

  • Fixes a bug where a hard key, that might nog exists, is called. Props to mikeschinkel

Relevant technical choices:

  • Use reset($terms)->name instead of $terms[0]->name because the first element may be $terms[1].
  • Use !empty($terms) instead of $terms !== array() because it is clearer.

Test instructions

This PR can be tested by following these steps:

  • Select more than more category.
  • Do NOT set a Primary category
  • Add 'get_the_categories' filter that unsets $term[0] (as a commercial theme a client was using does. It removes parent categories if child categories are selected.)

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended

Fixes #

Copy link
Contributor

@andizer andizer left a comment

Choose a reason for hiding this comment

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

Thank you for your pull request 👍

I have one single comment. If you processed that I will take this pull request into our process to get it merged.

Thanks!

// We can only show one section here, so we take the first one.
$this->og_tag( 'article:section', $terms[0]->name );

$this->og_tag( 'article:section', reset( $terms )->name );
Copy link
Contributor

Choose a reason for hiding this comment

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

I would store the result of reset in a separate variable ($term). This makes it more clear that it's about a single term. Thus:

$term = reset( $terms ); 
$this->og_tag( 'article:section', $term->name );

Have you considered using array_shift instead of reset ? For me personally I needed to check what the return value of reset exactly is and it was the first time I see it being used in this context. It might be something to think of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @andizer - Sure, there are several ways to skin this cat, I just picked the most concise version, and intuitively chose reset() over array_shift() because reset()just changes the internal "current" pointer whereas array_shift() actually modifies the $terms array which requires both a new variable allocation and modification to an existing array and is thus a heavier approach.

I was curious so I benchmarked both and found reset() to be 19% faster than array_shift(), but doing it once the difference is so infinitesimal that it does not really matter which way it is done.

Same with copying to a variable. It is were me for shipping code where most people are users vs. readers of the code I would stick with reset() but since its your repo I'll make the requested update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@andizer andizer self-assigned this Jul 3, 2018
@andizer andizer changed the base branch from master to trunk July 3, 2018 12:50
@andizer
Copy link
Contributor

andizer commented Jul 3, 2018

Thanks again! I have done a code review and an acceptance test. Besides of that I've added some unit tests to cover the situation.

@andizer andizer merged commit 4448f53 into Yoast:trunk Jul 3, 2018
@andizer andizer added this to the 7.9 milestone Jul 3, 2018
@mikeschinkel mikeschinkel deleted the newclarity-patch-1 branch July 3, 2018 16:00
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.

None yet

3 participants