Completed
Push — fix/space-twitter-4455 ( 859b70...d25e05 )
by Jeremy
20:31 queued 11:02
created

site-breadcrumbs.php ➔ jetpack_breadcrumbs()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 24
nc 28
nop 0
dl 0
loc 40
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Plugin Name: Site Breadcrumbs
4
 * Plugin URI: http://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: http://wordpress.com
9
 * License: GPL2 or later
10
 */
11
12
function jetpack_breadcrumbs() {
13
	$taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' );
14
	$is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy );
15
16
	$post_type = is_page() ? 'page' : get_query_var( 'post_type' );
17
	$is_post_type_hierarchical = is_post_type_hierarchical( $post_type );
18
19
	if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) {
20
		return;
21
	}
22
23
	$breadcrumb = '';
24
25
	if ( $is_post_type_hierarchical ) {
26
		$post_id = get_queried_object_id();
27
		$ancestors = array_reverse( get_post_ancestors( $post_id ) );
28
		if ( $ancestors ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ancestors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
29
			foreach ( $ancestors as $ancestor ) {
30
				$breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>';
31
			}
32
		}
33
		$breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>';
34
	} elseif ( $is_taxonomy_hierarchical ) {
35
		$current = get_term( get_queried_object_id(), $taxonomy );
36
37
		if ( is_wp_error( $current ) ) {
38
			return;
39
		}
40
41
		if ( $current->parent ) {
42
			$breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy );
43
		}
44
45
		$breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>';
46
	}
47
48
	$home = '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>';
49
50
	echo '<nav class="entry-breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>';
51
}
52
53
/**
54
 * Return the parents for a given taxonomy term ID.
55
 *
56
 * @param int $term Taxonomy term whose parents will be returned.
57
 * @param string $taxonomy Taxonomy name that the term belongs to.
58
 * @param array $visited Terms already added to prevent duplicates.
59
 *
60
 * @return string A list of links to the term parents.
61
 */
62
function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) {
63
	$parent = get_term( $term, $taxonomy );
64
65
	if ( is_wp_error( $parent ) ) {
66
		return $parent;
67
	}
68
69
	$chain = '';
70
71
	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) {
72
		$visited[] = $parent->parent;
73
		$chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited );
74
	}
75
76
	$chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
77
78
	return $chain;
79
}
80