public function get_ready_to_display_tree($root = self::root_id, $json = false, $depth = false, $load_data = true) { $ready_to_display_tree = array(); if ($this->tree_is_empty()) { return $json ? json_encode($ready_to_display_tree) : $ready_to_display_tree; } //TODO: handle any $depth... if ($depth !== false) { switch ($depth) { case 0: $this->tree_state->fold_node($root); $this->save(); break; case 1: $direct_children = $this->apm_tree->get_children($root); if (!empty($direct_children)) { $this->tree_state->fold_nodes($direct_children); $this->save(); } break; } } $unfolded_nodes = $this->tree_state->get_unfolded_nodes($this->apm_tree->get_nodes_flat($root)); $root_depth = $this->apm_tree->get_node_depth($root); //Retrieve visible nodes from APM tree (this may retrieve nodes that don't exist in WP database, //if deleted from outside the plugin) $tree_to_display = $this->apm_tree->get_ready_to_display_tree($root, $root_depth, false, $unfolded_nodes); $apm_ids_to_display = array_keys($tree_to_display); if ($load_data) { //Load Wordpress data just before display, to be sure to load only necessary data: $this->nodes_data->load_multiple($apm_ids_to_display); //Now, $this->nodes_data contains only nodes that really exist as WP pages. //However, they may have the 'auto-draft' or 'trash' status. } //Retrieve number of first level node to know if we can move nodes : //A node is movable only if : depth > 1 OR (depth == 1 AND there is other nodes with depth == 1) $nb_first_level_nodes = $this->get_apm_tree_nb_nodes(true); //We loop on the nodes existing in our APM tree so that we can warn about pages that //exist in APM tree and not in WP tree (deleted from outside the plugin) : foreach ($tree_to_display as $apm_id => $node_tree_infos) { $node_data = $this->nodes_data->get($apm_id); //A node exists in APM tree but not in WP : set special data to warn about it : if (empty($node_data)) { $node_data = new ApmNodeDataDisplay(); $node_data->set_intern_data(new ApmNodeDataIntern(array('apm_id' => $apm_id, 'type' => 'page', 'wp_id' => $apm_id))); $node_data->set_node_is_not_in_wp(); } $node_data->set_node_position(array('depth' => $node_tree_infos['depth'], 'parent' => $node_tree_infos['parent'], 'children' => $node_tree_infos['children'], 'nb_children' => $node_tree_infos['nb_children'])); $is_folded = $node_tree_infos['nb_children'] > 0 && !in_array($apm_id, $unfolded_nodes); $node_data->set_is_folded($is_folded); $node_data->set_is_movable($node_tree_infos['depth'] > 1 || $nb_first_level_nodes > 1); $ready_to_display_tree[$apm_id] = $json ? $node_data->get_flattened() : $node_data; } return $json ? json_encode($ready_to_display_tree) : $ready_to_display_tree; }