Skip to content

Commit 09b0813

Browse files
committed
Sort terms by hierarchical order and remove deprecated attibute from psalm config
1 parent a8307b3 commit 09b0813

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

classes/cli/_helper.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,29 @@ public static function get_all_terms( $args = array(), $terms_args = array() ) {
4141
return false;
4242
}
4343

44-
return $results;
44+
return self::hierarchical_sort_terms( $results );
45+
}
46+
47+
/**
48+
* Sort an array of WP_Terms by hierarchical order (parents first, then children, then grand-children...)
49+
*
50+
* @param WP_Term[] $terms
51+
* @param int $parent
52+
* @return WP_Term[]
53+
* @author Ingrid Azéma
54+
*/
55+
private static function hierarchical_sort_terms( array $terms, int $parent = 0 ): array {
56+
$sorted_terms = [];
57+
foreach ( $terms as $term ) {
58+
if ( $term->parent !== $parent || ! $term instanceof WP_Term ) {
59+
continue;
60+
}
61+
$sorted_terms[] = $term;
62+
$children = self::hierarchical_sort_terms( $terms, $term->term_id );
63+
$sorted_terms = array_merge( $sorted_terms, $children );
64+
}
65+
66+
return $sorted_terms;
4567
}
4668

4769
/**

psalm.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="false"
43
resolveFromConfigFile="true"
54
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
65
xmlns="https://getpsalm.org/schema/config"

0 commit comments

Comments
 (0)