Completed
Push — hotfix/fix_notices ( 9d56c9...88019d )
by Ravinder
17:04
created

offline-donations.php ➔ give_offline_add_settings()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 81
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 57
nc 2
nop 1
dl 0
loc 81
rs 8.8076
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
0 ignored issues
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 21 and the first side effect is on line 31.

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
 * Offline Donations Gateway
4
 *
5
 * @package     Give
6
 * @subpackage  Gateways
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
/**
13
 * Register the payment gateway
14
 *
15
 * @since  1.0
16
 *
17
 * @param array $gateways
18
 *
19
 * @return array
20
 */
21
function give_offline_register_gateway( $gateways ) {
22
	// Format: ID => Name
23
	$gateways['offline'] = array(
24
		'admin_label'    => esc_attr__( 'Offline Donation', 'give' ),
25
		'checkout_label' => esc_attr__( 'Offline Donation', 'give' )
26
	);
27
28
	return $gateways;
29
}
30
31
add_filter( 'give_payment_gateways', 'give_offline_register_gateway', 1 );
32
33
34
/**
35
 * Add our payment instructions to the checkout
36
 *
37
 * @since  1.0
38
 *
39
 * @param  int $form_id Give form id.
40
 *
41
 * @return void
42
 */
43
function give_offline_payment_cc_form( $form_id ) {
44
	// Get offline payment instruction.
45
	$offline_instructions = give_get_offline_payment_instruction( $form_id, true );
46
47
	ob_start();
48
49
	/**
50
	 * Fires before the offline info fields.
51
	 *
52
	 * @since 1.0
53
	 *
54
	 * @param int $form_id Give form id.
55
	 */
56
	do_action( 'give_before_offline_info_fields', $form_id );
57
	?>
58
	<fieldset id="give_offline_payment_info">
59
		<?php echo stripslashes( $offline_instructions ); ?>
60
	</fieldset>
61
	<?php
62
	/**
63
	 * Fires after the offline info fields.
64
	 *
65
	 * @since 1.0
66
	 *
67
	 * @param int $form_id Give form id.
68
	 */
69
	do_action( 'give_after_offline_info_fields', $form_id );
70
71
	echo ob_get_clean();
72
}
73
74
add_action( 'give_offline_cc_form', 'give_offline_payment_cc_form' );
75
76
/**
77
 * Give Offline Billing Field
78
 *
79
 * @param $form_id
80
 */
81
function give_offline_billing_fields( $form_id ) {
82
	//Enable Default CC fields (billing info)
83
	$post_offline_cc_fields        = get_post_meta( $form_id, '_give_offline_donation_enable_billing_fields_single', true );
84
	$post_offline_customize_option = get_post_meta( $form_id, '_give_customize_offline_donations', true );
85
86
	$global_offline_cc_fields      = give_get_option( 'give_offline_donation_enable_billing_fields' );
87
88
	//Output CC Address fields if global option is on and user hasn't elected to customize this form's offline donation options
89
	if (
90
		( give_is_setting_enabled( $post_offline_customize_option, 'global' ) && give_is_setting_enabled( $global_offline_cc_fields ) )
91
		|| ( give_is_setting_enabled( $post_offline_customize_option, 'enabled' ) && give_is_setting_enabled( $post_offline_cc_fields ) )
92
	) {
93
		give_default_cc_address_fields( $form_id );
94
	}
95
}
96
97
add_action( 'give_before_offline_info_fields', 'give_offline_billing_fields', 10, 1 );
98
99
/**
100
 * Process the payment
101
 *
102
 * @since  1.0
103
 *
104
 * @param $purchase_data
105
 *
106
 * @return void
107
 */
108
function give_offline_process_payment( $purchase_data ) {
109
110
	$purchase_summary = give_get_purchase_summary( $purchase_data );
111
112
	// setup the payment details
113
	$payment_data = array(
114
		'price'           => $purchase_data['price'],
115
		'give_form_title' => $purchase_data['post_data']['give-form-title'],
116
		'give_form_id'    => intval( $purchase_data['post_data']['give-form-id'] ),
117
		'give_price_id'   => isset( $purchase_data['post_data']['give-price-id'] ) ? $purchase_data['post_data']['give-price-id'] : '',
118
		'date'            => $purchase_data['date'],
119
		'user_email'      => $purchase_data['user_email'],
120
		'purchase_key'    => $purchase_data['purchase_key'],
121
		'currency'        => give_get_currency(),
122
		'user_info'       => $purchase_data['user_info'],
123
		'status'          => 'pending',
124
		'gateway'         => 'offline'
125
	);
126
127
128
	// record the pending payment
129
	$payment = give_insert_payment( $payment_data );
130
131
	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...
132
		give_offline_send_admin_notice( $payment );
133
		give_offline_send_donor_instructions( $payment );
134
		give_send_to_success_page();
135
	} else {
136
		// if errors are present, send the user back to the donation form so they can be corrected
137
		give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
138
	}
139
140
}
141
142
add_action( 'give_gateway_offline', 'give_offline_process_payment' );
143
144
145
/**
146
 * Send Offline Donation Instructions
147
 *
148
 * Sends a notice to the donor with offline instructions; can be customized per form
149
 *
150
 * @param int $payment_id
151
 *
152
 * @since       1.0
153
 * @return void
154
 */
155
function give_offline_send_donor_instructions( $payment_id = 0 ) {
156
157
	$payment_data                      = give_get_payment_meta( $payment_id );
158
	$post_offline_customization_option = get_post_meta( $payment_data['form_id'], '_give_customize_offline_donations', true );
159
160
	//Customize email content depending on whether the single form has been customized
161
	$email_content = give_get_option( 'global_offline_donation_email' );
162
163
	if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
164
		$email_content = get_post_meta( $payment_data['form_id'], '_give_offline_donation_email', true );
165
	}
166
167
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
168
169
	/**
170
	 * Filters the from name.
171
	 *
172
	 * @since 1.7
173
	 */
174
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
175
176
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
177
178
	/**
179
	 * Filters the from email.
180
	 *
181
	 * @since 1.7
182
	 */
183
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
184
185
	$to_email = give_get_payment_user_email( $payment_id );
186
187
	$subject = give_get_option( 'offline_donation_subject', __( 'Offline Donation Instructions', 'give' ) );
188
	if ( give_is_setting_enabled( $post_offline_customization_option,  'enabled' ) ) {
189
		$subject = get_post_meta( $payment_data['form_id'], '_give_offline_donation_subject', true );
190
	}
191
192
	$subject = apply_filters( 'give_offline_donation_subject', wp_strip_all_tags( $subject ), $payment_id );
193
	$subject = give_do_email_tags( $subject, $payment_id );
194
195
	$attachments = apply_filters( 'give_offline_donation_attachments', array(), $payment_id, $payment_data );
196
	$message     = give_do_email_tags( $email_content, $payment_id );
197
198
	$emails = Give()->emails;
199
200
	$emails->__set( 'from_name', $from_name );
201
	$emails->__set( 'from_email', $from_email );
202
	$emails->__set( 'heading', __( 'Offline Donation Instructions', 'give' ) );
203
204
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), $payment_id, $payment_data );
205
	$emails->__set( 'headers', $headers );
206
207
	$emails->send( $to_email, $subject, $message, $attachments );
208
209
}
210
211
212
/**
213
 * Send Offline Donation Admin Notice.
214
 *
215
 * Sends a notice to site admins about the pending donation.
216
 *
217
 * @since       1.0
218
 *
219
 * @param int $payment_id
220
 *
221
 * @return void
222
 *
223
 */
224
function give_offline_send_admin_notice( $payment_id = 0 ) {
225
226
	/* Send an email notification to the admin */
227
	$admin_email = give_get_admin_notice_emails();
228
	$user_info   = give_get_payment_meta_user_info( $payment_id );
229
230
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 ) {
231
		$user_data = get_userdata( $user_info['id'] );
232
		$name      = $user_data->display_name;
233
	} elseif ( isset( $user_info['first_name'] ) && isset( $user_info['last_name'] ) ) {
234
		$name = $user_info['first_name'] . ' ' . $user_info['last_name'];
235
	} else {
236
		$name = $user_info['email'];
237
	}
238
239
	$amount = give_currency_filter( give_format_amount( give_get_payment_amount( $payment_id ) ) );
240
241
	$admin_subject = apply_filters( 'give_offline_admin_donation_notification_subject', __( 'New Pending Donation', 'give' ), $payment_id );
242
243
	$admin_message = __( 'Dear Admin,', 'give' ) . "\n\n";
244
	$admin_message .= __( 'An offline donation has been made on your website:', 'give' ) . ' ' . get_bloginfo( 'name' ) . ' ';
245
	$admin_message .= __( 'Hooray! 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";
246
247
248
	$admin_message .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
249
	$admin_message .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n\n";
250
251
	$admin_message .= sprintf(
252
		'<a href="%1$s">%2$s</a>',
253
		admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $payment_id ),
254
		__( 'Click Here to View and/or Update Donation Details', 'give' )
255
	) . "\n\n";
256
257
	$admin_message = apply_filters( 'give_offline_admin_donation_notification', $admin_message, $payment_id );
258
	$admin_message = give_do_email_tags( $admin_message, $payment_id );
259
260
	$attachments   = apply_filters( 'give_offline_admin_donation_notification_attachments', array(), $payment_id );
261
	$admin_headers = apply_filters( 'give_offline_admin_donation_notification_headers', array(), $payment_id );
262
263
	//Send Email
264
	$emails = Give()->emails;
265
	if ( ! empty( $admin_headers ) ) {
266
		$emails->__set( 'headers', $admin_headers );
267
	}
268
269
	$emails->send( $admin_email, $admin_subject, $admin_message, $attachments );
270
271
}
272
273
274
/**
275
 * Register gateway settings.
276
 *
277
 * @param $settings
278
 *
279
 * @return array
280
 */
281
function give_offline_add_settings( $settings ) {
282
283
	//Vars
284
	$prefix = '_give_';
285
286
	$is_gateway_active = give_is_gateway_active( 'offline' );
287
288
	//this gateway isn't active
289
	if ( ! $is_gateway_active ) {
290
		//return settings and bounce
291
		return $settings;
292
	}
293
294
	//Fields
295
	$check_settings = array(
296
297
		array(
298
			'name'    => __( 'Offline Donations', 'give' ),
299
			'desc'    => __( 'Do you want to customize the donation instructions for this form?', 'give' ),
300
			'id'      => $prefix . 'customize_offline_donations',
301
			'type'    => 'radio_inline',
302
			'default' => 'global',
303
			'options' => apply_filters( 'give_forms_content_options_select', array(
304
					'global'   => __( 'Global Options', 'give' ),
305
					'enabled'  => __( 'Customize', 'give' ),
306
					'disabled' => __( 'Disable', 'give' ),
307
				)
308
			),
309
		),
310
		array(
311
			'name'        => __( 'Billing Fields', 'give' ),
312
			'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' ),
313
			'id'          => $prefix . 'offline_donation_enable_billing_fields_single',
314
			'row_classes' => 'give-subfield',
315
			'type'        => 'radio_inline',
316
			'default'     => 'disabled',
317
			'options'     => array(
318
				'enabled'  => __( 'Enabled', 'give' ),
319
				'disabled' => __( 'Disabled', 'give' ),
320
			),
321
		),
322
		array(
323
			'id'          => $prefix . 'offline_checkout_notes',
324
			'name'        => __( 'Donation Instructions', 'give' ),
325
			'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' ),
326
			'default'     => give_get_default_offline_donation_content(),
327
			'type'        => 'wysiwyg',
328
			'row_classes' => 'give-subfield',
329
			'options'     => array(
330
				'textarea_rows' => 6,
331
			)
332
		),
333
		array(
334
			'id'          => $prefix . 'offline_donation_subject',
335
			'name'        => __( 'Email Subject', 'give' ),
336
			'desc'        => __( 'Enter the subject line for the donation receipt email.', 'give' ),
337
			'default'     => __( '{form_title} - Offline Donation Instructions', 'give' ),
338
			'row_classes' => 'give-subfield',
339
			'type'        => 'text'
340
		),
341
		array(
342
			'id'          => $prefix . 'offline_donation_email',
343
			'name'        => __( 'Email Instructions', 'give' ),
344
			'desc'        => __( 'Enter the instructions you want emailed to the donor after they have submitted the donation form. Most likely this would include important information like mailing address and who to make the check out to.', 'give' ),
345
			'default'     => give_get_default_offline_donation_email_content(),
346
			'type'        => 'wysiwyg',
347
			'row_classes' => 'give-subfield',
348
			'options'     => array(
349
				'textarea_rows' => 6,
350
			)
351
		),
352
		array(
353
			'name'         => 'offline_docs',
354
			'type'         => 'docs_link',
355
			'url'          => 'http://docs.givewp.com/offlinegateway',
356
			'title'        => __('Offline Donations', 'give'),
357
		),
358
	);
359
360
	return array_merge( $settings, $check_settings );
361
}
362
363
add_filter( 'give_forms_offline_donations_metabox_fields', 'give_offline_add_settings' );
364
365
366
/**
367
 * Offline Donation Content
368
 *
369
 * Get default offline donation text
370
 *
371
 * @return string
372
 */
373
function give_get_default_offline_donation_content() {
374
375
	$sitename = get_bloginfo( 'sitename' );
376
377
	$default_text = '<p>' . __( 'In order to make an offline donation we ask that you please follow these instructions', 'give' ) . ': </p>';
378
	$default_text .= '<ol>';
379
	$default_text .= '<li>';
380
	$default_text .= sprintf(
381
		/* translators: %s: site name */
382
		__( 'Make a check payable to "%s"', 'give' ),
383
		$sitename
384
	);
385
	$default_text .= '</li>';
386
	$default_text .= '<li>';
387
	$default_text .= sprintf(
388
		/* translators: %s: site name */
389
		__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
390
		$sitename
391
	);
392
	$default_text .= '</li>';
393
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
394
	$default_text .= '</ol>';
395
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
396
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
397
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
398
	$default_text .= '<p>' . __( 'All contributions will be gratefully acknowledged and are tax deductible.', 'give' ) . '</p>';
399
400
	return apply_filters( 'give_default_offline_donation_content', $default_text );
401
402
}
403
404
/**
405
 * Offline Donation Email Content
406
 *
407
 * Gets the default offline donation email content
408
 *
409
 * @return string
410
 */
411
function give_get_default_offline_donation_email_content() {
412
413
	$sitename      = get_bloginfo( 'sitename' );
414
	$default_text  = '<p>' . __( 'Dear {name},', 'give' ) . '</p>';
415
	$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>';
416
	$default_text .= '<ol>';
417
	$default_text .= '<li>';
418
	$default_text .= sprintf(
419
		/* translators: %s: site name */
420
		__( 'Make a check payable to "%s"', 'give' ),
421
		$sitename
422
	);
423
	$default_text .= '</li>';
424
	$default_text .= '<li>';
425
	$default_text .= sprintf(
426
		/* translators: %s: site name */
427
		__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
428
		$sitename
429
	);
430
	$default_text .= '</li>';
431
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
432
	$default_text .= '</ol>';
433
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
434
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
435
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
436
	$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>';
437
	$default_text .= '<p>' . __( 'Sincerely,', 'give' ) . '</p>';
438
	$default_text .= '<p>' . $sitename . '</p>';
439
440
	return apply_filters( 'give_default_offline_donation_content', $default_text );
441
442
}
443
444
/**
445
 * Set notice for offline donation.
446
 *
447
 * @since 1.7
448
 *
449
 * @param string $notice
450
 * @param int    $id
451
 *
452
 * @return string
453
 */
454
function give_offline_donation_receipt_status_notice( $notice, $id ) {
455
	$payment = new Give_Payment( $id );
456
457
	if ( 'offline' !== $payment->gateway ) {
458
		return $notice;
459
	}
460
461
	return give_output_error( 'Payment Pending: Please follow the instructions below to complete your donation.', false, 'warning' );
462
}
463
464
add_filter( 'give_receipt_status_notice', 'give_offline_donation_receipt_status_notice', 10, 2 );
465
466
/**
467
 * Add offline payment instruction on payment receipt.
468
 *
469
 * @since 1.7
470
 *
471
 * @param WP_Post $payment
472
 *
473
 * @return mixed
474
 */
475
function give_offline_payment_receipt_after( $payment ) {
476
	// Get payment object.
477
	$payment = new Give_Payment( $payment->ID );
478
479
	// Bailout.
480
	if ( 'offline' !== $payment->gateway ) {
481
		return false;
482
	}
483
484
	?>
485
	<tr>
486
		<td scope="row"><strong><?php esc_html_e( 'Offline Payment Instruction:', 'give' ); ?></strong></td>
487
		<td>
488
			<?php echo give_get_offline_payment_instruction( $payment->form_id, true ); ?>
489
		</td>
490
	</tr>
491
	<?php
492
}
493
494
add_filter( 'give_payment_receipt_after', 'give_offline_payment_receipt_after' );
495
496
/**
497
 * Get offline payment instructions.
498
 *
499
 * @since 1.7
500
 *
501
 * @param int  $form_id
502
 * @param bool $wpautop
503
 *
504
 * @return string
505
 */
506
function give_get_offline_payment_instruction( $form_id, $wpautop = false ) {
507
	// Bailout.
508
	if ( ! $form_id ) {
509
		return '';
510
	}
511
512
	$post_offline_customization_option = get_post_meta( $form_id, '_give_customize_offline_donations', true );
513
	$post_offline_instructions         = get_post_meta( $form_id, '_give_offline_checkout_notes', true );
514
	$global_offline_instruction        = give_get_option( 'global_offline_donation_content' );
515
	$offline_instructions              = $global_offline_instruction;
516
517
	if ( $post_offline_customization_option == 'yes' ) {
518
		$offline_instructions = $post_offline_instructions;
519
	}
520
521
	$settings_url = admin_url( 'post.php?post=' . $form_id . '&action=edit&message=1' );
522
523
	/* translators: %s: form settings url */
524
	$offline_instructions = ! empty( $offline_instructions ) ? $offline_instructions : sprintf( __( 'Please enter offline donation instructions in <a href="%s">this form\'s settings</a>.', 'give' ), $settings_url );
525
526
	return ( $wpautop ? wpautop( $offline_instructions ) : $offline_instructions );
527
}
528
529
530
/**
531
 * Remove offline gateway from gateway list of offline disable for form.
532
 *
533
 * @since  1.8
534
 *
535
 * @param  array $gateway_list
536
 * @param        $form_id
537
 *
538
 * @return array
539
 */
540
function give_filter_offline_gateway( $gateway_list, $form_id ) {
541
	if ( $form_id && ! give_is_setting_enabled( get_post_meta( $form_id, '_give_customize_offline_donations', true ), array( 'enabled', 'global' ) ) ) {
542
		unset( $gateway_list['offline'] );
543
	}
544
545
	// Output.
546
	return $gateway_list;
547
}
548
549
add_filter( 'give_enabled_payment_gateways', 'give_filter_offline_gateway', 10, 2 );
550
551
/**
552
 * Set default gateway to global default payment gateway
553
 * if current default gateways selected offline and offline payment gateway is disabled.
554
 *
555
 * @since 1.8
556
 *
557
 * @param  string $meta_key Meta key.
558
 * @param  string $meta_value Meta value.
559
 * @param  int    $postid Form ID.
560
 *
561
 * @return void
562
 */
563
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...
564
	if ( ( 'no' === $meta_value ) && ( 'offline' === get_post_meta( $postid, '_give_default_gateway', true ) ) ) {
565
		update_post_meta( $postid, '_give_default_gateway', 'global' );
566
	}
567
}
568
569
add_filter( 'give_save__give_customize_offline_donations', '_give_customize_offline_donations_on_save_callback', 10, 3 );
570