offline-donations.php ➔ give_offline_add_settings()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 67
rs 8.72
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
 * Offline Donations Gateway
4
 *
5
 * @package     Give
6
 * @subpackage  Gateways
7
 * @copyright   Copyright (c) 2016, GiveWP
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
/**
13
 * Add our payment instructions to the checkout
14
 *
15
 * @since  1.0
16
 *
17
 * @param  int $form_id Give form id.
18
 *
19
 * @return void
20
 */
21
function give_offline_payment_cc_form( $form_id ) {
22
	// Get offline payment instruction.
23
	$offline_instructions = give_get_offline_payment_instruction( $form_id, true );
24
25
	ob_start();
26
27
	/**
28
	 * Fires before the offline info fields.
29
	 *
30
	 * @since 1.0
31
	 *
32
	 * @param int $form_id Give form id.
33
	 */
34
	do_action( 'give_before_offline_info_fields', $form_id );
35
	?>
36
    <fieldset id="give_offline_payment_info">
37
		<?php echo stripslashes( $offline_instructions ); ?>
38
    </fieldset>
39
	<?php
40
	/**
41
	 * Fires after the offline info fields.
42
	 *
43
	 * @since 1.0
44
	 *
45
	 * @param int $form_id Give form id.
46
	 */
47
	do_action( 'give_after_offline_info_fields', $form_id );
48
49
	echo ob_get_clean();
50
}
51
52
add_action( 'give_offline_cc_form', 'give_offline_payment_cc_form' );
53
54
/**
55
 * Give Offline Billing Field
56
 *
57
 * @param $form_id
58
 */
59
function give_offline_billing_fields( $form_id ) {
60
	//Enable Default CC fields (billing info)
61
	$post_offline_cc_fields        = give_get_meta( $form_id, '_give_offline_donation_enable_billing_fields_single', true );
62
	$post_offline_customize_option = give_get_meta( $form_id, '_give_customize_offline_donations', true, 'global' );
0 ignored issues
show
Documentation introduced by
'global' is of type string, but the function expects a 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...
63
64
	$global_offline_cc_fields = give_get_option( 'give_offline_donation_enable_billing_fields' );
65
66
	//Output CC Address fields if global option is on and user hasn't elected to customize this form's offline donation options
67
	if (
68
		( give_is_setting_enabled( $post_offline_customize_option, 'global' ) && give_is_setting_enabled( $global_offline_cc_fields ) )
69
		|| ( give_is_setting_enabled( $post_offline_customize_option, 'enabled' ) && give_is_setting_enabled( $post_offline_cc_fields ) )
70
	) {
71
		give_default_cc_address_fields( $form_id );
72
	}
73
}
74
75
add_action( 'give_before_offline_info_fields', 'give_offline_billing_fields', 10, 1 );
76
77
/**
78
 * Process the payment
79
 *
80
 * @since  1.0
81
 *
82
 * @param $purchase_data
83
 *
84
 * @return void
85
 */
86
function give_offline_process_payment( $purchase_data ) {
87
88
	// Setup the payment details.
89
	$payment_data = array(
90
		'price'           => $purchase_data['price'],
91
		'give_form_title' => $purchase_data['post_data']['give-form-title'],
92
		'give_form_id'    => intval( $purchase_data['post_data']['give-form-id'] ),
93
		'give_price_id'   => isset( $purchase_data['post_data']['give-price-id'] ) ? $purchase_data['post_data']['give-price-id'] : '',
94
		'date'            => $purchase_data['date'],
95
		'user_email'      => $purchase_data['user_email'],
96
		'purchase_key'    => $purchase_data['purchase_key'],
97
		'currency'        => give_get_currency( $purchase_data['post_data']['give-form-id'], $purchase_data ),
98
		'user_info'       => $purchase_data['user_info'],
99
		'status'          => 'pending',
100
		'gateway'         => 'offline',
101
	);
102
103
104
	// record the pending payment
105
	$payment = give_insert_payment( $payment_data );
106
107
	if ( $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
108
		give_send_to_success_page();
109
	} else {
110
		// if errors are present, send the user back to the donation form so they can be corrected
111
		give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
112
	}
113
114
}
115
116
add_action( 'give_gateway_offline', 'give_offline_process_payment' );
117
118
119
/**
120
 * Send Offline Donation Instructions
121
 *
122
 * Sends a notice to the donor with offline instructions; can be customized per form
123
 *
124
 * @param int $payment_id
125
 *
126
 * @since       1.0
127
 * @return void
128
 */
129
function give_offline_send_donor_instructions( $payment_id = 0 ) {
130
131
	$payment_data                      = give_get_payment_meta( $payment_id );
132
	$post_offline_customization_option = give_get_meta( $payment_data['form_id'], '_give_customize_offline_donations', true );
133
134
	//Customize email content depending on whether the single form has been customized
135
	$email_content = give_get_option( 'global_offline_donation_email' );
136
137
	if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
138
		$email_content = give_get_meta( $payment_data['form_id'], '_give_offline_donation_email', true );
139
	}
140
141
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
142
143
	/**
144
	 * Filters the from name.
145
	 *
146
	 * @since 1.7
147
	 */
148
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
149
150
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
151
152
	/**
153
	 * Filters the from email.
154
	 *
155
	 * @since 1.7
156
	 */
157
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
158
159
	$to_email = give_get_payment_user_email( $payment_id );
160
161
	$subject = give_get_option( 'offline_donation_subject', __( 'Offline Donation Instructions', 'give' ) );
162
	if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
163
		$subject = give_get_meta( $payment_data['form_id'], '_give_offline_donation_subject', true );
164
	}
165
166
	$subject = apply_filters( 'give_offline_donation_subject', wp_strip_all_tags( $subject ), $payment_id );
167
	$subject = give_do_email_tags( $subject, $payment_id );
168
169
	$attachments = apply_filters( 'give_offline_donation_attachments', array(), $payment_id, $payment_data );
170
	$message     = give_do_email_tags( $email_content, $payment_id );
171
172
	$emails = Give()->emails;
173
174
	$emails->__set( 'from_name', $from_name );
175
	$emails->__set( 'from_email', $from_email );
176
	$emails->__set( 'heading', __( 'Offline Donation Instructions', 'give' ) );
177
178
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), $payment_id, $payment_data );
179
	$emails->__set( 'headers', $headers );
180
181
	$emails->send( $to_email, $subject, $message, $attachments );
182
183
}
184
185
186
/**
187
 * Send Offline Donation Admin Notice.
188
 *
189
 * Sends a notice to site admins about the pending donation.
190
 *
191
 * @since       1.0
192
 *
193
 * @param int $payment_id
194
 *
195
 * @return void
196
 *
197
 */
198
function give_offline_send_admin_notice( $payment_id = 0 ) {
199
200
	/* Send an email notification to the admin */
201
	$admin_email = give_get_admin_notice_emails();
202
	$user_info   = give_get_payment_meta_user_info( $payment_id );
203
204
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 ) {
205
		$user_data = get_userdata( $user_info['id'] );
206
		$name      = $user_data->display_name;
0 ignored issues
show
Unused Code introduced by
$name 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
	} elseif ( isset( $user_info['first_name'] ) && isset( $user_info['last_name'] ) ) {
208
		$name = $user_info['first_name'] . ' ' . $user_info['last_name'];
0 ignored issues
show
Unused Code introduced by
$name 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...
209
	} else {
210
		$name = $user_info['email'];
0 ignored issues
show
Unused Code introduced by
$name 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...
211
	}
212
213
	$amount = give_donation_amount( $payment_id );
214
215
	$admin_subject = apply_filters( 'give_offline_admin_donation_notification_subject', __( 'New Pending Donation', 'give' ), $payment_id );
216
217
	$admin_message = __( 'Dear Admin,', 'give' ) . "\n\n";
218
	$admin_message .= sprintf(__( 'A new offline donation has been made on your website for %s.', 'give' ), $amount) . "\n\n";
219
	$admin_message .= __( 'The donation is in a pending status and is awaiting payment. Donation instructions have been emailed to the donor. Once you receive payment, be sure to mark the donation as complete using the link below.', 'give' ) . "\n\n";
220
221
222
	$admin_message .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
223
	$admin_message .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n\n";
224
225
	$admin_message .= sprintf(
226
		                  '<a href="%1$s">%2$s</a>',
227
		                  admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&id=' . $payment_id ),
228
		                  __( 'View Donation Details &raquo;', 'give' )
229
	                  ) . "\n\n";
230
231
	$admin_message = apply_filters( 'give_offline_admin_donation_notification', $admin_message, $payment_id );
232
	$admin_message = give_do_email_tags( $admin_message, $payment_id );
233
234
	$attachments   = apply_filters( 'give_offline_admin_donation_notification_attachments', array(), $payment_id );
235
	$admin_headers = apply_filters( 'give_offline_admin_donation_notification_headers', array(), $payment_id );
236
237
	//Send Email
238
	$emails = Give()->emails;
239
	$emails->__set( 'heading', __( 'New Offline Donation', 'give' ) );
240
241
	if ( ! empty( $admin_headers ) ) {
242
		$emails->__set( 'headers', $admin_headers );
243
	}
244
245
	$emails->send( $admin_email, $admin_subject, $admin_message, $attachments );
246
247
}
248
249
250
/**
251
 * Register gateway settings.
252
 *
253
 * @param $settings
254
 *
255
 * @return array
256
 */
257
function give_offline_add_settings( $settings ) {
258
259
	// Bailout: Do not show offline gateways setting in to metabox if its disabled globally.
260
	if ( in_array( 'offline', (array) give_get_option( 'gateways' ) ) ) {
261
		return $settings;
262
	}
263
264
	//Vars
265
	$prefix = '_give_';
266
267
	$is_gateway_active = give_is_gateway_active( 'offline' );
268
269
	//this gateway isn't active
270
	if ( ! $is_gateway_active ) {
271
		//return settings and bounce
272
		return $settings;
273
	}
274
275
	//Fields
276
	$check_settings = array(
277
278
		array(
279
			'name'    => __( 'Offline Donations', 'give' ),
280
			'desc'    => __( 'Do you want to customize the donation instructions for this form?', 'give' ),
281
			'id'      => $prefix . 'customize_offline_donations',
282
			'type'    => 'radio_inline',
283
			'default' => 'global',
284
			'options' => apply_filters( 'give_forms_content_options_select', array(
285
					'global'   => __( 'Global Option', 'give' ),
286
					'enabled'  => __( 'Customize', 'give' ),
287
					'disabled' => __( 'Disable', 'give' ),
288
				)
289
			),
290
		),
291
		array(
292
			'name'        => __( 'Billing Fields', 'give' ),
293
			'desc'        => __( 'This option will enable the billing details section for this form\'s offline donation payment gateway. The fieldset will appear above the offline donation instructions.', 'give' ),
294
			'id'          => $prefix . 'offline_donation_enable_billing_fields_single',
295
			'row_classes' => 'give-subfield give-hidden',
296
			'type'        => 'radio_inline',
297
			'default'     => 'disabled',
298
			'options'     => array(
299
				'enabled'  => __( 'Enabled', 'give' ),
300
				'disabled' => __( 'Disabled', 'give' ),
301
			),
302
		),
303
		array(
304
			'id'          => $prefix . 'offline_checkout_notes',
305
			'name'        => __( 'Donation Instructions', 'give' ),
306
			'desc'        => __( 'Enter the instructions you want to display to the donor during the donation process. Most likely this would include important information like mailing address and who to make the check out to.', 'give' ),
307
			'default'     => give_get_default_offline_donation_content(),
308
			'type'        => 'wysiwyg',
309
			'row_classes' => 'give-subfield give-hidden',
310
			'options'     => array(
311
				'textarea_rows' => 6,
312
			)
313
		),
314
		array(
315
			'name'  => 'offline_docs',
316
			'type'  => 'docs_link',
317
			'url'   => 'http://docs.givewp.com/settings-gateway-offline-donations',
318
			'title' => __( 'Offline Donations', 'give' ),
319
		),
320
	);
321
322
	return array_merge( $settings, $check_settings );
323
}
324
325
add_filter( 'give_forms_offline_donations_metabox_fields', 'give_offline_add_settings' );
326
327
328
/**
329
 * Offline Donation Content
330
 *
331
 * Get default offline donation text
332
 *
333
 * @return string
334
 */
335
function give_get_default_offline_donation_content() {
336
	$default_text = '<p>' . __( 'In order to make an offline donation we ask that you please follow these instructions', 'give' ) . ': </p>';
337
	$default_text .= '<ol>';
338
	$default_text .= '<li>';
339
	$default_text .= sprintf(
340
	/* translators: %s: site name */
341
		__( 'Make a check payable to "{sitename}"', 'give' ) );
342
	$default_text .= '</li>';
343
	$default_text .= '<li>';
344
	$default_text .= sprintf(
345
	/* translators: %s: site name */
346
		__( 'On the memo line of the check, please indicate that the donation is for "{sitename}"', 'give' ) );
347
	$default_text .= '</li>';
348
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
349
	$default_text .= '</ol>';
350
	$default_text .= '{offline_mailing_address}<br>';
351
	$default_text .= '<p>' . __( 'All contributions will be gratefully acknowledged and are tax deductible.', 'give' ) . '</p>';
352
353
	return apply_filters( 'give_default_offline_donation_content', $default_text );
354
355
}
356
357
/**
358
 * Offline Donation Email Content
359
 *
360
 * Gets the default offline donation email content
361
 *
362
 * @return string
363
 */
364
function give_get_default_offline_donation_email_content() {
365
	$default_text = '<p>' . __( 'Dear {name},', 'give' ) . '</p>';
366
	$default_text .= '<p>' . __( 'Thank you for your offline donation request! Your generosity is greatly appreciated. In order to make an offline donation we ask that you please follow these instructions:', 'give' ) . '</p>';
367
	$default_text .= '<ol>';
368
	$default_text .= '<li>';
369
	$default_text .= sprintf(
370
	/* translators: %s: site name */
371
		__( 'Make a check payable to "{sitename}"', 'give' )
372
	);
373
	$default_text .= '</li>';
374
	$default_text .= '<li>';
375
	$default_text .= sprintf(
376
		__( 'On the memo line of the check, please indicate that the donation is for "{sitename}"', 'give' )
377
	);
378
	$default_text .= '</li>';
379
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
380
	$default_text .= '</ol>';
381
	$default_text .= '{offline_mailing_address}<br>';
382
	$default_text .= '<p>' . __( 'Once your donation has been received we will mark it as complete and you will receive an email receipt for your records. Please contact us with any questions you may have!', 'give' ) . '</p>';
383
	$default_text .= '<p>' . __( 'Sincerely,', 'give' ) . '</p>';
384
	$default_text .= '<p>{sitename}</p>';
385
386
	return apply_filters( 'give_default_offline_donation_content', $default_text );
387
388
}
389
390
/**
391
 * Get offline payment instructions.
392
 *
393
 * @since 1.7
394
 *
395
 * @param int  $form_id
396
 * @param bool $wpautop
397
 *
398
 * @return string
399
 */
400
function give_get_offline_payment_instruction( $form_id, $wpautop = false ) {
401
	// Bailout.
402
	if ( ! $form_id ) {
403
		return '';
404
	}
405
406
	$post_offline_customization_option = give_get_meta( $form_id, '_give_customize_offline_donations', true );
407
	$post_offline_instructions         = give_get_meta( $form_id, '_give_offline_checkout_notes', true );
408
	$global_offline_instruction        = give_get_option( 'global_offline_donation_content' );
409
	$offline_instructions              = $global_offline_instruction;
410
411
	if ( give_is_setting_enabled( $post_offline_customization_option ) ) {
412
		$offline_instructions = $post_offline_instructions;
413
	}
414
415
	$settings_url = admin_url( 'post.php?post=' . $form_id . '&action=edit&message=1' );
416
417
	/* translators: %s: form settings url */
418
	$offline_instructions = ! empty( $offline_instructions )
419
		? $offline_instructions
420
		: sprintf(
421
			__( 'Please enter offline donation instructions in <a href="%s">this form\'s settings</a>.', 'give' ),
422
			$settings_url
423
		);
424
425
	$offline_instructions = give_do_email_tags( $offline_instructions, null );
426
427
	$formmated_offline_instructions = $wpautop
428
		? wpautop( do_shortcode( $offline_instructions ) )
429
		: $offline_instructions;
430
431
	/**
432
	 * Filter the offline instruction content
433
	 *
434
	 * @since 2.2.0
435
	 *
436
	 */
437
	$formmated_offline_instructions = apply_filters(
438
		'give_the_offline_instructions_content',
439
		$formmated_offline_instructions,
440
		$offline_instructions,
441
		$form_id,
442
		$wpautop
443
	);
444
445
	return $formmated_offline_instructions;
446
}
447
448
449
/**
450
 * Remove offline gateway from gateway list of offline disable for form.
451
 *
452
 * @since  1.8
453
 *
454
 * @param  array $gateway_list
455
 * @param        $form_id
456
 *
457
 * @return array
458
 */
459
function give_filter_offline_gateway( $gateway_list, $form_id ) {
460
	if (
461
		// Show offline payment gateway if enable for new donation form.
462
		( false === strpos( $_SERVER['REQUEST_URI'], '/wp-admin/post-new.php?post_type=give_forms' ) )
463
		&& $form_id
464
		&& ! give_is_setting_enabled( give_get_meta( $form_id, '_give_customize_offline_donations', true, 'global' ), array( 'enabled', 'global' ) )
0 ignored issues
show
Documentation introduced by
array('enabled', 'global') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

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...
Documentation introduced by
'global' is of type string, but the function expects a 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...
465
	) {
466
		unset( $gateway_list['offline'] );
467
	}
468
469
	// Output.
470
	return $gateway_list;
471
}
472
473
add_filter( 'give_enabled_payment_gateways', 'give_filter_offline_gateway', 10, 2 );
474
475
/**
476
 * Set default gateway to global default payment gateway
477
 * if current default gateways selected offline and offline payment gateway is disabled.
478
 *
479
 * @since 1.8
480
 *
481
 * @param  string $meta_key   Meta key.
482
 * @param  string $meta_value Meta value.
483
 * @param  int    $postid     Form ID.
484
 *
485
 * @return void
486
 */
487
function _give_customize_offline_donations_on_save_callback( $meta_key, $meta_value, $postid ) {
0 ignored issues
show
Unused Code introduced by
The parameter $meta_key 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...
488
	if (
489
		! give_is_setting_enabled( $meta_value, array( 'global', 'enabled' ) )
0 ignored issues
show
Documentation introduced by
array('global', 'enabled') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

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...
490
		&& ( 'offline' === give_get_meta( $postid, '_give_default_gateway', true ) )
491
	) {
492
		give_update_meta( $postid, '_give_default_gateway', 'global' );
493
	}
494
}
495
496
add_filter( 'give_save__give_customize_offline_donations', '_give_customize_offline_donations_on_save_callback', 10, 3 );
497