Skip to content

2.x Remove unused Term::get_term_from_query() function#2664

Merged
gchtr merged 2 commits into2.xfrom
2.x-get-term-from-query
Mar 1, 2023
Merged

2.x Remove unused Term::get_term_from_query() function#2664
gchtr merged 2 commits into2.xfrom
2.x-get-term-from-query

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Oct 27, 2022

Related:

Issue

I found a function that is not used anymore.

Solution

Remove the function.

Impact

Minimal. The function that gets the term from the main query is now covered through Timber::get_term() and the term factory.

timber/src/Timber.php

Lines 714 to 718 in 056c6a1

if (null === $term) {
// get the fallback term_id from the current query
global $wp_query;
$term = $wp_query->queried_object->term_id ?? null;
}

Only the protected function Term::get_term_from_query() can’t be used anymore.

Considerations

There’s an additional check for a tax query that was added in #521:

timber/src/Term.php

Lines 142 to 144 in 056c6a1

if (isset($wp_query->tax_query->queries[0]['terms'][0])) {
return $wp_query->tax_query->queries[0]['terms'][0];
}

The logic there seems to be: When the global query includes a tax query, get the first term from that query. Is that a case we still want to support? Is this the intended behavior for Timber::get_term()? Or could it also be considered as unexcepted?

Testing

Do we need more testing?

@gchtr gchtr added the 2.0 label Oct 27, 2022
@gchtr gchtr mentioned this pull request Nov 4, 2022
1 task
@gchtr gchtr added the Ready for Review Ready for a contrib to take a look at and review/merge label Dec 30, 2022
nlemoine
nlemoine previously approved these changes Feb 28, 2023
Copy link
Copy Markdown
Member

@nlemoine nlemoine left a comment

Choose a reason for hiding this comment

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

Is that a case we still want to support? Is this the intended behavior for Timber::get_term()?

I didn't event know that method existed 😅

This an absolute no to me, I don't see any valid use case where getting the term from the tax query should be returned with this kind of method. It should only return the current queried term.

@gchtr
Copy link
Copy Markdown
Member Author

gchtr commented Mar 1, 2023

@nlemoine Thanks for your feedback!

I added a note to the Upgrade Guide about the removed logic in ee42528 (#2664).

I’m sure in the past this logic made some developer’s life easier in very specific use cases. But these kind of gems are the ones that can lead to unexpected behavior quickly. So I’m happy to remove them.

@gchtr gchtr merged commit 2b6c87a into 2.x Mar 1, 2023
@Levdbas Levdbas deleted the 2.x-get-term-from-query branch November 9, 2023 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.0 Ready for Review Ready for a contrib to take a look at and review/merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants