- /*
- * This is a preparation for having two different items with the same uri in one thread
- * This will otherwise lead to an endless loop.
- */
- foreach ($arr as $x) {
- if (!isset($newarr[$x['uri']])) {
- $newarr[$x['uri']] = $x;
+ if (isset($child['children']) && count($child['children'])) {
+ // This helps counting only the regular posts
+ $count_post_closure = function($var) {
+ return $var['verb'] === ACTIVITY_POST;
+ };
+
+ $child_post_count = count(array_filter($child['children'], $count_post_closure));
+
+ $remaining_post_count = count(array_filter(array_slice($parent['children'], $i), $count_post_closure));
+
+ // If there's only one child's children post and this is the last child post
+ if ($child_post_count == 1 && $remaining_post_count == 1) {
+
+ // Searches the post item in the children
+ $j = 0;
+ while($child['children'][$j]['verb'] !== ACTIVITY_POST && $j < count($child['children'])) {
+ $j ++;
+ }
+
+ $moved_item = $child['children'][$j];
+ unset($parent['children'][$i]['children'][$j]);
+ $parent['children'][] = $moved_item;
+ } else {
+ $parent['children'][$i] = smart_flatten_conversation($child);
+ }