Skip to content

Commit

Permalink
Prevent infinite loop during tag retrieval
Browse files Browse the repository at this point in the history
In the case of corrupt data or orphaned tags, terminate the loop when it stops being productive
  • Loading branch information
colemanw committed Sep 5, 2020
1 parent a4d7c53 commit a913370
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions includes/utils.inc
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ function wf_crm_get_tags($used_for, $parent_id = NULL) {
}
// Fetch child tags
unset($params['parent_id']);
$params += ['return' => ['name', 'parent_id'], 'parent_id.is_tagset' => 0, 'parent_id.is_selectable' => 1];
$params += ['return' => ['name', 'parent_id'], 'parent_id.is_tagset' => 0, 'parent_id.is_selectable' => 1, 'parent_id.used_for' => $params['used_for']];
$unsorted = wf_crm_apivalues('Tag', 'get', $params);
$parents = array_fill_keys(array_keys($tags), ['depth' => 1]);
// Loop until all children are placed under their parents
while ($unsorted) {
// Place children under their parents.
$prevLoop = NULL;
while ($unsorted && count($unsorted) !== $prevLoop) {
// If count stops going down then we are left with only orphan tags & will abort the loop
$prevLoop = count($unsorted);
foreach ($unsorted as $id => $tag) {
$parent = $tag['parent_id'];
if (isset($parents[$parent])) {
Expand Down

0 comments on commit a913370

Please sign in to comment.