Yoast\WP\SEO\Builders
Indexable_Hierarchy_Builder::find_deepest_term_id
Find the deepest term in an array of term objects.
Метод класса: Indexable_Hierarchy_Builder{}
Хуков нет.
Возвращает
int. The deepest term ID.
Использование
// private - только в коде основоного (родительского) класса $result = $this->find_deepest_term_id( $terms );
- $terms(array
) (обязательный) - Terms set.
Код Indexable_Hierarchy_Builder::find_deepest_term_id() Indexable Hierarchy Builder::find deepest term id Yoast 26.9
private function find_deepest_term_id( $terms ) {
/*
* Let's find the deepest term in this array, by looping through and then
* unsetting every term that is used as a parent by another one in the array.
*/
$terms_by_id = [];
foreach ( $terms as $term ) {
$terms_by_id[ $term->term_id ] = $term;
}
foreach ( $terms as $term ) {
unset( $terms_by_id[ $term->parent ] );
}
/*
* As we could still have two subcategories, from different parent categories,
* let's pick the one with the lowest ordered ancestor.
*/
$parents_count = -1;
$term_order = 9999; // Because ASC.
$deepest_term = \reset( $terms_by_id );
foreach ( $terms_by_id as $term ) {
$parents = $this->get_term_parents( $term );
$new_parents_count = \count( $parents );
if ( $new_parents_count < $parents_count ) {
continue;
}
$parent_order = 9999; // Set default order.
foreach ( $parents as $parent ) {
if ( $parent->parent === 0 && isset( $parent->term_order ) ) {
$parent_order = $parent->term_order;
}
}
// Check if parent has lowest order.
if ( $new_parents_count > $parents_count || $parent_order < $term_order ) {
$term_order = $parent_order;
$deepest_term = $term;
}
$parents_count = $new_parents_count;
}
return $deepest_term->term_id;
}