Advertisement
Guest User

bbPress Forum - Twitter Bootstrap Breadcrumbs

a guest
Nov 1st, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.68 KB | None | 0 0
  1. <?php
  2. /** Breadcrumbs **** ( bbPress Forum - Twitter Bootstrap Breadcrumbs ) **********/
  3.  
  4. /**
  5.  * Output the page title as a breadcrumb
  6.  *
  7.  * @since bbp_shmooress (r2589)
  8.  *
  9.  * @param string $sep Separator. Defaults to '&larr;'
  10.  * @param bool $current_page Include the current item
  11.  * @param bool $root Include the root page if one exists
  12.  * @uses bbp_shmoo_get_breadcrumb() To get the breadcrumb
  13.  */
  14. function bbp_shmoo_title_breadcrumb( $args = array() ) {
  15.     echo bbp_shmoo_get_breadcrumb( $args );
  16. }
  17.  
  18. /**
  19.  * Output a breadcrumb
  20.  *
  21.  * @since bbp_shmooress (r2589)
  22.  *
  23.  * @param string $sep Separator. Defaults to '&larr;'
  24.  * @param bool $current_page Include the current item
  25.  * @param bool $root Include the root page if one exists
  26.  * @uses bbp_shmoo_get_breadcrumb() To get the breadcrumb
  27.  */
  28. function bbp_shmoo_breadcrumb( $args = array() ) {
  29.     echo bbp_shmoo_get_breadcrumb( $args );
  30. }
  31.     /**
  32.      * Return a breadcrumb ( forum -> topic -> reply )
  33.      *
  34.      * @since bbp_shmooress (r2589)
  35.      *
  36.      * @param string $sep Separator. Defaults to '&larr;'
  37.      * @param bool $current_page Include the current item
  38.      * @param bool $root Include the root page if one exists
  39.      *
  40.      * @uses get_post() To get the post
  41.      * @uses bbp_shmoo_get_forum_permalink() To get the forum link
  42.      * @uses bbp_shmoo_get_topic_permalink() To get the topic link
  43.      * @uses bbp_shmoo_get_reply_permalink() To get the reply link
  44.      * @uses get_permalink() To get the permalink
  45.      * @uses bbp_shmoo_get_forum_post_type() To get the forum post type
  46.      * @uses bbp_shmoo_get_topic_post_type() To get the topic post type
  47.      * @uses bbp_shmoo_get_reply_post_type() To get the reply post type
  48.      * @uses bbp_shmoo_get_forum_title() To get the forum title
  49.      * @uses bbp_shmoo_get_topic_title() To get the topic title
  50.      * @uses bbp_shmoo_get_reply_title() To get the reply title
  51.      * @uses get_the_title() To get the title
  52.      * @uses apply_filters() Calls 'bbp_shmoo_get_breadcrumb' with the crumbs
  53.      * @return string Breadcrumbs
  54.      */
  55.     function bbp_shmoo_get_breadcrumb( $args = array() ) {
  56.  
  57.         // Turn off breadcrumbs
  58.         if ( apply_filters( 'bbp_shmoo_no_breadcrumb', is_front_page() ) )
  59.             return;
  60.  
  61.         // Define variables
  62.         $front_id         = $root_id                                 = 0;
  63.         $ancestors        = $crumbs           = $tag_data            = array();
  64.         $pre_root_text    = $pre_front_text   = $pre_current_text    = '';
  65.         $pre_include_root = $pre_include_home = $pre_include_current = true;
  66.  
  67.         /** Home Text *********************************************************/
  68.  
  69.         // No custom home text
  70.         if ( empty( $args['home_text'] ) ) {
  71.  
  72.             // Set home text to page title
  73.             $front_id = get_option( 'page_on_front' );
  74.             if ( !empty( $front_id ) ) {
  75.                 $pre_front_text = get_the_title( $front_id );
  76.  
  77.             // Default to 'Home'
  78.             } else {
  79.                 $pre_front_text = __( 'Home', 'bbpress' );
  80.             }
  81.         }
  82.  
  83.         /** Root Text *********************************************************/
  84.  
  85.         // No custom root text
  86.         if ( empty( $args['root_text'] ) ) {
  87.             $page = bbp_get_page_by_path( bbp_get_root_slug() );
  88.             if ( !empty( $page ) ) {
  89.                 $root_id = $page->ID;
  90.             }
  91.             $pre_root_text = bbp_get_forum_archive_title();
  92.         }
  93.  
  94.         /** Includes **********************************************************/
  95.  
  96.         // Root slug is also the front page
  97.         if ( !empty( $front_id ) && ( $front_id == $root_id ) )
  98.             $pre_include_root = false;
  99.  
  100.         // Don't show root if viewing forum archive
  101.         if ( bbp_is_forum_archive() )
  102.             $pre_include_root = false;
  103.  
  104.         // Don't show root if viewing page in place of forum archive
  105.         if ( !empty( $root_id ) && ( ( is_single() || is_page() ) && ( $root_id == get_the_ID() ) ) )
  106.             $pre_include_root = false;
  107.  
  108.         /** Current Text ******************************************************/
  109.  
  110.         // Forum archive
  111.         if ( bbp_is_forum_archive() ) {
  112.             $pre_current_text = bbp_get_forum_archive_title();
  113.  
  114.         // Topic archive
  115.         } elseif ( bbp_is_topic_archive() ) {
  116.             $pre_current_text = bbp_get_topic_archive_title();
  117.  
  118.         // View
  119.         } elseif ( bbp_is_single_view() ) {
  120.             $pre_current_text = bbp_get_view_title();
  121.  
  122.         // Single Forum
  123.         } elseif ( bbp_is_single_forum() ) {
  124.             $pre_current_text = bbp_get_forum_title();
  125.  
  126.         // Single Topic
  127.         } elseif ( bbp_is_single_topic() ) {
  128.             $pre_current_text = bbp_get_topic_title();
  129.  
  130.         // Single Topic
  131.         } elseif ( bbp_is_single_reply() ) {
  132.             $pre_current_text = bbp_get_reply_title();
  133.  
  134.         // Topic Tag (or theme compat topic tag)
  135.         } elseif ( bbp_is_topic_tag() || ( get_query_var( 'bbp_topic_tag' ) && !bbp_is_topic_tag_edit() ) ) {
  136.  
  137.             // Always include the tag name
  138.             $tag_data[] = bbp_get_topic_tag_name();
  139.  
  140.             // If capable, include a link to edit the tag
  141.             if ( current_user_can( 'manage_topic_tags' ) ) {
  142.                 $tag_data[] = '<a href="' . bbp_get_topic_tag_edit_link() . '" class="bbp-edit-topic-tag-link">' . __( '(Edit)', 'bbpress' ) . '</a>';
  143.             }
  144.  
  145.             // Implode the results of the tag data
  146.             $pre_current_text = sprintf( __( 'Topic Tag: %s', 'bbpress' ), implode( ' ', $tag_data ) );
  147.  
  148.         // Edit Topic Tag
  149.         } elseif ( bbp_is_topic_tag_edit() ) {
  150.             $pre_current_text = __( 'Edit', 'bbpress' );
  151.  
  152.         // Single
  153.         } else {
  154.             $pre_current_text = get_the_title();
  155.         }
  156.  
  157.         /** Parse Args ********************************************************/
  158.  
  159.         // Parse args
  160.         $defaults = array(
  161.  
  162.             // HTML
  163.             'before'          => '<ul class="breadcrumb">',
  164.             'after'           => '</ul>',
  165.             'sep'             => __( '&rsaquo;', 'bbpress' ),
  166.             'pad_sep'         => 1,
  167.  
  168.             // Home
  169.             'include_home'    => $pre_include_home,
  170.             'home_text'       => $pre_front_text,
  171.  
  172.             // Forum root
  173.             'include_root'    => $pre_include_root,
  174.             'root_text'       => $pre_root_text,
  175.  
  176.             // Current
  177.             'include_current' => $pre_include_current,
  178.             'current_text'    => $pre_current_text
  179.         );
  180.         $r = bbp_parse_args( $args, $defaults, 'get_breadcrumb' );
  181.         extract( $r );
  182.  
  183.         /** Ancestors *********************************************************/
  184.  
  185.         // Get post ancestors
  186.         if ( is_page() || is_single() || bbp_is_forum_edit() || bbp_is_topic_edit() || bbp_is_reply_edit() )
  187.             $ancestors = array_reverse( get_post_ancestors( get_the_ID() ) );
  188.  
  189.         // Do we want to include a link to home?
  190.         if ( !empty( $include_home ) || empty( $home_text ) )
  191.             $crumbs[] = '<li><a href="' . trailingslashit( home_url() ) . '" class="bbp-breadcrumb-home">' . $home_text . '</a><span class="divider"> /</span></li>';
  192.  
  193.         // Do we want to include a link to the forum root?
  194.         if ( !empty( $include_root ) || empty( $root_text ) ) {
  195.  
  196.             // Page exists at root slug path, so use its permalink
  197.             $page = bbp_get_page_by_path( bbp_get_root_slug() );
  198.             if ( !empty( $page ) ) {
  199.                 $root_url = get_permalink( $page->ID );
  200.  
  201.             // Use the root slug
  202.             } else {
  203.                 $root_url = get_post_type_archive_link( bbp_get_forum_post_type() );
  204.             }
  205.  
  206.             // Add the breadcrumb
  207.             $crumbs[] = '<li><a href="' . $root_url . '" class="bbp-breadcrumb-root">' . $root_text . '</a><span class="divider"> /</span></li>';
  208.         }
  209.  
  210.         // Ancestors exist
  211.         if ( !empty( $ancestors ) ) {
  212.  
  213.             // Loop through parents
  214.             foreach( (array) $ancestors as $parent_id ) {
  215.  
  216.                 // Parents
  217.                 $parent = get_post( $parent_id );
  218.  
  219.                 // Switch through post_type to ensure correct filters are applied
  220.                 switch ( $parent->post_type ) {
  221.  
  222.                     // Forum
  223.                     case bbp_get_forum_post_type() :
  224.                         $crumbs[] = '<li><a href="' . bbp_get_forum_permalink( $parent->ID ) . '" class="bbp-breadcrumb-forum">' . bbp_get_forum_title( $parent->ID ) . '</a><span class="divider"> /</span></li>';
  225.                         break;
  226.  
  227.                     // Topic
  228.                     case bbp_get_topic_post_type() :
  229.                         $crumbs[] = '<li><a href="' . bbp_get_topic_permalink( $parent->ID ) . '" class="bbp-breadcrumb-topic">' . bbp_get_topic_title( $parent->ID ) . '</a><span class="divider"> /</span></li>';
  230.                         break;
  231.  
  232.                     // Reply (Note: not in most themes)
  233.                     case bbp_get_reply_post_type() :
  234.                         $crumbs[] = '<li><a href="' . bbp_get_reply_permalink( $parent->ID ) . '" class="bbp-breadcrumb-reply">' . bbp_get_reply_title( $parent->ID ) . '</a><span class="divider"> /</span></li>';
  235.                         break;
  236.  
  237.                     // WordPress Post/Page/Other
  238.                     default :
  239.                         $crumbs[] = '<li><a href="' . get_permalink( $parent->ID ) . '" class="bbp-breadcrumb-item">' . get_the_title( $parent->ID ) . '</a><span class="divider"> /</span></li>';
  240.                         break;
  241.                 }
  242.             }
  243.  
  244.         // Edit topic tag
  245.         } elseif ( bbp_is_topic_tag_edit() ) {
  246.             $crumbs[] = '<a href="' . get_term_link( bbp_get_topic_tag_id(), bbp_get_topic_tag_tax_id() ) . '" class="bbp-breadcrumb-topic-tag">' . sprintf( __( 'Topic Tag: %s', 'bbpress' ), bbp_get_topic_tag_name() ) . '</a>';
  247.         }
  248.  
  249.         /** Current ***********************************************************/
  250.  
  251.         // Add current page to breadcrumb
  252.         if ( !empty( $include_current ) || empty( $pre_current_text ) )
  253.             $crumbs[] = '<li><span class="active">' . $current_text . '</span></li>';
  254.  
  255.         /** Separator *********************************************************/
  256.  
  257.         // Wrap the separator in a span before padding and filter
  258.         if ( !empty( $sep ) )
  259.             $sep = '<span class="bbp-breadcrumb-separator">' . $sep . '</span>';
  260.  
  261.         // Pad the separator
  262.         if ( !empty( $pad_sep ) )
  263.             $sep = str_pad( $sep, strlen( $sep ) + ( (int) $pad_sep * 2 ), ' ', STR_PAD_BOTH );
  264.  
  265.         /** Finish Up *********************************************************/
  266.  
  267.         // Filter the separator and breadcrumb
  268.         $sep    = apply_filters( 'bbp_shmoo_breadcrumb_separator', $sep    );
  269.         $crumbs = apply_filters( 'bbp_shmoo_breadcrumbs',          $crumbs );
  270.  
  271.         // Build the trail
  272.         $trail = !empty( $crumbs ) ? ( $before . implode( $sep, $crumbs ) . $after ) : '';
  273.  
  274.         return apply_filters( 'bbp_shmoo_get_breadcrumb', $trail, $crumbs, $r );
  275.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement