1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Post breadcrumb item locator |
4
|
|
|
* |
5
|
|
|
* @package carbon-breadcrumbs |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Post breadcrumb item locator class. |
10
|
|
|
* |
11
|
|
|
* Used to locate the breadcrumb items for post types. |
12
|
|
|
*/ |
13
|
|
|
class Carbon_Breadcrumb_Locator_Post 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
|
|
|
return is_singular() && get_post_type() === $this->get_subtype(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Retrieve the items, found by this locator. |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* |
31
|
|
|
* @param int $priority The priority of the located items. |
32
|
|
|
* @param int $post_id The post ID, used to go up the post type tree. |
33
|
|
|
* @return Carbon_Breadcrumb_Item[] $items The items, found by this locator. |
34
|
|
|
*/ |
35
|
|
|
public function get_items( $priority = 1000, $post_id = 0 ) { |
36
|
|
|
// Get the current post ID, if not specified. |
37
|
|
|
if ( ! $post_id ) { |
38
|
|
|
$post_id = get_the_ID(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// If this is the front page, skip it, as it is added separately. |
42
|
|
|
if ( is_front_page() && get_option( 'page_on_front' ) === $post_id ) { |
43
|
|
|
return array(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Walk the tree of ancestors of the post up to the top. |
47
|
|
|
return $this->get_item_hierarchy( $post_id, $priority ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generate a set of breadcrumb items that found by this locator type and any subtype. |
52
|
|
|
* Will generate all necessary breadcrumb items of all post types. |
53
|
|
|
* |
54
|
|
|
* @access public |
55
|
|
|
* |
56
|
|
|
* @return array $items The items, generated by this locator. |
57
|
|
|
*/ |
58
|
|
|
public function generate_items() { |
59
|
|
|
$post_types = get_post_types( |
60
|
|
|
array( |
61
|
|
|
'public' => true, |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $this->generate_items_for_subtypes( $post_types ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the parent ID of a specific post ID |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* |
73
|
|
|
* @param int $id The ID of the post to retrieve the parent of. |
74
|
|
|
* @return int $parent The parent ID. |
75
|
|
|
*/ |
76
|
|
|
public function get_parent_id( $id ) { |
77
|
|
|
return get_post_field( 'post_parent', $id ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|