Carbon_Breadcrumb_Locator_Term::generate_items()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Term breadcrumb item locator
4
 *
5
 * @package carbon-breadcrumbs
6
 */
7
8
/**
9
 * Taxonomy term breadcrumb item locator class.
10
 *
11
 * Used to locate the breadcrumb items for taxonomy terms.
12
 */
13
class Carbon_Breadcrumb_Locator_Term extends Carbon_Breadcrumb_Locator_Hierarchical {
14
15
	/**
16
	 * Whether this the items of this locator should be included in the trail.
17
	 *
18
	 * @access public
19
	 *
20
	 * @return bool $is_included Whether the found items should be included.
21
	 */
22
	public function is_included() {
23
		$queried_object = get_queried_object();
24
25
		if ( ! empty( $queried_object->taxonomy ) && $queried_object->taxonomy == $this->get_subtype() ) {
26
			return true;
27
		}
28
29
		return false;
30
	}
31
32
	/**
33
	 * Retrieve the items, found by this locator.
34
	 *
35
	 * @access public
36
	 *
37
	 * @param int $priority The priority of the located items.
38
	 * @param int $term_id The term ID, used to go up the taxonomy term tree.
39
	 * @return Carbon_Breadcrumb_Item[] $items The items, found by this locator.
40
	 */
41
	public function get_items( $priority = 1000, $term_id = 0 ) {
42
		// Get the current term ID, if not specified.
43
		if ( ! $term_id ) {
44
			$term_id = get_queried_object_id();
45
		}
46
47
		// Walk the tree of ancestors of the taxonomy term up to the top.
48
		return $this->get_item_hierarchy( $term_id, $priority );
49
	}
50
51
	/**
52
	 * Generate a set of breadcrumb items that found by this locator type and any subtype.
53
	 * Will generate all necessary breadcrumb items of all taxonomies.
54
	 *
55
	 * @access public
56
	 *
57
	 * @return array $items The items, generated by this locator.
58
	 */
59
	public function generate_items() {
60
		$taxonomies = get_taxonomies(
61
			array(
62
				'public' => true,
63
			)
64
		);
65
66
		return $this->generate_items_for_subtypes( $taxonomies );
67
	}
68
69
	/**
70
	 * Get the parent ID of a specific term ID
71
	 *
72
	 * @access public
73
	 *
74
	 * @param int $id The ID of the term to retrieve the parent of.
75
	 * @return int $parent The parent ID.
76
	 */
77
	public function get_parent_id( $id ) {
78
		$term = get_term_by( 'id', $id, $this->get_subtype() );
79
		return $term->parent;
80
	}
81
82
}
83