Carbon_Breadcrumb_Item_Renderer::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Breadcrumb item renderer.
4
 *
5
 * @package carbon-breadcrumbs
6
 */
7
8
/**
9
 * Breadcrumb item renderer class.
10
 *
11
 * Used to render a particular item of the breadcrumb trail.
12
 */
13
class Carbon_Breadcrumb_Item_Renderer {
14
15
	/**
16
	 * The item to render.
17
	 *
18
	 * @access protected
19
	 * @var Carbon_Breadcrumb_Item
20
	 */
21
	protected $item;
22
23
	/**
24
	 * The trail object this item belongs to.
25
	 *
26
	 * @access protected
27
	 * @var Carbon_Breadcrumb_Trail
28
	 */
29
	protected $trail;
30
31
	/**
32
	 * The trail renderer.
33
	 *
34
	 * @access protected
35
	 * @var Carbon_Breadcrumb_Trail_Renderer
36
	 */
37
	protected $trail_renderer;
38
39
	/**
40
	 * Index of this item among the rest of the items, zero-based.
41
	 *
42
	 * @access protected
43
	 * @var int
44
	 */
45
	protected $index = 0;
46
47
	/**
48
	 * Constructor.
49
	 *
50
	 * Creates and configures a new breadcrumb item renderer.
51
	 *
52
	 * @access public
53
	 *
54
	 * @param Carbon_Breadcrumb_Item           $item The item to render.
55
	 * @param Carbon_Breadcrumb_Trail          $trail The trail this item belongs to.
56
	 * @param Carbon_Breadcrumb_Trail_Renderer $trail_renderer The trail renderer.
57
	 * @param int                              $index Index of this item.
58
	 */
59
	public function __construct( Carbon_Breadcrumb_Item $item, Carbon_Breadcrumb_Trail $trail, Carbon_Breadcrumb_Trail_Renderer $trail_renderer, $index = 0 ) {
60
		$this->set_item( $item );
61
		$this->set_trail( $trail );
62
		$this->set_trail_renderer( $trail_renderer );
63
		$this->set_index( $index );
64
	}
65
66
	/**
67
	 * Render the item.
68
	 *
69
	 * @access public
70
	 *
71
	 * @return string $item_output The HTML of this item.
72
	 */
73
	public function render() {
74
		$item_output    = '';
75
		$item           = $this->get_item();
76
		$index          = $this->get_index();
77
		$trail          = $this->get_trail();
78
		$trail_renderer = $this->get_trail_renderer();
79
80
		// Link before and opening <a>.
81
		$item_output .= $this->render_link_before();
82
83
		// Title along with its wrappers.
84
		$item_output .= $this->render_title();
85
86
		// Closing </a> and link after.
87
		$item_output .= $this->render_link_after();
88
89
		// Allow item output to be filtered.
90
		return apply_filters( 'carbon_breadcrumbs_item_output', $item_output, $item, $trail, $trail_renderer, $index );
91
	}
92
93
	/**
94
	 * Retrieve the item link URL.
95
	 *
96
	 * @access public
97
	 *
98
	 * @return string $item_link The link URL of this item.
99
	 */
100
	public function get_item_link() {
101
		$item      = $this->get_item();
102
		$item_link = apply_filters( 'carbon_breadcrumbs_item_link', $item->get_link(), $item );
103
		return $item_link;
104
	}
105
106
	/**
107
	 * Render the item link opening tag and its "before" wrapper.
108
	 *
109
	 * @access public
110
	 *
111
	 * @return string $output The output HTML.
112
	 */
113
	public function render_link_before() {
114
		$trail_renderer = $this->get_trail_renderer();
115
		$item_link      = $this->get_item_link();
116
117
		// HTML before link opening tag.
118
		$output = $trail_renderer->get_link_before();
119
120
		// Link can be optional or disabled.
121
		if ( $item_link && $this->is_link_enabled() ) {
122
			$output .= '<a href="' . $item_link . '"' . $this->get_item_attributes_html() . '>';
123
		}
124
125
		return $output;
126
	}
127
128
	/**
129
	 * Render the item link closing tag and its "after" wrapper.
130
	 *
131
	 * @access public
132
	 *
133
	 * @return string $output The output HTML.
134
	 */
135
	public function render_link_after() {
136
		$trail_renderer = $this->get_trail_renderer();
137
		$item_link      = $this->get_item_link();
138
		$output         = '';
139
140
		// Link can be optional or disabled.
141
		if ( $item_link && $this->is_link_enabled() ) {
142
			$output .= '</a>';
143
		}
144
145
		// HTML after link closing tag.
146
		$output .= $trail_renderer->get_link_after();
147
148
		return $output;
149
	}
150
151
	/**
152
	 * Render the item title, along with its before & after wrappers.
153
	 *
154
	 * @access public
155
	 *
156
	 * @return string $output The output HTML.
157
	 */
158
	public function render_title() {
159
		$item           = $this->get_item();
160
		$trail_renderer = $this->get_trail_renderer();
161
162
		// HTML before title.
163
		$output = $trail_renderer->get_title_before();
164
165
		// Breadcrumb item title.
166
		$output .= apply_filters( 'carbon_breadcrumbs_item_title', $item->get_title(), $item );
167
168
		// HTML after title.
169
		$output .= $trail_renderer->get_title_after();
170
171
		return $output;
172
	}
173
174
	/**
175
	 * Retrieve the attributes of the item link.
176
	 *
177
	 * @access public
178
	 *
179
	 * @return string $attributes_html The HTML of the item attributes.
180
	 */
181
	public function get_item_attributes_html() {
182
		$item = $this->get_item();
183
184
		// Get the item attributes.
185
		$item_attributes = apply_filters( 'carbon_breadcrumbs_item_attributes', $item->get_attributes(), $item );
186
187
		// Prepare the item attributes.
188
		$attributes_html = '';
189
		foreach ( $item_attributes as $attr => $attr_value ) {
190
			$attributes_html .= ' ' . $attr . '="' . esc_attr( $attr_value ) . '"';
191
		}
192
193
		return $attributes_html;
194
	}
195
196
	/**
197
	 * Whether the link of this item is enabled.
198
	 *
199
	 * @access public
200
	 *
201
	 * @return bool
202
	 */
203
	public function is_link_enabled() {
204
		$trail          = $this->get_trail();
205
		$trail_renderer = $this->get_trail_renderer();
206
		$total_items    = $trail->get_total_items();
207
		$index          = $this->get_index();
208
209
		return $trail_renderer->get_last_item_link() || $index < $total_items;
210
	}
211
212
	/**
213
	 * Retrieve the item to render.
214
	 *
215
	 * @access public
216
	 *
217
	 * @return Carbon_Breadcrumb_Item $item Item to render.
218
	 */
219
	public function get_item() {
220
		return $this->item;
221
	}
222
223
	/**
224
	 * Modify the item to render.
225
	 *
226
	 * @access public
227
	 *
228
	 * @param Carbon_Breadcrumb_Item $item Item to render.
229
	 */
230
	public function set_item( $item ) {
231
		$this->item = $item;
232
	}
233
234
	/**
235
	 * Retrieve the trail this item belongs to.
236
	 *
237
	 * @access public
238
	 *
239
	 * @return Carbon_Breadcrumb_Trail $trail Trail this item belongs to.
240
	 */
241
	public function get_trail() {
242
		return $this->trail;
243
	}
244
245
	/**
246
	 * Modify the trail this item belongs to.
247
	 *
248
	 * @access public
249
	 *
250
	 * @param Carbon_Breadcrumb_Trail $trail Trail this item belongs to.
251
	 */
252
	public function set_trail( $trail ) {
253
		$this->trail = $trail;
254
	}
255
256
	/**
257
	 * Retrieve the trail renderer.
258
	 *
259
	 * @access public
260
	 *
261
	 * @return Carbon_Breadcrumb_Trail_Renderer $trail_renderer Trail renderer.
262
	 */
263
	public function get_trail_renderer() {
264
		return $this->trail_renderer;
265
	}
266
267
	/**
268
	 * Modify the trail renderer.
269
	 *
270
	 * @access public
271
	 *
272
	 * @param Carbon_Breadcrumb_Trail_Renderer $trail_renderer Trail renderer.
273
	 */
274
	public function set_trail_renderer( $trail_renderer ) {
275
		$this->trail_renderer = $trail_renderer;
276
	}
277
278
	/**
279
	 * Retrieve the index of this item.
280
	 *
281
	 * @access public
282
	 *
283
	 * @return int $index Index of this item.
284
	 */
285
	public function get_index() {
286
		return $this->index;
287
	}
288
289
	/**
290
	 * Modify the index of this item.
291
	 *
292
	 * @access public
293
	 *
294
	 * @param int $index Index of this item.
295
	 */
296
	public function set_index( $index = 0 ) {
297
		$this->index = $index;
298
	}
299
300
}
301