onetone_Breadcrumb_Trail

Creates a breadcrumbs menu for the site based on the current page that's being viewed by the user.

Defined (1)

The class is defined in the following location(s).

/includes/breadcrumbs.php  
  1. class onetone_Breadcrumb_Trail { 
  2. /** 
  3. * Array of items belonging to the current breadcrumb trail. 
  4. * @since 0.1.0 
  5. * @access public 
  6. * @var array 
  7. */ 
  8. public $items = array(); 
  9. /** 
  10. * Arguments used to build the breadcrumb trail. 
  11. * @since 0.1.0 
  12. * @access public 
  13. * @var array 
  14. */ 
  15. public $args = array(); 
  16. /** 
  17. * Sets up the breadcrumb trail. 
  18. * @since 0.6.0 
  19. * @access public 
  20. * @param array $args The arguments for how to build the breadcrumb trail. 
  21. * @return void 
  22. */ 
  23. public function __construct( $args = array() ) { 
  24. /** Remove the bbPress breadcrumbs. */ 
  25. add_filter( 'bbp_get_breadcrumb', '__return_false' ); 
  26. $defaults = array( 
  27. 'container' => 'div',  
  28. 'separator' => '/',  
  29. 'before' => '',  
  30. 'after' => '',  
  31. 'show_on_front' => true,  
  32. 'network' => false,  
  33. //'show_edit_link' => false,  
  34. 'show_title' => true,  
  35. 'show_browse' => true,  
  36. 'echo' => true,  
  37. /** Post taxonomy (examples follow). */ 
  38. 'post_taxonomy' => array( 
  39. 'portfolio' => 'portfolio-category',  
  40. // 'book' => 'genre',  
  41. ),  
  42. /** Labels for text used (see Breadcrumb_Trail::default_labels). */ 
  43. 'labels' => array() 
  44. ); 
  45. $this->args = apply_filters( 'breadcrumb_trail_args', wp_parse_args( $args, $defaults ) ); 
  46. /** Merge the user-added labels with the defaults. */ 
  47. $this->args['labels'] = wp_parse_args( $this->args['labels'], $this->default_labels() ); 
  48. $this->do_trail_items(); 
  49. /** 
  50. * Formats and outputs the breadcrumb trail. 
  51. * @since 0.6.0 
  52. * @access public 
  53. * @return string 
  54. */ 
  55. public function trail() { 
  56. $breadcrumb = ''; 
  57. /** Connect the breadcrumb trail if there are items in the trail. */ 
  58. if ( !empty( $this->items ) && is_array( $this->items ) ) { 
  59. /** Make sure we have a unique array of items. */ 
  60. $this->items = array_unique( $this->items ); 
  61. /** Open the breadcrumb trail containers. */ 
  62. $breadcrumb = "\n\t\t" . '<' . tag_escape( $this->args['container'] ) . ' class="breadcrumb-nav breadcrumbs" itemprop="breadcrumb">'; 
  63. /** If $before was set, wrap it in a container. */ 
  64. $breadcrumb .= ( !empty( $this->args['before'] ) ? "\n\t\t\t" . '<span class="trail-before">' . $this->args['before'] . '</span> ' . "\n\t\t\t" : '' ); 
  65. /** Add 'browse' label if it should be shown. */ 
  66. if ( true === $this->args['show_browse'] ) 
  67. $breadcrumb .= "\n\t\t\t" . '<span class="trail-browse">' . $this->args['labels']['browse'] . '</span> '; 
  68. /** Adds the 'trail-begin' class around first item if there's more than one item. */ 
  69. if ( 1 < count( $this->items ) ) 
  70. array_unshift( $this->items, '<span class="trail-begin">' . array_shift( $this->items ) . '</span>' ); 
  71. /** Adds the 'trail-end' class around last item. */ 
  72. array_push( $this->items, '<span class="trail-end">' . array_pop( $this->items ) . '</span>' ); 
  73. /** Format the separator. */ 
  74. $separator = ( !empty( $this->args['separator'] ) ? '<span class="sep">' . $this->args['separator'] . '</span>' : '<span class="sep">/</span>' ); 
  75. /** Join the individual trail items into a single string. */ 
  76. $breadcrumb .= join( "\n\t\t\t {$separator} ", $this->items ); 
  77. /** If $after was set, wrap it in a container. */ 
  78. $breadcrumb .= ( !empty( $this->args['after'] ) ? "\n\t\t\t" . ' <span class="trail-after">' . $this->args['after'] . '</span>' : '' ); 
  79. /** Close the breadcrumb trail containers. */ 
  80. $breadcrumb .= "\n\t\t" . '</' . tag_escape( $this->args['container'] ) . '>'; 
  81. /** Allow developers to filter the breadcrumb trail HTML. */ 
  82. $breadcrumb = apply_filters( 'breadcrumb_trail', $breadcrumb, $this->args ); 
  83. if ( true === $this->args['echo'] ) 
  84. echo $breadcrumb; 
  85. else 
  86. return $breadcrumb; 
  87. /** 
  88. * Returns an array of the default labels. 
  89. * @since 0.6.0 
  90. * @access public 
  91. * @return array 
  92. */ 
  93. public function default_labels() { 
  94. $labels = array( 
  95. 'browse' => __( 'Browse:', 'onetone' ),  
  96. 'home' => __( 'Home', 'onetone' ),  
  97. 'error_404' => __( '404 Not Found', 'onetone' ),  
  98. 'archives' => __( 'Archives', 'onetone' ),  
  99. /** Translators: %s is the search query. The HTML entities are opening and closing curly quotes. */ 
  100. 'search' => __( 'Search results for “%s”', 'onetone' ),  
  101. /** Translators: %s is the page number. */ 
  102. 'paged' => __( 'Page %s', 'onetone' ),  
  103. /** Translators: Minute archive title. %s is the minute time format. */ 
  104. 'archive_minute' => __( 'Minute %s', 'onetone' ),  
  105. /** Translators: Weekly archive title. %s is the week date format. */ 
  106. 'archive_week' => __( 'Week %s', 'onetone' ),  
  107. /** "%s" is replaced with the translated date/time format. */ 
  108. 'archive_minute_hour' => '%s',  
  109. 'archive_hour' => '%s',  
  110. 'archive_day' => '%s',  
  111. 'archive_month' => '%s',  
  112. 'archive_year' => '%s',  
  113. ); 
  114. return $labels; 
  115. /** 
  116. * Runs through the various WordPress conditional tags to check the current page being viewed. Once 
  117. * a condition is met, a specific method is launched to add items to the $items array. 
  118. * @since 0.6.0 
  119. * @access public 
  120. * @return void 
  121. */ 
  122. public function do_trail_items() { 
  123. /** If viewing the front page. */ 
  124. if ( is_front_page() ) { 
  125. $this->do_front_page_items(); 
  126. /** If not viewing the front page. */ 
  127. else { 
  128. /** Add the network and site home links. */ 
  129. $this->do_network_home_link(); 
  130. $this->do_site_home_link(); 
  131. /** If viewing the home/blog page. */ 
  132. if ( is_home() ) { 
  133. $this->do_posts_page_items(); 
  134. /** If viewing a single post. */ 
  135. elseif ( is_singular() ) { 
  136. $this->do_singular_items(); 
  137. /** If viewing an archive page. */ 
  138. elseif ( is_archive() ) { 
  139. if ( is_post_type_archive() ) 
  140. $this->do_post_type_archive_items(); 
  141. elseif ( is_category() || is_tag() || is_tax() ) 
  142. $this->do_term_archive_items(); 
  143. elseif ( is_author() ) 
  144. $this->do_user_archive_items(); 
  145. elseif ( get_query_var( 'minute' ) && get_query_var( 'hour' ) ) 
  146. $this->do_minute_hour_archive_items(); 
  147. elseif ( get_query_var( 'minute' ) ) 
  148. $this->do_minute_archive_items(); 
  149. elseif ( get_query_var( 'hour' ) ) 
  150. $this->do_hour_archive_items(); 
  151. elseif ( is_day() ) 
  152. $this->do_day_archive_items(); 
  153. elseif ( get_query_var( 'w' ) ) 
  154. $this->do_week_archive_items(); 
  155. elseif ( is_month() ) 
  156. $this->do_month_archive_items(); 
  157. elseif ( is_year() ) 
  158. $this->do_year_archive_items(); 
  159. else 
  160. $this->do_default_archive_items(); 
  161. /** If viewing a search results page. */ 
  162. elseif ( is_search() ) { 
  163. $this->do_search_items(); 
  164. /** If viewing the 404 page. */ 
  165. elseif ( is_404() ) { 
  166. $this->do_404_items(); 
  167. /** Add paged items if they exist. */ 
  168. $this->do_paged_items(); 
  169. /** Allow developers to overwrite the items for the breadcrumb trail. */ 
  170. $this->items = apply_filters( 'breadcrumb_trail_items', $this->items, $this->args ); 
  171. /** 
  172. * Gets front items based on $wp_rewrite->front. 
  173. * @since 0.6.0 
  174. * @access public 
  175. * @return void 
  176. */ 
  177. public function do_rewrite_front_items() { 
  178. global $wp_rewrite; 
  179. if ( $wp_rewrite->front ) 
  180. $this->do_path_parents( $wp_rewrite->front ); 
  181. /** 
  182. * Adds the page/paged number to the items array. 
  183. * @since 0.6.0 
  184. * @access public 
  185. * @return void 
  186. */ 
  187. public function do_paged_items() { 
  188. /** If viewing a paged singular post. */ 
  189. if ( is_singular() && 1 < get_query_var( 'page' ) && true === $this->args['show_title'] ) 
  190. $this->items[] = sprintf( $this->args['labels']['paged'], number_format_i18n( absint( get_query_var( 'page' ) ) ) ); 
  191. /** If viewing a paged archive-type page. */ 
  192. elseif ( is_paged() && true === $this->args['show_title'] ) 
  193. $this->items[] = sprintf( $this->args['labels']['paged'], number_format_i18n( absint( get_query_var( 'paged' ) ) ) ); 
  194. /** 
  195. * Adds the network (all sites) home page link to the items array. 
  196. * @since 0.6.0 
  197. * @access public 
  198. * @return void 
  199. */ 
  200. public function do_network_home_link() { 
  201. if ( is_multisite() && !is_main_site() && true === $this->args['network'] ) 
  202. $this->items[] = '<a href="' . network_home_url() . '" title="' . esc_attr( $this->args['labels']['home'] ) . '" rel="home">' . $this->args['labels']['home'] . '</a>'; 
  203. /** 
  204. * Adds the current site's home page link to the items array. 
  205. * @since 0.6.0 
  206. * @access public 
  207. * @return void 
  208. */ 
  209. public function do_site_home_link() { 
  210. $label = ( is_multisite() && !is_main_site() && true === $this->args['network'] ) ? get_bloginfo( 'name' ) : $this->args['labels']['home']; 
  211. $rel = ( is_multisite() && !is_main_site() && true === $this->args['network'] ) ? '' : ' rel="home"'; 
  212. $this->items[] = '<a href="' . home_url() . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . '"' . $rel .'>' . $label . '</a>'; 
  213. /** 
  214. * Adds items for the front page to the items array. 
  215. * @since 0.6.0 
  216. * @access public 
  217. * @return void 
  218. */ 
  219. public function do_front_page_items() { 
  220. /** Only show front items if the 'show_on_front' argument is set to 'true'. */ 
  221. if ( true === $this->args['show_on_front'] || is_paged() || ( is_singular() && 1 < get_query_var( 'page' ) ) ) { 
  222. /** If on a paged view, add the home link items. */ 
  223. if ( is_paged() ) { 
  224. $this->do_network_home_link(); 
  225. $this->do_site_home_link(); 
  226. /** If on the main front page, add the network home link item and the home item. */ 
  227. else { 
  228. $this->do_network_home_link(); 
  229. if ( true === $this->args['show_title'] ) 
  230. $this->items[] = ( is_multisite() && true === $this->args['network'] ) ? get_bloginfo( 'name' ) : $this->args['labels']['home']; 
  231. /** 
  232. * Adds items for the posts page (i.e., is_home()) to the items array. 
  233. * @since 0.6.0 
  234. * @access public 
  235. * @return void 
  236. */ 
  237. public function do_posts_page_items() { 
  238. /** Get the post ID and post. */ 
  239. $post_id = get_queried_object_id(); 
  240. $post = get_page( $post_id ); 
  241. /** If the post has parents, add them to the trail. */ 
  242. if ( 0 < $post->post_parent ) 
  243. $this->do_post_parents( $post->post_parent ); 
  244. /** Get the page title. */ 
  245. $title = get_the_title( $post_id ); 
  246. /** Add the posts page item. */ 
  247. if ( is_paged() ) 
  248. $this->items[] = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( $title ) . '">' . $title . '</a>'; 
  249. elseif ( $title && true === $this->args['show_title'] ) 
  250. $this->items[] = $title; 
  251. /** 
  252. * Adds singular post items to the items array. 
  253. * @since 0.6.0 
  254. * @access public 
  255. * @return void 
  256. */ 
  257. public function do_singular_items() { 
  258.  
  259. $show_categories = onetone_option('breadcrumb_show_categories', 'yes'); 
  260. $show_post_type_archive = onetone_option('breadcrumb_show_post_type_archive', 'yes'); 
  261. /** Get the queried post. */ 
  262. $post = get_queried_object(); 
  263. $post_id = get_queried_object_id(); 
  264.  
  265. if( $show_categories == 'yes') { 
  266. /** If the post has a parent, follow the parent trail. */ 
  267. if ( 0 < $post->post_parent ) 
  268. $this->do_post_parents( $post->post_parent ); 
  269. /** If the post doesn't have a parent, get its hierarchy based off the post type. */ 
  270. else 
  271. $this->do_post_hierarchy( $post_id ); 
  272. if( $show_post_type_archive == 'yes') { 
  273. /** Display terms for specific post type taxonomy if requested. */ 
  274. $this->do_post_terms( $post_id ); 
  275. /** End with the post title. */ 
  276. if ( $post_title = single_post_title( '', false ) ) { 
  277. if ( 1 < get_query_var( 'page' ) ) 
  278. $this->items[] = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a>'; 
  279. elseif ( true === $this->args['show_title'] ) 
  280. $this->items[] = $post_title; 
  281. /** 
  282. * Adds a specific post's parents to the items array. 
  283. * @since 0.6.0 
  284. * @access public 
  285. * @param int $post_id The ID of the post to get the parents of. 
  286. * @return void 
  287. */ 
  288. public function do_post_parents( $post_id ) { 
  289. $parents = array(); 
  290. while ( $post_id ) { 
  291. /** Get the post by ID. */ 
  292. $post = get_post( $post_id ); 
  293. /** Add the formatted post link to the array of parents. */ 
  294. $parents[] = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . get_the_title( $post_id ) . '</a>'; 
  295. /** If there's no longer a post parent, brea out of the loop. */ 
  296. if ( 0 >= $post->post_parent ) 
  297. break; 
  298. /** Change the post ID to the parent post to continue looping. */ 
  299. $post_id = $post->post_parent; 
  300. /** Get the post hierarchy based off the final parent post. */ 
  301. $this->do_post_hierarchy( $post_id ); 
  302. /** Merge the parent items into the items array. */ 
  303. $this->items = array_merge( $this->items, array_reverse( $parents ) ); 
  304. /** 
  305. * Adds a post's terms from a specific taxonomy to the items array. 
  306. * @since 0.6.0 
  307. * @access public 
  308. * @param int $post_id The ID of the post to get the terms for. 
  309. * @return void 
  310. */ 
  311. public function do_post_terms( $post_id ) { 
  312. /** Get the post type. */ 
  313. $post_type = get_post_type( $post_id ); 
  314. /** Add the terms of the taxonomy for this post. */ 
  315. if ( !empty( $this->args['post_taxonomy'][ $post_type ] ) ) 
  316. $this->items[] = get_the_term_list( $post_id, $this->args['post_taxonomy'][ $post_type ], '', ', ', '' ); 
  317. /** 
  318. * Adds a specific post's hierarchy to the items array. The hierarchy is determined by post type's 
  319. * rewrite arguments and whether it has an archive page. 
  320. * @since 0.6.0 
  321. * @access public 
  322. * @param int $post_id The ID of the post to get the hierarchy for. 
  323. * @return void 
  324. */ 
  325. public function do_post_hierarchy( $post_id ) { 
  326. /** Get the post type. */ 
  327. $post_type = get_post_type( $post_id ); 
  328. $post_type_object = get_post_type_object( $post_type ); 
  329. /** If this is the 'post' post type, get the rewrite front items and map the rewrite tags. */ 
  330. if ( 'post' === $post_type ) { 
  331. /** Add $wp_rewrite->front to the trail. */ 
  332. $this->do_rewrite_front_items(); 
  333. /** Map the rewrite tags. */ 
  334. $this->map_rewrite_tags( $post_id, get_option( 'permalink_structure' ) ); 
  335. /** If the post type has rewrite rules. */ 
  336. elseif ( false !== $post_type_object->rewrite ) { 
  337. /** If 'with_front' is true, add $wp_rewrite->front to the trail. */ 
  338. if ( $post_type_object->rewrite['with_front'] ) 
  339. $this->do_rewrite_front_items(); 
  340. /** If there's a path, check for parents. */ 
  341. if ( !empty( $post_type_object->rewrite['slug'] ) ) 
  342. $this->do_path_parents( $post_type_object->rewrite['slug'] ); 
  343. /** If there's an archive page, add it to the trail. */ 
  344. if ( !empty( $post_type_object->has_archive ) ) { 
  345. /** Add support for a non-standard label of 'archive_title' (special use case). */ 
  346. $label = !empty( $post_type_object->labels->archive_title ) ? $post_type_object->labels->archive_title : $post_type_object->labels->name; 
  347. $this->items[] = '<a href="' . get_post_type_archive_link( $post_type ) . '">' . $label . '</a>'; 
  348. /** 
  349. * Gets post types by slug. This is needed because the get_post_types() function doesn't exactly 
  350. * match the 'has_archive' argument when it's set as a string instead of a boolean. 
  351. * @since 0.6.0 
  352. * @access public 
  353. * @param int $slug The post type archive slug to search for. 
  354. * @return void 
  355. */ 
  356. public function get_post_types_by_slug( $slug ) { 
  357. $return = array(); 
  358. $post_types = get_post_types( array(), 'objects' ); 
  359. foreach ( $post_types as $type ) { 
  360. if ( $slug === $type->has_archive || ( true === $type->has_archive && $slug === $type->rewrite['slug'] ) ) 
  361. $return[] = $type; 
  362. return $return; 
  363. /** 
  364. * Adds the items to the trail items array for taxonomy term archives. 
  365. * @since 0.6.0 
  366. * @access public 
  367. * @global object $wp_rewrite 
  368. * @return void 
  369. */ 
  370. public function do_term_archive_items() { 
  371. global $wp_rewrite; 
  372. /** Get some taxonomy and term variables. */ 
  373. $term = get_queried_object(); 
  374. $taxonomy = get_taxonomy( $term->taxonomy ); 
  375. /** If there are rewrite rules for the taxonomy. */ 
  376. if ( false !== $taxonomy->rewrite ) { 
  377. /** If 'with_front' is true, dd $wp_rewrite->front to the trail. */ 
  378. if ( $taxonomy->rewrite['with_front'] && $wp_rewrite->front ) 
  379. $this->do_rewrite_front_items(); 
  380. /** Get parent pages by path if they exist. */ 
  381. $this->do_path_parents( $taxonomy->rewrite['slug'] ); 
  382. /** Add post type archive if its 'has_archive' matches the taxonomy rewrite 'slug'. */ 
  383. if ( $taxonomy->rewrite['slug'] ) { 
  384. $slug = trim( $taxonomy->rewrite['slug'], '/' ); 
  385. /** 
  386. * Deals with the situation if the slug has a '/' between multiple strings. For 
  387. * example, "movies/genres" where "movies" is the post type archive. 
  388. */ 
  389. $matches = explode( '/', $slug ); 
  390. /** If matches are found for the path. */ 
  391. if ( isset( $matches ) ) { 
  392. /** Reverse the array of matches to search for posts in the proper order. */ 
  393. $matches = array_reverse( $matches ); 
  394. /** Loop through each of the path matches. */ 
  395. foreach ( $matches as $match ) { 
  396. /** If a match is found. */ 
  397. $slug = $match; 
  398. /** Get public post types that match the rewrite slug. */ 
  399. $post_types = $this->get_post_types_by_slug( $match ); 
  400. if ( !empty( $post_types ) ) { 
  401. $post_type_object = $post_types[0]; 
  402. /** Add support for a non-standard label of 'archive_title' (special use case). */ 
  403. $label = !empty( $post_type_object->labels->archive_title ) ? $post_type_object->labels->archive_title : $post_type_object->labels->name; 
  404. /** Add the post type archive link to the trail. */ 
  405. $this->items[] = '<a href="' . get_post_type_archive_link( $post_type_object->name ) . '" title="' . esc_attr( $label ) . '">' . $label . '</a>'; 
  406. /** Break out of the loop. */ 
  407. break; 
  408. /** If the taxonomy is hierarchical, list its parent terms. */ 
  409. if ( is_taxonomy_hierarchical( $term->taxonomy ) && $term->parent ) 
  410. $this->do_term_parents( $term->parent, $term->taxonomy ); 
  411. /** Add the term name to the trail end. */ 
  412. if ( is_paged() ) 
  413. $this->items[] = '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '" title="' . esc_attr( single_term_title( '', false ) ) . '">' . single_term_title( '', false ) . '</a>'; 
  414. elseif ( true === $this->args['show_title'] ) 
  415. $this->items[] = single_term_title( '', false ); 
  416. /** 
  417. * Adds the items to the trail items array for post type archives. 
  418. * @since 0.6.0 
  419. * @access public 
  420. * @return void 
  421. */ 
  422. public function do_post_type_archive_items() { 
  423. /** Get the post type object. */ 
  424. $post_type_object = get_post_type_object( get_query_var( 'post_type' ) ); 
  425. if ( false !== $post_type_object->rewrite ) { 
  426. /** If 'with_front' is true, add $wp_rewrite->front to the trail. */ 
  427. if ( $post_type_object->rewrite['with_front'] ) 
  428. $this->do_rewrite_front_items(); 
  429. /** If there's a rewrite slug, check for parents. */ 
  430. if ( !empty( $post_type_object->rewrite['slug'] ) ) 
  431. $this->do_path_parents( $post_type_object->rewrite['slug'] ); 
  432. /** Add the post type [plural] name to the trail end. */ 
  433. if ( is_paged() ) 
  434. $this->items[] = '<a href="' . esc_url( get_post_type_archive_link( $post_type_object->name ) ) . '" title="' . esc_attr( post_type_archive_title( '', false ) ) . '">' . post_type_archive_title( '', false ) . '</a>'; 
  435. elseif ( true === $this->args['show_title'] ) { 
  436. if( function_exists('is_shop') && function_exists('woocommerce_page_title') && is_shop()) 
  437. $this->items[] = woocommerce_page_title(false); 
  438. else 
  439. $this->items[] = post_type_archive_title( '', false ); 
  440. /** 
  441. * Adds the items to the trail items array for user (author) archives. 
  442. * @since 0.6.0 
  443. * @access public 
  444. * @global object $wp_rewrite 
  445. * @return void 
  446. */ 
  447. public function do_user_archive_items() { 
  448. global $wp_rewrite; 
  449. /** Add $wp_rewrite->front to the trail. */ 
  450. $this->do_rewrite_front_items(); 
  451. /** Get the user ID. */ 
  452. $user_id = get_query_var( 'author' ); 
  453. /** If $author_base exists, check for parent pages. */ 
  454. if ( !empty( $wp_rewrite->author_base ) ) 
  455. $this->do_path_parents( $wp_rewrite->author_base ); 
  456. /** Add the author's display name to the trail end. */ 
  457. if ( is_paged() ) 
  458. $this->items[] = '<a href="'. esc_url( get_author_posts_url( $user_id ) ) . '" title="' . esc_attr( get_the_author_meta( 'display_name', $user_id ) ) . '">' . get_the_author_meta( 'display_name', $user_id ) . '</a>'; 
  459. elseif ( true === $this->args['show_title'] ) 
  460. $this->items[] = get_the_author_meta( 'display_name', $user_id ); 
  461. /** 
  462. * Adds the items to the trail items array for minute + hour archives. 
  463. * @since 0.6.0 
  464. * @access public 
  465. * @return void 
  466. */ 
  467. public function do_minute_hour_archive_items() { 
  468. /** Add $wp_rewrite->front to the trail. */ 
  469. $this->do_rewrite_front_items(); 
  470. /** Add the minute + hour item. */ 
  471. if ( true === $this->args['show_title'] ) 
  472. $this->items[] = sprintf( $this->args['labels']['archive_minute_hour'], get_the_time( _x( 'g:i a', 'minute and hour archives time format', 'onetone' ) ) ); 
  473. /** 
  474. * Adds the items to the trail items array for minute archives. 
  475. * @since 0.6.0 
  476. * @access public 
  477. * @return void 
  478. */ 
  479. public function do_minute_archive_items() { 
  480. /** Add $wp_rewrite->front to the trail. */ 
  481. $this->do_rewrite_front_items(); 
  482. /** Add the minute item. */ 
  483. if ( true === $this->args['show_title'] ) 
  484. $this->items[] = sprintf( $this->args['labels']['archive_minute'], get_the_time( _x( 'i', 'minute archives time format', 'onetone' ) ) ); 
  485. /** 
  486. * Adds the items to the trail items array for hour archives. 
  487. * @since 0.6.0 
  488. * @access public 
  489. * @return void 
  490. */ 
  491. public function do_hour_archive_items() { 
  492. /** Add $wp_rewrite->front to the trail. */ 
  493. $this->do_rewrite_front_items(); 
  494. /** Add the hour item. */ 
  495. if ( true === $this->args['show_title'] ) 
  496. $this->items[] = sprintf( $this->args['labels']['archive_hour'], get_the_time( _x( 'g a', 'hour archives time format', 'onetone' ) ) ); 
  497. /** 
  498. * Adds the items to the trail items array for day archives. 
  499. * @since 0.6.0 
  500. * @access public 
  501. * @return void 
  502. */ 
  503. public function do_day_archive_items() { 
  504. /** Add $wp_rewrite->front to the trail. */ 
  505. $this->do_rewrite_front_items(); 
  506. /** Get year, month, and day. */ 
  507. $year = sprintf( $this->args['labels']['archive_year'], get_the_time( _x( 'Y', 'yearly archives date format', 'onetone' ) ) ); 
  508. $month = sprintf( $this->args['labels']['archive_month'], get_the_time( _x( 'F', 'monthly archives date format', 'onetone' ) ) ); 
  509. $day = sprintf( $this->args['labels']['archive_day'], get_the_time( _x( 'j', 'daily archives date format', 'onetone' ) ) ); 
  510. /** Add the year and month items. */ 
  511. $this->items[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . esc_attr( $year ) . '">' . $year . '</a>'; 
  512. $this->items[] = '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" title="' . esc_attr( $month ) . '">' . $month . '</a>'; 
  513. /** Add the day item. */ 
  514. if ( is_paged() ) 
  515. $this->items[] = '<a href="' . get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ) . '" title="' . esc_attr( $day ) . '">' . $day . '</a>'; 
  516. elseif ( true === $this->args['show_title'] ) 
  517. $this->items[] = $day; 
  518. /** 
  519. * Adds the items to the trail items array for week archives. 
  520. * @since 0.6.0 
  521. * @access public 
  522. * @return void 
  523. */ 
  524. public function do_week_archive_items() { 
  525. /** Add $wp_rewrite->front to the trail. */ 
  526. $this->do_rewrite_front_items(); 
  527. /** Get the year and week. */ 
  528. $year = sprintf( $this->args['labels']['archive_year'], get_the_time( _x( 'Y', 'yearly archives date format', 'onetone' ) ) ); 
  529. $week = sprintf( $this->args['labels']['archive_week'], get_the_time( _x( 'W', 'weekly archives date format', 'onetone' ) ) ); 
  530. /** Add the year item. */ 
  531. $this->items[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . esc_attr( $year ) . '">' . $year . '</a>'; 
  532. /** Add the week item. */ 
  533. if ( is_paged() ) 
  534. $this->items[] = get_archives_link( add_query_arg( array( 'm' => get_the_time( 'Y' ), 'w' => get_the_time( 'W' ) ), home_url() ), $week, false ); 
  535. elseif ( true === $this->args['show_title'] ) 
  536. $this->items[] = $week; 
  537. /** 
  538. * Adds the items to the trail items array for month archives. 
  539. * @since 0.6.0 
  540. * @access public 
  541. * @return void 
  542. */ 
  543. public function do_month_archive_items() { 
  544. /** Add $wp_rewrite->front to the trail. */ 
  545. $this->do_rewrite_front_items(); 
  546. /** Get the year and month. */ 
  547. $year = sprintf( $this->args['labels']['archive_year'], get_the_time( _x( 'Y', 'yearly archives date format', 'onetone' ) ) ); 
  548. $month = sprintf( $this->args['labels']['archive_month'], get_the_time( _x( 'F', 'monthly archives date format', 'onetone' ) ) ); 
  549. /** Add the year item. */ 
  550. $this->items[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . esc_attr( $year ) . '">' . $year . '</a>'; 
  551. /** Add the month item. */ 
  552. if ( is_paged() ) 
  553. $this->items[] = '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" title="' . esc_attr( $month ) . '">' . $month . '</a>'; 
  554. elseif ( true === $this->args['show_title'] ) 
  555. $this->items[] = $month; 
  556. /** 
  557. * Adds the items to the trail items array for year archives. 
  558. * @since 0.6.0 
  559. * @access public 
  560. * @return void 
  561. */ 
  562. public function do_year_archive_items() { 
  563. /** Add $wp_rewrite->front to the trail. */ 
  564. $this->do_rewrite_front_items(); 
  565. /** Get the year. */ 
  566. $year = sprintf( $this->args['labels']['archive_year'], get_the_time( _x( 'Y', 'yearly archives date format', 'onetone' ) ) ); 
  567. /** Add the year item. */ 
  568. if ( is_paged() ) 
  569. $this->items[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . esc_attr( $year ) . '">' . $year . '</a>'; 
  570. elseif ( true === $this->args['show_title'] ) 
  571. $this->items[] = $year; 
  572. /** 
  573. * Adds the items to the trail items array for archives that don't have a more specific method 
  574. * defined in this class. 
  575. * @since 0.6.0 
  576. * @access public 
  577. * @return void 
  578. */ 
  579. public function do_default_archive_items() { 
  580. /** If this is a date-/time-based archive, add $wp_rewrite->front to the trail. */ 
  581. if ( is_date() || is_time() ) 
  582. $this->do_rewrite_front_items(); 
  583. if ( true === $this->args['show_title'] ) 
  584. $this->items[] = $this->args['labels']['archives']; 
  585. /** 
  586. * Adds the items to the trail items array for search results. 
  587. * @since 0.6.0 
  588. * @access public 
  589. * @return void 
  590. */ 
  591. public function do_search_items() { 
  592. if ( is_paged() ) 
  593. $this->items[] = '<a href="' . get_search_link() . '" title="' . esc_attr( sprintf( $this->args['labels']['search'], get_search_query() ) ) . '">' . sprintf( $this->args['labels']['search'], get_search_query() ) . '</a>'; 
  594. elseif ( true === $this->args['show_title'] ) 
  595. $this->items[] = sprintf( $this->args['labels']['search'], get_search_query() ); 
  596. /** 
  597. * Adds the items to the trail items array for 404 pages. 
  598. * @since 0.6.0 
  599. * @access public 
  600. * @return void 
  601. */ 
  602. public function do_404_items() { 
  603. if ( true === $this->args['show_title'] ) 
  604. $this->items[] = $this->args['labels']['error_404']; 
  605. /** 
  606. * Get parent posts by path. Currently, this method only supports getting parents of the 'page' 
  607. * post type. The goal of this function is to create a clear path back to home given what would 
  608. * normally be a "ghost" directory. If any page matches the given path, it'll be added. 
  609. * @since 0.6.0 
  610. * @access public 
  611. * @param string $path The path (slug) to search for posts by. 
  612. * @return void 
  613. */ 
  614. function do_path_parents( $path ) { 
  615. /** Trim '/' off $path in case we just got a simple '/' instead of a real path. */ 
  616. $path = trim( $path, '/' ); 
  617. /** If there's no path, return. */ 
  618. if ( empty( $path ) ) 
  619. return; 
  620. /** Get parent post by the path. */ 
  621. $post = get_page_by_path( $path ); 
  622. if ( !empty( $post ) ) { 
  623. $this->do_post_parents( $post->ID ); 
  624. elseif ( is_null( $post ) ) { 
  625. /** Separate post names into separate paths by '/'. */ 
  626. $path = trim( $path, '/' ); 
  627. preg_match_all( "/\/.*?\z/", $path, $matches ); 
  628. /** If matches are found for the path. */ 
  629. if ( isset( $matches ) ) { 
  630. /** Reverse the array of matches to search for posts in the proper order. */ 
  631. $matches = array_reverse( $matches ); 
  632. /** Loop through each of the path matches. */ 
  633. foreach ( $matches as $match ) { 
  634. /** If a match is found. */ 
  635. if ( isset( $match[0] ) ) { 
  636. /** Get the parent post by the given path. */ 
  637. $path = str_replace( $match[0], '', $path ); 
  638. $post = get_page_by_path( trim( $path, '/' ) ); 
  639. /** If a parent post is found, set the $post_id and break out of the loop. */ 
  640. if ( !empty( $post ) && 0 < $post->ID ) { 
  641. $this->do_post_parents( $post->ID ); 
  642. break; 
  643. /** 
  644. * Searches for term parents of hierarchical taxonomies. This function is similar to the WordPress 
  645. * function get_category_parents() but handles any type of taxonomy. 
  646. * @since 0.6.0 
  647. * @param int $term_id ID of the term to get the parents of. 
  648. * @param string $taxonomy Name of the taxonomy for the given term. 
  649. * @return void 
  650. */ 
  651. function do_term_parents( $term_id, $taxonomy ) { 
  652. /** Set up some default arrays. */ 
  653. $parents = array(); 
  654. /** While there is a parent ID, add the parent term link to the $parents array. */ 
  655. while ( $term_id ) { 
  656. /** Get the parent term. */ 
  657. $term = get_term( $term_id, $taxonomy ); 
  658. /** Add the formatted term link to the array of parent terms. */ 
  659. $parents[] = '<a href="' . get_term_link( $term, $taxonomy ) . '" title="' . esc_attr( $term->name ) . '">' . $term->name . '</a>'; 
  660. /** Set the parent term's parent as the parent ID. */ 
  661. $term_id = $term->parent; 
  662. /** If we have parent terms, reverse the array to put them in the proper order for the trail. */ 
  663. if ( !empty( $parents ) ) 
  664. $this->items = array_merge( $this->items, $parents ); 
  665. /** 
  666. * Turns %tag% from permalink structures into usable links for the breadcrumb trail. This feels kind of 
  667. * hackish for now because we're checking for specific %tag% examples and only doing it for the 'post' 
  668. * post type. In the future, maybe it'll handle a wider variety of possibilities, especially for custom post 
  669. * types. 
  670. * @since 0.6.0 
  671. * @access public 
  672. * @param int $post_id ID of the post whose parents we want. 
  673. * @param string $path Path of a potential parent page. 
  674. * @param array $args Mixed arguments for the menu. 
  675. * @return array 
  676. */ 
  677. public function map_rewrite_tags( $post_id, $path ) { 
  678. /** Get the post based on the post ID. */ 
  679. $post = get_post( $post_id ); 
  680. /** If no post is returned, an error is returned, or the post does not have a 'post' post type, return. */ 
  681. if ( empty( $post ) || is_wp_error( $post ) || 'post' !== $post->post_type ) 
  682. return $trail; 
  683. /** Trim '/' from both sides of the $path. */ 
  684. $path = trim( $path, '/' ); 
  685. /** Split the $path into an array of strings. */ 
  686. $matches = explode( '/', $path ); 
  687. /** If matches are found for the path. */ 
  688. if ( is_array( $matches ) ) { 
  689. /** Loop through each of the matches, adding each to the $trail array. */ 
  690. foreach ( $matches as $match ) { 
  691. /** Trim any '/' from the $match. */ 
  692. $tag = trim( $match, '/' ); 
  693. /** If using the %year% tag, add a link to the yearly archive. */ 
  694. if ( '%year%' == $tag ) 
  695. $this->items[] = '<a href="' . get_year_link( get_the_time( 'Y', $post_id ) ) . '">' . sprintf( $this->args['labels']['archive_year'], get_the_time( _x( 'Y', 'yearly archives date format', 'onetone' ) ) ) . '</a>'; 
  696. /** If using the %monthnum% tag, add a link to the monthly archive. */ 
  697. elseif ( '%monthnum%' == $tag ) 
  698. $this->items[] = '<a href="' . get_month_link( get_the_time( 'Y', $post_id ), get_the_time( 'm', $post_id ) ) . '">' . sprintf( $this->args['labels']['archive_month'], get_the_time( _x( 'F', 'monthly archives date format', 'onetone' ) ) ) . '</a>'; 
  699. /** If using the %day% tag, add a link to the daily archive. */ 
  700. elseif ( '%day%' == $tag ) 
  701. $this->items[] = '<a href="' . get_day_link( get_the_time( 'Y', $post_id ), get_the_time( 'm', $post_id ), get_the_time( 'd', $post_id ) ) . '">' . sprintf( $this->args['labels']['archive_day'], get_the_time( _x( 'j', 'daily archives date format', 'onetone' ) ) ) . '</a>'; 
  702. /** If using the %author% tag, add a link to the post author archive. */ 
  703. elseif ( '%author%' == $tag ) 
  704. $this->items[] = '<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>'; 
  705. /** If using the %category% tag, add a link to the first category archive to match permalinks. */ 
  706. elseif ( '%category%' == $tag ) { 
  707. /** Force override terms in this post type. */ 
  708. $this->args['post_taxonomy'][ $post->post_type ] = false; 
  709. /** Get the post categories. */ 
  710. $terms = get_the_category( $post_id ); 
  711. /** Check that categories were returned. */ 
  712. if ( $terms ) { 
  713. /** Sort the terms by ID and get the first category. */ 
  714. usort( $terms, '_usort_terms_by_ID' ); 
  715. $term = get_term( $terms[0], 'category' ); 
  716. /** If the category has a parent, add the hierarchy to the trail. */ 
  717. if ( 0 < $term->parent ) 
  718. $this->do_term_parents( $term->parent, 'category' ); 
  719. /** Add the category archive link to the trail. */ 
  720. $this->items[] = '<a href="' . get_term_link( $term, 'category' ) . '" title="' . esc_attr( $term->name ) . '">' . $term->name . '</a>';