|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Breadcrumb trail. |
|
4
|
|
|
* |
|
5
|
|
|
* @package carbon-breadcrumbs |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The main breadcrumb trail class. |
|
10
|
|
|
* |
|
11
|
|
|
* Contains and manages the breadcrumb trail settings and breadcrumb items. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Carbon_Breadcrumb_Trail { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Breadcrumb items. |
|
17
|
|
|
* |
|
18
|
|
|
* @access protected |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $items = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Breadcrumb trail renderer. |
|
25
|
|
|
* |
|
26
|
|
|
* @access protected |
|
27
|
|
|
* @var Carbon_Breadcrumb_Trail_Renderer |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $renderer; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* Creates and configures a new breadcrumb trail with the provided settings. |
|
35
|
|
|
* |
|
36
|
|
|
* @access public |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $settings Configuration options to modify the breadcrumb trail output. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( $settings = array() ) { |
|
41
|
|
|
|
|
42
|
|
|
// Make sure renderer is specified. |
|
43
|
|
|
if ( ! isset( $settings['renderer'] ) ) { |
|
44
|
|
|
$settings['renderer'] = 'Carbon_Breadcrumb_Trail_Renderer'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Determine the renderer class. |
|
48
|
|
|
$renderer_class = apply_filters( 'carbon_breadcrumbs_renderer_class', $settings['renderer'] ); |
|
49
|
|
|
|
|
50
|
|
|
// Build a new renderer. |
|
51
|
|
|
$renderer = new $renderer_class( $settings ); |
|
52
|
|
|
|
|
53
|
|
|
// Set the renderer. |
|
54
|
|
|
$this->set_renderer( $renderer ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Retrieve the renderer object. |
|
59
|
|
|
* |
|
60
|
|
|
* @access public |
|
61
|
|
|
* |
|
62
|
|
|
* @return Carbon_Breadcrumb_Trail_Renderer $renderer The renderer object. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function get_renderer() { |
|
65
|
|
|
return $this->renderer; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Modify the rendering object. |
|
70
|
|
|
* |
|
71
|
|
|
* @access public |
|
72
|
|
|
* |
|
73
|
|
|
* @param Carbon_Breadcrumb_Trail_Renderer $renderer The modified rendering object. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function set_renderer( Carbon_Breadcrumb_Trail_Renderer $renderer ) { |
|
76
|
|
|
$this->renderer = $renderer; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Populate the breadcrumb items for the current context. |
|
81
|
|
|
* |
|
82
|
|
|
* @access public |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setup() { |
|
85
|
|
|
|
|
86
|
|
|
// Start setup. |
|
87
|
|
|
do_action( 'carbon_breadcrumbs_before_setup_trail', $this ); |
|
88
|
|
|
|
|
89
|
|
|
// Perform setup. |
|
90
|
|
|
new Carbon_Breadcrumb_Trail_Setup( $this ); |
|
91
|
|
|
|
|
92
|
|
|
// End setup. |
|
93
|
|
|
do_action( 'carbon_breadcrumbs_after_setup_trail', $this ); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add a single Carbon_Breadcrumb_Item or an array of them to the trail. |
|
99
|
|
|
* |
|
100
|
|
|
* @access public |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $item The item or array of items to add. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function add_item( $item ) { |
|
105
|
|
|
if ( is_array( $item ) ) { |
|
106
|
|
|
foreach ( $item as $single_item ) { |
|
107
|
|
|
$this->add_item( $single_item ); |
|
108
|
|
|
} |
|
109
|
|
|
} else { |
|
110
|
|
|
$priority = $item->get_priority(); |
|
111
|
|
|
$this->items[ $priority ][] = $item; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a custom breadcrumb item to the trail. |
|
117
|
|
|
* |
|
118
|
|
|
* @access public |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $title Breadcrumb item title. |
|
121
|
|
|
* @param string $link Breadcrumb item link. |
|
122
|
|
|
* @param int $priority Breadcrumb item priority. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function add_custom_item( $title, $link = '', $priority = 1000 ) { |
|
125
|
|
|
$custom_item = Carbon_Breadcrumb_Item::factory( 'custom', $priority ); |
|
126
|
|
|
$custom_item->set_title( $title ); |
|
127
|
|
|
$custom_item->set_link( $link ); |
|
128
|
|
|
$custom_item->setup(); |
|
129
|
|
|
$this->add_item( $custom_item ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Remove an item from the breadcrumb trail by both title and link. |
|
134
|
|
|
* |
|
135
|
|
|
* @access public |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $title Title to remove breadcrumb item by. |
|
138
|
|
|
* @param string $link Link URL to remove breadcrumb item by. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function remove_item( $title = '', $link = '' ) { |
|
141
|
|
|
// If both title and link are specified, search for exact match. |
|
142
|
|
|
$all_items = $this->get_items(); |
|
143
|
|
|
foreach ( $all_items as $items_priority => $items ) { |
|
144
|
|
|
foreach ( $items as $item_key => $item ) { |
|
145
|
|
|
if ( 0 === strcasecmp( $item->get_title(), $title ) && 0 === strcasecmp( $item->get_link(), $link ) ) { |
|
146
|
|
|
// If we have a match, remove that item. |
|
147
|
|
|
unset( $all_items[ $items_priority ][ $item_key ] ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// Update the items. |
|
153
|
|
|
$this->set_items( $all_items ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Remove an item from the breadcrumb trail by a specified method. |
|
158
|
|
|
* |
|
159
|
|
|
* @access public |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $method Item method to remove breadcrumb item by. |
|
162
|
|
|
* @param string $data Additional data to pass to the method. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function remove_item_by_method( $method, $data ) { |
|
165
|
|
|
// Search all items for one with the same title. |
|
166
|
|
|
$all_items = $this->get_items(); |
|
167
|
|
|
foreach ( $all_items as $priority => $items ) { |
|
168
|
|
|
foreach ( $items as $item_key => $item ) { |
|
169
|
|
|
$method_result = call_user_func( array( $item, $method ), $data ); |
|
170
|
|
|
if ( 0 === strcasecmp( $method_result, $data ) ) { |
|
171
|
|
|
// If we have a match, remove that item. |
|
172
|
|
|
unset( $all_items[ $priority ][ $item_key ] ); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// Update the items. |
|
178
|
|
|
$this->set_items( $all_items ); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Remove an item from the breadcrumb trail by its title. |
|
183
|
|
|
* |
|
184
|
|
|
* @access public |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $title Title to remove breadcrumb item by. |
|
187
|
|
|
*/ |
|
188
|
|
|
public function remove_item_by_title( $title = '' ) { |
|
189
|
|
|
$this->remove_item_by_method( 'get_title', $title ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Remove an item from the breadcrumb trail by its link. |
|
194
|
|
|
* |
|
195
|
|
|
* @access public |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $link Link URL to remove breadcrumb item by. |
|
198
|
|
|
*/ |
|
199
|
|
|
public function remove_item_by_link( $link = '' ) { |
|
200
|
|
|
$this->remove_item_by_method( 'get_link', $link ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Remove an item from the breadcrumb trail by its priority. |
|
205
|
|
|
* |
|
206
|
|
|
* @access public |
|
207
|
|
|
* |
|
208
|
|
|
* @param int $priority Priority to remove breadcrumb item by. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function remove_item_by_priority( $priority = 0 ) { |
|
211
|
|
|
// Search all items for the same priority. |
|
212
|
|
|
$all_items = $this->get_items(); |
|
213
|
|
|
if ( array_key_exists( $priority, $all_items ) ) { |
|
214
|
|
|
// Remove all items with that priority. |
|
215
|
|
|
unset( $all_items[ $priority ] ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
// Update the items. |
|
219
|
|
|
$this->set_items( $all_items ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Retrieve the breadcrumb items that are currently loaded. |
|
224
|
|
|
* |
|
225
|
|
|
* @access public |
|
226
|
|
|
* |
|
227
|
|
|
* @return array $items The breadcrumb items, contained in the trail. |
|
228
|
|
|
*/ |
|
229
|
|
|
public function get_items() { |
|
230
|
|
|
return $this->items; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Retrieve the breadcrumb items in a flat list. |
|
235
|
|
|
* |
|
236
|
|
|
* @access public |
|
237
|
|
|
* |
|
238
|
|
|
* @return array $flat_items The breadcrumb items, contained in the trail. |
|
239
|
|
|
*/ |
|
240
|
|
|
public function get_flat_items() { |
|
241
|
|
|
$flat_items = array(); |
|
242
|
|
|
|
|
243
|
|
|
foreach ( $this->items as $priority => $items ) { |
|
244
|
|
|
$flat_items = array_merge( $flat_items, $items ); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $flat_items; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Modify the currently loaded breadcrumb items. |
|
252
|
|
|
* |
|
253
|
|
|
* @access public |
|
254
|
|
|
* |
|
255
|
|
|
* @param array $items The new set of breadcrumb items. |
|
256
|
|
|
*/ |
|
257
|
|
|
public function set_items( $items = array() ) { |
|
258
|
|
|
$this->items = $items; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Sort the currently loaded breadcrumb items by their priority. |
|
263
|
|
|
* |
|
264
|
|
|
* @access public |
|
265
|
|
|
*/ |
|
266
|
|
|
public function sort_items() { |
|
267
|
|
|
$items = $this->get_items(); |
|
268
|
|
|
ksort( $items ); |
|
269
|
|
|
$this->set_items( $items ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Retrieve the total number of breadcrumb items in the trail. |
|
274
|
|
|
* |
|
275
|
|
|
* @access public |
|
276
|
|
|
* |
|
277
|
|
|
* @return int $total_items Number of items in the breadcrumb trail. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function get_total_items() { |
|
280
|
|
|
$all_items = $this->get_flat_items(); |
|
281
|
|
|
return count( $all_items ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Render the breadcrumb trail. |
|
286
|
|
|
* |
|
287
|
|
|
* @access public |
|
288
|
|
|
* |
|
289
|
|
|
* @param bool $return Whether to return the output. |
|
290
|
|
|
* @return string|void $output The output HTML if $return is true. |
|
291
|
|
|
*/ |
|
292
|
|
|
public function render( $return = false ) { |
|
293
|
|
|
|
|
294
|
|
|
// Get the rendered output. |
|
295
|
|
|
$output = $this->get_renderer()->render( $this, true ); |
|
296
|
|
|
|
|
297
|
|
|
if ( $return ) { |
|
298
|
|
|
return $output; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
echo wp_kses( $output, wp_kses_allowed_html( 'post' ) ); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Build, setup and display a new breadcrumb trail. |
|
306
|
|
|
* |
|
307
|
|
|
* @static |
|
308
|
|
|
* @access public |
|
309
|
|
|
* |
|
310
|
|
|
* @param array $args Configuration options to modify the breadcrumb trail output. |
|
311
|
|
|
*/ |
|
312
|
|
|
public static function output( $args = array() ) { |
|
313
|
|
|
$trail = new self( $args ); |
|
314
|
|
|
$trail->setup(); |
|
315
|
|
|
$trail->render(); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
} |
|
319
|
|
|
|