Completed
Push — master ( 196d18...e7d63d )
by Marin
02:25
created

Carbon_Breadcrumb_Trail_Setup   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 46.39%
Metric Value
wmc 33
lcom 1
cbo 3
dl 0
loc 234
ccs 45
cts 97
cp 0.4639
rs 9.4

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A populate_post_type_archive_items() 0 16 4
A populate_page_for_posts_items() 0 12 3
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
A populate_db_object_items() 0 12 2
A populate_date_archive_items() 0 9 2
A populate_search_items() 0 8 2
A populate_404_items() 0 8 2
A populate_category_items() 0 12 3
A is_post_archive() 0 3 4
A is_post_context() 0 3 4
1
<?php
2
/**
3
 * The main breadcrumb trail setup class.
4
 *
5
 * Generates and populates the default breadcrumb trail items.
6
 */
7
class Carbon_Breadcrumb_Trail_Setup {
8
9
	/**
10
	 * Breadcrumb trail.
11
	 *
12
	 * @access protected
13
	 * @var Carbon_Breadcrumb_Trail
14
	 */
15
	protected $trail;
16
17
	/**
18
	 * Constructor.
19
	 *
20
	 * Hooks all breadcrumb trail item population methods.
21
	 *
22
	 * @access public
23
	 *
24
	 * @param Carbon_Breadcrumb_Trail $trail Trail object to populate.
25
	 */
26
	public function __construct( Carbon_Breadcrumb_Trail $trail ) {
27
		$this->set_trail( $trail );
28
29
		$this->populate_db_object_items();
30
		$this->populate_date_archive_items();
31
		$this->populate_post_type_archive_items();
32
		$this->populate_search_items();
33
		$this->populate_404_items();
34
		$this->populate_category_items();
35
		$this->populate_page_for_posts_items();
36
		$this->populate_home_items();
37
	}
38
39
	/**
40
	 * Populate DB object items (post types, terms, authors).
41
	 *
42
	 * @access public
43
	 */
44 4
	public function populate_db_object_items() {
45
		$locators = array(
46 4
			'post',
47 4
			'term',
48 4
			'user',
49 4
		);
50
51 4
		foreach ( $locators as $locator_name ) {
52 4
			$items = $this->generate_locator_items( $locator_name );
53 4
			$this->get_trail()->add_item( $items );
54 4
		}
55 4
	}
56
57
	/**
58
	 * Populate date archives.
59
	 *
60
	 * @access public
61
	 */
62 3
	public function populate_date_archive_items() {
63 3
		if ( ! is_date() ) {
64
			return;
65
		}
66
67 3
		$locator = Carbon_Breadcrumb_Locator::factory( 'date' );
68 3
		$items = $locator->get_items( 700 );
69 3
		$this->get_trail()->add_item( $items );
70 3
	}
71
72
	/**
73
	 * Populate post type archives.
74
	 *
75
	 * @access public
76
	 */
77
	public function populate_post_type_archive_items() {
78
		if ( is_post_type_archive() ) {
79
			$post_type = get_post_type_object( get_query_var( 'post_type' ) );
80
		} elseif ( is_singular() ) {
81
			$post_type = get_post_type_object( get_post_type() );
82
			if ( ! $post_type->has_archive ) {
83
				return;
84
			}
85
		} else {
86
			return;
87
		}
88
89
		$title = $post_type->labels->name;
90
		$link = get_post_type_archive_link( $post_type->name );
91
		$this->get_trail()->add_custom_item( $title, $link, 700 );
92
	}
93
94
95
	/**
96
	 * Populate search items.
97
	 *
98
	 * @access public
99
	 */
100 1
	public function populate_search_items() {
101 1
		if ( ! is_search() ) {
102
			return;
103
		}
104
105 1
		$search_title = sprintf( __( 'Search results for: "%1$s"', 'carbon_breadcrumbs' ), get_search_query() );
106 1
		$this->get_trail()->add_custom_item( $search_title, '', 700 );
107 1
	}
108
109
	/**
110
	 * Populate 404 items.
111
	 *
112
	 * @access public
113
	 */
114 1
	public function populate_404_items() {
115 1
		if ( ! is_404() ) {
116
			return;
117
		}
118
119 1
		$not_found_title = __( 'Error 404 - Not Found', 'carbon_breadcrumbs' );
120 1
		$this->get_trail()->add_custom_item( $not_found_title, '', 700 );
121 1
	}
122
123
	/**
124
	 * Populate category hierarchy when on a single post.
125
	 *
126
	 * @access public
127
	 */
128 2
	public function populate_category_items() {
129 2
		if ( ! ( is_single() && 'post' == get_post_type() ) ) {
130 1
			return;
131
		}
132
133 1
		$taxonomy = 'category';
134 1
		$categories = wp_get_object_terms( get_the_ID(), $taxonomy, 'orderby=term_id' );
135 1
		$last_category = array_pop( $categories );
136 1
		$locator = Carbon_Breadcrumb_Locator::factory( 'term', $taxonomy );
137 1
		$items = $locator->get_items( 700, $last_category->term_id );
0 ignored issues
show
Unused Code introduced by
The call to Carbon_Breadcrumb_Locator::get_items() has too many arguments starting with $last_category->term_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
138 1
		$this->get_trail()->add_item( $items );
139 1
	}
140
141
	/**
142
	 * Populate page for posts item where necessary.
143
	 *
144
	 * @access public
145
	 */
146
	public function populate_page_for_posts_items() {
147
		$page_for_posts = get_option( 'page_for_posts' );
148
		if ( ! $page_for_posts ) {
149
			return;
150
		}
151
152
		if ( $this->is_post_context() ) {
153
			$locator = Carbon_Breadcrumb_Locator::factory( 'post', 'page' );
154
			$items = $locator->get_items( 500, $page_for_posts );
0 ignored issues
show
Unused Code introduced by
The call to Carbon_Breadcrumb_Locator::get_items() has too many arguments starting with $page_for_posts.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
155
			$this->get_trail()->add_item( $items );
156
		}
157
	}
158
159
	/**
160
	 * True if on a post archive, false otherwise.
161
	 * Post archives are: category, tag, date (year, month, day) and author pages.
162
	 *
163
	 * @access public
164
	 *
165
	 * @return bool
166
	 */
167 8
	public function is_post_archive() {
168 8
		return is_category() || is_tag() || is_date() || is_author();
169
	}
170
171
	/**
172
	 * True if on a post archive, on posts page or on a single post; false otherwise.
173
	 *
174
	 * @access public
175
	 *
176
	 * @return bool
177
	 */
178 9
	public function is_post_context() {
179 9
		return is_home() || $this->is_post_archive() || ( is_single() && 'post' == get_post_type() );
180
	}
181
182
	/**
183
	 * Populate home item.
184
	 *
185
	 * @access public
186
	 */
187
	public function populate_home_items() {
188
		$trail = $this->get_trail();
189
		$renderer = $trail->get_renderer();
190
		if ( ! $renderer->get_display_home_item() ) {
191
			return;
192
		}
193
194
		$home_title = $renderer->get_home_item_title();
195
		$home_link = home_url( '/' );
196
		$trail->add_custom_item( $home_title, $home_link, 10 );
197
	}
198
199
	/**
200
	 * Generate the items of a certain locator.
201
	 *
202
	 * @access protected
203
	 *
204
	 * @param string $locator_name Name of the locator.
205
	 * @return array $items Items generated by this locator.
206
	 */
207
	protected function generate_locator_items( $locator_name ) {
208
		$locator = Carbon_Breadcrumb_Locator::factory( $locator_name );
209
		$items = $locator->generate_items();
210
211
		if ( $items ) {
212
			return $items;
213
		}
214
215
		return array();
216
	}
217
218
	/**
219
	 * Retrieve the trail object.
220
	 *
221
	 * @access public
222
	 *
223
	 * @return Carbon_Breadcrumb_Trail $trail The trail object.
224
	 */
225 1
	public function get_trail() {
226 1
		return $this->trail;
227
	}
228
229
	/**
230
	 * Modify the trail object.
231
	 *
232
	 * @access public
233
	 *
234
	 * @param Carbon_Breadcrumb_Trail $trail The modified rendering object.
235
	 */
236 1
	public function set_trail( Carbon_Breadcrumb_Trail $trail ) {
237 1
		$this->trail = $trail;
238 1
	}
239
240
}