Completed
Pull Request — master (#9770)
by Jeff
10:37
created

WC_Payment_Gateways::set_current_gateway()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 6.7273
cc 7
eloc 10
nc 5
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * WooCommerce Payment Gateways class
9
 *
10
 * Loads payment gateways via hooks for use in the store.
11
 *
12
 * @class 		WC_Payment_Gateways
13
 * @version		2.2.0
14
 * @package		WooCommerce/Classes/Payment
15
 * @category	Class
16
 * @author 		WooThemes
17
 */
18
class WC_Payment_Gateways {
19
20
	/** @var array Array of payment gateway classes. */
21
	public $payment_gateways;
22
23
	/**
24
	 * @var WC_Payment_Gateways The single instance of the class
25
	 * @since 2.1
26
	 */
27
	protected static $_instance = null;
28
29
	/**
30
	 * Main WC_Payment_Gateways Instance.
31
	 *
32
	 * Ensures only one instance of WC_Payment_Gateways is loaded or can be loaded.
33
	 *
34
	 * @since 2.1
35
	 * @static
36
	 * @return WC_Payment_Gateways Main instance
37
	 */
38
	public static function instance() {
39
		if ( is_null( self::$_instance ) ) {
40
			self::$_instance = new self();
41
		}
42
		return self::$_instance;
43
	}
44
45
	/**
46
	 * Cloning is forbidden.
47
	 *
48
	 * @since 2.1
49
	 */
50
	public function __clone() {
51
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
52
	}
53
54
	/**
55
	 * Unserializing instances of this class is forbidden.
56
	 *
57
	 * @since 2.1
58
	 */
59
	public function __wakeup() {
60
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
61
	}
62
63
	/**
64
	 * Initialize payment gateways.
65
	 */
66
	public function __construct() {
67
		$this->init();
68
	}
69
70
	/**
71
	 * Load gateways and hook in functions.
72
	 */
73
	public function init() {
74
		$load_gateways = array(
75
			'WC_Gateway_BACS',
76
			'WC_Gateway_Cheque',
77
			'WC_Gateway_COD',
78
			'WC_Gateway_Paypal',
79
		);
80
81
		$simplify_countries = (array) apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) );
82
83
		if ( in_array( WC()->countries->get_base_country(), $simplify_countries ) ) {
84
			if ( class_exists( 'WC_Subscriptions_Order' ) || class_exists( 'WC_Pre_Orders_Order' ) ) {
85
				if ( ! function_exists( 'wcs_create_renewal_order' ) ) { // Subscriptions < 2.0
86
					$load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce_Deprecated';
87
				} else {
88
					$load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce';
89
				}
90
			} else {
91
				$load_gateways[] = 'WC_Gateway_Simplify_Commerce';
92
			}
93
		}
94
95
		// Filter
96
		$load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways );
97
98
		// Get sort order option
99
		$ordering  = (array) get_option( 'woocommerce_gateway_order' );
100
		$order_end = 999;
101
102
		// Load gateways in order
103
		foreach ( $load_gateways as $gateway ) {
104
			$load_gateway = is_string( $gateway ) ? new $gateway() : $gateway;
105
106
			if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) {
107
				// Add in position
108
				$this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway;
109
			} else {
110
				// Add to end of the array
111
				$this->payment_gateways[ $order_end ] = $load_gateway;
112
				$order_end++;
113
			}
114
		}
115
116
		ksort( $this->payment_gateways );
117
	}
118
119
	/**
120
	 * Get gateways.
121
	 *
122
	 * @access public
123
	 * @return array
124
	 */
125
	public function payment_gateways() {
126
		$_available_gateways = array();
127
128
		if ( sizeof( $this->payment_gateways ) > 0 ) {
129
			foreach ( $this->payment_gateways as $gateway ) {
130
				$_available_gateways[ $gateway->id ] = $gateway;
131
			}
132
		}
133
134
		return $_available_gateways;
135
	}
136
137
	/**
138
	 * Get available gateways.
139
	 *
140
	 * @access public
141
	 * @return array
142
	 */
143
	public function get_available_payment_gateways() {
144
		$_available_gateways = array();
145
146
		foreach ( $this->payment_gateways as $gateway ) {
147
			if ( $gateway->is_available() ) {
148
				if ( ! is_add_payment_method_page() ) {
149
					$_available_gateways[ $gateway->id ] = $gateway;
150
				} elseif( $gateway->supports( 'add_payment_method' ) ) {
151
					$_available_gateways[ $gateway->id ] = $gateway;
152
				}
153
			}
154
		}
155
156
		return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways );
157
	}
158
159
	/**
160
	 * Set the current, active gateway.
161
	 *
162
	 * @access public
163
	 * @param array $gateway Available payment gateways.
0 ignored issues
show
Documentation introduced by
There is no parameter named $gateway. Did you maybe mean $gateways?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
164
	 */
165
	public function set_current_gateway( $gateways ) {
166
167
		// Be on the defensive
168
		if ( ! is_array( $gateways ) || empty( $gateways ) ) {
169
170
			return;
171
172
		}
173
174
		$current = WC()->session->get( 'chosen_payment_method' );
175
176
		if ( $current && isset( $gateways[ $current ] ) ) {
177
178
			$current_gateway = $gateways[ $current ];
179
180
		} else {
181
182
			$current_gateway = current( $gateways );
183
184
		}
185
186
		// Ensure we can make a call to set_current() without triggering an error
187
		if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) {
188
189
			$current_gateway->set_current();
190
191
		}
192
193
	}
194
195
	/**
196
	 * Save options in admin.
197
	 */
198
	public function process_admin_options() {
199
		$gateway_order = isset( $_POST['gateway_order'] ) ? $_POST['gateway_order'] : '';
200
		$order         = array();
201
202
		if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) {
203
			$loop = 0;
204
			foreach ( $gateway_order as $gateway_id ) {
205
				$order[ esc_attr( $gateway_id ) ] = $loop;
206
				$loop++;
207
			}
208
		}
209
210
		update_option( 'woocommerce_gateway_order', $order );
211
	}
212
}
213