Completed
Pull Request — master (#986)
by Rami
20:36
created

offline-donations.php ➔ give_offline_send_donor_instructions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 55
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
nc 4
nop 1
dl 0
loc 55
ccs 0
cts 27
cp 0
crap 12
rs 9.7692
c 6
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     http://opensource.org/licenses/gpl-2.0.php 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 44
	$gateways['offline'] = array(
24 44
		'admin_label'    => esc_attr__( 'Offline Donation', 'give' ),
25 44
		'checkout_label' => esc_attr__( 'Offline Donation', 'give' )
26 44
	);
27
28 44
	return $gateways;
29
}
30
31
add_filter( 'give_donation_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
45
	$post_offline_customization_option = get_post_meta( $form_id, '_give_customize_offline_donations', true );
46
	$post_offline_instructions         = get_post_meta( $form_id, '_give_offline_checkout_notes', true );
47
	$global_offline_instruction        = give_get_option( 'global_offline_donation_content' );
48
	$offline_instructions              = $global_offline_instruction;
49
50
	if ( $post_offline_customization_option == 'yes' ) {
51
		$offline_instructions = $post_offline_instructions;
52
	}
53
54
	ob_start();
55
56
	/**
57
	 * Fires before the offline info fields.
58
	 *
59
	 * @since 1.0
60
	 *
61
	 * @param int $form_id Give form id.
62
	 */
63
	do_action( 'give_before_offline_info_fields', $form_id );
64
	?>
65
	<fieldset id="give_offline_payment_info">
66
		<?php
67
		$settings_url         = admin_url( 'post.php?post=' . $form_id . '&action=edit&message=1' );
68
		/* translators: %s: form settings url */
69
		$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 );
70
		echo wpautop( stripslashes( $offline_instructions ) );
71
		?>
72
	</fieldset>
73
	<?php
74
	/**
75
	 * Fires after the offline info fields.
76
	 *
77
	 * @since 1.0
78
	 *
79
	 * @param int $form_id Give form id.
80
	 */
81
	do_action( 'give_after_offline_info_fields', $form_id );
82
83
	echo ob_get_clean();
84
}
85
86
add_action( 'give_offline_cc_form', 'give_offline_payment_cc_form' );
87
88
/**
89
 * Give Offline Billing Field
90
 *
91
 * @param $form_id
92
 */
93
function give_offline_billing_fields( $form_id ) {
94
	//Enable Default CC fields (billing info)
95
	$post_offline_cc_fields        = get_post_meta( $form_id, '_give_offline_donation_enable_billing_fields_single', true );
96
	$post_offline_customize_option = get_post_meta( $form_id, '_give_customize_offline_donations', true );
97
98
	$global_offline_cc_fields      = give_get_option( 'give_offline_donation_enable_billing_fields' );
99
100
	//Output CC Address fields if global option is on and user hasn't elected to customize this form's offline donation options
101
	if ( $global_offline_cc_fields == 'on' && $post_offline_customize_option !== 'yes' ) {
102
		give_default_cc_address_fields( $form_id );
103
	} elseif($post_offline_customize_option == 'yes' && $post_offline_cc_fields == 'on') {
104
		give_default_cc_address_fields( $form_id );
105
	}
106
}
107
108
add_action( 'give_before_offline_info_fields', 'give_offline_billing_fields', 10, 1 );
109
110
/**
111
 * Process the payment
112
 *
113
 * @since  1.0
114
 *
115
 * @param $purchase_data
116
 *
117
 * @return void
118
 */
119
function give_offline_process_payment( $purchase_data ) {
120
121
	$purchase_summary = give_get_purchase_summary( $purchase_data );
122
123
	// setup the payment details
124
	$payment_data = array(
125
		'price'           => $purchase_data['price'],
126
		'give_form_title' => $purchase_data['post_data']['give-form-title'],
127
		'give_form_id'    => intval( $purchase_data['post_data']['give-form-id'] ),
128
		'give_price_id'   => isset( $purchase_data['post_data']['give-price-id'] ) ? $purchase_data['post_data']['give-price-id'] : '',
129
		'date'            => $purchase_data['date'],
130
		'user_email'      => $purchase_data['user_email'],
131
		'purchase_key'    => $purchase_data['purchase_key'],
132
		'currency'        => give_get_currency(),
133
		'user_info'       => $purchase_data['user_info'],
134
		'status'          => 'pending',
135
		'gateway'         => 'offline'
136
	);
137
138
139
	// record the pending payment
140
	$payment = give_insert_payment( $payment_data );
141
142
	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...
143
		give_offline_send_admin_notice( $payment );
144
		give_offline_send_donor_instructions( $payment );
145
		give_send_to_success_page();
146
	} else {
147
		// if errors are present, send the user back to the donation form so they can be corrected
148
		give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
149
	}
150
151
}
152
153
add_action( 'give_gateway_offline', 'give_offline_process_payment' );
154
155
156
/**
157
 * Send Offline Donation Instructions
158
 *
159
 * Sends a notice to the donor with offline instructions; can be customized per form
160
 *
161
 * @param int $payment_id
162
 *
163
 * @since       1.0
164
 * @return void
165
 */
166
function give_offline_send_donor_instructions( $payment_id = 0 ) {
167
168
	$payment_data                      = give_get_payment_meta( $payment_id );
169
	$post_offline_customization_option = get_post_meta( $payment_data['form_id'], '_give_customize_offline_donations', true );
170
171
	//Customize email content depending on whether the single form has been customized
172
	$email_content = give_get_option( 'global_offline_donation_email' );
173
174
	if ( $post_offline_customization_option === 'yes' ) {
175
		$email_content = get_post_meta( $payment_data['form_id'], '_give_offline_donation_email', true );
176
	}
177
178
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
179
180
	/**
181
	 * Filters the from name.
182
	 *
183
	 * @since 1.7
184
	 */
185
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
186
187
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
188
189
	/**
190
	 * Filters the from email.
191
	 *
192
	 * @since 1.7
193
	 */
194
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
195
196
	$to_email = give_get_payment_user_email( $payment_id );
197
198
	$subject = give_get_option( 'offline_donation_subject', esc_html__( 'Offline Donation Instructions', 'give' ) );
199
	if ( $post_offline_customization_option === 'yes' ) {
200
		$subject = get_post_meta( $payment_data['form_id'], '_give_offline_donation_subject', true );
201
	}
202
203
	$subject = apply_filters( 'give_offline_donation_subject', wp_strip_all_tags( $subject ), $payment_id );
204
	$subject = give_do_email_tags( $subject, $payment_id );
205
206
	$attachments = apply_filters( 'give_offline_donation_attachments', array(), $payment_id, $payment_data );
207
	$message     = give_do_email_tags( $email_content, $payment_id );
208
209
	$emails = Give()->emails;
210
211
	$emails->__set( 'from_name', $from_name );
212
	$emails->__set( 'from_email', $from_email );
213
	$emails->__set( 'heading', esc_html__( 'Offline Donation Instructions', 'give' ) );
214
215
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), $payment_id, $payment_data );
216
	$emails->__set( 'headers', $headers );
217
218
	$emails->send( $to_email, $subject, $message, $attachments );
219
220
}
221
222
223
/**
224
 * Send Offline Donation Admin Notice
225
 *
226
 * Sends a notice to site admins about the pending donation
227
 *
228
 * @since       1.0
229
 *
230
 * @param int $payment_id
231
 *
232
 * @return void
233
 *
234
 */
235
function give_offline_send_admin_notice( $payment_id = 0 ) {
236
237
	/* Send an email notification to the admin */
238
	$admin_email = give_get_admin_notice_emails();
239
	$user_info   = give_get_payment_meta_user_info( $payment_id );
240
241
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 ) {
242
		$user_data = get_userdata( $user_info['id'] );
243
		$name      = $user_data->display_name;
244
	} elseif ( isset( $user_info['first_name'] ) && isset( $user_info['last_name'] ) ) {
245
		$name = $user_info['first_name'] . ' ' . $user_info['last_name'];
246
	} else {
247
		$name = $user_info['email'];
248
	}
249
250
	$amount = give_currency_filter( give_format_amount( give_get_payment_amount( $payment_id ) ) );
251
252
	$admin_subject = apply_filters( 'give_offline_admin_donation_notification_subject', esc_attr__( 'New Pending Donation', 'give' ), $payment_id );
253
254
	$admin_message = esc_attr__( 'Dear Admin,', 'give' ) . "\n\n";
255
	$admin_message .= esc_attr__( 'An offline donation has been made on your website:', 'give' ) . ' ' . get_bloginfo( 'name' ) . ' ';
256
	$admin_message .= esc_attr__( '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";
257
258
259
	$admin_message .= '<strong>' . esc_attr__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
260
	$admin_message .= '<strong>' . esc_attr__( 'Amount:', 'give' ) . '</strong> {price}' . "\n\n";
261
262
	$admin_message .= sprintf(
263
		'<a href="%1$s">%2$s</a>',
264
		admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $payment_id ),
265
		esc_html__( 'Click Here to View and/or Update Donation Details', 'give' )
266
	) . "\n\n";
267
268
	$admin_message = apply_filters( 'give_offline_admin_donation_notification', $admin_message, $payment_id );
269
	$admin_message = give_do_email_tags( $admin_message, $payment_id );
270
271
	$attachments   = apply_filters( 'give_offline_admin_donation_notification_attachments', array(), $payment_id );
272
	$admin_headers = apply_filters( 'give_offline_admin_donation_notification_headers', array(), $payment_id );
273
274
	//Send Email
275
	$emails = Give()->emails;
276
	if ( ! empty( $admin_headers ) ) {
277
		$emails->__set( 'headers', $admin_headers );
278
	}
279
280
	$emails->send( $admin_email, $admin_subject, $admin_message, $attachments );
281
282
}
283
284
285
/**
286
 * Register gateway settings
287
 *
288
 * @since  1.0
289
 * @return array
290
 */
291
function give_offline_add_settings( $settings ) {
292
293
	//Vars
294
	$prefix = '_give_';
295
296
	$is_gateway_active = give_is_gateway_active( 'offline' );
297
298
	//this gateway isn't active
299
	if ( ! $is_gateway_active ) {
300
		//return settings and bounce
301
		return $settings;
302
	}
303
304
	//Fields
305
	$check_settings = array(
306
307
		array(
308
			'name'    => esc_attr__( 'Customize Offline Donations', 'give' ),
309
			'desc'    => esc_attr__( 'If you would like to customize the donation instructions for this specific forms check this option.', 'give' ),
310
			'id'      => $prefix . 'customize_offline_donations',
311
			'type'    => 'radio_inline',
312
			'default' => 'no',
313
			'options' => array(
314
				'yes' => esc_attr__( 'Yes', 'give' ),
315
				'no'  => esc_attr__( 'No', 'give' ),
316
			),
317
		),
318
		array(
319
			'name'        => esc_attr__( 'Request Billing Information', 'give' ),
320
			'desc'        => esc_attr__( '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' ),
321
			'id'          => $prefix . 'offline_donation_enable_billing_fields_single',
322
			'row_classes' => 'give-subfield',
323
			'type'        => 'checkbox'
324
		),
325
		array(
326
			'id'          => $prefix . 'offline_checkout_notes',
327
			'name'        => esc_attr__( 'Offline Donation Instructions', 'give' ),
328
			'desc'        => esc_attr__( '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' ),
329
			'default'     => give_get_default_offline_donation_content(),
330
			'type'        => 'wysiwyg',
331
			'row_classes' => 'give-subfield',
332
			'options'     => array(
333
				'textarea_rows' => 6,
334
			)
335
		),
336
		array(
337
			'id'          => $prefix . 'offline_donation_subject',
338
			'name'        => esc_attr__( 'Offline Donation Email Instructions Subject', 'give' ),
339
			'desc'        => esc_attr__( 'Enter the subject line for the donation receipt email.', 'give' ),
340
			'default'     => esc_attr__( '{donation} - Offline Donation Instructions', 'give' ),
341
			'row_classes' => 'give-subfield',
342
			'type'        => 'text'
343
		),
344
		array(
345
			'id'          => $prefix . 'offline_donation_email',
346
			'name'        => esc_attr__( 'Offline Donation Email Instructions', 'give' ),
347
			'desc'        => esc_attr__( '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' ),
348
			'default'     => give_get_default_offline_donation_email_content(),
349
			'type'        => 'wysiwyg',
350
			'row_classes' => 'give-subfield',
351
			'options'     => array(
352
				'textarea_rows' => 6,
353
			)
354
		)
355
	);
356
357
	return array_merge( $settings, $check_settings );
358
}
359
360
add_filter( 'give_forms_display_options_metabox_fields', 'give_offline_add_settings' );
361
362
363
/**
364
 * Offline Donation Content
365
 *
366
 * Get default offline donation text
367
 *
368
 * @return mixed|void
369
 */
370
function give_get_default_offline_donation_content() {
371
372
	$sitename = get_bloginfo( 'sitename' );
373
374
	$default_text = '<p>' . esc_attr__( 'In order to make an offline donation we ask that you please follow these instructions', 'give' ) . ': </p>';
375
	$default_text .= '<ol>';
376
	$default_text .= '<li>';
377
	$default_text .= sprintf(
378
		/* translators: %s: site name */
379
		esc_html__( 'Make a check payable to "%s"', 'give' ),
380
		$sitename
381
	);
382
	$default_text .= '</li>';
383
	$default_text .= '<li>';
384
	$default_text .= sprintf(
385
		/* translators: %s: site name */
386
		esc_html__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
387
		$sitename
388
	);
389
	$default_text .= '</li>';
390
	$default_text .= '<li>' . esc_html__( 'Please mail your check to:', 'give' ) . '</li>';
391
	$default_text .= '</ol>';
392
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
393
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
394
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
395
	$default_text .= '<p>' . esc_attr__( 'All contributions will be gratefully acknowledged and are tax deductible.', 'give' ) . '</p>';
396
397
	return apply_filters( 'give_default_offline_donation_content', $default_text );
398
399
}
400
401
/**
402
 * Offline Donation Email Content
403
 *
404
 * Gets the default offline donation email content
405
 *
406
 * @return mixed|void
407
 */
408
function give_get_default_offline_donation_email_content() {
409
410
	$sitename      = get_bloginfo( 'sitename' );
411
	$default_text  = '<p>' . esc_html__( 'Dear {name},', 'give' ) . '</p>';
412
	$default_text .= '<p>' . esc_html__( '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>';
413
	$default_text .= '<ol>';
414
	$default_text .= '<li>';
415
	$default_text .= sprintf(
416
		/* translators: %s: site name */
417
		esc_html__( 'Make a check payable to "%s"', 'give' ),
418
		$sitename
419
	);
420
	$default_text .= '</li>';
421
	$default_text .= '<li>';
422
	$default_text .= sprintf(
423
		/* translators: %s: site name */
424
		esc_html__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
425
		$sitename
426
	);
427
	$default_text .= '</li>';
428
	$default_text .= '<li>' . esc_html__( 'Please mail your check to:', 'give' ) . '</li>';
429
	$default_text .= '</ol>';
430
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
431
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
432
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
433
	$default_text .= '<p>' . esc_html__( '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>';
434
	$default_text .= '<p>' . esc_html__( 'Sincerely,', 'give' ) . '</p>';
435
	$default_text .= '<p>' . $sitename . '</p>';
436
437
	return apply_filters( 'give_default_offline_donation_content', $default_text );
438
439
}
440