|
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
|
|
|
public function __construct( Carbon_Breadcrumb_Trail $trail ) { |
|
27
|
|
|
$this->set_trail( $trail ); |
|
28
|
|
|
|
|
29
|
|
|
$this->populate_db_object_items(); |
|
30
|
|
|
$this->populate_date_archive_items(); |
|
31
|
|
|
$this->populate_post_type_archive_items(); |
|
32
|
|
|
$this->populate_search_items(); |
|
33
|
|
|
$this->populate_404_items(); |
|
34
|
|
|
$this->populate_category_items(); |
|
35
|
|
|
$this->populate_page_for_posts_items(); |
|
36
|
|
|
$this->populate_home_items(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Populate DB object items (post types, terms, authors). |
|
41
|
|
|
* |
|
42
|
|
|
* @access public |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function populate_db_object_items() { |
|
45
|
|
|
$locators = array( |
|
46
|
|
|
'post', |
|
47
|
|
|
'term', |
|
48
|
|
|
'user', |
|
49
|
1 |
|
); |
|
50
|
|
|
|
|
51
|
|
|
foreach ( $locators as $locator_name ) { |
|
52
|
|
|
$items = $this->generate_locator_items( $locator_name ); |
|
53
|
|
|
$this->get_trail()->add_item( $items ); |
|
54
|
|
|
} |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Populate date archives. |
|
59
|
|
|
* |
|
60
|
|
|
* @access public |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function populate_date_archive_items() { |
|
63
|
|
|
if ( ! is_date() ) { |
|
64
|
1 |
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$locator = Carbon_Breadcrumb_Locator::factory( 'date' ); |
|
68
|
|
|
$items = $locator->get_items( 700 ); |
|
69
|
|
|
$this->get_trail()->add_item( $items ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Populate post type archives. |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function populate_post_type_archive_items() { |
|
78
|
|
|
if ( is_post_type_archive() ) { |
|
79
|
|
|
$post_type = get_post_type_object( get_query_var( 'post_type' ) ); |
|
80
|
|
|
} elseif ( is_singular() ) { |
|
81
|
|
|
$post_type = get_post_type_object( get_post_type() ); |
|
82
|
|
|
if ( ! $post_type->has_archive ) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
|
|
return; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$title = $post_type->labels->name; |
|
90
|
|
|
$link = get_post_type_archive_link( $post_type->name ); |
|
91
|
|
|
$this->get_trail()->add_custom_item( $title, $link, 700 ); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Populate search items. |
|
97
|
|
|
* |
|
98
|
|
|
* @access public |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function populate_search_items() { |
|
101
|
|
|
if ( ! is_search() ) { |
|
102
|
1 |
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$search_title = sprintf( __( 'Search results for: "%1$s"', 'carbon_breadcrumbs' ), get_search_query() ); |
|
106
|
|
|
$this->get_trail()->add_custom_item( $search_title, '', 700 ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Populate 404 items. |
|
111
|
|
|
* |
|
112
|
|
|
* @access public |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function populate_404_items() { |
|
115
|
|
|
if ( ! is_404() ) { |
|
116
|
1 |
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$not_found_title = __( 'Error 404 - Not Found', 'carbon_breadcrumbs' ); |
|
120
|
|
|
$this->get_trail()->add_custom_item( $not_found_title, '', 700 ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Populate category hierarchy when on a single post. |
|
125
|
|
|
* |
|
126
|
|
|
* @access public |
|
127
|
|
|
*/ |
|
128
|
|
|
public function populate_category_items() { |
|
129
|
|
|
if ( ! ( is_single() && 'post' == get_post_type() ) ) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$taxonomy = 'category'; |
|
134
|
|
|
$categories = wp_get_object_terms( get_the_ID(), $taxonomy, array( 'orderby' => 'term_id' ) ); |
|
135
|
|
|
$last_category = array_pop( $categories ); |
|
136
|
|
|
$locator = Carbon_Breadcrumb_Locator::factory( 'term', $taxonomy ); |
|
137
|
|
|
$items = $locator->get_items( 700, $last_category->term_id ); |
|
138
|
|
|
$this->get_trail()->add_item( $items ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Populate page for posts item where necessary. |
|
143
|
|
|
* |
|
144
|
|
|
* @access public |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function populate_page_for_posts_items() { |
|
147
|
|
|
$page_for_posts = get_option( 'page_for_posts' ); |
|
148
|
1 |
|
if ( ! $page_for_posts ) { |
|
149
|
|
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ( $this->is_post_context() ) { |
|
153
|
|
|
$locator = Carbon_Breadcrumb_Locator::factory( 'post', 'page' ); |
|
154
|
|
|
$items = $locator->get_items( 500, $page_for_posts ); |
|
155
|
|
|
$this->get_trail()->add_item( $items ); |
|
156
|
|
|
} |
|
157
|
1 |
|
} |
|
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
|
|
|
public function is_post_archive() { |
|
168
|
|
|
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
|
|
|
public function is_post_context() { |
|
179
|
|
|
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
|
1 |
|
public function populate_home_items() { |
|
188
|
|
|
$trail = $this->get_trail(); |
|
189
|
|
|
$renderer = $trail->get_renderer(); |
|
190
|
|
|
if ( ! $renderer->get_display_home_item() ) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$home_title = $renderer->get_home_item_title(); |
|
195
|
|
|
$home_link = home_url( '/' ); |
|
196
|
|
|
$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
|
1 |
|
protected function generate_locator_items( $locator_name ) { |
|
208
|
|
|
$locator = Carbon_Breadcrumb_Locator::factory( $locator_name ); |
|
209
|
|
|
$items = $locator->generate_items(); |
|
210
|
|
|
|
|
211
|
1 |
|
if ( $items ) { |
|
212
|
1 |
|
return $items; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
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
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |