Exemplo n.º 1
0
 /**
  * TODO: documentation
  */
 public function get_post_descendants()
 {
     check_ajax_referer('add-menu_item', 'menu-settings-column-nonce');
     if (!current_user_can('edit_theme_options')) {
         wp_die(-1);
     }
     require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     $descendants = Mega_Menu::flatten_menu_structure(Mega_Menu::load_hierarchy($_GET['post_id']), false);
     $object_to_menu_map = array();
     $menu_items = array();
     array_walk($descendants, function ($descendant) use(&$object_to_menu_map, &$menu_items) {
         $menu_item = array('menu-item-object' => $descendant->post_type, 'menu-item-object-id' => $descendant->ID, 'menu-item-parent-id' => $_GET['post_id'] == $descendant->post_parent ? $_GET['db_id'] : $object_to_menu_map[$descendant->post_parent], 'menu-item-type' => 'post_type', 'menu-item-title' => $descendant->post_title, 'menu-item-url' => get_permalink($descendant->ID));
         $item_ids = wp_save_nav_menu_items($_GET['menu'], array($menu_item));
         if (is_wp_error($item_ids)) {
             wp_die(0);
         }
         $object_to_menu_map[$descendant->ID] = $item_ids[0];
         $menu_obj = get_post($item_ids[0]);
         if (!empty($menu_obj->ID)) {
             $menu_obj = wp_setup_nav_menu_item($menu_obj);
             $menu_obj->label = $menu_obj->title;
             // don't show "(pending)" in ajax-added items
             $menu_items[] = $menu_obj;
         }
     });
     $walker_class_name = apply_filters('wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_GET['menu']);
     if (!class_exists($walker_class_name)) {
         wp_die(0);
     }
     if (!empty($menu_items)) {
         $args = array('after' => '', 'before' => '', 'link_after' => '', 'link_before' => '', 'walker' => new $walker_class_name());
         $output = walk_nav_menu_tree($menu_items, 0, (object) $args);
         $output = preg_replace_callback('/(menu-item-depth-)([0-9]+)/', function ($matches) {
             return $matches[1] . ($matches[2] + $_GET['depth'] + 1);
         }, $output);
         echo $output;
         wp_die();
     }
 }