Exemplo n.º 1
0
 /**
  * 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);
 }