Completed
Pull Request — master (#664)
by Devin
19:01
created

offline-donations.php ➔ give_offline_billing_fields()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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