1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Breadcrumb trail renderer. |
4
|
|
|
* |
5
|
|
|
* @package carbon-breadcrumbs |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The breadcrumb trail render class. |
10
|
|
|
* |
11
|
|
|
* Responsible for the breadcrumb trail rendering settings and process. |
12
|
|
|
*/ |
13
|
|
|
class Carbon_Breadcrumb_Trail_Renderer { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* String used between the breadcrumb items when displaying the breadcrumbs. |
17
|
|
|
* |
18
|
|
|
* @access protected |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $glue = ' > '; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* String before the opening link tag. |
25
|
|
|
* |
26
|
|
|
* @access protected |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $link_before = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* String after the closing link tag. |
33
|
|
|
* |
34
|
|
|
* @access protected |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $link_after = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* String before all breadcrumb items. |
41
|
|
|
* |
42
|
|
|
* @access protected |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $wrapper_before = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* String after all breadcrumb items. |
49
|
|
|
* |
50
|
|
|
* @access protected |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $wrapper_after = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* String before the title of a breadcrumb item. |
57
|
|
|
* |
58
|
|
|
* @access protected |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $title_before = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* String before the title of a breadcrumb item. |
65
|
|
|
* |
66
|
|
|
* @access protected |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $title_after = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Minimum items necessary to display the breadcrumb trail. |
73
|
|
|
* |
74
|
|
|
* @access protected |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
protected $min_items = 2; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Whether to display the last item as link. |
81
|
|
|
* |
82
|
|
|
* @access protected |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
protected $last_item_link = true; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Whether to display the home item. |
89
|
|
|
* |
90
|
|
|
* @access protected |
91
|
|
|
* @var bool |
92
|
|
|
*/ |
93
|
|
|
protected $display_home_item = true; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The title of the home item. |
97
|
|
|
* |
98
|
|
|
* @access protected |
99
|
|
|
* @var string |
100
|
|
|
*/ |
101
|
|
|
protected $home_item_title = ''; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Constructor. |
105
|
|
|
* |
106
|
|
|
* Creates and configures a new breadcrumb trail with the provided settings. |
107
|
|
|
* |
108
|
|
|
* @access public |
109
|
|
|
* |
110
|
|
|
* @param array $args Configuration options to modify the breadcrumb trail output. |
111
|
|
|
*/ |
112
|
|
|
public function __construct( $args = array() ) { |
113
|
|
|
|
114
|
|
|
// Default configuration options. |
115
|
|
|
$defaults = array( |
116
|
|
|
'home_item_title' => __( 'Home', 'carbon_breadcrumbs' ), |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
// Allow default options to be filtered. |
120
|
|
|
$defaults = apply_filters( 'carbon_breadcrumbs_renderer_default_options', $defaults ); |
121
|
|
|
|
122
|
|
|
// Parse configuration options. |
123
|
|
|
$args = wp_parse_args( $args, $defaults ); |
124
|
|
|
|
125
|
|
|
// Set configuration options. |
126
|
|
|
foreach ( $args as $arg_name => $arg_value ) { |
127
|
|
|
$method = 'set_' . $arg_name; |
128
|
|
|
if ( method_exists( $this, $method ) ) { |
129
|
|
|
call_user_func( array( $this, $method ), $arg_value ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieve the string, used for concatenating the breadcrumb items. |
137
|
|
|
* |
138
|
|
|
* @access public |
139
|
|
|
* |
140
|
|
|
* @return string $glue String, used for concatenating the breadcrumb items. |
141
|
|
|
*/ |
142
|
|
|
public function get_glue() { |
143
|
|
|
return $this->glue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Modify the string, used for concatenating the breadcrumb items. |
148
|
|
|
* |
149
|
|
|
* @access public |
150
|
|
|
* |
151
|
|
|
* @param string $glue String, used for concatenating the breadcrumb items. |
152
|
|
|
*/ |
153
|
|
|
public function set_glue( $glue = '' ) { |
154
|
|
|
$this->glue = $glue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Retrieve the string before the opening link tag of a breadcrumb item. |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* |
162
|
|
|
* @return string $link_before String before the opening link tag of a breadcrumb item. |
163
|
|
|
*/ |
164
|
|
|
public function get_link_before() { |
165
|
|
|
return $this->link_before; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Modify the string before the opening link tag of a breadcrumb item. |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* |
173
|
|
|
* @param string $link_before String before the opening link tag of a breadcrumb item. |
174
|
|
|
*/ |
175
|
|
|
public function set_link_before( $link_before = '' ) { |
176
|
|
|
$this->link_before = $link_before; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Retrieve the string after the closing link tag of a breadcrumb item. |
181
|
|
|
* |
182
|
|
|
* @access public |
183
|
|
|
* |
184
|
|
|
* @return string $link_after String after the closing link tag of a breadcrumb item. |
185
|
|
|
*/ |
186
|
|
|
public function get_link_after() { |
187
|
|
|
return $this->link_after; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Modify the string after the closing link tag of a breadcrumb item. |
192
|
|
|
* |
193
|
|
|
* @access public |
194
|
|
|
* |
195
|
|
|
* @param string $link_after String after the closing link tag of a breadcrumb item. |
196
|
|
|
*/ |
197
|
|
|
public function set_link_after( $link_after = '' ) { |
198
|
|
|
$this->link_after = $link_after; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Retrieve the string before the breadcrumb items. |
203
|
|
|
* |
204
|
|
|
* @access public |
205
|
|
|
* |
206
|
|
|
* @return string $wrapper_before String before the breadcrumb items. |
207
|
|
|
*/ |
208
|
|
|
public function get_wrapper_before() { |
209
|
|
|
return $this->wrapper_before; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Modify the string before the breadcrumb items. |
214
|
|
|
* |
215
|
|
|
* @access public |
216
|
|
|
* |
217
|
|
|
* @param string $wrapper_before String before the breadcrumb items. |
218
|
|
|
*/ |
219
|
|
|
public function set_wrapper_before( $wrapper_before = '' ) { |
220
|
|
|
$this->wrapper_before = $wrapper_before; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Retrieve the string after the breadcrumb items. |
225
|
|
|
* |
226
|
|
|
* @access public |
227
|
|
|
* |
228
|
|
|
* @return string $wrapper_after String after the breadcrumb items. |
229
|
|
|
*/ |
230
|
|
|
public function get_wrapper_after() { |
231
|
|
|
return $this->wrapper_after; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Modify the string after the breadcrumb items. |
236
|
|
|
* |
237
|
|
|
* @access public |
238
|
|
|
* |
239
|
|
|
* @param string $wrapper_after String after the breadcrumb items. |
240
|
|
|
*/ |
241
|
|
|
public function set_wrapper_after( $wrapper_after = '' ) { |
242
|
|
|
$this->wrapper_after = $wrapper_after; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Retrieve the string before the title of a breadcrumb item. |
247
|
|
|
* |
248
|
|
|
* @access public |
249
|
|
|
* |
250
|
|
|
* @return string $title_before String before the title of a breadcrumb item. |
251
|
|
|
*/ |
252
|
|
|
public function get_title_before() { |
253
|
|
|
return $this->title_before; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Modify the string before the title of a breadcrumb item. |
258
|
|
|
* |
259
|
|
|
* @access public |
260
|
|
|
* |
261
|
|
|
* @param string $title_before String before the title of a breadcrumb item. |
262
|
|
|
*/ |
263
|
|
|
public function set_title_before( $title_before = '' ) { |
264
|
|
|
$this->title_before = $title_before; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Retrieve the string after the title of a breadcrumb item. |
269
|
|
|
* |
270
|
|
|
* @access public |
271
|
|
|
* |
272
|
|
|
* @return string $title_after String after the title of a breadcrumb item. |
273
|
|
|
*/ |
274
|
|
|
public function get_title_after() { |
275
|
|
|
return $this->title_after; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Modify the string after the title of a breadcrumb item. |
280
|
|
|
* |
281
|
|
|
* @access public |
282
|
|
|
* |
283
|
|
|
* @param string $title_after String after the title of a breadcrumb item. |
284
|
|
|
*/ |
285
|
|
|
public function set_title_after( $title_after = '' ) { |
286
|
|
|
$this->title_after = $title_after; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Retrieve the minimum number of items, necessary to display the trail. |
291
|
|
|
* |
292
|
|
|
* @access public |
293
|
|
|
* |
294
|
|
|
* @return int $min_items Minimum number of items, necessary to display the trail |
295
|
|
|
*/ |
296
|
|
|
public function get_min_items() { |
297
|
|
|
return $this->min_items; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Modify the minimum number of items, necessary to display the trail. |
302
|
|
|
* |
303
|
|
|
* @access public |
304
|
|
|
* |
305
|
|
|
* @param int $min_items Minimum number of items, necessary to display the trail. |
306
|
|
|
*/ |
307
|
|
|
public function set_min_items( $min_items ) { |
308
|
|
|
$this->min_items = $min_items; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Whether the last item will be displayed as a link. |
313
|
|
|
* |
314
|
|
|
* @access public |
315
|
|
|
* |
316
|
|
|
* @return bool $last_item_link Whether the last item will be displayed as a link. |
317
|
|
|
*/ |
318
|
|
|
public function get_last_item_link() { |
319
|
|
|
return (bool) $this->last_item_link; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Change whether the last item will be displayed as a link. |
324
|
|
|
* |
325
|
|
|
* @access public |
326
|
|
|
* |
327
|
|
|
* @param bool $last_item_link Whether the last item will be displayed as a link. |
328
|
|
|
*/ |
329
|
|
|
public function set_last_item_link( $last_item_link ) { |
330
|
|
|
$this->last_item_link = (bool) $last_item_link; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Whether the home item will be displayed. |
335
|
|
|
* |
336
|
|
|
* @access public |
337
|
|
|
* |
338
|
|
|
* @return bool $display_home_item Whether the home item will be displayed. |
339
|
|
|
*/ |
340
|
|
|
public function get_display_home_item() { |
341
|
|
|
return (bool) $this->display_home_item; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Change whether the home item will be displayed. |
346
|
|
|
* |
347
|
|
|
* @access public |
348
|
|
|
* |
349
|
|
|
* @param bool $display_home_item Whether the home item will be displayed. |
350
|
|
|
*/ |
351
|
|
|
public function set_display_home_item( $display_home_item ) { |
352
|
|
|
$this->display_home_item = (bool) $display_home_item; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Retrieve the title of the home item. |
357
|
|
|
* |
358
|
|
|
* @access public |
359
|
|
|
* |
360
|
|
|
* @return string $home_item_title The title of the home item. |
361
|
|
|
*/ |
362
|
|
|
public function get_home_item_title() { |
363
|
|
|
return $this->home_item_title; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Modify the title of the home item. |
368
|
|
|
* |
369
|
|
|
* @access public |
370
|
|
|
* |
371
|
|
|
* @param string $home_item_title The title of the home item. |
372
|
|
|
*/ |
373
|
|
|
public function set_home_item_title( $home_item_title = '' ) { |
374
|
|
|
$this->home_item_title = $home_item_title; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Prepare for rendering. |
379
|
|
|
* Allows renderer to be modified in the last second. |
380
|
|
|
* Also autosorts the trail items, if autosorting is enabled. |
381
|
|
|
* |
382
|
|
|
* @access public |
383
|
|
|
* |
384
|
|
|
* @param Carbon_Breadcrumb_Trail $trail Trail object. |
385
|
|
|
*/ |
386
|
|
|
public function prepare_for_rendering( $trail ) { |
387
|
|
|
// Last chance to modify render settings before rendering. |
388
|
|
|
do_action( 'carbon_breadcrumbs_before_render', $this ); |
389
|
|
|
|
390
|
|
|
// Whether to auto-sort the items. |
391
|
|
|
$auto_sort = apply_filters( 'carbon_breadcrumbs_auto_sort_items', true ); |
392
|
|
|
if ( $auto_sort ) { |
393
|
|
|
$trail->sort_items(); |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Render the given breadcrumb trail. |
399
|
|
|
* |
400
|
|
|
* @access public |
401
|
|
|
* |
402
|
|
|
* @param Carbon_Breadcrumb_Trail $trail The trail object. |
403
|
|
|
* @param bool $return Whether to return the output. |
404
|
|
|
* @return string|void $output The output HTML if $return is true. |
405
|
|
|
*/ |
406
|
|
|
public function render( Carbon_Breadcrumb_Trail $trail, $return = false ) { |
407
|
|
|
// If the items are less than the minimum, nothing should be rendered. |
408
|
|
|
if ( $trail->get_total_items() < $this->get_min_items() ) { |
409
|
|
|
return; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// Prepare the trail & renderer for rendering. |
413
|
|
|
$this->prepare_for_rendering( $trail ); |
414
|
|
|
|
415
|
|
|
// Render the items. |
416
|
|
|
$items_output = $this->render_items( $trail ); |
417
|
|
|
|
418
|
|
|
// Implode the breadcrumb items and wrap them with the configured wrappers. |
419
|
|
|
$output = $this->get_wrapper_before(); |
420
|
|
|
$output .= implode( $this->get_glue(), $items_output ); |
421
|
|
|
$output .= $this->get_wrapper_after(); |
422
|
|
|
|
423
|
|
|
if ( $return ) { |
424
|
|
|
return $output; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
echo wp_kses( $output, wp_kses_allowed_html( 'post' ) ); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Render the breadcrumb trail items. |
432
|
|
|
* |
433
|
|
|
* @access public |
434
|
|
|
* |
435
|
|
|
* @param Carbon_Breadcrumb_Trail $trail The trail object. |
436
|
|
|
* @return array $output The output elements. |
437
|
|
|
*/ |
438
|
|
|
public function render_items( $trail ) { |
439
|
|
|
$items_output = array(); |
440
|
|
|
$counter = 0; |
441
|
|
|
$all_items = $trail->get_flat_items(); |
442
|
|
|
|
443
|
|
|
foreach ( $all_items as $item ) { |
444
|
|
|
// Allow each item to be filtered right before rendering. |
445
|
|
|
$item = apply_filters( 'carbon_breadcrumbs_item', $item, $trail, $this, $counter ); |
446
|
|
|
|
447
|
|
|
// Skip if $item is not a Carbon_Breadcrumb_Item instance. |
448
|
|
|
if ( ! ( $item instanceof Carbon_Breadcrumb_Item ) ) { |
449
|
|
|
continue; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// Increase the counter and render the item. |
453
|
|
|
$counter++; |
454
|
|
|
$item_renderer = new Carbon_Breadcrumb_Item_Renderer( $item, $trail, $this, $counter ); |
455
|
|
|
$items_output[] = $item_renderer->render(); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
return $items_output; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
} |
462
|
|
|
|