Example #1
0
 /**
  * To add a new node somewhere in the tree. 
  * It creates the WP page and insert it in the tree.
  * Handles multiple insertions at once.
  */
 public static function tree_add_new_node()
 {
     $tree = new ApmTreeData();
     $tree->load_last_tree();
     switch ($_POST['node_type']) {
         case 'page':
             $new_node_name = trim(stripslashes($_POST['node_id']));
             $default_node_data = array('node_template' => '');
             $new_node_data = isset($_POST['node_data']) && !empty($_POST['node_data']) ? $_POST['node_data'] : $default_node_data;
             $new_node_template = array_key_exists('node_template', $new_node_data) ? $new_node_data['node_template'] : '';
             parent::$json_data['insert_type'] = $_POST['edit_action'];
             if (isset($_POST['nodes_number']) && !empty($_POST['nodes_number']) && is_numeric($_POST['nodes_number']) && $_POST['nodes_number'] > 1) {
                 if (!empty($new_node_name)) {
                     $new_node_ids = array();
                     $nb_digits = strlen(strval($_POST['nodes_number']));
                     if ($nb_digits == 1) {
                         $nb_digits = 2;
                         //We still want 01, 02, 03 ... if nodes_number < 10
                     }
                     for ($i = 1; $i <= $_POST['nodes_number']; $i++) {
                         $insert_after = $_POST['edit_action'] == 'insert_after';
                         $node_name = sprintf($new_node_name . ' - %0' . $nb_digits . 'd', !$insert_after ? $i : $_POST['nodes_number'] - $i + 1);
                         $new_page_id = ApmTreeData::insert_wp_page($node_name, $new_node_template);
                         if (!empty($new_page_id)) {
                             if (is_numeric($_POST['index_node'])) {
                                 $new_node_ids[] = $tree->add_new_node($_POST['edit_action'], $_POST['index_node'], 'page', $new_page_id);
                                 ApmTreeData::force_wp_page_slug_from_title($new_page_id);
                                 //Call this after WP tree synchronisation
                             }
                         }
                     }
                     parent::$json_data['new_nodes'] = $insert_after ? array_reverse($new_node_ids) : $new_node_ids;
                 }
             } else {
                 if (!empty($new_node_name)) {
                     $new_page_id = ApmTreeData::insert_wp_page($new_node_name, $new_node_template);
                     if (!empty($new_page_id)) {
                         if (is_numeric($_POST['index_node'])) {
                             $new_node_id = $tree->add_new_node($_POST['edit_action'], $_POST['index_node'], 'page', $new_page_id);
                             ApmTreeData::force_wp_page_slug_from_title($new_page_id);
                             //Call this after WP tree synchronisation
                         }
                     }
                     parent::$json_data['new_node'] = $new_node_id;
                 }
             }
             break;
     }
     parent::send_json();
 }