Guest User

Untitled

a guest
Jun 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. <?php
  2.  
  3. class WP_Breadcrumbs {
  4.  
  5. // Declare variables
  6. private $home_link;
  7. private $home_text;
  8. private $link_before;
  9. private $link_after;
  10. private $link_attr;
  11. private $link;
  12. private $delimiter;
  13. private $before;
  14. private $after;
  15. private $page_addon;
  16. private $breadcrumb_trail;
  17. private $category_links;
  18.  
  19. function __construct()
  20. {
  21. // Set variables
  22. $this->home_link = site_url('/');
  23. $this->home_text = __( 'Home' );
  24. $this->link_before = '<span typeof="v:Breadcrumb">';
  25. $this->link_after = '</span>';
  26. $this->link_attr = ' rel="v:url" property="v:title"';
  27. $this->link = $this->link_before . '<a' . $this->link_attr . ' href="%1$s">%2$s</a>' . $this->link_after;
  28. $this->delimiter = ' <span class="divider">></span> '; // Delimiter between crumbs
  29. $this->before = '<span class="current">'; // Tag before the current crumb
  30. $this->after = '</span>'; // Tag after the current crumb
  31. $this->page_addon = ''; // Adds the page number if the query is paged
  32. $this->breadcrumb_trail = '';
  33. $this->category_links = '';
  34. } // __construct()
  35.  
  36. public function generate()
  37. {
  38. /**
  39. * Set our own $wp_the_query variable. Do not use the global variable version due to
  40. * reliability
  41. */
  42. $wp_the_query = $GLOBALS['wp_the_query'];
  43. $queried_object = $wp_the_query->get_queried_object();
  44.  
  45. // Handle single post requests which includes single pages, posts and attatchments
  46. if ( is_singular() )
  47. {
  48. /**
  49. * Set our own $post variable. Do not use the global variable version due to
  50. * reliability. We will set $post_object variable to $GLOBALS['wp_the_query']
  51. */
  52. $post_object = sanitize_post( $queried_object );
  53.  
  54. // Set variables
  55. $title = apply_filters( 'the_title', $post_object->post_title );
  56. $parent = $post_object->post_parent;
  57. $post_type = $post_object->post_type;
  58. $post_id = $post_object->ID;
  59. $post_link = $this->before . $title . $this->after;
  60. $parent_string = '';
  61. $post_type_link = '';
  62.  
  63. if ( 'post' === $post_type )
  64. {
  65. // Get the post categories
  66. $categories = get_the_category( $post_id );
  67. if ( $categories ) {
  68. // Lets grab the first category
  69. $category = $categories[0];
  70.  
  71. $this->category_links = get_category_parents( $category, true, $this->delimiter );
  72. $this->category_links = str_replace( '<a', $this->link_before . '<a' . $this->link_attr, $this->category_links );
  73. $this->category_links = str_replace( '</a>', '</a>' . $this->link_after, $this->category_links );
  74. }
  75. }
  76.  
  77. if ( !in_array( $post_type, ['post', 'page', 'attachment'] ) )
  78. {
  79. $post_type_object = get_post_type_object( $post_type );
  80. $archive_link = esc_url( get_post_type_archive_link( $post_type ) );
  81.  
  82. $post_type_link = sprintf( $this->link, $archive_link, $post_type_object->labels->singular_name );
  83. }
  84.  
  85. // Get post parents if $parent !== 0
  86. if ( 0 !== $parent )
  87. {
  88. $parent_links = [];
  89. while ( $parent ) {
  90. $post_parent = get_post( $parent );
  91.  
  92. $parent_links[] = sprintf( $this->link, esc_url( get_permalink( $post_parent->ID ) ), get_the_title( $post_parent->ID ) );
  93.  
  94. $parent = $post_parent->post_parent;
  95. }
  96.  
  97. $parent_links = array_reverse( $parent_links );
  98.  
  99. $parent_string = implode( $this->delimiter, $parent_links );
  100. }
  101.  
  102. // Lets build the breadcrumb trail
  103. if ( $parent_string ) {
  104. $this->breadcrumb_trail = $parent_string . $this->delimiter . $post_link;
  105. } else {
  106. $this->breadcrumb_trail = $post_link;
  107. }
  108.  
  109. if ( $post_type_link )
  110. $this->breadcrumb_trail = $post_type_link . $this->delimiter . $this->breadcrumb_trail;
  111.  
  112. if ( $this->category_links )
  113. $this->breadcrumb_trail = $this->category_links . $this->breadcrumb_trail;
  114. }
  115.  
  116. // Handle archives which includes category-, tag-, taxonomy-, date-, custom post type archives and author archives
  117. if( is_archive() )
  118. {
  119. if ( is_category()
  120. || is_tag()
  121. || is_tax()
  122. ) {
  123. // Set the variables for this section
  124. $term_object = get_term( $queried_object );
  125. $taxonomy = $term_object->taxonomy;
  126. $term_id = $term_object->term_id;
  127. $term_name = $term_object->name;
  128. $term_parent = $term_object->parent;
  129. $taxonomy_object = get_taxonomy( $taxonomy );
  130. $current_term_link = $this->before . $taxonomy_object->labels->singular_name . ': ' . $term_name . $this->after;
  131. $parent_term_string = '';
  132.  
  133. if ( 0 !== $term_parent )
  134. {
  135. // Get all the current term ancestors
  136. $parent_term_links = [];
  137. while ( $term_parent ) {
  138. $term = get_term( $term_parent, $taxonomy );
  139.  
  140. $parent_term_links[] = sprintf( $this->link, esc_url( get_term_link( $term ) ), $term->name );
  141.  
  142. $term_parent = $term->parent;
  143. }
  144.  
  145. $parent_term_links = array_reverse( $parent_term_links );
  146. $parent_term_string = implode( $this->delimiter, $parent_term_links );
  147. }
  148.  
  149. if ( $parent_term_string ) {
  150. $this->breadcrumb_trail = $parent_term_string . $this->delimiter . $current_term_link;
  151. } else {
  152. $this->breadcrumb_trail = $current_term_link;
  153. }
  154.  
  155. } elseif ( is_author() ) {
  156.  
  157. $this->breadcrumb_trail = __( 'Author archive for ') . $this->before . $queried_object->data->display_name . $this->after;
  158.  
  159. } elseif ( is_date() ) {
  160. // Set default variables
  161. $year = $wp_the_query->query_vars['year'];
  162. $monthnum = $wp_the_query->query_vars['monthnum'];
  163. $day = $wp_the_query->query_vars['day'];
  164.  
  165. // Get the month name if $monthnum has a value
  166. if ( $monthnum ) {
  167. $date_time = DateTime::createFromFormat( '!m', $monthnum );
  168. $month_name = $date_time->format( 'F' );
  169. }
  170.  
  171. if ( is_year() ) {
  172.  
  173. $this->breadcrumb_trail = $this->before . $year . $this->after;
  174.  
  175. } elseif( is_month() ) {
  176.  
  177. $year_link = sprintf( $this->link, esc_url( get_year_link( $year ) ), $year );
  178.  
  179. $this->breadcrumb_trail = $year_link . $this->delimiter . $this->before . $month_name . $this->after;
  180.  
  181. } elseif( is_day() ) {
  182.  
  183. $year_link = sprintf( $this->link, esc_url( get_year_link( $year ) ), $year );
  184. $month_link = sprintf( $this->link, esc_url( get_month_link( $year, $monthnum ) ), $month_name );
  185.  
  186. $this->breadcrumb_trail = $year_link . $this->delimiter . $month_link . $this->delimiter . $this->before . $day . $this->after;
  187. }
  188.  
  189. } elseif ( is_post_type_archive() ) {
  190.  
  191. $post_type = $wp_the_query->query_vars['post_type'];
  192. $post_type_object = get_post_type_object( $post_type );
  193.  
  194. $this->breadcrumb_trail = $this->before . $post_type_object->labels->singular_name . $this->after;
  195.  
  196. }
  197. }
  198.  
  199. // Handle the search page
  200. if ( is_search() ) {
  201. $this->breadcrumb_trail = __( 'Search query for: ' ) . $this->before . get_search_query() . $this->after;
  202. }
  203.  
  204. // Handle 404's
  205. if ( is_404() ) {
  206. $this->breadcrumb_trail = $this->before . __( 'Error 404' ) . $this->after;
  207. }
  208.  
  209. // Handle paged pages
  210. if ( is_paged() ) {
  211. $current_page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  212. $this->page_addon = $this->before . sprintf( __( ' ( Page %s )' ), number_format_i18n( $current_page ) ) . $this->after;
  213. }
  214.  
  215. $breadcrumb_output_link = '';
  216. $breadcrumb_output_link .= '<div class="awc-breadcrumbs">';
  217. if ( is_home()
  218. || is_front_page()
  219. ) {
  220. // Do not show breadcrumbs on page one of home and frontpage
  221. if ( is_paged() ) {
  222. $breadcrumb_output_link .= '<a href="' . $this->home_link . '">' . $this->home_text . '</a>';
  223. $breadcrumb_output_link .= $this->page_addon;
  224. }
  225. } else {
  226. $breadcrumb_output_link .= '<a href="' . $this->home_link . '" rel="v:url" property="v:title">' . $this->home_text . '</a>';
  227. $breadcrumb_output_link .= $this->delimiter;
  228. $breadcrumb_output_link .= $this->breadcrumb_trail;
  229. $breadcrumb_output_link .= $this->page_addon;
  230. }
  231. $breadcrumb_output_link .= '</div><!-- .breadcrumbs -->';
  232.  
  233. return $breadcrumb_output_link;
  234. }
  235.  
  236. } // end WP_Breadcrumbs
Add Comment
Please, Sign In to add comment