Test Failed
Push — backup/issue/4010 ( 70b4ce )
by Ravinder
10:16 queued 10s
created

actions.php ➔ give_stripe_credit_card_form()   D

Complexity

Conditions 16
Paths 97

Size

Total Lines 178

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 97
nop 3
dl 0
loc 178
rs 4.4532
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
 * Give - Stripe Frontend Actions
4
 *
5
 * @since 2.5.0
6
 *
7
 * @package    Give
8
 * @subpackage Stripe Core
9
 * @copyright  Copyright (c) 2019, GiveWP
10
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
11
 */
12
13
// Exit, if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Stripe uses it's own credit card form because the card details are tokenized.
20
 *
21
 * We don't want the name attributes to be present on the fields in order to
22
 * prevent them from getting posted to the server.
23
 *
24
 * @param int  $form_id Donation Form ID.
25
 * @param int  $args    Donation Form Arguments.
26
 * @param bool $echo    Status to display or not.
27
 *
28
 * @access public
29
 * @since  1.0
30
 *
31
 * @return string $form
32
 */
33
function give_stripe_credit_card_form( $form_id, $args, $echo = true ) {
34
35
	// No CC or billing fields for Stripe Checkout.
36
	if ( give_stripe_is_checkout_enabled() ) {
37
		return false;
38
	}
39
40
	$id_prefix = ! empty( $args['id_prefix'] ) ? $args['id_prefix'] : '';
41
42
	$fallback_option    = give_get_option( 'stripe_js_fallback' );
43
	$stripe_js_fallback = ! empty( $fallback_option );
0 ignored issues
show
Unused Code introduced by
$stripe_js_fallback 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...
44
45
	$stripe_cc_field_format = give_get_option( 'stripe_cc_fields_format', 'multi' );
46
47
	// Get User Agent.
48
	$user_agent = give_get_user_agent();
0 ignored issues
show
Unused Code introduced by
$user_agent 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...
49
50
	ob_start();
51
52
	do_action( 'give_before_cc_fields', $form_id ); ?>
53
54
	<fieldset id="give_cc_fields" class="give-do-validate">
55
		<legend>
56
			<?php esc_attr_e( 'Credit Card Info', 'give' ); ?>
57
		</legend>
58
59
		<?php
60
		if ( is_ssl() ) {
61
			?>
62
			<div id="give_secure_site_wrapper">
63
				<span class="give-icon padlock"></span>
64
				<span>
65
					<?php esc_attr_e( 'This is a secure SSL encrypted payment.', 'give' ); ?>
66
				</span>
67
			</div>
68
			<?php
69
		}
70
71
		if (
72
				(
73
					! is_ssl() &&
74
					! give_is_test_mode() &&
75
					(
76
						empty( give_stripe_get_publishable_key() ) ||
77
						empty( give_stripe_get_secret_key() )
78
					)
79
				)
80
			) {
81
					Give()->notices->print_frontend_notice(
82
						sprintf(
83
							'<strong>%1$s</strong> %2$s',
84
							esc_html__( 'Notice:', 'give' ),
85
							esc_html__( 'Credit card fields are disabled because Stripe is not connected and your site is not running securely over HTTPS.', 'give' )
86
						)
87
					);
88
		} elseif (
89
			empty( give_stripe_get_publishable_key() ) ||
90
			empty( give_stripe_get_secret_key() )
91
		) {
92
			Give()->notices->print_frontend_notice(
93
				sprintf(
94
					'<strong>%1$s</strong> %2$s',
95
					esc_html__( 'Notice:', 'give' ),
96
					esc_html__( 'Credit card fields are disabled because Stripe is not connected.', 'give' )
97
				)
98
			);
99
		} elseif ( ! is_ssl() && ! give_is_test_mode() ) {
100
			Give()->notices->print_frontend_notice(
101
				sprintf(
102
					'<strong>%1$s</strong> %2$s',
103
					esc_html__( 'Notice:', 'give' ),
104
					esc_html__( 'Credit card fields are disabled because your site is not running securely over HTTPS.', 'give' )
105
				)
106
			);
107
		} else {
108
			if ( 'single' === $stripe_cc_field_format ) {
109
110
				// Display the stripe container which can be occupied by Stripe for CC fields.
111
				echo '<div id="give-stripe-single-cc-fields-' . esc_html( $id_prefix ) . '" class="give-stripe-single-cc-field-wrap"></div>';
112
113
			} elseif ( 'multi' === $stripe_cc_field_format ) {
114
				?>
115
				<div id="give-card-number-wrap" class="form-row form-row-two-thirds form-row-responsive give-stripe-cc-field-wrap">
116
					<div>
117
						<label for="give-card-number-field-<?php echo esc_html( $id_prefix ); ?>" class="give-label">
118
							<?php esc_attr_e( 'Card Number', 'give' ); ?>
119
							<span class="give-required-indicator">*</span>
120
							<span class="give-tooltip give-icon give-icon-question"
121
								data-tooltip="<?php esc_attr_e( 'The (typically) 16 digits on the front of your credit card.', 'give' ); ?>"></span>
122
							<span class="card-type"></span>
123
						</label>
124
						<div id="give-card-number-field-<?php echo esc_html( $id_prefix ); ?>" class="input empty give-stripe-cc-field give-stripe-card-number-field"></div>
125
					</div>
126
				</div>
127
128
				<div id="give-card-cvc-wrap" class="form-row form-row-one-third form-row-responsive give-stripe-cc-field-wrap">
129
					<div>
130
						<label for="give-card-cvc-field-<?php echo esc_html( $id_prefix ); ?>" class="give-label">
131
							<?php esc_attr_e( 'CVC', 'give' ); ?>
132
							<span class="give-required-indicator">*</span>
133
							<span class="give-tooltip give-icon give-icon-question"
134
								data-tooltip="<?php esc_attr_e( 'The 3 digit (back) or 4 digit (front) value on your card.', 'give' ); ?>"></span>
135
						</label>
136
						<div id="give-card-cvc-field-<?php echo esc_html( $id_prefix ); ?>" class="input empty give-stripe-cc-field give-stripe-card-cvc-field"></div>
137
					</div>
138
				</div>
139
140
				<div id="give-card-name-wrap" class="form-row form-row-two-thirds form-row-responsive">
141
					<label for="card_name" class="give-label">
142
						<?php esc_attr_e( 'Cardholder Name', 'give' ); ?>
143
						<span class="give-required-indicator">*</span>
144
						<span class="give-tooltip give-icon give-icon-question"
145
							data-tooltip="<?php esc_attr_e( 'The name of the credit card account holder.', 'give' ); ?>"></span>
146
					</label>
147
					<input
148
						type="text"
149
						autocomplete="off"
150
						id="card_name"
151
						name="card_name"
152
						class="card-name give-input required"
153
						placeholder="<?php esc_attr_e( 'Cardholder Name', 'give' ); ?>"
154
					/>
155
				</div>
156
157
				<?php do_action( 'give_before_cc_expiration' ); ?>
158
159
				<div id="give-card-expiration-wrap" class="card-expiration form-row form-row-one-third form-row-responsive give-stripe-cc-field-wrap">
160
					<div>
161
						<label for="give-card-expiration-field-<?php echo esc_html( $id_prefix ); ?>" class="give-label">
162
							<?php esc_attr_e( 'Expiration', 'give' ); ?>
163
							<span class="give-required-indicator">*</span>
164
							<span class="give-tooltip give-icon give-icon-question"
165
								data-tooltip="<?php esc_attr_e( 'The date your credit card expires, typically on the front of the card.', 'give' ); ?>"></span>
166
						</label>
167
168
						<div id="give-card-expiration-field-<?php echo esc_html( $id_prefix ); ?>" class="input empty give-stripe-cc-field give-stripe-card-expiration-field"></div>
169
					</div>
170
				</div>
171
				<?php
172
			} // End if().
173
174
			/**
175
			 * This action hook is used to display content after the Credit Card expiration field.
176
			 *
177
			 * Note: Kept this hook as it is.
178
			 *
179
			 * @param int   $form_id Donation Form ID.
180
			 * @param array $args    List of additional arguments.
181
			 */
182
			do_action( 'give_after_cc_expiration', $form_id, $args );
183
184
			/**
185
			 * This action hook is used to display content after the Credit Card expiration field.
186
			 *
187
			 * @param int   $form_id Donation Form ID.
188
			 * @param array $args    List of additional arguments.
189
			 */
190
			do_action( 'give_stripe_after_cc_expiration', $form_id, $args );
191
		}
192
		?>
193
    </fieldset>
194
	<?php
195
	// Remove Address Fields if user has option enabled.
196
	$billing_fields_enabled = give_get_option( 'stripe_collect_billing' );
197
	if ( ! $billing_fields_enabled ) {
198
		remove_action( 'give_after_cc_fields', 'give_default_cc_address_fields' );
199
	}
200
201
	do_action( 'give_after_cc_fields', $form_id, $args );
202
203
	$form = ob_get_clean();
204
205
	if ( false !== $echo ) {
206
		echo $form;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form'
Loading history...
207
	}
208
209
	return $form;
210
}
211
212
add_action( 'give_stripe_cc_form', 'give_stripe_credit_card_form', 10, 3 );
213
214
/**
215
 * Add an errors div per form.
216
 *
217
 * @param int   $form_id Donation Form ID.
218
 * @param array $args    List of Donation Arguments.
219
 *
220
 * @access public
221
 * @since  2.5.0
222
 *
223
 * @return void
224
 */
225
function give_stripe_add_stripe_errors( $form_id, $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
226
	echo '<div id="give-stripe-payment-errors-' . esc_html( $args['id_prefix'] ) . '"></div>';
227
}
228
229
add_action( 'give_donation_form_after_cc_form', 'give_stripe_add_stripe_errors', 8899, 2 );
230