1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The main breadcrumb trail setup class. |
4
|
|
|
* |
5
|
|
|
* Generates and populates the default breadcrumb trail items. |
6
|
|
|
*/ |
7
|
|
|
class Carbon_Breadcrumb_Trail_Setup { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Breadcrumb trail. |
11
|
|
|
* |
12
|
|
|
* @access protected |
13
|
|
|
* @var Carbon_Breadcrumb_Trail |
14
|
|
|
*/ |
15
|
|
|
protected $trail; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor. |
19
|
|
|
* |
20
|
|
|
* Hooks all breadcrumb trail item population methods. |
21
|
|
|
* |
22
|
|
|
* @access public |
23
|
|
|
* |
24
|
|
|
* @param Carbon_Breadcrumb_Trail $trail Trail object to populate. |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct( Carbon_Breadcrumb_Trail $trail ) { |
27
|
1 |
|
$this->set_trail( $trail ); |
28
|
|
|
|
29
|
1 |
|
$this->populate_db_object_items(); |
30
|
1 |
|
$this->populate_date_archive_items(); |
31
|
1 |
|
$this->populate_post_type_archive_items(); |
32
|
1 |
|
$this->populate_search_items(); |
33
|
1 |
|
$this->populate_404_items(); |
34
|
1 |
|
$this->populate_category_items(); |
35
|
1 |
|
$this->populate_page_for_posts_items(); |
36
|
1 |
|
$this->populate_home_items(); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Populate DB object items (post types, terms, authors). |
41
|
|
|
* |
42
|
|
|
* @access public |
43
|
|
|
*/ |
44
|
4 |
|
public function populate_db_object_items() { |
45
|
|
|
$locators = array( |
46
|
4 |
|
'post', |
47
|
4 |
|
'term', |
48
|
4 |
|
'user', |
49
|
4 |
|
); |
50
|
|
|
|
51
|
4 |
|
foreach ( $locators as $locator_name ) { |
52
|
4 |
|
$items = $this->generate_locator_items( $locator_name ); |
53
|
4 |
|
$this->get_trail()->add_item( $items ); |
54
|
4 |
|
} |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Populate date archives. |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
*/ |
62
|
4 |
|
public function populate_date_archive_items() { |
63
|
4 |
|
if ( ! is_date() ) { |
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
$locator = Carbon_Breadcrumb_Locator::factory( 'date' ); |
68
|
3 |
|
$items = $locator->get_items( 700 ); |
69
|
3 |
|
$this->get_trail()->add_item( $items ); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Populate post type archives. |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
*/ |
77
|
4 |
|
public function populate_post_type_archive_items() { |
78
|
4 |
|
if ( is_post_type_archive() ) { |
79
|
1 |
|
$post_type = get_post_type_object( get_query_var( 'post_type' ) ); |
80
|
4 |
|
} elseif ( is_singular() ) { |
81
|
2 |
|
$post_type = get_post_type_object( get_post_type() ); |
82
|
2 |
|
if ( ! $post_type->has_archive ) { |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
1 |
|
} else { |
86
|
1 |
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$title = $post_type->labels->name; |
90
|
2 |
|
$link = get_post_type_archive_link( $post_type->name ); |
91
|
2 |
|
$this->get_trail()->add_custom_item( $title, $link, 700 ); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Populate search items. |
97
|
|
|
* |
98
|
|
|
* @access public |
99
|
|
|
*/ |
100
|
2 |
|
public function populate_search_items() { |
101
|
2 |
|
if ( ! is_search() ) { |
102
|
1 |
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$search_title = sprintf( __( 'Search results for: "%1$s"', 'carbon_breadcrumbs' ), get_search_query() ); |
106
|
1 |
|
$this->get_trail()->add_custom_item( $search_title, '', 700 ); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Populate 404 items. |
111
|
|
|
* |
112
|
|
|
* @access public |
113
|
|
|
*/ |
114
|
2 |
|
public function populate_404_items() { |
115
|
2 |
|
if ( ! is_404() ) { |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$not_found_title = __( 'Error 404 - Not Found', 'carbon_breadcrumbs' ); |
120
|
1 |
|
$this->get_trail()->add_custom_item( $not_found_title, '', 700 ); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Populate category hierarchy when on a single post. |
125
|
|
|
* |
126
|
|
|
* @access public |
127
|
|
|
*/ |
128
|
2 |
|
public function populate_category_items() { |
129
|
2 |
|
if ( ! ( is_single() && 'post' == get_post_type() ) ) { |
130
|
1 |
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$taxonomy = 'category'; |
134
|
1 |
|
$categories = wp_get_object_terms( get_the_ID(), $taxonomy, 'orderby=term_id' ); |
135
|
1 |
|
$last_category = array_pop( $categories ); |
136
|
1 |
|
$locator = Carbon_Breadcrumb_Locator::factory( 'term', $taxonomy ); |
137
|
1 |
|
$items = $locator->get_items( 700, $last_category->term_id ); |
138
|
1 |
|
$this->get_trail()->add_item( $items ); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Populate page for posts item where necessary. |
143
|
|
|
* |
144
|
|
|
* @access public |
145
|
|
|
*/ |
146
|
3 |
|
public function populate_page_for_posts_items() { |
147
|
3 |
|
$page_for_posts = get_option( 'page_for_posts' ); |
148
|
3 |
|
if ( ! $page_for_posts ) { |
149
|
1 |
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
2 |
|
if ( $this->is_post_context() ) { |
153
|
1 |
|
$locator = Carbon_Breadcrumb_Locator::factory( 'post', 'page' ); |
154
|
1 |
|
$items = $locator->get_items( 500, $page_for_posts ); |
155
|
1 |
|
$this->get_trail()->add_item( $items ); |
156
|
1 |
|
} |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* True if on a post archive, false otherwise. |
161
|
|
|
* Post archives are: category, tag, date (year, month, day) and author pages. |
162
|
|
|
* |
163
|
|
|
* @access public |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
8 |
|
public function is_post_archive() { |
168
|
8 |
|
return is_category() || is_tag() || is_date() || is_author(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* True if on a post archive, on posts page or on a single post; false otherwise. |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
9 |
|
public function is_post_context() { |
179
|
9 |
|
return is_home() || $this->is_post_archive() || ( is_single() && 'post' == get_post_type() ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Populate home item. |
184
|
|
|
* |
185
|
|
|
* @access public |
186
|
|
|
*/ |
187
|
2 |
|
public function populate_home_items() { |
188
|
2 |
|
$trail = $this->get_trail(); |
189
|
2 |
|
$renderer = $trail->get_renderer(); |
190
|
2 |
|
if ( ! $renderer->get_display_home_item() ) { |
191
|
1 |
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$home_title = $renderer->get_home_item_title(); |
195
|
1 |
|
$home_link = home_url( '/' ); |
196
|
1 |
|
$trail->add_custom_item( $home_title, $home_link, 10 ); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Generate the items of a certain locator. |
201
|
|
|
* |
202
|
|
|
* @access protected |
203
|
|
|
* |
204
|
|
|
* @param string $locator_name Name of the locator. |
205
|
|
|
* @return array $items Items generated by this locator. |
206
|
|
|
*/ |
207
|
2 |
|
protected function generate_locator_items( $locator_name ) { |
208
|
2 |
|
$locator = Carbon_Breadcrumb_Locator::factory( $locator_name ); |
209
|
2 |
|
$items = $locator->generate_items(); |
210
|
|
|
|
211
|
2 |
|
if ( $items ) { |
212
|
1 |
|
return $items; |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
return array(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Retrieve the trail object. |
220
|
|
|
* |
221
|
|
|
* @access public |
222
|
|
|
* |
223
|
|
|
* @return Carbon_Breadcrumb_Trail $trail The trail object. |
224
|
|
|
*/ |
225
|
1 |
|
public function get_trail() { |
226
|
1 |
|
return $this->trail; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Modify the trail object. |
231
|
|
|
* |
232
|
|
|
* @access public |
233
|
|
|
* |
234
|
|
|
* @param Carbon_Breadcrumb_Trail $trail The modified rendering object. |
235
|
|
|
*/ |
236
|
1 |
|
public function set_trail( Carbon_Breadcrumb_Trail $trail ) { |
237
|
1 |
|
$this->trail = $trail; |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
} |