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