| @@ -11,79 +11,93 @@ | ||
| 11 | 11 | * |
| 12 | 12 | * @package automattic/jetpack |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | -/** | |
| 16 | - * Echos a set of breadcrumbs. | |
| 17 | - * | |
| 18 | - * Themes can call this function where the breadcrumbs should be outputted. | |
| 19 | - */ | |
| 20 | -function jetpack_breadcrumbs() { | |
| 21 | - $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' ); | |
| 22 | - $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy ); | |
| 15 | +if ( ! class_exists( '\Automattic\Jetpack\Classic_Theme_Helper\Main' ) ) { | |
| 23 | 16 | |
| 24 | - $post_type = is_page() ? 'page' : get_query_var( 'post_type' ); | |
| 25 | - $is_post_type_hierarchical = is_post_type_hierarchical( $post_type ); | |
| 17 | + if ( ! function_exists( 'jetpack_breadcrumbs' ) ) { | |
| 18 | + /** | |
| 19 | + * Echos a set of breadcrumbs. | |
| 20 | + * | |
| 21 | + * Themes can call this function where the breadcrumbs should be outputted. | |
| 22 | + * | |
| 23 | + * @phan-suppress PhanRedefineFunction -- Covered by function_exists check. | |
| 24 | + */ | |
| 25 | + function jetpack_breadcrumbs() { | |
| 26 | + _deprecated_function( __FUNCTION__, 'jetpack-13.8' ); | |
| 27 | + $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' ); | |
| 28 | + $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy ); | |
| 26 | 29 | |
| 27 | - if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) { | |
| 28 | - return; | |
| 29 | - } | |
| 30 | + $post_type = is_page() ? 'page' : get_query_var( 'post_type' ); | |
| 31 | + $is_post_type_hierarchical = is_post_type_hierarchical( $post_type ); | |
| 30 | 32 | |
| 31 | - $breadcrumb = ''; | |
| 32 | - $position = 1; | |
| 33 | + if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) { | |
| 34 | + return; | |
| 35 | + } | |
| 33 | 36 | |
| 34 | - if ( $is_post_type_hierarchical ) { | |
| 35 | - $post_id = get_queried_object_id(); | |
| 36 | - $ancestors = array_reverse( get_post_ancestors( $post_id ) ); | |
| 37 | - if ( $ancestors ) { | |
| 38 | - foreach ( $ancestors as $ancestor ) { | |
| 39 | - $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>'; | |
| 40 | - ++$position; | |
| 37 | + $breadcrumb = ''; | |
| 38 | + $position = 1; | |
| 39 | + | |
| 40 | + if ( $is_post_type_hierarchical ) { | |
| 41 | + $post_id = get_queried_object_id(); | |
| 42 | + $ancestors = array_reverse( get_post_ancestors( $post_id ) ); | |
| 43 | + if ( $ancestors ) { | |
| 44 | + foreach ( $ancestors as $ancestor ) { | |
| 45 | + $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>'; | |
| 46 | + ++$position; | |
| 47 | + } | |
| 48 | + } | |
| 49 | + $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>'; | |
| 50 | + } elseif ( $is_taxonomy_hierarchical ) { | |
| 51 | + $current = get_term( get_queried_object_id(), $taxonomy ); | |
| 52 | + | |
| 53 | + if ( is_wp_error( $current ) ) { | |
| 54 | + return; | |
| 55 | + } | |
| 56 | + | |
| 57 | + if ( $current->parent ) { | |
| 58 | + $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta property="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>'; | |
| 41 | 62 | } |
| 42 | - } | |
| 43 | - $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>'; | |
| 44 | - } elseif ( $is_taxonomy_hierarchical ) { | |
| 45 | - $current = get_term( get_queried_object_id(), $taxonomy ); | |
| 46 | 63 | |
| 47 | - if ( is_wp_error( $current ) ) { | |
| 48 | - return; | |
| 49 | - } | |
| 64 | + $home = '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><meta itemprop="position" content="0"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>'; | |
| 50 | 65 | |
| 51 | - if ( $current->parent ) { | |
| 52 | - $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy ); | |
| 66 | + echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 53 | 67 | } |
| 54 | 68 | |
| 55 | - $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta property="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>'; | |
| 56 | 69 | } |
| 57 | 70 | |
| 58 | - $home = '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><meta itemprop="position" content="0"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>'; | |
| 71 | + if ( ! function_exists( 'jetpack_get_term_parents' ) ) { | |
| 72 | + /** | |
| 73 | + * Return the parents for a given taxonomy term ID. | |
| 74 | + * | |
| 75 | + * @phan-suppress PhanRedefineFunction -- Covered by function_exists check. | |
| 76 | + * | |
| 77 | + * @param int $term Taxonomy term whose parents will be returned. | |
| 78 | + * @param string $taxonomy Taxonomy name that the term belongs to. | |
| 79 | + * @param array $visited Terms already added to prevent duplicates. | |
| 80 | + * | |
| 81 | + * @return string A list of links to the term parents. | |
| 82 | + */ | |
| 83 | + function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) { | |
| 84 | + _deprecated_function( __FUNCTION__, 'jetpack-13.8' ); | |
| 85 | + $parent = get_term( $term, $taxonomy ); | |
| 59 | 86 | |
| 60 | - echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 61 | -} | |
| 87 | + if ( is_wp_error( $parent ) ) { | |
| 88 | + return $parent; | |
| 89 | + } | |
| 62 | 90 | |
| 63 | -/** | |
| 64 | - * Return the parents for a given taxonomy term ID. | |
| 65 | - * | |
| 66 | - * @param int $term Taxonomy term whose parents will be returned. | |
| 67 | - * @param string $taxonomy Taxonomy name that the term belongs to. | |
| 68 | - * @param array $visited Terms already added to prevent duplicates. | |
| 69 | - * | |
| 70 | - * @return string A list of links to the term parents. | |
| 71 | - */ | |
| 72 | -function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) { | |
| 73 | - $parent = get_term( $term, $taxonomy ); | |
| 91 | + $chain = ''; | |
| 74 | 92 | |
| 75 | - if ( is_wp_error( $parent ) ) { | |
| 76 | - return $parent; | |
| 77 | - } | |
| 93 | + if ( $parent->parent && ( $parent->parent !== $parent->term_id ) && ! in_array( $parent->parent, $visited, true ) ) { | |
| 94 | + $visited[] = $parent->parent; | |
| 95 | + $chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited ); | |
| 96 | + } | |
| 78 | 97 | |
| 79 | - $chain = ''; | |
| 98 | + $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>'; | |
| 80 | 99 | |
| 81 | - if ( $parent->parent && ( $parent->parent !== $parent->term_id ) && ! in_array( $parent->parent, $visited, true ) ) { | |
| 82 | - $visited[] = $parent->parent; | |
| 83 | - $chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited ); | |
| 100 | + return $chain; | |
| 101 | + } | |
| 84 | 102 | } |
| 85 | - | |
| 86 | - $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>'; | |
| 87 | - | |
| 88 | - return $chain; | |
| 89 | 103 | } |