PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/extensions/breadcrumb-trail.php

https://bitbucket.org/lgorence/quickpress
PHP | 692 lines | 312 code | 156 blank | 224 comment | 95 complexity | 9ab8defa5a6005cf08409f09223fc4ce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Breadcrumb Trail - A breadcrumb menu script for WordPress.
  4. *
  5. * Breadcrumb Trail is a script for showing a breadcrumb trail for any type of page. It tries to
  6. * anticipate any type of structure and display the best possible trail that matches your site's
  7. * permalink structure. While not perfect, it attempts to fill in the gaps left by many other
  8. * breadcrumb scripts.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  11. * General Public License as published by the Free Software Foundation; either version 2 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  15. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * @package BreadcrumbTrail
  18. * @version 0.5.2
  19. * @author Justin Tadlock <justin@justintadlock.com>
  20. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  21. * @link http://justintadlock.com/archives/2009/04/05/breadcrumb-trail-wordpress-plugin
  22. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. */
  24. /**
  25. * Shows a breadcrumb for all types of pages. This function is formatting the final output of the
  26. * breadcrumb trail. The breadcrumb_trail_get_items() function returns the items and this function
  27. * formats those items.
  28. *
  29. * @since 0.1.0
  30. * @access public
  31. * @param array $args Mixed arguments for the menu.
  32. * @return string Output of the breadcrumb menu.
  33. */
  34. function breadcrumb_trail( $args = array() ) {
  35. /* Create an empty variable for the breadcrumb. */
  36. $breadcrumb = '';
  37. /* Set up the default arguments for the breadcrumb. */
  38. $defaults = array(
  39. 'container' => 'div', // div, nav, p, etc.
  40. 'separator' => '/',
  41. 'before' => __( 'Browse:', 'breadcrumb-trail' ),
  42. 'after' => false,
  43. 'front_page' => true,
  44. 'show_home' => __( 'Home', 'breadcrumb-trail' ),
  45. 'echo' => true
  46. );
  47. /* Allow singular post views to have a taxonomy's terms prefixing the trail. */
  48. if ( is_singular() ) {
  49. $post = get_queried_object();
  50. $defaults["singular_{$post->post_type}_taxonomy"] = false;
  51. }
  52. /* Apply filters to the arguments. */
  53. $args = apply_filters( 'breadcrumb_trail_args', $args );
  54. /* Parse the arguments and extract them for easy variable naming. */
  55. $args = wp_parse_args( $args, $defaults );
  56. /* Get the trail items. */
  57. $trail = breadcrumb_trail_get_items( $args );
  58. /* Connect the breadcrumb trail if there are items in the trail. */
  59. if ( !empty( $trail ) && is_array( $trail ) ) {
  60. /* Open the breadcrumb trail containers. */
  61. $breadcrumb = '<' . tag_escape( $args['container'] ) . ' class="breadcrumb-trail breadcrumbs" itemprop="breadcrumb">';
  62. /* If $before was set, wrap it in a container. */
  63. $breadcrumb .= ( !empty( $args['before'] ) ? '<span class="trail-before">' . $args['before'] . '</span> ' : '' );
  64. /* Adds the 'trail-begin' class around first item if there's more than one item. */
  65. if ( 1 < count( $trail ) )
  66. array_unshift( $trail, '<span class="trail-begin">' . array_shift( $trail ) . '</span>' );
  67. /* Adds the 'trail-end' class around last item. */
  68. array_push( $trail, '<span class="trail-end">' . array_pop( $trail ) . '</span>' );
  69. /* Format the separator. */
  70. $separator = ( !empty( $args['separator'] ) ? '<span class="sep">' . $args['separator'] . '</span>' : '<span class="sep">/</span>' );
  71. /* Join the individual trail items into a single string. */
  72. $breadcrumb .= join( " {$separator} ", $trail );
  73. /* If $after was set, wrap it in a container. */
  74. $breadcrumb .= ( !empty( $args['after'] ) ? ' <span class="trail-after">' . $args['after'] . '</span>' : '' );
  75. /* Close the breadcrumb trail containers. */
  76. $breadcrumb .= '</' . tag_escape( $args['container'] ) . '>';
  77. }
  78. /* Allow developers to filter the breadcrumb trail HTML. */
  79. $breadcrumb = apply_filters( 'breadcrumb_trail', $breadcrumb, $args );
  80. /* Output the breadcrumb. */
  81. if ( $args['echo'] )
  82. echo $breadcrumb;
  83. else
  84. return $breadcrumb;
  85. }
  86. /**
  87. * Gets the items for the breadcrumb trail. This is the heart of the script. It checks the current page
  88. * being viewed and decided based on the information provided by WordPress what items should be
  89. * added to the breadcrumb trail.
  90. *
  91. * @since 0.4.0
  92. * @todo Build in caching based on the queried object ID.
  93. * @access private
  94. * @param array $args Mixed arguments for the menu.
  95. * @return array List of items to be shown in the trail.
  96. */
  97. function breadcrumb_trail_get_items( $args = array() ) {
  98. global $wp_rewrite;
  99. /* Set up an empty trail array and empty path. */
  100. $trail = array();
  101. $path = '';
  102. /* If $show_home is set and we're not on the front page of the site, link to the home page. */
  103. if ( !is_front_page() && $args['show_home'] )
  104. $trail[] = '<a href="' . home_url() . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . '" rel="home" class="trail-begin">' . $args['show_home'] . '</a>';
  105. /* If bbPress is installed and we're on a bbPress page. */
  106. if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
  107. $trail = array_merge( $trail, breadcrumb_trail_get_bbpress_items() );
  108. }
  109. /* If viewing the front page of the site. */
  110. elseif ( is_front_page() ) {
  111. if ( $args['show_home'] && $args['front_page'] )
  112. $trail[] = "{$args['show_home']}";
  113. }
  114. /* If viewing the "home"/posts page. */
  115. elseif ( is_home() ) {
  116. $home_page = get_page( get_queried_object_id() );
  117. $trail = array_merge( $trail, breadcrumb_trail_get_parents( $home_page->post_parent, '' ) );
  118. $trail[] = get_the_title( $home_page->ID );
  119. }
  120. /* If viewing a singular post (page, attachment, etc.). */
  121. elseif ( is_singular() ) {
  122. /* Get singular post variables needed. */
  123. $post = get_queried_object();
  124. $post_id = absint( get_queried_object_id() );
  125. $post_type = $post->post_type;
  126. $parent = absint( $post->post_parent );
  127. /* Get the post type object. */
  128. $post_type_object = get_post_type_object( $post_type );
  129. /* If viewing a singular 'post'. */
  130. if ( 'post' == $post_type ) {
  131. /* If $front has been set, add it to the $path. */
  132. $path .= trailingslashit( $wp_rewrite->front );
  133. /* If there's a path, check for parents. */
  134. if ( !empty( $path ) )
  135. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  136. /* Map the permalink structure tags to actual links. */
  137. $trail = array_merge( $trail, breadcrumb_trail_map_rewrite_tags( $post_id, get_option( 'permalink_structure' ), $args ) );
  138. }
  139. /* If viewing a singular 'attachment'. */
  140. elseif ( 'attachment' == $post_type ) {
  141. /* If $front has been set, add it to the $path. */
  142. $path .= trailingslashit( $wp_rewrite->front );
  143. /* If there's a path, check for parents. */
  144. if ( !empty( $path ) )
  145. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  146. /* Map the post (parent) permalink structure tags to actual links. */
  147. $trail = array_merge( $trail, breadcrumb_trail_map_rewrite_tags( $post->post_parent, get_option( 'permalink_structure' ), $args ) );
  148. }
  149. /* If a custom post type, check if there are any pages in its hierarchy based on the slug. */
  150. elseif ( 'page' !== $post_type ) {
  151. /* If $front has been set, add it to the $path. */
  152. if ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front )
  153. $path .= trailingslashit( $wp_rewrite->front );
  154. /* If there's a slug, add it to the $path. */
  155. if ( !empty( $post_type_object->rewrite['slug'] ) )
  156. $path .= $post_type_object->rewrite['slug'];
  157. /* If there's a path, check for parents. */
  158. if ( !empty( $path ) )
  159. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  160. /* If there's an archive page, add it to the trail. */
  161. if ( !empty( $post_type_object->has_archive ) )
  162. $trail[] = '<a href="' . get_post_type_archive_link( $post_type ) . '" title="' . esc_attr( $post_type_object->labels->name ) . '">' . $post_type_object->labels->name . '</a>';
  163. }
  164. /* If the post type path returns nothing and there is a parent, get its parents. */
  165. if ( ( empty( $path ) && 0 !== $parent ) || ( 'attachment' == $post_type ) )
  166. $trail = array_merge( $trail, breadcrumb_trail_get_parents( $parent, '' ) );
  167. /* Or, if the post type is hierarchical and there's a parent, get its parents. */
  168. elseif ( 0 !== $parent && is_post_type_hierarchical( $post_type ) )
  169. $trail = array_merge( $trail, breadcrumb_trail_get_parents( $parent, '' ) );
  170. /* Display terms for specific post type taxonomy if requested. */
  171. if ( !empty( $args["singular_{$post_type}_taxonomy"] ) && $terms = get_the_term_list( $post_id, $args["singular_{$post_type}_taxonomy"], '', ', ', '' ) )
  172. $trail[] = $terms;
  173. /* End with the post title. */
  174. $post_title = single_post_title( '', false );
  175. if ( !empty( $post_title ) )
  176. $trail[] = $post_title;
  177. }
  178. /* If we're viewing any type of archive. */
  179. elseif ( is_archive() ) {
  180. /* If viewing a taxonomy term archive. */
  181. if ( is_tax() || is_category() || is_tag() ) {
  182. /* Get some taxonomy and term variables. */
  183. $term = get_queried_object();
  184. $taxonomy = get_taxonomy( $term->taxonomy );
  185. /* Get the path to the term archive. Use this to determine if a page is present with it. */
  186. if ( is_category() )
  187. $path = get_option( 'category_base' );
  188. elseif ( is_tag() )
  189. $path = get_option( 'tag_base' );
  190. else {
  191. if ( $taxonomy->rewrite['with_front'] && $wp_rewrite->front )
  192. $path = trailingslashit( $wp_rewrite->front );
  193. $path .= $taxonomy->rewrite['slug'];
  194. }
  195. /* Get parent pages by path if they exist. */
  196. if ( $path )
  197. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  198. /* If the taxonomy is hierarchical, list its parent terms. */
  199. if ( is_taxonomy_hierarchical( $term->taxonomy ) && $term->parent )
  200. $trail = array_merge( $trail, breadcrumb_trail_get_term_parents( $term->parent, $term->taxonomy ) );
  201. /* Add the term name to the trail end. */
  202. $trail[] = single_term_title( '', false );
  203. }
  204. /* If viewing a post type archive. */
  205. elseif ( is_post_type_archive() ) {
  206. /* Get the post type object. */
  207. $post_type_object = get_post_type_object( get_query_var( 'post_type' ) );
  208. /* If $front has been set, add it to the $path. */
  209. if ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front )
  210. $path .= trailingslashit( $wp_rewrite->front );
  211. /* If there's a slug, add it to the $path. */
  212. if ( !empty( $post_type_object->rewrite['slug'] ) )
  213. $path .= $post_type_object->rewrite['slug'];
  214. /* If there's a path, check for parents. */
  215. if ( !empty( $path ) )
  216. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  217. /* Add the post type [plural] name to the trail end. */
  218. $trail[] = $post_type_object->labels->name;
  219. }
  220. /* If viewing an author archive. */
  221. elseif ( is_author() ) {
  222. /* If $front has been set, add it to $path. */
  223. if ( !empty( $wp_rewrite->front ) )
  224. $path .= trailingslashit( $wp_rewrite->front );
  225. /* If an $author_base exists, add it to $path. */
  226. if ( !empty( $wp_rewrite->author_base ) )
  227. $path .= $wp_rewrite->author_base;
  228. /* If $path exists, check for parent pages. */
  229. if ( !empty( $path ) )
  230. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $path ) );
  231. /* Add the author's display name to the trail end. */
  232. $trail[] = get_the_author_meta( 'display_name', get_query_var( 'author' ) );
  233. }
  234. /* If viewing a time-based archive. */
  235. elseif ( is_time() ) {
  236. if ( get_query_var( 'minute' ) && get_query_var( 'hour' ) )
  237. $trail[] = get_the_time( __( 'g:i a', 'breadcrumb-trail' ) );
  238. elseif ( get_query_var( 'minute' ) )
  239. $trail[] = sprintf( __( 'Minute %1$s', 'breadcrumb-trail' ), get_the_time( __( 'i', 'breadcrumb-trail' ) ) );
  240. elseif ( get_query_var( 'hour' ) )
  241. $trail[] = get_the_time( __( 'g a', 'breadcrumb-trail' ) );
  242. }
  243. /* If viewing a date-based archive. */
  244. elseif ( is_date() ) {
  245. /* If $front has been set, check for parent pages. */
  246. if ( $wp_rewrite->front )
  247. $trail = array_merge( $trail, breadcrumb_trail_get_parents( '', $wp_rewrite->front ) );
  248. if ( is_day() ) {
  249. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'breadcrumb-trail' ) ) . '">' . get_the_time( __( 'Y', 'breadcrumb-trail' ) ) . '</a>';
  250. $trail[] = '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" title="' . get_the_time( esc_attr__( 'F', 'breadcrumb-trail' ) ) . '">' . get_the_time( __( 'F', 'breadcrumb-trail' ) ) . '</a>';
  251. $trail[] = get_the_time( __( 'd', 'breadcrumb-trail' ) );
  252. }
  253. elseif ( get_query_var( 'w' ) ) {
  254. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'breadcrumb-trail' ) ) . '">' . get_the_time( __( 'Y', 'breadcrumb-trail' ) ) . '</a>';
  255. $trail[] = sprintf( __( 'Week %1$s', 'breadcrumb-trail' ), get_the_time( esc_attr__( 'W', 'breadcrumb-trail' ) ) );
  256. }
  257. elseif ( is_month() ) {
  258. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'breadcrumb-trail' ) ) . '">' . get_the_time( __( 'Y', 'breadcrumb-trail' ) ) . '</a>';
  259. $trail[] = get_the_time( __( 'F', 'breadcrumb-trail' ) );
  260. }
  261. elseif ( is_year() ) {
  262. $trail[] = get_the_time( __( 'Y', 'breadcrumb-trail' ) );
  263. }
  264. }
  265. }
  266. /* If viewing search results. */
  267. elseif ( is_search() )
  268. $trail[] = sprintf( __( 'Search results for &quot;%1$s&quot;', 'breadcrumb-trail' ), esc_attr( get_search_query() ) );
  269. /* If viewing a 404 error page. */
  270. elseif ( is_404() )
  271. $trail[] = __( '404 Not Found', 'breadcrumb-trail' );
  272. /* Allow devs to step in and filter the $trail array. */
  273. return apply_filters( 'breadcrumb_trail_items', $trail, $args );
  274. }
  275. /**
  276. * Gets the items for the breadcrumb trail if bbPress is installed.
  277. *
  278. * @since 0.5.0
  279. * @access private
  280. * @param array $args Mixed arguments for the menu.
  281. * @return array List of items to be shown in the trail.
  282. */
  283. function breadcrumb_trail_get_bbpress_items( $args = array() ) {
  284. /* Set up a new trail items array. */
  285. $trail = array();
  286. /* Get the forum post type object. */
  287. $post_type_object = get_post_type_object( bbp_get_forum_post_type() );
  288. /* If not viewing the forum root/archive page and a forum archive exists, add it. */
  289. if ( !empty( $post_type_object->has_archive ) && !bbp_is_forum_archive() )
  290. $trail[] = '<a href="' . get_post_type_archive_link( bbp_get_forum_post_type() ) . '">' . bbp_get_forum_archive_title() . '</a>';
  291. /* If viewing the forum root/archive. */
  292. if ( bbp_is_forum_archive() ) {
  293. $trail[] = bbp_get_forum_archive_title();
  294. }
  295. /* If viewing the topics archive. */
  296. elseif ( bbp_is_topic_archive() ) {
  297. $trail[] = bbp_get_topic_archive_title();
  298. }
  299. /* If viewing a topic tag archive. */
  300. elseif ( bbp_is_topic_tag() ) {
  301. $trail[] = bbp_get_topic_tag_name();
  302. }
  303. /* If viewing a topic tag edit page. */
  304. elseif ( bbp_is_topic_tag_edit() ) {
  305. $trail[] = '<a href="' . bbp_get_topic_tag_link() . '">' . bbp_get_topic_tag_name() . '</a>';
  306. $trail[] = __( 'Edit', 'breadcrumb-trail' );
  307. }
  308. /* If viewing a "view" page. */
  309. elseif ( bbp_is_single_view() ) {
  310. $trail[] = bbp_get_view_title();
  311. }
  312. /* If viewing a single topic page. */
  313. elseif ( bbp_is_single_topic() ) {
  314. /* Get the queried topic. */
  315. $topic_id = get_queried_object_id();
  316. /* Get the parent items for the topic, which would be its forum (and possibly forum grandparents). */
  317. $trail = array_merge( $trail, breadcrumb_trail_get_parents( bbp_get_topic_forum_id( $topic_id ) ) );
  318. /* If viewing a split, merge, or edit topic page, show the link back to the topic. Else, display topic title. */
  319. if ( bbp_is_topic_split() || bbp_is_topic_merge() || bbp_is_topic_edit() )
  320. $trail[] = '<a href="' . bbp_get_topic_permalink( $topic_id ) . '">' . bbp_get_topic_title( $topic_id ) . '</a>';
  321. else
  322. $trail[] = bbp_get_topic_title( $topic_id );
  323. /* If viewing a topic split page. */
  324. if ( bbp_is_topic_split() )
  325. $trail[] = __( 'Split', 'breadcrumb-trail' );
  326. /* If viewing a topic merge page. */
  327. elseif ( bbp_is_topic_merge() )
  328. $trail[] = __( 'Merge', 'breadcrumb-trail' );
  329. /* If viewing a topic edit page. */
  330. elseif ( bbp_is_topic_edit() )
  331. $trail[] = __( 'Edit', 'breadcrumb-trail' );
  332. }
  333. /* If viewing a single reply page. */
  334. elseif ( bbp_is_single_reply() ) {
  335. /* Get the queried reply object ID. */
  336. $reply_id = get_queried_object_id();
  337. /* Get the parent items for the reply, which should be its topic. */
  338. $trail = array_merge( $trail, breadcrumb_trail_get_parents( bbp_get_reply_topic_id( $reply_id ) ) );
  339. /* If viewing a reply edit page, link back to the reply. Else, display the reply title. */
  340. if ( bbp_is_reply_edit() ) {
  341. $trail[] = '<a href="' . bbp_get_reply_url( $reply_id ) . '">' . bbp_get_reply_title( $reply_id ) . '</a>';
  342. $trail[] = __( 'Edit', 'breadcrumb-trail' );
  343. } else {
  344. $trail[] = bbp_get_reply_title( $reply_id );
  345. }
  346. }
  347. /* If viewing a single forum. */
  348. elseif ( bbp_is_single_forum() ) {
  349. /* Get the queried forum ID and its parent forum ID. */
  350. $forum_id = get_queried_object_id();
  351. $forum_parent_id = bbp_get_forum_parent_id( $forum_id );
  352. /* If the forum has a parent forum, get its parent(s). */
  353. if ( 0 !== $forum_parent_id)
  354. $trail = array_merge( $trail, breadcrumb_trail_get_parents( $forum_parent_id ) );
  355. /* Add the forum title to the end of the trail. */
  356. $trail[] = bbp_get_forum_title( $forum_id );
  357. }
  358. /* If viewing a user page or user edit page. */
  359. elseif ( bbp_is_single_user() || bbp_is_single_user_edit() ) {
  360. if ( bbp_is_single_user_edit() ) {
  361. $trail[] = '<a href="' . bbp_get_user_profile_url() . '">' . bbp_get_displayed_user_field( 'display_name' ) . '</a>';
  362. $trail[] = __( 'Edit', 'breadcrumb-trail' );
  363. } else {
  364. $trail[] = bbp_get_displayed_user_field( 'display_name' );
  365. }
  366. }
  367. /* Return the bbPress breadcrumb trail items. */
  368. return apply_filters( 'breadcrumb_trail_get_bbpress_items', $trail, $args );
  369. }
  370. /**
  371. * Turns %tag% from permalink structures into usable links for the breadcrumb trail. This feels kind of
  372. * hackish for now because we're checking for specific %tag% examples and only doing it for the 'post'
  373. * post type. In the future, maybe it'll handle a wider variety of possibilities, especially for custom post
  374. * types.
  375. *
  376. * @since 0.4.0
  377. * @access private
  378. * @param int $post_id ID of the post whose parents we want.
  379. * @param string $path Path of a potential parent page.
  380. * @param array $args Mixed arguments for the menu.
  381. * @return array $trail Array of links to the post breadcrumb.
  382. */
  383. function breadcrumb_trail_map_rewrite_tags( $post_id = '', $path = '', $args = array() ) {
  384. /* Set up an empty $trail array. */
  385. $trail = array();
  386. /* Make sure there's a $path and $post_id before continuing. */
  387. if ( empty( $path ) || empty( $post_id ) )
  388. return $trail;
  389. /* Get the post based on the post ID. */
  390. $post = get_post( $post_id );
  391. /* If no post is returned, an error is returned, or the post does not have a 'post' post type, return. */
  392. if ( empty( $post ) || is_wp_error( $post ) || 'post' !== $post->post_type )
  393. return $trail;
  394. /* Trim '/' from both sides of the $path. */
  395. $path = trim( $path, '/' );
  396. /* Split the $path into an array of strings. */
  397. $matches = explode( '/', $path );
  398. /* If matches are found for the path. */
  399. if ( is_array( $matches ) ) {
  400. /* Loop through each of the matches, adding each to the $trail array. */
  401. foreach ( $matches as $match ) {
  402. /* Trim any '/' from the $match. */
  403. $tag = trim( $match, '/' );
  404. /* If using the %year% tag, add a link to the yearly archive. */
  405. if ( '%year%' == $tag )
  406. $trail[] = '<a href="' . get_year_link( get_the_time( 'Y', $post_id ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'breadcrumb-trail' ), $post_id ) . '">' . get_the_time( __( 'Y', 'breadcrumb-trail' ), $post_id ) . '</a>';
  407. /* If using the %monthnum% tag, add a link to the monthly archive. */
  408. elseif ( '%monthnum%' == $tag )
  409. $trail[] = '<a href="' . get_month_link( get_the_time( 'Y', $post_id ), get_the_time( 'm', $post_id ) ) . '" title="' . get_the_time( esc_attr__( 'F Y', 'breadcrumb-trail' ), $post_id ) . '">' . get_the_time( __( 'F', 'breadcrumb-trail' ), $post_id ) . '</a>';
  410. /* If using the %day% tag, add a link to the daily archive. */
  411. elseif ( '%day%' == $tag )
  412. $trail[] = '<a href="' . get_day_link( get_the_time( 'Y', $post_id ), get_the_time( 'm', $post_id ), get_the_time( 'd', $post_id ) ) . '" title="' . get_the_time( esc_attr__( 'F j, Y', 'breadcrumb-trail' ), $post_id ) . '">' . get_the_time( __( 'd', 'breadcrumb-trail' ), $post_id ) . '</a>';
  413. /* If using the %author% tag, add a link to the post author archive. */
  414. elseif ( '%author%' == $tag )
  415. $trail[] = '<a href="' . get_author_posts_url( $post->post_author ) . '" title="' . esc_attr( get_the_author_meta( 'display_name', $post->post_author ) ) . '">' . get_the_author_meta( 'display_name', $post->post_author ) . '</a>';
  416. /* If using the %category% tag, add a link to the first category archive to match permalinks. */
  417. elseif ( '%category%' == $tag && 'category' !== $args["singular_{$post->post_type}_taxonomy"] ) {
  418. /* Get the post categories. */
  419. $terms = get_the_category( $post_id );
  420. /* Check that categories were returned. */
  421. if ( $terms ) {
  422. /* Sort the terms by ID and get the first category. */
  423. usort( $terms, '_usort_terms_by_ID' );
  424. $term = get_term( $terms[0], 'category' );
  425. /* If the category has a parent, add the hierarchy to the trail. */
  426. if ( 0 !== $term->parent )
  427. $trail = array_merge( $trail, breadcrumb_trail_get_term_parents( $term->parent, 'category' ) );
  428. /* Add the category archive link to the trail. */
  429. $trail[] = '<a href="' . get_term_link( $term, 'category' ) . '" title="' . esc_attr( $term->name ) . '">' . $term->name . '</a>';
  430. }
  431. }
  432. }
  433. }
  434. /* Return the $trail array. */
  435. return $trail;
  436. }
  437. /**
  438. * Gets parent pages of any post type or taxonomy by the ID or Path. The goal of this function is to create
  439. * a clear path back to home given what would normally be a "ghost" directory. If any page matches the given
  440. * path, it'll be added. But, it's also just a way to check for a hierarchy with hierarchical post types.
  441. *
  442. * @since 0.3.0
  443. * @access private
  444. * @param int $post_id ID of the post whose parents we want.
  445. * @param string $path Path of a potential parent page.
  446. * @return array $trail Array of parent page links.
  447. */
  448. function breadcrumb_trail_get_parents( $post_id = '', $path = '' ) {
  449. /* Set up an empty trail array. */
  450. $trail = array();
  451. /* Trim '/' off $path in case we just got a simple '/' instead of a real path. */
  452. $path = trim( $path, '/' );
  453. /* If neither a post ID nor path set, return an empty array. */
  454. if ( empty( $post_id ) && empty( $path ) )
  455. return $trail;
  456. /* If the post ID is empty, use the path to get the ID. */
  457. if ( empty( $post_id ) ) {
  458. /* Get parent post by the path. */
  459. $parent_page = get_page_by_path( $path );
  460. /* If a parent post is found, set the $post_id variable to it. */
  461. if ( !empty( $parent_page ) )
  462. $post_id = $parent_page->ID;
  463. }
  464. /* If a post ID and path is set, search for a post by the given path. */
  465. if ( $post_id == 0 && !empty( $path ) ) {
  466. /* Separate post names into separate paths by '/'. */
  467. $path = trim( $path, '/' );
  468. preg_match_all( "/\/.*?\z/", $path, $matches );
  469. /* If matches are found for the path. */
  470. if ( isset( $matches ) ) {
  471. /* Reverse the array of matches to search for posts in the proper order. */
  472. $matches = array_reverse( $matches );
  473. /* Loop through each of the path matches. */
  474. foreach ( $matches as $match ) {
  475. /* If a match is found. */
  476. if ( isset( $match[0] ) ) {
  477. /* Get the parent post by the given path. */
  478. $path = str_replace( $match[0], '', $path );
  479. $parent_page = get_page_by_path( trim( $path, '/' ) );
  480. /* If a parent post is found, set the $post_id and break out of the loop. */
  481. if ( !empty( $parent_page ) && $parent_page->ID > 0 ) {
  482. $post_id = $parent_page->ID;
  483. break;
  484. }
  485. }
  486. }
  487. }
  488. }
  489. /* While there's a post ID, add the post link to the $parents array. */
  490. while ( $post_id ) {
  491. /* Get the post by ID. */
  492. $page = get_page( $post_id );
  493. /* Add the formatted post link to the array of parents. */
  494. $parents[] = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . get_the_title( $post_id ) . '</a>';
  495. /* Set the parent post's parent to the post ID. */
  496. $post_id = $page->post_parent;
  497. }
  498. /* If we have parent posts, reverse the array to put them in the proper order for the trail. */
  499. if ( isset( $parents ) )
  500. $trail = array_reverse( $parents );
  501. /* Return the trail of parent posts. */
  502. return $trail;
  503. }
  504. /**
  505. * Searches for term parents of hierarchical taxonomies. This function is similar to the WordPress
  506. * function get_category_parents() but handles any type of taxonomy.
  507. *
  508. * @since 0.3.0
  509. * @access private
  510. * @param int $parent_id The ID of the first parent.
  511. * @param object|string $taxonomy The taxonomy of the term whose parents we want.
  512. * @return array $trail Array of links to parent terms.
  513. */
  514. function breadcrumb_trail_get_term_parents( $parent_id = '', $taxonomy = '' ) {
  515. /* Set up some default arrays. */
  516. $trail = array();
  517. $parents = array();
  518. /* If no term parent ID or taxonomy is given, return an empty array. */
  519. if ( empty( $parent_id ) || empty( $taxonomy ) )
  520. return $trail;
  521. /* While there is a parent ID, add the parent term link to the $parents array. */
  522. while ( $parent_id ) {
  523. /* Get the parent term. */
  524. $parent = get_term( $parent_id, $taxonomy );
  525. /* Add the formatted term link to the array of parent terms. */
  526. $parents[] = '<a href="' . get_term_link( $parent, $taxonomy ) . '" title="' . esc_attr( $parent->name ) . '">' . $parent->name . '</a>';
  527. /* Set the parent term's parent as the parent ID. */
  528. $parent_id = $parent->parent;
  529. }
  530. /* If we have parent terms, reverse the array to put them in the proper order for the trail. */
  531. if ( !empty( $parents ) )
  532. $trail = array_reverse( $parents );
  533. /* Return the trail of parent terms. */
  534. return $trail;
  535. }
  536. ?>