Completed
Push — master ( a85a3b...a9e055 )
by Justin
11:13
created

wpsc_breadcrumbs::wpsc_breadcrumbs()   C

Complexity

Conditions 17
Paths 128

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 52
rs 5.4376
cc 17
eloc 37
nc 128
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
* wpsc has breadcrumbs function
4
* @return boolean - true if we have and use them, false otherwise
5
*/
6
function wpsc_has_breadcrumbs() {
7
	global $wpsc_breadcrumbs;
8
	$wpsc_breadcrumbs = new wpsc_breadcrumbs();
9
10
	if(($wpsc_breadcrumbs->breadcrumb_count > 0) && (get_option("show_breadcrumbs") == 1)){
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $wpsc_breadcrumbs...how_breadcrumbs') == 1;.
Loading history...
11
		return true;
12
	} else {
13
		return false;
14
	}
15
}
16
17
/**
18
* wpsc have breadcrumbs function
19
* @return boolean - true if we have breadcrumbs to loop through
20
*/
21
function wpsc_have_breadcrumbs() {
22
	global $wpsc_breadcrumbs;
23
24
	return $wpsc_breadcrumbs->have_breadcrumbs();
25
}
26
27
/**
28
* wpsc the breadcrumbs function
29
* @return nothing - iterate through the breadcrumbs
30
*/
31
function wpsc_the_breadcrumb() {
32
	global $wpsc_breadcrumbs;
33
34
	$wpsc_breadcrumbs->the_breadcrumb();
35
}
36
37
/**
38
* wpsc breadcrumb name function
39
* @return string - the breadcrumb name
40
*/
41
function wpsc_breadcrumb_name() {
42
	global $wpsc_breadcrumbs;
43
44
	return $wpsc_breadcrumbs->breadcrumb['name'];
45
}
46
47
/**
48
* wpsc breadcrumb slug function
49
* @return string - the breadcrumb slug - for use in the CSS ID
50
*/
51
function wpsc_breadcrumb_slug() {
52
	global $wpsc_breadcrumbs;
53
54
	return ( isset( $wpsc_breadcrumbs->breadcrumb['slug'] ) ? sanitize_title_with_dashes( $wpsc_breadcrumbs->breadcrumb['slug'] ) : '' );
55
}
56
57
/**
58
* wpsc breadcrumb URL function
59
* @return string - the breadcrumb URL
60
*/
61
function wpsc_breadcrumb_url() {
62
	global $wpsc_breadcrumbs;
63
64
	if($wpsc_breadcrumbs->breadcrumb['url'] == '') {
65
		return false;
66
	} else {
67
		return $wpsc_breadcrumbs->breadcrumb['url'];
68
	}
69
}
70
71
/**
72
* Output breadcrumbs if configured
73
* @return None - outputs breadcrumb HTML
74
*/
75
function wpsc_output_breadcrumbs( $options = null ) {
76
77
	/**
78
	 * Filter the arguments passed to wpsc_output_breadcrumbs.
79
	 *
80
	 * @since 3.9.0
81
	 * @param array $options {
82
	 *     associative array of options for outputting the breadcrumbs to this page.
83
	 *
84
	 *     'before-breadcrumbs' string  HTML output to start the breadcrumb.
85
	 *     'after-breadcrumbs'  string  HTML output at the end of the breadcrumb eg closing tag.
86
	 *     'before-crumb'       string  output before each step in path, typically decorative.
87
	 *     'after-crumb'        string  output after each step in path.
88
	 *     'crumb-separator'    string  demarks space between steps in path.
89
	 *     'show_home_page'     bool    Whether to include home page as step at start of path.
90
	 *     'show_products_page' bool    Whether to include products-page as second step in path.
91
	 *     'echo'               bool    if true, outputs constructed string; if false, returns constructed string.
92
	 *     'products_page_id'   int     Override the page id used for products_page
93
	 *     }
94
	 *
95
	 */
96
	// Defaults
97
	$options = apply_filters( 'wpsc_output_breadcrumbs_options', $options );
98
	$options = wp_parse_args( (array) $options, array(
99
		'before-breadcrumbs' => '<div class="wpsc-breadcrumbs">',
100
		'after-breadcrumbs'  => '</div>',
101
		'before-crumb'       => '',
102
		'after-crumb'        => '',
103
		'crumb-separator'    => ' &raquo; ',
104
		'show_home_page'     => true,
105
		'show_products_page' => true,
106
		'echo'               => true,
107
		'products_page_id'   => wpsc_get_the_post_id_by_shortcode( '[productspage]' )
108
	) );
109
110
	$output = '';
111
	$products_page_id = absint( $options['products_page_id'] );
112
	$products_page = get_post( $products_page_id );
113
114
	if ( ! wpsc_has_breadcrumbs() ) {
115
		return;
116
	}
117
118
	$filtered_products_page = array(
119
		'url'  => get_option( 'product_list_url' ),
120
		'name' => apply_filters ( 'the_title', $products_page->post_title, $products_page_id )
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
121
	);
122
123
	/**
124
	 * Filter the values used to create the products-page step.
125
	 *
126
	 * Overrides the url and text show for the products-page step in breadcrumb.
127
	 *
128
	 * @since 3.9.0
129
	 * @param array $filtered_products_page {
130
	 *     associative array of options for outputting the breadcrumbs to this page.
131
	 *
132
	 *     'url'   string  Fully-qualified url to products-page.
133
	 *     'name'  string  The name that will appear for the products-page in breadcrumb.
134
	 *     }
135
	 *
136
	 */
137
	$filtered_products_page = apply_filters( 'wpsc_change_pp_breadcrumb', $filtered_products_page );
138
139
	// Home Page Crumb
140
	// If home if the same as products page only show the products-page link and not the home link
141
	if ( get_option( 'page_on_front' ) != $products_page_id && $options['show_home_page'] ) {
142
		$output .= $options['before-crumb'];
143
		$output .= '<a class="wpsc-crumb" id="wpsc-crumb-home" href="' . get_option( 'home' ) . '">' . get_option( 'blogname' ) . '</a>';
144
		$output .= $options['after-crumb'];
145
	}
146
147
	// Products Page Crumb
148
	if ( $options['show_products_page'] ) {
149
		if ( !empty( $output ) ) {
150
			$output .= $options['crumb-separator'];
151
		}
152
		$output .= $options['before-crumb'];
153
		$output .= '<a class="wpsc-crumb" id="wpsc-crumb-' . $products_page_id . '" href="' . $filtered_products_page['url'] . '">' . $filtered_products_page['name'] . '</a>';
154
		$output .= $options['after-crumb'];
155
	}
156
157
	// Remaining Crumbs
158
	while ( wpsc_have_breadcrumbs() ) {
159
		wpsc_the_breadcrumb();
160
		if ( !empty( $output ) ) {
161
			$output .= $options['crumb-separator'];
162
		}
163
		$output .= $options['before-crumb'];
164
		if ( wpsc_breadcrumb_url() ) {
165
			$output .= '<a class="wpsc-crumb" id="wpsc-crumb-' . wpsc_breadcrumb_slug() . '" href="' . wpsc_breadcrumb_url() . '">' . wpsc_breadcrumb_name() . '</a>';
166
		} else {
167
			$output .= '<span class="wpsc-crumb" id="wpsc-crumb-' . wpsc_breadcrumb_slug() . '">' . wpsc_breadcrumb_name() . '</span>';
168
		}
169
		$output .= $options['after-crumb'];
170
	}
171
172
	/**
173
	 * Filter the assembled breadcrumb.
174
	 *
175
	 * @since 3.9.0
176
	 * @param string $output The constructed breadcrumb string.
177
	 * @param array $options {@see 'wpsc_output_breadcrumbs_options'}
178
	 *
179
	 */
180
	$output = $options['before-breadcrumbs'] . apply_filters( 'wpsc_output_breadcrumbs', $output, $options ) . $options['after-breadcrumbs'];
181
	if ( $options['echo'] ) {
182
		echo $output;
183
	} else {
184
		return $output;
185
	}
186
}
187
188
/**
189
 * wpsc_breadcrumbs class.
190
 *
191
 */
192
class wpsc_breadcrumbs {
0 ignored issues
show
Coding Style introduced by
Class name "wpsc_breadcrumbs" is not in camel caps format
Loading history...
193
	var $breadcrumbs;
194
	var $breadcrumb_count = 0;
195
	var $current_breadcrumb = -1;
196
	var $breadcrumb;
197
198
	/**
199
	 * wpsc__breadcrumbs function.
200
	 *
201
	 * @access public
202
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
203
	 */
204
	public function __construct() {
205
		global $wp_query, $wpsc_query;
206
		$this->breadcrumbs = array();
207
		$query_data = Array();
0 ignored issues
show
introduced by
Array keyword should be lower case; expected "array" but found "Array"
Loading history...
208
		if ( isset($wp_query->query_vars['post_type']) && 'wpsc-product' == $wp_query->query_vars['post_type'] && 1 == $wp_query->query_vars['posts_per_page'] && isset($wp_query->post) && is_single() )
209
			$query_data['product'] = $wp_query->post->post_title;
210
211
		if ( !empty($wpsc_query->query_vars['wpsc_product_category']) )
212
			$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
213
214
		if(!empty($query_data['product']) && !empty($wp_query->post)) {
215
			$this->breadcrumbs[] = array(
216
				'name' => esc_html( $wp_query->post->post_title ),
217
				'url'  => '',
218
				'slug' => $query_data['product']
219
			);
220
		}
221
		if( is_single() ){
222
			$categories = wpsc_get_product_terms( $wp_query->post->ID , 'wpsc_product_category' );
223
			//if product is associated w more than one category
224
			if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
225
				$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
226
			elseif(count($categories) > 0)
227
				$query_data['category'] = $categories[0]->slug;
228
229
		}
230
		if( isset( $query_data['category'] ) )
231
			$term_data = get_term_by('slug', $query_data['category'], 'wpsc_product_category');
232
		else
233
			$term_data = get_term_by('slug', 'uncategorized', 'wpsc_product_category');
234
235
		if( $term_data != false) {
236
			$this->breadcrumbs[] = array(
237
				'name' => esc_html( $term_data->name ),
238
				'url'  => get_term_link( $term_data->slug, 'wpsc_product_category'),
239
				'slug' => $term_data->slug
240
			);
241
242
			$i = 0;
243
244
			while(($term_data->parent > 0) && ($i <= 20)) {
245
				$term_data = get_term($term_data->parent, 'wpsc_product_category');
246
				$this->breadcrumbs[] = array(
247
					'name' => esc_html( $term_data->name ),
248
					'url'  => get_term_link( $term_data->slug, 'wpsc_product_category')
249
				);
250
				$i++;
251
			}
252
		}
253
		$this->breadcrumbs = apply_filters( 'wpsc_breadcrumbs', array_reverse( $this->breadcrumbs ) );
254
		$this->breadcrumb_count = count($this->breadcrumbs);
255
	}
256
257
	/**
258
	 * next_breadcrumbs function.
259
	 *
260
	 * @access public
261
	 * @return void
262
	 */
263
	public function next_breadcrumbs() {
264
		$this->current_breadcrumb++;
265
		$this->breadcrumb = $this->breadcrumbs[$this->current_breadcrumb];
266
		return $this->breadcrumb;
267
	}
268
269
270
	/**
271
	 * the_breadcrumb function.
272
	 *
273
	 * @access public
274
	 * @return void
275
	 */
276
	public function the_breadcrumb() {
277
		$this->breadcrumb = $this->next_breadcrumbs();
278
	}
279
280
	/**
281
	 * have_breadcrumbs function.
282
	 *
283
	 * @access public
284
	 * @return void
285
	 */
286
	public function have_breadcrumbs() {
287
		if ($this->current_breadcrumb + 1 < $this->breadcrumb_count) {
288
			return true;
289
		} else if ($this->current_breadcrumb + 1 == $this->breadcrumb_count && $this->breadcrumb_count > 0) {
290
			$this->rewind_breadcrumbs();
291
		}
292
		return false;
293
	}
294
295
	/**
296
	 * rewind_breadcrumbs function.
297
	 *
298
	 * @access public
299
	 * @return void
300
	 */
301
	public function rewind_breadcrumbs() {
302
		$this->current_breadcrumb = -1;
303
		if ($this->breadcrumb_count > 0) {
304
			$this->breadcrumb = $this->breadcrumbs[0];
305
		}
306
	}
307
308
}