wps_classic_checkout   F
last analyzed

Complexity

Total Complexity 163

Size/Duplication

Total Lines 541
Duplicated Lines 15.34 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 83
loc 541
rs 1.5789
c 0
b 0
f 0
wmc 163
lcom 0
cbo 8

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 21 21 1
A add_scripts() 0 4 1
F show_classic_checkout() 32 146 45
A wps_classic_confirmation_message() 0 11 3
C get_checkout_step_indicator() 0 44 15
A wps_summary_order() 0 7 1
D wps_checkout_valid_step_four() 3 48 18
F wps_checkout_valid_step_five() 0 66 24
B checking_stock() 0 14 6
F wps_checkout_valid_step_three() 27 129 49

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like wps_classic_checkout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use wps_classic_checkout, and based on these observations, apply Extract Interface, too.

1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
 * WP Shop Classic Checkout bootstrap file
4
 * @author Jérôme ALLEGRE - Eoxia dev team <[email protected]>
5
 * @version 0.1
6
 * @package includes
7
 * @subpackage modules
8
 *
9
 */
10
11
if ( !class_exists("wps_classic_checkout") ) {
12
13
	/** Template Global vars **/
14
	DEFINE('WPS_CLASSIC_CHECKOUT_DIR', basename(dirname(__FILE__)));
15
	DEFINE('WPS_CLASSIC_CHECKOUT_PATH', str_replace( "\\", "/", str_replace( WPS_CLASSIC_CHECKOUT_DIR, "", dirname( __FILE__ ) ) ) );
16
	DEFINE('WPS_CLASSIC_CHECKOUT_URL', str_replace( str_replace( "\\", "/", ABSPATH), site_url() . '/', WPS_CLASSIC_CHECKOUT_PATH ) );
17
18
19
	class wps_classic_checkout {
20
		/**
21
		 * Define the main directory containing the template for the current plugin
22
		 * @var string
23
		 */
24
		private $template_dir;
25
26
		/**
27
		 * Define the directory name for the module in order to check into frontend
28
		 * @var string
29
		 */
30
		private $plugin_dirname = WPS_CLASSIC_CHECKOUT_DIR;
0 ignored issues
show
Unused Code introduced by
The property $plugin_dirname is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
33 View Code Duplication
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
34
			/** Template Load **/
35
			$this->template_dir = WPS_CLASSIC_CHECKOUT_PATH . WPS_CLASSIC_CHECKOUT_DIR . "/templates/";
36
37
			/** Classic Checkout Shortcode **/
38
			add_shortcode( 'wps_checkout', array( &$this, 'show_classic_checkout') );
39
			add_shortcode( 'wpshop_checkout', array( &$this, 'show_classic_checkout') );
40
			/** Checkout Step indicator **/
41
			add_shortcode('wps_checkout_step_indicator', array(&$this, 'get_checkout_step_indicator') );
42
43
			// Add scripts
44
			add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts') );
45
46
			/** Ajax Actions **/
47
			add_action( 'wp_ajax_wps-checkout_valid_step_three', array( &$this, 'wps_checkout_valid_step_three') );
48
			add_action( 'wp_ajax_wps-checkout_valid_step_four', array( &$this, 'wps_checkout_valid_step_four') );
49
			add_action( 'wp_ajax_wps-checkout_valid_step_five', array( &$this, 'wps_checkout_valid_step_five') );
50
51
			add_action( 'admin_post_wps_direct_payment_link', array( 'wpshop_checkout', 'wps_direct_payment_link' ) );
52
			add_action( 'admin_post_nopriv_wps_direct_payment_link', array( 'wpshop_checkout', 'wps_direct_payment_link_nopriv' ) );
53
		}
54
55
		/**
56
		 * Add scripts
57
		 */
58
		function add_scripts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
			wp_enqueue_script('jquery');
60
			wp_enqueue_script( 'wps_classic_checkout', plugins_url('templates/frontend/js/wps_classic_checkout.js', __FILE__) );
61
		}
62
63
		/**
64
		 * Display Classic Checkout
65
		 */
66
		function show_classic_checkout() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
			$order_id = !empty( $_GET['order_id'] ) ? (int) $_GET['order_id'] : '';
68
69
			$wpshop_cart_option = get_option( 'wpshop_cart_option' );
70
			$current_step = !empty( $_GET[ 'order_step' ] ) && is_int( (int)$_GET[ 'order_step' ] ) ? (int)$_GET[ 'order_step' ] : null;
71
72
			/**	Cas spécial lorsqu'il n'y a qu'un seul produit autorisé dans le panier / Special case when there is only one product allowed into cart	*/
73
			if ( ( empty( $current_step ) || ( 1 == $current_step ) ) && ( !empty( $wpshop_cart_option ) && !empty( $wpshop_cart_option[ 'total_nb_of_item_allowed' ] ) && ( 1 == $wpshop_cart_option[ 'total_nb_of_item_allowed' ][0] ) ) ) {
74
				if( empty($_SESSION) || empty($_SESSION['cart']) || empty($_SESSION['cart']['order_items']) ) {
75
					$product_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_product_page_id' ) );
76
					$url = get_permalink( $product_page_id );
77
					wpshop_tools::wpshop_safe_redirect( $url );
78
				}
79
				else {
80
					$current_step = 2;
81
				}
82
			}
83
84
			$checkout_step_indicator = do_shortcode( '[wps_checkout_step_indicator]');
85
			$checkout_content = '';
86
87
			if ( !empty($current_step) ) {
88
				switch( $current_step) {
89 View Code Duplication
					case 1 :
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...
90
						ob_start();
91
						require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-one") );
92
						$checkout_content .= ob_get_contents();
93
						ob_end_clean();
94
					break;
95
					case 2 :
96
						if ( get_current_user_id() != 0 ) {
97
							$permalink_option = get_option( 'permalink_structure' );
98
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
99
							$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=3';
100
							wpshop_tools::wpshop_safe_redirect( $url );
101
						}
102
						else {
103
							ob_start();
104
							require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-two") );
105
							$checkout_content .= ob_get_contents();
106
							ob_end_clean();
107
						}
108
					break;
109
					case 3 :
110
						if ( get_current_user_id() == 0 ) {
111
							$permalink_option = get_option( 'permalink_structure' );
112
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
113
							$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=2';
114
							wpshop_tools::wpshop_safe_redirect( $url );
115
						}
116
						else {
117
							if( !empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) ) {
118
								ob_start();
119
								require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-three") );
120
								$checkout_content .= ob_get_contents();
121
								ob_end_clean();
122
								$url = apply_filters('wps_extra_signup_actions', ( isset( $url ) ? $url : '' ) );
123
								if(!empty($url)) {
124
									wpshop_tools::wpshop_safe_redirect( $url );
125
								}
126
							}
127 View Code Duplication
							else {
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...
128
								$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
129
								$url = get_permalink( $checkout_page_id  );
130
								wpshop_tools::wpshop_safe_redirect( $url );
131
							}
132
						}
133
					break;
134
					case 4 :
135
						if ( get_current_user_id() == 0 ) {
136
							$permalink_option = get_option( 'permalink_structure' );
137
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
138
							$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=2';
139
							wpshop_tools::wpshop_safe_redirect( $url );
140
						}
141
						else {
142
							if( !empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) ) {
143
								ob_start();
144
								require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-four") );
145
								$checkout_content .= ob_get_contents();
146
								ob_end_clean();
147
							}
148 View Code Duplication
							else {
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...
149
								$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
150
								$url = get_permalink( $checkout_page_id  );
151
								wpshop_tools::wpshop_safe_redirect( $url );
152
							}
153
						}
154
					break;
155
					case 5 :
156
						if ( get_current_user_id() == 0 ) {
157
							$permalink_option = get_option( 'permalink_structure' );
158
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
159
							$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=2';
160
							wpshop_tools::wpshop_safe_redirect( $url );
161
						}
162
						else {
163
							$wps_cart = new wps_cart();
164
							$order = $wps_cart->calcul_cart_information( array() );
165
							$wps_cart->store_cart_in_session($order);
166
							$shipping_option = get_option( 'wpshop_shipping_address_choice' );
167
							if ( !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) && ( ( !empty($shipping_option) && !empty($shipping_option['activate']) && !empty($_SESSION['shipping_method']) ) || ( !empty($shipping_option) && empty($shipping_option['activate']) )  ) ) {
168
								$order_id = ( !empty($_SESSION['cart']['order_id']) ) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0;
169
								ob_start();
170
								require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-five") );
171
								$checkout_content .= ob_get_contents();
172
								ob_end_clean();
173
								$checkout_content = apply_filters( 'classic_checkout_step_six_extra_content', $checkout_content );
0 ignored issues
show
Unused Code introduced by
$checkout_content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174
							}
175 View Code Duplication
							else {
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...
176
								$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
177
								$url = get_permalink( $checkout_page_id  );
178
								wpshop_tools::wpshop_safe_redirect( $url );
179
							}
180
						}
181
					break;
182
					case 6 :
183
184
						if ( !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) ){
185
						 	$wps_marketing_tools_ctr = new wps_marketing_tools_ctr();
186
						 	$checkout_content .=  $wps_marketing_tools_ctr->display_ecommerce_ga_tracker( $_SESSION['order_id'] );
187
						 	$checkout_content .= $this->wps_classic_confirmation_message();
188
						 	$checkout_content .= $this->wps_summary_order();
189
						}
190 View Code Duplication
						else {
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...
191
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
192
							$url = get_permalink( $checkout_page_id  );
193
							wpshop_tools::wpshop_safe_redirect( $url );
194
						}
195
					break;
196 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...
197
						ob_start();
198
						require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic-checkout", "step-one") );
199
						$checkout_content .= ob_get_contents();
200
						ob_end_clean();
201
					break;
202
				}
203
204
			}
205
			else {
206
				$checkout_content = do_shortcode('[wps_cart]');
0 ignored issues
show
Unused Code introduced by
$checkout_content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
207
			}
208
209
			require_once( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir, "frontend", "classic_checkout") );
210
211
		}
212
213
		/**
214
		 * Display the Step 6 confirmation message
215
		 * @return string
216
		 */
217
		function wps_classic_confirmation_message() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
218
			$output = '';
219
			$wps_cart = new wps_cart();
220
			$available_templates = array( 'banktransfer', 'checks', 'free', 'paypal', 'cic', 'quotation', 'cash_on_delivery' );
221
			$payment_method = ( !empty($_SESSION['payment_method']) && in_array($_SESSION['payment_method'], $available_templates) ) ? $_SESSION['payment_method'] : 'others';
222
			ob_start();
223
			require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir,"frontend", "confirmation/confirmation", $payment_method) );
224
			$output .= ob_get_contents();
225
			ob_end_clean();
226
			return $output;
227
		}
228
229
		function wps_summary_order() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
230
			ob_start();
231
			require( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir,"frontend", "confirmation/confirmation-summary") );
232
			$output = ob_get_contents();
233
			ob_end_clean();
234
			return $output;
235
		}
236
237
		/**
238
		 * Display Checkout Step indicator
239
		 * @return String
240
		 */
241
		function get_checkout_step_indicator() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
242
			$default_step = ( !empty( $_GET['order_step'] ) ) ? wpshop_tools::varSanitizer( $_GET['order_step'] ) : 1;
0 ignored issues
show
Unused Code introduced by
$default_step is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
243
			$steps = array();
244
245
			$shipping_address_option = get_option( 'wpshop_shipping_address_choice' );
246
			$wpshop_cart_option = get_option( 'wpshop_cart_option' );
247
			$permalink_option = get_option( 'permalink_structure' );
0 ignored issues
show
Unused Code introduced by
$permalink_option is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
248
			$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
0 ignored issues
show
Unused Code introduced by
$checkout_page_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
249
250
251
			$no_cart = false;
0 ignored issues
show
Unused Code introduced by
$no_cart is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
252
			if ( empty( $wpshop_cart_option ) || empty( $wpshop_cart_option[ 'total_nb_of_item_allowed' ] ) || ( 1 != (int) $wpshop_cart_option[ 'total_nb_of_item_allowed' ][0] ) ) {
253
				$steps[] = __('Cart', 'wpshop');
254
			}
255
			else {
256
				$no_cart = true;
0 ignored issues
show
Unused Code introduced by
$no_cart is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
257
			}
258
259
			$steps[] = __('Identification', 'wpshop');
260
			$steps[] = __('Addresses', 'wpshop');
261
262
			$no_shipping = false;
0 ignored issues
show
Unused Code introduced by
$no_shipping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
263
			if ( !empty( $shipping_address_option ) && !empty( $shipping_address_option[ 'activate' ] ) && !empty( $shipping_address_option[ 'choice' ] ) ) {
264
				$steps[] = __('Shipping Mode', 'wpshop');
265
			}
266
			else {
267
				$no_shipping = true;
0 ignored issues
show
Unused Code introduced by
$no_shipping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
268
			}
269
270
			$no_payment = false;
0 ignored issues
show
Unused Code introduced by
$no_payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
271
			$partial_payment_for_quotation = get_option('wpshop_payment_partial', array('for_quotation' => array()));
0 ignored issues
show
Documentation introduced by
array('for_quotation' => array()) is of type array<string,array,{"for_quotation":"array"}>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
272
			if( !empty($_SESSION) && !empty( $_SESSION['cart'] ) && ( ( !empty($_SESSION['cart']['order_amount_to_pay_now']) && number_format( $_SESSION['cart']['order_amount_to_pay_now'], 2, '.', '' ) == '0.00' ) || ( !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation' && isset( $partial_payment_for_quotation['for_quotation']['activate'] ) ) ) ) {
273
				$no_payment = true;
0 ignored issues
show
Unused Code introduced by
$no_payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
274
			}
275
			else {
276
				$steps[] = __('Payment', 'wpshop');
277
			}
278
279
			$steps[] = __('Confirmation', 'wpshop');
280
281
			$steps = apply_filters('wps_extra_action_checkout_indicator', $steps);
0 ignored issues
show
Unused Code introduced by
$steps is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
282
283
			require_once( wpshop_tools::get_template_part( WPS_CLASSIC_CHECKOUT_DIR, $this->template_dir,"frontend", "checkout_step_indicator/checkout_step_indicator") );
284
		}
285
286
		/**
287
		 * AJAX - Valid Checkout Step three
288
		 */
289
		function wps_checkout_valid_step_three() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
290
			$_wpnonce = !empty( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
291
292
			if ( !wp_verify_nonce( $_wpnonce, 'wps_checkout_valid_step_three' ) )
293
				wp_die();
294
295
			$response = ''; $status = true;
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
296
297
			$shipping_address = ( !empty($_POST['shipping_address_id']) ) ? wpshop_tools::varSanitizer( $_POST['shipping_address_id'] ): null;
298
			$billing_address = ( !empty($_POST['billing_address_id']) ) ? wpshop_tools::varSanitizer( $_POST['billing_address_id'] ): null;
299
300
			$user_id = get_current_user_id();
301
302
			$response = '<div class="wps-alert-error"><ul>';
303
304
			if( $user_id != 0 ) {
305
				$shipping_option = get_option( 'wpshop_shipping_address_choice' );
306
				$billing_option = get_option( 'wpshop_billing_address' );
307
				$user_addresses = wps_address::get_addresses_list( $user_id );
308
309
				// Check if is only downloadable else display address
310
				$cart_is_downloadable = false;
311
				if ( !empty( $_SESSION['cart'] ) && !empty( $_SESSION['cart']['order_items'] ) ) {
312
					foreach( $_SESSION['cart']['order_items'] as $c ) {
313
						$product = wpshop_products::get_product_data( $c['item_id'] );
314
						/** Check if it's a variation and check the parent product **/
315
						if ( get_post_type( $c['item_id'] ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) {
316
							$parent_def = wpshop_products::get_parent_variation( $c['item_id'] );
317
							if ( !empty($parent_def) && !empty($parent_def['parent_post_meta']) && !empty($parent_def['parent_post_meta']['is_downloadable_']) ) {
318
								$product['is_downloadable_'] = $parent_def['parent_post_meta']['is_downloadable_'];
319
							}
320
						}
321
						if( !empty($product['is_downloadable_']) && ( __( $product['is_downloadable_'], 'wpshop') == __('Yes', 'wpshop') || __( $product['is_downloadable_'], 'wpshop') == __('yes', 'wpshop') ) ) {
322
							$cart_is_downloadable = true;
323
						} else {
324
							$cart_is_downloadable = false;
325
							break;
326
						}
327
					}
328
				}
329
330
				if( !empty($shipping_option) && !empty($shipping_option['activate']) && !$cart_is_downloadable ) {
331
					/** Check Shipping address **/
332 View Code Duplication
					if ( empty($shipping_address) ) {
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...
333
						$status = false;
334
						/** Check if user have already create a shipping address **/
335
						if ( !empty($shipping_option['choice']) && !empty($user_addresses) && !empty($user_addresses[ $shipping_option['choice'] ]) ){
336
							$response .= '<li>'.__( 'You must select a shipping address', 'wpshop' ).'</li>';
337
						}
338
						else {
339
							$response .= '<li>'.__( 'You must create a shipping address', 'wpshop' ).'</li>';
340
						}
341
					}
342
343
				}
344
				/** Check Billing address **/
345 View Code Duplication
				if( empty($billing_address) ) {
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...
346
					$status = false;
347
					if ( !empty($billing_option['choice']) && !empty($user_addresses) && !empty($user_addresses[ $billing_option['choice'] ]) ){
348
						$response .= '<li>'.__( 'You must select a billing address', 'wpshop' ).'</li>';
349
					}
350
					else {
351
						$response .= '<li>'.__( 'You must create a billing address', 'wpshop' ).'</li>';
352
					}
353
				}
354
			}
355
			else {
356
				$status = false;
357
				$response .= '<li>'.__( 'You must be logged to pass to next step', 'wpshop' ).'</li>';
358
			}
359
			$response .= '</ul></div>';
360
361
			/** If no error **/
362
			if( $status ) {
363
				$_SESSION['shipping_address'] = $shipping_address;
364
				$_SESSION['billing_address'] = $billing_address;
365
366
				$permalink_option = get_option( 'permalink_structure' );
367
				$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
368
				/** Checking if no shipping method is required and it is a quotation or a free order **/
369
				$shipping_option = get_option( 'wps_shipping_mode' );
370
371
				/** Quotation, no shipping, no payment */
372
				if ( !empty($_SESSION) && !empty( $_SESSION['cart'] ) && !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation') {
373
					$status = true;
374
					$_SESSION['shipping_method'] = 'No Shipping method required';
375
					$payment_method = $_SESSION['payment_method'] = 'quotation';
376
					$order_id = wpshop_checkout::process_checkout( $payment_method, ( !empty($_SESSION['cart']['order_id']) ) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address'] );
0 ignored issues
show
Unused Code introduced by
$order_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
377
					$response = get_permalink( wpshop_tools::get_page_id( $checkout_page_id )  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=6';
378
				} else {
379
					$available_shipping_method = false;
380
					if( !empty($shipping_option) && !empty($shipping_option['modes']) && !$cart_is_downloadable ) {
0 ignored issues
show
Bug introduced by
The variable $cart_is_downloadable does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
381
						foreach( $shipping_option['modes'] as $shipping_mode_id => $shipping_mode ) {
0 ignored issues
show
Bug introduced by
The expression $shipping_option['modes'] of type string is not traversable.
Loading history...
382
							if( !empty($shipping_mode['active']) && $shipping_mode['active'] == 'on' ) {
383
								$available_shipping_method = true;
384
							}
385
						}
386
					}
387
388
					if( !$available_shipping_method ) {
389
						$_SESSION['shipping_method'] = 'No Shipping method required';
390
						$order_id = ( !empty($_SESSION['cart']['order_id']) ) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0;
391
392
						if( !empty($_SESSION) && !empty( $_SESSION['cart'] ) && isset($_SESSION['cart']['order_amount_to_pay_now']) && number_format( $_SESSION['cart']['order_amount_to_pay_now'], 2, '.', '' ) == '0.00' ) {
393
							$status = true;
394
							$payment_method = $_SESSION['payment_method'] = 'free';
395
							$order_id = wpshop_checkout::process_checkout( $payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address'] );
0 ignored issues
show
Unused Code introduced by
$order_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
396
							$permalink_option = get_option( 'permalink_structure' );
397
							$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
398
							$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=6';
399
	// 						wpshop_tools::wpshop_safe_redirect( $url );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
400
							$response = $url;
401
						}
402 View Code Duplication
						else {
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...
403
							$status = true;
404
							$response = get_permalink( wpshop_tools::get_page_id( $checkout_page_id )  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=5';
405
						}
406
					}
407 View Code Duplication
					else {
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...
408
						$status = true;
409
						$response = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=4';
410
					}
411
				}
412
			}
413
			//Stock checking verification
414
			$this->checking_stock();
415
416
			wp_die( json_encode( array( 'status' => $status, 'response' => $response ) ) );
417
		}
418
419
		/**
420
		 * AJAX - Valid Checkout step four
421
		 */
422
		function wps_checkout_valid_step_four() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
423
			$_wpnonce = !empty( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
424
425
			if ( !wp_verify_nonce( $_wpnonce, 'wps_checkout_valid_step_four' ) )
426
				wp_die();
427
428
			$shipping_method = ( !empty($_POST['shipping_mode']) ) ? wpshop_tools::varSanitizer($_POST['shipping_mode']) : null;
429
			$status = false;
430
			$response = '';
431
			$permalink_option = get_option( 'permalink_structure' );
432
			$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
433
			if ( !empty($shipping_method) ) {
434
				$status = true;
435
				$_SESSION['shipping_method'] = $shipping_method;
436
				$order_id = ( !empty($_SESSION['cart']['order_id']) ) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0;
437
				if ( !empty($_SESSION) && !empty( $_SESSION['cart'] ) && !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation') {
438
					$partial_payment_for_quotation = get_option('wpshop_payment_partial', array('for_quotation' => array()));
0 ignored issues
show
Documentation introduced by
array('for_quotation' => array()) is of type array<string,array,{"for_quotation":"array"}>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
439
					if( isset( $partial_payment_for_quotation['for_quotation']['activate'] ) ) {
440
						$step = '5';
441
					} else {
442
						$payment_method = $_SESSION['payment_method'] = 'quotation';
443
						$order_id = wpshop_checkout::process_checkout( $payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address'] );
0 ignored issues
show
Unused Code introduced by
$order_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
444
						$step = '6';
445
					}
446
					$response = get_permalink( wpshop_tools::get_page_id( $checkout_page_id )  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step='.$step;
447
				}
448
				elseif( !empty($_SESSION) && !empty( $_SESSION['cart'] ) && !empty($_SESSION['cart']['order_amount_to_pay_now']) && number_format( $_SESSION['cart']['order_amount_to_pay_now'], 2, '.', '' ) == '0.00' ) {
449
					$payment_method = $_SESSION['payment_method'] = 'free';
450
					$order_id = wpshop_checkout::process_checkout( $payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address'] );
0 ignored issues
show
Unused Code introduced by
$order_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
451
					$permalink_option = get_option( 'permalink_structure' );
452
					$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
453
					$url = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=6';
454
					$response = $url;
455
				}
456 View Code Duplication
				else {
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...
457
					$response = get_permalink( wpshop_tools::get_page_id( $checkout_page_id )  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=5';
458
				}
459
			}
460
			else {
461
				$response .= '<div class="wps-alert-error">'.__( 'You must select a shipping method', 'wpshop' ).'</div>';
462
			}
463
464
			//Stock checking verification
465
			$this->checking_stock();
466
467
			echo json_encode( array( 'status' => $status, 'response' => $response) );
468
			die();
469
		}
470
471
		/**
472
		 * AJAX - Valid Checkout step four
473
		 */
474
		function wps_checkout_valid_step_five() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
475
			$_wpnonce = !empty( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
476
			if ( !wp_verify_nonce( $_wpnonce, 'wps_checkout_valid_step_five' ) )
477
				wp_die();
478
479
			$status = false;
480
			$response = '';
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
481
			$payment_method = ( !empty($_POST['wps-payment-method']) ) ? wpshop_tools::varSanitizer( $_POST['wps-payment-method'] ): null;
482
			$order_id = ( !empty($_SESSION['cart']['order_id']) ) ? (int) $_SESSION['cart']['order_id'] : 0;
483
			$customer_comment = ( !empty($_POST['wps-customer-comment']) ) ? wpshop_tools::varSanitizer( $_POST['wps-customer-comment'] ) : null;
484
485
			$terms_of_sale_required = isset( $_POST['terms_of_sale_indicator'] ) && !empty( $_POST['terms_of_sale_indicator'] ) ? (bool)$_POST['terms_of_sale_indicator'] : (bool)false;
486
			$terms_of_sale_checked = isset( $_POST['terms_of_sale'] ) && !empty( $_POST['terms_of_sale'] ) ? (bool)$_POST['terms_of_sale_indicator'] : (bool)false;
487
488
			if ( ( $terms_of_sale_required && $terms_of_sale_checked ) || !$terms_of_sale_required ) {
489
				if ( !empty($payment_method) ) {
490
					/** Check if the payment method exist for the shop **/
491
					$payment_option = get_option( 'wps_payment_mode' );
492
493
					if( !empty($payment_option) && !empty($payment_option['mode']) && array_key_exists( $payment_method, $payment_option['mode']) && !empty($payment_option['mode'][$payment_method]['active']) ) {
494
						if( !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation' ) {
495
							$new_payment_method = $payment_method;
496
							$payment_method = 'quotation';
497
							$is_quotation = true;
498
						} else {
499
							$is_quotation = false;
500
						}
501
						$order_id = wpshop_checkout::process_checkout( $payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address'] );
502
						if( !empty($order_id) && !empty($customer_comment) ) {
503
							$wps_back_office_orders_mdl = new wps_back_office_orders_mdl();
504
							$wps_back_office_orders_mdl->add_private_comment($order_id, $customer_comment);
505
							//wp_update_post( array('ID' => $order_id, 'post_excerpt' => $customer_comment) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
506
						}
507
						$permalink_option = get_option( 'permalink_structure' );
508
						$checkout_page_id = wpshop_tools::get_page_id( get_option( 'wpshop_checkout_page_id' ) );
509
						$response = get_permalink( $checkout_page_id  ).( ( !empty($permalink_option) ) ? '?' : '&').'order_step=6';
510
						if( $is_quotation ) {
511
							$order_meta = get_post_meta( $order_id, '_order_postmeta', true );
512
							$params = array( 'method' => $new_payment_method, 'waited_amount' => $order_meta['order_partial_payment'], 'status' => 'waiting_payment', 'author' => get_current_user_id() );
0 ignored issues
show
Bug introduced by
The variable $new_payment_method does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
513
							$order_meta['order_payment']['received'][0] = $params;
514
							update_post_meta( $order_id, '_order_postmeta', $order_meta );
515
							$_SESSION['payment_method'] = $new_payment_method;
516
						} else {
517
							$_SESSION['payment_method'] = $payment_method;
518
						}
519
						$status = true;
520
						//Add an action to extra actions on order save
521
						// @TODO : REQUEST
522
						$args = array( 'order_id' => $order_id, 'posted_data' => $_REQUEST);
523
						wpshop_tools::create_custom_hook( 'wps_order_extra_save_action', $args );
524
					}
525
					else {
526
						$response = '<div class="wps-alert-error">' .__( 'This payment method is unavailable', 'wpshop' ).'</div>';
527
					}
528
				}
529
				else {
530
					$response = '<div class="wps-alert-error">' .__( 'You must choose a payment method', 'wpshop' ).'</div>';
531
				}
532
			}
533
			else {
534
				$response = '<div class="wps-alert-error">' .__( 'You must accept the terms of sale to order', 'wpshop' ).'</div>';
535
			}
536
537
			echo json_encode( array('status' => $status, 'response' => $response) );
538
			die();
539
		}
540
541
		/**
542
		 * Checking stock in differents checkout steps
543
		 */
544
		function checking_stock() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
545
			if( !empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) ) {
546
				foreach( $_SESSION['cart']['order_items'] as $item_id => $item ) {
547
					$wps_product = new wps_product_ctr();
548
					$checking = $wps_product->check_stock( $item['item_id'], $item['item_qty'], $item_id );
549
					if( $checking !== true ) {
550
						unset(  $_SESSION['cart']['order_items'][$item_id] );
551
						$wps_cart_ctr = new wps_cart();
552
						$order = $wps_cart_ctr->calcul_cart_information( array() );
553
						$wps_cart_ctr->store_cart_in_session( $order );
554
					}
555
				}
556
			}
557
		}
558
559
	}
560
}
561
if ( class_exists("wps_classic_checkout") ) {
562
	$wps_classic_checkout = new wps_classic_checkout();
563
}
564