Carbon_Breadcrumb_Trail_Setup   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 0
loc 235
rs 9.76
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A populate_db_object_items() 0 12 2
A populate_date_archive_items() 0 9 2
A populate_post_type_archive_items() 0 16 4
A populate_search_items() 0 9 2
A populate_404_items() 0 8 2
A populate_category_items() 0 12 3
A populate_page_for_posts_items() 0 12 3
A is_post_archive() 0 3 4
A is_post_context() 0 3 4
A populate_home_items() 0 11 2
A generate_locator_items() 0 10 2
A get_trail() 0 3 1
A set_trail() 0 3 1
1
<?php
2
/**
3
 * Breadcrumb trail setup.
4
 *
5
 * @package carbon-breadcrumbs
6
 */
7
8
/**
9
 * The main breadcrumb trail setup class.
10
 *
11
 * Generates and populates the default breadcrumb trail items.
12
 */
13
class Carbon_Breadcrumb_Trail_Setup {
14
15
	/**
16
	 * Breadcrumb trail.
17
	 *
18
	 * @access protected
19
	 * @var Carbon_Breadcrumb_Trail
20
	 */
21
	protected $trail;
22
23
	/**
24
	 * Constructor.
25
	 *
26
	 * Hooks all breadcrumb trail item population methods.
27
	 *
28
	 * @access public
29
	 *
30
	 * @param Carbon_Breadcrumb_Trail $trail Trail object to populate.
31
	 */
32
	public function __construct( Carbon_Breadcrumb_Trail $trail ) {
33
		$this->set_trail( $trail );
34
35
		$this->populate_db_object_items();
36
		$this->populate_date_archive_items();
37
		$this->populate_post_type_archive_items();
38
		$this->populate_search_items();
39
		$this->populate_404_items();
40
		$this->populate_category_items();
41
		$this->populate_page_for_posts_items();
42
		$this->populate_home_items();
43
	}
44
45
	/**
46
	 * Populate DB object items (post types, terms, authors).
47
	 *
48
	 * @access public
49
	 */
50
	public function populate_db_object_items() {
51
		$locators = array(
52
			'post',
53
			'term',
54
			'user',
55
		);
56
57
		foreach ( $locators as $locator_name ) {
58
			$items = $this->generate_locator_items( $locator_name );
59
			$this->get_trail()->add_item( $items );
60
		}
61
	}
62
63
	/**
64
	 * Populate date archives.
65
	 *
66
	 * @access public
67
	 */
68
	public function populate_date_archive_items() {
69
		if ( ! is_date() ) {
70
			return;
71
		}
72
73
		$locator = Carbon_Breadcrumb_Locator::factory( 'date' );
74
		$items   = $locator->get_items( 700 );
75
		$this->get_trail()->add_item( $items );
76
	}
77
78
	/**
79
	 * Populate post type archives.
80
	 *
81
	 * @access public
82
	 */
83
	public function populate_post_type_archive_items() {
84
		if ( is_post_type_archive() ) {
85
			$post_type = get_post_type_object( get_query_var( 'post_type' ) );
86
		} elseif ( is_singular() ) {
87
			$post_type = get_post_type_object( get_post_type() );
88
			if ( ! $post_type->has_archive ) {
89
				return;
90
			}
91
		} else {
92
			return;
93
		}
94
95
		$title = $post_type->labels->name;
96
		$link  = get_post_type_archive_link( $post_type->name );
97
		$this->get_trail()->add_custom_item( $title, $link, 700 );
98
	}
99
100
101
	/**
102
	 * Populate search items.
103
	 *
104
	 * @access public
105
	 */
106
	public function populate_search_items() {
107
		if ( ! is_search() ) {
108
			return;
109
		}
110
111
		// Translators: %1$s - search query.
112
		$search_title = sprintf( __( 'Search results for: "%1$s"', 'carbon_breadcrumbs' ), get_search_query() );
113
		$this->get_trail()->add_custom_item( $search_title, '', 700 );
114
	}
115
116
	/**
117
	 * Populate 404 items.
118
	 *
119
	 * @access public
120
	 */
121
	public function populate_404_items() {
122
		if ( ! is_404() ) {
123
			return;
124
		}
125
126
		$not_found_title = __( 'Error 404 - Not Found', 'carbon_breadcrumbs' );
127
		$this->get_trail()->add_custom_item( $not_found_title, '', 700 );
128
	}
129
130
	/**
131
	 * Populate category hierarchy when on a single post.
132
	 *
133
	 * @access public
134
	 */
135
	public function populate_category_items() {
136
		if ( ! ( is_single() && 'post' == get_post_type() ) ) {
137
			return;
138
		}
139
140
		$taxonomy      = 'category';
141
		$categories    = wp_get_object_terms( get_the_ID(), $taxonomy, array( 'orderby' => 'term_id' ) );
142
		$last_category = array_pop( $categories );
143
		$locator       = Carbon_Breadcrumb_Locator::factory( 'term', $taxonomy );
144
		$items         = $locator->get_items( 700, $last_category->term_id );
145
		$this->get_trail()->add_item( $items );
146
	}
147
148
	/**
149
	 * Populate page for posts item where necessary.
150
	 *
151
	 * @access public
152
	 */
153
	public function populate_page_for_posts_items() {
154
		$page_for_posts = get_option( 'page_for_posts' );
155
		if ( ! $page_for_posts ) {
156
			return;
157
		}
158
159
		if ( $this->is_post_context() ) {
160
			$locator = Carbon_Breadcrumb_Locator::factory( 'post', 'page' );
161
			$items   = $locator->get_items( 500, $page_for_posts );
162
			$this->get_trail()->add_item( $items );
163
		}
164
	}
165
166
	/**
167
	 * True if on a post archive, false otherwise.
168
	 * Post archives are: category, tag, date (year, month, day) and author pages.
169
	 *
170
	 * @access public
171
	 *
172
	 * @return bool
173
	 */
174
	public function is_post_archive() {
175
		return is_category() || is_tag() || is_date() || is_author();
176
	}
177
178
	/**
179
	 * True if on a post archive, on posts page or on a single post; false otherwise.
180
	 *
181
	 * @access public
182
	 *
183
	 * @return bool
184
	 */
185
	public function is_post_context() {
186
		return is_home() || $this->is_post_archive() || ( is_single() && 'post' == get_post_type() );
187
	}
188
189
	/**
190
	 * Populate home item.
191
	 *
192
	 * @access public
193
	 */
194
	public function populate_home_items() {
195
		$trail    = $this->get_trail();
196
		$renderer = $trail->get_renderer();
197
		if ( ! $renderer->get_display_home_item() ) {
198
			return;
199
		}
200
201
		$home_title = $renderer->get_home_item_title();
202
		$home_link  = home_url( '/' );
203
		$trail->add_custom_item( $home_title, $home_link, 10 );
204
	}
205
206
	/**
207
	 * Generate the items of a certain locator.
208
	 *
209
	 * @access protected
210
	 *
211
	 * @param string $locator_name Name of the locator.
212
	 * @return array $items Items generated by this locator.
213
	 */
214
	protected function generate_locator_items( $locator_name ) {
215
		$locator = Carbon_Breadcrumb_Locator::factory( $locator_name );
216
		$items   = $locator->generate_items();
217
218
		if ( $items ) {
219
			return $items;
220
		}
221
222
		return array();
223
	}
224
225
	/**
226
	 * Retrieve the trail object.
227
	 *
228
	 * @access public
229
	 *
230
	 * @return Carbon_Breadcrumb_Trail $trail The trail object.
231
	 */
232
	public function get_trail() {
233
		return $this->trail;
234
	}
235
236
	/**
237
	 * Modify the trail object.
238
	 *
239
	 * @access public
240
	 *
241
	 * @param Carbon_Breadcrumb_Trail $trail The modified rendering object.
242
	 */
243
	public function set_trail( Carbon_Breadcrumb_Trail $trail ) {
244
		$this->trail = $trail;
245
	}
246
247
}
248