Completed
Push — es6/issue-1475 ( 93c1ad )
by Ravinder
1139:39 queued 1133:44
created

Give_Settings_Gateways::render_enabled_gateways()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 76
rs 8.2125
c 0
b 0
f 0

How to fix   Long Method   

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
 * Give Settings Page/Tab
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Settings_Gateways
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit; // Exit if accessed directly
14
}
15
16
if ( ! class_exists( 'Give_Settings_Gateways' ) ) :
17
18
	/**
19
	 * Give_Settings_Gateways.
20
	 *
21
	 * @sine 1.8
22
	 */
23
	class Give_Settings_Gateways extends Give_Settings_Page {
24
25
		/**
26
		 * Constructor.
27
		 */
28
		public function __construct() {
29
			$this->id    = 'gateways';
30
			$this->label = esc_html__( 'Payment Gateways', 'give' );
31
32
			$this->default_tab = 'gateways-settings';
33
34
			parent::__construct();
35
36
			// Do not use main form for this tab.
37
			if ( give_get_current_setting_tab() === $this->id ) {
38
				add_action( 'give_admin_field_enabled_gateways', array( $this, 'render_enabled_gateways' ), 10, 2 );
39
			}
40
		}
41
42
		/**
43
		 * Get settings array.
44
		 *
45
		 * @since  1.8
46
		 * @return array
47
		 */
48
		public function get_settings() {
49
			$settings        = array();
50
			$current_section = give_get_current_setting_section();
51
52
			switch ( $current_section ) {
53
				case 'paypal-standard':
54
					$settings = array(
55
						// Section 2: PayPal Standard.
56
						array(
57
							'type' => 'title',
58
							'id'   => 'give_title_gateway_settings_2',
59
						),
60
						array(
61
							'name' => __( 'PayPal Email', 'give' ),
62
							'desc' => __( 'Enter your PayPal account\'s email.', 'give' ),
63
							'id'   => 'paypal_email',
64
							'type' => 'email',
65
						),
66
						array(
67
							'name' => __( 'PayPal Page Style', 'give' ),
68
							'desc' => __( 'Enter the name of the PayPal page style to use, or leave blank to use the default.', 'give' ),
69
							'id'   => 'paypal_page_style',
70
							'type' => 'text',
71
						),
72
						array(
73
							'name'    => __( 'PayPal Transaction Type', 'give' ),
74
							'desc'    => __( 'Nonprofits must verify their status to withdraw donations they receive via PayPal. PayPal users that are not verified nonprofits must demonstrate how their donations will be used, once they raise more than $10,000. By default, Give transactions are sent to PayPal as donations. You may change the transaction type using this option if you feel you may not meet PayPal\'s donation requirements.', 'give' ),
75
							'id'      => 'paypal_button_type',
76
							'type'    => 'radio_inline',
77
							'options' => array(
78
								'donation' => __( 'Donation', 'give' ),
79
								'standard' => __( 'Standard Transaction', 'give' )
80
							),
81
							'default' => 'donation',
82
						),
83
						array(
84
							'name'    => __( 'Billing Details', 'give' ),
85
							'desc'    => __( 'This option will enable the billing details section for PayPal Standard which requires the donor\'s address to complete the donation. These fields are not required by PayPal to process the transaction, but you may have a need to collect the data.', 'give' ),
86
							'id'      => 'paypal_standard_billing_details',
87
							'type'    => 'radio_inline',
88
							'default' => 'disabled',
89
							'options' => array(
90
								'enabled'  => __( 'Enabled', 'give' ),
91
								'disabled' => __( 'Disabled', 'give' ),
92
							)
93
						),
94
						array(
95
							'name'    => __( 'PayPal IPN Verification', 'give' ),
96
							'desc'    => __( 'If donations are not getting marked as complete, use a slightly less secure method of verifying donations.', 'give' ),
97
							'id'      => 'paypal_verification',
98
							'type'    => 'radio_inline',
99
							'default' => 'enabled',
100
							'options' => array(
101
								'enabled'  => __( 'Enabled', 'give' ),
102
								'disabled' => __( 'Disabled', 'give' ),
103
							)
104
						),
105
						array(
106
							'name'  => __( 'PayPal Standard Gateway Settings Docs Link', 'give' ),
107
							'id'    => 'paypal_standard_gateway_settings_docs_link',
108
							'url'   => esc_url( 'http://docs.givewp.com/settings-gateway-paypal-standard' ),
109
							'title' => __( 'PayPal Standard Gateway Settings', 'give' ),
110
							'type'  => 'give_docs_link',
111
						),
112
						array(
113
							'type' => 'sectionend',
114
							'id'   => 'give_title_gateway_settings_2',
115
						)
116
					);
117
					break;
118
119
				case 'offline-donations' :
120
					$settings = array(
121
						// Section 3: Offline gateway.
122
						array(
123
							'type' => 'title',
124
							'id'   => 'give_title_gateway_settings_3',
125
						),
126
						array(
127
							'name'    => __( 'Collect Billing Details', 'give' ),
128
							'desc'    => __( 'Enable to request billing details for offline donations. Will appear above offline donation instructions. Can be enabled/disabled per form.', 'give' ),
129
							'id'      => 'give_offline_donation_enable_billing_fields',
130
							'type'    => 'radio_inline',
131
							'default' => 'disabled',
132
							'options' => array(
133
								'enabled'  => __( 'Enabled', 'give' ),
134
								'disabled' => __( 'Disabled', 'give' )
135
							)
136
						),
137
						array(
138
							'name'    => __( 'Offline Donation Instructions', 'give' ),
139
							'desc'    => __( 'The following content will appear for all forms when the user selects the offline donation payment option. Note: You may customize the content per form as needed.', 'give' ),
140
							'id'      => 'global_offline_donation_content',
141
							'default' => give_get_default_offline_donation_content(),
142
							'type'    => 'wysiwyg',
143
							'options' => array(
144
								'textarea_rows' => 6,
145
							)
146
						),
147
						array(
148
							'name'  => esc_html__( 'Offline Donations Settings Docs Link', 'give' ),
149
							'id'    => 'offline_gateway_settings_docs_link',
150
							'url'   => esc_url( 'http://docs.givewp.com/offlinegateway' ),
151
							'title' => __( 'Offline Gateway Settings', 'give' ),
152
							'type'  => 'give_docs_link',
153
						),
154
						array(
155
							'type' => 'sectionend',
156
							'id'   => 'give_title_gateway_settings_3',
157
						)
158
					);
159
					break;
160
161
				case 'gateways-settings':
162
					$settings = array(
163
						// Section 1: Gateways.
164
						array(
165
							'id'   => 'give_title_gateway_settings_1',
166
							'type' => 'title'
167
						),
168
						array(
169
							'name'    => __( 'Test Mode', 'give' ),
170
							'desc'    => __( 'While in test mode no live donations are processed. To fully use test mode, you must have a sandbox (test) account for the payment gateway you are testing.', 'give' ),
171
							'id'      => 'test_mode',
172
							'type'    => 'radio_inline',
173
							'default' => 'disabled',
174
							'options' => array(
175
								'enabled'  => __( 'Enabled', 'give' ),
176
								'disabled' => __( 'Disabled', 'give' ),
177
							)
178
						),
179
						array(
180
							'name' => __( 'Enabled Gateways', 'give' ),
181
							'desc' => __( 'Enable your payment gateway. Can be ordered by dragging.', 'give' ),
182
							'id'   => 'gateways',
183
							'type' => 'enabled_gateways'
184
						),
185
186
						/**
187
						 * "Enabled Gateways" setting field contains gateways label setting but when you save gateway settings then label will not save
188
						 *  because this is not registered setting API and code will not recognize them.
189
						 *
190
						 * This setting will not render on admin setting screen but help internal code to recognize "gateways_label"  setting and add them to give setting when save.
191
						 */
192
						array(
193
							'name' => __( 'Gateways Label', 'give' ),
194
							'desc' => '',
195
							'id'   => 'gateways_label',
196
							'type' => 'gateways_label_hidden'
197
						),
198
199
						/**
200
						 * "Enabled Gateways" setting field contains default gateway setting but when you save gateway settings then this setting will not save
201
						 *  because this is not registered setting API and code will not recognize them.
202
						 *
203
						 * This setting will not render on admin setting screen but help internal code to recognize "default_gateway"  setting and add them to give setting when save.
204
						 */
205
						array(
206
							'name' => __( 'Default Gateway', 'give' ),
207
							'desc' => __( 'The gateway that will be selected by default.', 'give' ),
208
							'id'   => 'default_gateway',
209
							'type' => 'default_gateway_hidden'
210
						),
211
212
						array(
213
							'name'  => __( 'Gateways Docs Link', 'give' ),
214
							'id'    => 'gateway_settings_docs_link',
215
							'url'   => esc_url( 'http://docs.givewp.com/settings-gateways' ),
216
							'title' => __( 'Gateway Settings', 'give' ),
217
							'type'  => 'give_docs_link',
218
						),
219
						array(
220
							'id'   => 'give_title_gateway_settings_1',
221
							'type' => 'sectionend'
222
						),
223
					);
224
					break;
225
			}
226
227
			/**
228
			 * Filter the payment gateways settings.
229
			 * Backward compatibility: Please do not use this filter. This filter is deprecated in 1.8
230
			 */
231
			$settings = apply_filters( 'give_settings_gateways', $settings );
232
233
			/**
234
			 * Filter the settings.
235
			 *
236
			 * @since  1.8
237
			 *
238
			 * @param  array $settings
239
			 */
240
			$settings = apply_filters( 'give_get_settings_' . $this->id, $settings );
241
242
			// Output.
243
			return $settings;
244
		}
245
246
		/**
247
		 * Get sections.
248
		 *
249
		 * @since 1.8
250
		 * @return array
251
		 */
252
		public function get_sections() {
253
			$sections = array(
254
				'gateways-settings' => __( 'Gateways', 'give' ),
255
				'paypal-standard'   => __( 'PayPal Standard', 'give' ),
256
				'offline-donations' => __( 'Offline Donations', 'give' )
257
			);
258
259
			return apply_filters( 'give_get_sections_' . $this->id, $sections );
260
		}
261
262
263
		/**
264
		 * Render enabled gateways
265
		 *
266
		 * @since  2.0.5
267
		 * @access public
268
		 *
269
		 * @param $field
270
		 * @param $settings
271
		 */
272
		public function render_enabled_gateways( $field, $settings ) {
273
			$id              = $field['id'];
274
			$gateways        = give_get_ordered_payment_gateways( give_get_payment_gateways() );
275
			$gateways_label  = give_get_option( 'gateways_label', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|boolean.

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...
276
			$default_gateway = give_get_option( 'default_gateway', current( array_keys( $gateways ) ) );
277
278
			ob_start();
279
280
			echo '<div class="gateway-enabled-wrap">';
281
282
			echo '<div class="gateway-enabled-settings-title">';
283
			printf(
284
				'
285
						<span></span>
286
						<span>%1$s</span>
287
						<span>%2$s</span>
288
						<span>%3$s</span>
289
						<span>%4$s</span>
290
						',
291
				__( 'Gateway', 'give' ),
292
				__( 'Label', 'give' ),
293
				__( 'Default', 'give' ),
294
				__( 'Enabled', 'give' )
295
			);
296
			echo '</div>';
297
298
			echo '<ul class="give-checklist-fields give-payment-gatways-list">';
299
			foreach ( $gateways as $key => $option ) :
300
				$enabled = null;
301
				if ( is_array( $settings ) && array_key_exists( $key, $settings ) ) {
302
					$enabled = '1';
303
				}
304
305
				echo '<li>';
306
				printf( '<span class="give-drag-handle"><span class="dashicons dashicons-menu"></span></span>' );
307
				printf( '<span class="admin-label">%s</span>', esc_html( $option['admin_label'] ) );
308
309
				$label = '';
310
				if ( ! empty( $gateways_label[ $key ] ) ) {
311
					$label = $gateways_label[ $key ];
312
				}
313
314
				printf(
315
					'<input class="checkout-label" type="text" id="%1$s[%2$s]" name="%1$s[%2$s]" value="%3$s" placeholder="%4$s"/>',
316
					'gateways_label',
317
					esc_attr( $key ),
318
					esc_html( $label ),
319
					esc_html( $option['checkout_label'] )
320
				);
321
322
				printf(
323
					'<input class="gateways-radio" type="radio" name="%1$s" value="%2$s" %3$s>',
324
					'default_gateway',
325
					$key,
326
					checked( $key, $default_gateway, false )
327
				);
328
329
				printf(
330
					'<input class="gateways-checkbox" name="%1$s[%2$s]" id="%1$s[%2$s]" type="checkbox" value="1" %3$s data-payment-gateway="%4$s"/>',
331
					esc_attr( $id ),
332
					esc_attr( $key ),
333
					checked( '1', $enabled, false ),
334
					esc_html( $option['admin_label'] )
335
				);
336
				echo '</li>';
337
			endforeach;
338
			echo '</ul>';
339
340
			echo '</div>'; // end gateway-enabled-wrap.
341
342
			printf(
343
				'<tr><th>%1$s</th><td>%2$s</td></tr>',
344
				$field['title'],
345
				ob_get_clean()
346
			);
347
		}
348
	}
349
350
endif;
351
352
return new Give_Settings_Gateways();
353