Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
jetpack_breadcrumbs
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
156
jetpack_get_term_parents
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Plugin Name: Site Breadcrumbs
4 * Plugin URI: https://wordpress.com
5 * Description: Quickly add breadcrumbs to the single view of a hierarchical post type or a hierarchical taxonomy.
6 * Author: Automattic
7 * Version: 1.0
8 * Author URI: https://wordpress.com
9 * License: GPL2 or later
10 * Text Domain: jetpack
11 *
12 * @package automattic/jetpack
13 */
14
15if ( ! class_exists( '\Automattic\Jetpack\Classic_Theme_Helper\Main' ) ) {
16
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 );
29
30            $post_type                 = is_page() ? 'page' : get_query_var( 'post_type' );
31            $is_post_type_hierarchical = is_post_type_hierarchical( $post_type );
32
33            if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) {
34                return;
35            }
36
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>';
62            }
63
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>';
65
66            echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
67        }
68
69    }
70
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 );
86
87            if ( is_wp_error( $parent ) ) {
88                return $parent;
89            }
90
91            $chain = '';
92
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            }
97
98            $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
99
100            return $chain;
101        }
102    }
103}