Completed
Push — master ( 435c67...2498e4 )
by Marin
03:09
created

Carbon_Breadcrumb_Trail_Renderer::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

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