/** * Generates the Related Links menu. Associated with the related_links * shortcode. * @param array The attributes passed by WordPress * @return string The Related Links menu */ public function related_links_shortcode($atts = array()) { self::$active_pages = array(); $menu_items = $this->init_shortcode($atts); $display_menu_items = array(); $post_menu_items = array_filter($menu_items, function ($menu_item) { // we need to use the current Post in here global $post; if ($menu_item->post_id == $post->ID) { return true; } else { return false; } }); foreach ($post_menu_items as $menu_item) { // get children items $children = $this->get_menu_items_by_parent_id($menu_items, $menu_item->ID); if (count($children)) { // display children items $display_menu_items = array_merge($display_menu_items, $children); } else { // else, display sibling items $display_menu_items = array_merge($display_menu_items, $this->get_menu_items_by_parent_id($menu_items, $menu_item->parent_id)); } } if (empty($display_menu_items)) { // we don't have anything to display yet, so display top-level items $display_menu_items = $this->get_menu_items_by_parent_id($menu_items, 0); } // filters out duplicates $display_menu_items = array_filter($display_menu_items, function ($display_menu_item) { static $ids = array(); // always include custom pages - they ALL have a post_id of 0, which // will break the below checks. if ($display_menu_item->object == 'custom' && $display_menu_item->post_id == 0) { return true; } if (in_array($display_menu_item->post_id, $ids)) { return false; } else { $ids[] = $display_menu_item->post_id; return true; } }); $args = (object) shortcode_atts(array('theme_location' => '', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => ''), $atts, 'related_links'); return $this->generate_menu_html('related-links-' . $args->theme_location, $display_menu_items, -1, $args); }
/** * 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(); } }