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: consider 'fields' value when returning terms from query #2806

Merged
merged 1 commit into from Oct 24, 2023

Conversation

jrathert
Copy link
Contributor

Related:

Issue

A fields argument other than allor all_with_object_id lead to a fatal error, as TermFactory::from_wp_term_query() did expect the result of a query to always be of type WP_term[]

Solution

In TermFactory::from_wp_term_query() call TermFactory::build() only if fields is all or all_with_object_id, otherwise, return plain result of WP_Term_Query::get_terms() (i.e., int[]|string[]|string)

Considerations

One could - instead of examining the fields argument - check if the object returned from WP_Term_Query::get_terms() actually is a WP_Term[]. That would be future proof (if there are fields values added for which the query also returns an array of WP_Term) - but might have some impact on performance, so I decided to solve it this way.

Testing

No specific

Copy link
Member

@gchtr gchtr left a comment

Choose a reason for hiding this comment

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

This feels like sensible approach to me.

I think we would also need tests for this. @jrathert Do you have testing set up and can you add tests? Or would you rather have us do it?

@nlemoine I’d be glad about your opinion on this.

src/Factory/TermFactory.php Outdated Show resolved Hide resolved
src/Factory/TermFactory.php Outdated Show resolved Hide resolved
@jrathert
Copy link
Contributor Author

@jrathert Do you have testing set up and can you add tests?

I did not have it when I wrote the code, but now I have ;-). Can add some in the the next days.

@jrathert
Copy link
Contributor Author

@gchtr Resolved your comments and added a number of tests covering all possible return values of WP_Term_Query::get_terms().

I tried to write the tests in a way that there is sort of an assertion problem with the old code that is now solved with my patch. I have to admit, I failed with that, as in the original code the root cause was the call of the build() function with inappropriate parameters. It is impossible to write a test that can detect that, IMHO.

@jrathert
Copy link
Contributor Author

Did a few optimizaitons for the assertions (assertSame, not assertEquals etc)

src/Factory/TermFactory.php Outdated Show resolved Hide resolved
@nlemoine
Copy link
Member

nlemoine commented Oct 20, 2023

Thank you @jrathert! We just need to take a decision on #2806 (comment)

And it will be good to go.

@jrathert
Copy link
Contributor Author

Removed the unnecessary '_ - should be ready to merge now.

@gchtr
Copy link
Member

gchtr commented Oct 23, 2023

I’m continuing the discussion we started in #2806 (comment) here.

Very good that the issue with count popped up here.

I would say: Timber::get_terms() should be used for getting objects or ids. If you need count, use get_terms. There's actually no benefit in using Timber::get_terms() for getting anything else than objects (event IDs) and keeping return types seems more important to me.

I agree with that.

Maybe in the fact that we allow a WP_Term_Query to be passed or maybe we should throw something when count is met, after all, it's a term factory, not a count term factory :) You should expect an iterable here.

We could use Timber\Helper::doing_it_wrong() here if count is found and advise to use get_terms() instead.

This could mean that we could keep iterable, right? Even though TermFactory::from() doesn’t have return types.

General error handling is still something we need to figure it in the future (#2210).

@gchtr
Copy link
Member

gchtr commented Oct 24, 2023

I just talked to @nlemoine. We going to drop iterable and will probably come back to this once we can use union types, which is when we drop support for PHP 7.4 in a future version.

@gchtr
Copy link
Member

gchtr commented Oct 24, 2023

Some tests only fail because of an issue with testing against WordPress 6.4. For now, we can ignore these tests. I’m merging this in.

@gchtr gchtr merged commit 5149aa7 into timber:2.x Oct 24, 2023
23 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants