woocommerce.php ➔ spurs_wc_form_field_args()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 68

Duplication

Lines 43
Ratio 63.24 %

Importance

Changes 0
Metric Value
cc 12
nc 12
nop 3
dl 43
loc 68
rs 6.2714
c 0
b 0
f 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
 * Add WooCommerce support
4
 *
5
 * @package spurs
6
 */
7
8
// Exit if accessed directly.
9
defined( 'ABSPATH' ) || exit;
10
11
add_action( 'after_setup_theme', 'spurs_woocommerce_support' );
12
if ( ! function_exists( 'spurs_woocommerce_support' ) ) {
13
	/**
14
	 * Declares WooCommerce theme support.
15
	 */
16
	function spurs_woocommerce_support() {
17
		add_theme_support( 'woocommerce' );
18
19
		// Add New Woocommerce 3.0.0 Product Gallery support
20
		add_theme_support( 'wc-product-gallery-lightbox' );
21
		add_theme_support( 'wc-product-gallery-zoom' );
22
		add_theme_support( 'wc-product-gallery-slider' );
23
24
		// hook in and customizer form fields.
25
		add_filter( 'woocommerce_form_field_args', 'spurs_wc_form_field_args', 10, 3 );
26
	}
27
}
28
29
30
/**
31
 * First unhook the WooCommerce wrappers
32
 */
33
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
34
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
35
36
/**
37
 * Then hook in your own functions to display the wrappers your theme requires
38
 */
39
add_action( 'woocommerce_before_main_content', 'spurs_woocommerce_wrapper_start', 10 );
40
add_action( 'woocommerce_after_main_content', 'spurs_woocommerce_wrapper_end', 10 );
41
if ( ! function_exists( 'spurs_woocommerce_wrapper_start' ) ) {
42
	function spurs_woocommerce_wrapper_start() {
43
		$container = get_theme_mod( 'spurs_container_type' );
44
		echo '<div class="wrapper" id="woocommerce-wrapper">';
45
		echo '<div class="' . esc_attr( $container ) . '" id="content" tabindex="-1">';
46
		echo '<div class="row">';
47
		get_template_part( 'global-templates/left-sidebar-check' );
48
		echo '<main class="site-main" id="main">';
49
	}
50
}
51
if ( ! function_exists( 'spurs_woocommerce_wrapper_end' ) ) {
52
	function spurs_woocommerce_wrapper_end() {
53
		echo '</main>';
54
		echo '</div>';
55
		get_template_part( 'global-templates/right-sidebar-check' );
56
		echo '</div><!-- .row -->';
57
		echo '</div><!-- Container end -->';
58
		echo '</div><!-- Wrapper end -->';
59
	}
60
}
61
62
63
/**
64
 * Filter hook function monkey patching form classes
65
 * Author: Adriano Monecchi http://stackoverflow.com/a/36724593/307826
66
 *
67
 * @param string $args Form attributes.
68
 * @param string $key Not in use.
69
 * @param null $value Not in use.
70
 *
71
 * @return mixed
72
 */
73
if ( ! function_exists( 'spurs_wc_form_field_args' ) ) {
74
	function spurs_wc_form_field_args( $args, $key, $value = null ) {
75
		// Start field type switch case.
76
		switch ( $args['type'] ) {
77
			/* Targets all select input type elements, except the country and state select input types */
78 View Code Duplication
			case 'select' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
				// Add a class to the field's html element wrapper - woocommerce
80
				// input types (fields) are often wrapped within a <p></p> tag.
81
				$args['class'][] = 'form-group';
82
				// Add a class to the form input itself.
83
				$args['input_class']       = array( 'form-control', 'input-lg' );
84
				$args['label_class']       = array( 'control-label' );
85
				$args['custom_attributes'] = array(
86
					'data-plugin'      => 'select2',
87
					'data-allow-clear' => 'true',
88
					'aria-hidden'      => 'true',
89
					// Add custom data attributes to the form input itself.
90
				);
91
				break;
92
			// By default WooCommerce will populate a select with the country names - $args
93
			// defined for this specific input type targets only the country select element.
94
			case 'country' :
95
				$args['class'][]     = 'form-group single-country';
96
				$args['label_class'] = array( 'control-label' );
97
				break;
98
			// By default WooCommerce will populate a select with state names - $args defined
99
			// for this specific input type targets only the country select element.
100 View Code Duplication
			case 'state' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
				// Add class to the field's html element wrapper.
102
				$args['class'][] = 'form-group';
103
				// add class to the form input itself.
104
				$args['input_class']       = array( '', 'input-lg' );
105
				$args['label_class']       = array( 'control-label' );
106
				$args['custom_attributes'] = array(
107
					'data-plugin'      => 'select2',
108
					'data-allow-clear' => 'true',
109
					'aria-hidden'      => 'true',
110
				);
111
				break;
112
			case 'password' :
113
			case 'text' :
114
			case 'email' :
115
			case 'tel' :
116
			case 'number' :
117
				$args['class'][]     = 'form-group';
118
				$args['input_class'] = array( 'form-control', 'input-lg' );
119
				$args['label_class'] = array( 'control-label' );
120
				break;
121 View Code Duplication
			case 'textarea' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
				$args['input_class'] = array( 'form-control', 'input-lg' );
123
				$args['label_class'] = array( 'control-label' );
124
				break;
125 View Code Duplication
			case 'checkbox' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
				$args['label_class'] = array( 'custom-control custom-checkbox' );
127
				$args['input_class'] = array( 'custom-control-input', 'input-lg' );
128
				break;
129 View Code Duplication
			case 'radio' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
				$args['label_class'] = array( 'custom-control custom-radio' );
131
				$args['input_class'] = array( 'custom-control-input', 'input-lg' );
132
				break;
133 View Code Duplication
			default :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
				$args['class'][]     = 'form-group';
135
				$args['input_class'] = array( 'form-control', 'input-lg' );
136
				$args['label_class'] = array( 'control-label' );
137
				break;
138
		} // end switch ($args).
139
140
		return $args;
141
	}
142
}
143
if ( ! is_admin() && ! function_exists( 'wc_review_ratings_enabled' ) ) {
144
	/**
145
	 * Check if reviews are enabled.
146
	 *
147
	 * Function introduced in WooCommerce 3.6.0., include it for backward compatibility.
148
	 *
149
	 * @return bool
150
	 */
151
	function wc_reviews_enabled() {
152
		return 'yes' === get_option( 'woocommerce_enable_reviews' );
153
	}
154
155
	/**
156
	 * Check if reviews ratings are enabled.
157
	 *
158
	 * Function introduced in WooCommerce 3.6.0., include it for backward compatibility.
159
	 *
160
	 * @return bool
161
	 */
162
	function wc_review_ratings_enabled() {
163
		return wc_reviews_enabled() && 'yes' === get_option( 'woocommerce_enable_review_rating' );
164
	}
165
}
166
167
/**
168
 * Change loop add-to-cart button class to Bootstrap
169
 */
170
add_filter( 'woocommerce_loop_add_to_cart_args', 'spurs_woocommerce_add_to_cart_args', 10, 2 );
171
172
if ( ! function_exists( 'spurs_woocommerce_add_to_cart_args' ) ) {
173
	function spurs_woocommerce_add_to_cart_args( $args, $product ) {
174
		$args['class'] = str_replace( 'button', 'btn btn-outline-primary', 'button' );
175
176
		return $args;
177
	}
178
}
179