Completed
Push — master ( 93c42a...c89a6c )
by Marin
03:05
created

Carbon_Breadcrumb_Item_Renderer::set_trail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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