Completed
Push — backup/611 ( 661115 )
by Ravinder
1411:51 queued 1394:16
created

offline-donations.php ➔ give_offline_send_admin_notice()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 6
nop 1
dl 0
loc 48
rs 8.551
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A offline-donations.php ➔ give_get_default_offline_donation_content() 0 30 1
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_send_to_success_page();
133
	} else {
134
		// if errors are present, send the user back to the donation form so they can be corrected
135
		give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
136
	}
137
138
}
139
140
add_action( 'give_gateway_offline', 'give_offline_process_payment' );
141
142
143
/**
144
 * Register gateway settings.
145
 *
146
 * @param $settings
147
 *
148
 * @return array
149
 */
150
function give_offline_add_settings( $settings ) {
151
152
	//Vars
153
	$prefix = '_give_';
154
155
	$is_gateway_active = give_is_gateway_active( 'offline' );
156
157
	//this gateway isn't active
158
	if ( ! $is_gateway_active ) {
159
		//return settings and bounce
160
		return $settings;
161
	}
162
163
	//Fields
164
	$check_settings = array(
165
166
		array(
167
			'name'    => __( 'Offline Donations', 'give' ),
168
			'desc'    => __( 'Do you want to customize the donation instructions for this form?', 'give' ),
169
			'id'      => $prefix . 'customize_offline_donations',
170
			'type'    => 'radio_inline',
171
			'default' => 'global',
172
			'options' => apply_filters( 'give_forms_content_options_select', array(
173
					'global'   => __( 'Global Options', 'give' ),
174
					'enabled'  => __( 'Customize', 'give' ),
175
					'disabled' => __( 'Disable', 'give' ),
176
				)
177
			),
178
		),
179
		array(
180
			'name'        => __( 'Billing Fields', 'give' ),
181
			'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' ),
182
			'id'          => $prefix . 'offline_donation_enable_billing_fields_single',
183
			'row_classes' => 'give-subfield',
184
			'type'        => 'radio_inline',
185
			'default'     => 'disabled',
186
			'options'     => array(
187
				'enabled'  => __( 'Enabled', 'give' ),
188
				'disabled' => __( 'Disabled', 'give' ),
189
			),
190
		),
191
		array(
192
			'id'          => $prefix . 'offline_checkout_notes',
193
			'name'        => __( 'Donation Instructions', 'give' ),
194
			'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' ),
195
			'default'     => give_get_default_offline_donation_content(),
196
			'type'        => 'wysiwyg',
197
			'row_classes' => 'give-subfield',
198
			'options'     => array(
199
				'textarea_rows' => 6,
200
			),
201
		),
202
		array(
203
			'id'          => $prefix . 'offline_donation_subject',
204
			'name'        => __( 'Email Subject', 'give' ),
205
			'desc'        => __( 'Enter the subject line for the donation receipt email.', 'give' ),
206
			'default'     => __( '{form_title} - Offline Donation Instructions', 'give' ),
207
			'row_classes' => 'give-subfield',
208
			'type'        => 'text',
209
		),
210
		array(
211
			'id'          => $prefix . 'offline_donation_email',
212
			'name'        => __( 'Email Instructions', 'give' ),
213
			'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' ),
214
			'default'     => give_get_default_offline_donation_email_content(),
215
			'type'        => 'wysiwyg',
216
			'row_classes' => 'give-subfield',
217
			'options'     => array(
218
				'textarea_rows' => 6,
219
			),
220
		),
221
		array(
222
			'name'  => 'offline_docs',
223
			'type'  => 'docs_link',
224
			'url'   => 'http://docs.givewp.com/offlinegateway',
225
			'title' => __( 'Offline Donations', 'give' ),
226
		),
227
	);
228
229
	return array_merge( $settings, $check_settings );
230
}
231
232
add_filter( 'give_forms_offline_donations_metabox_fields', 'give_offline_add_settings' );
233
234
235
/**
236
 * Offline Donation Content
237
 *
238
 * Get default offline donation text
239
 *
240
 * @return string
241
 */
242
function give_get_default_offline_donation_content() {
243
244
	$sitename = get_bloginfo( 'sitename' );
245
246
	$default_text = '<p>' . __( 'In order to make an offline donation we ask that you please follow these instructions', 'give' ) . ': </p>';
247
	$default_text .= '<ol>';
248
	$default_text .= '<li>';
249
	$default_text .= sprintf(
250
	/* translators: %s: site name */
251
		__( 'Make a check payable to "%s"', 'give' ),
252
		$sitename
253
	);
254
	$default_text .= '</li>';
255
	$default_text .= '<li>';
256
	$default_text .= sprintf(
257
	/* translators: %s: site name */
258
		__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
259
		$sitename
260
	);
261
	$default_text .= '</li>';
262
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
263
	$default_text .= '</ol>';
264
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
265
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
266
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
267
	$default_text .= '<p>' . __( 'All contributions will be gratefully acknowledged and are tax deductible.', 'give' ) . '</p>';
268
269
	return apply_filters( 'give_default_offline_donation_content', $default_text );
270
271
}
272
273
/**
274
 * Offline Donation Email Content
275
 *
276
 * Gets the default offline donation email content
277
 *
278
 * @return string
279
 */
280
function give_get_default_offline_donation_email_content() {
281
282
	$sitename     = get_bloginfo( 'sitename' );
283
	$default_text = '<p>' . __( 'Dear {name},', 'give' ) . '</p>';
284
	$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>';
285
	$default_text .= '<ol>';
286
	$default_text .= '<li>';
287
	$default_text .= sprintf(
288
	/* translators: %s: site name */
289
		__( 'Make a check payable to "%s"', 'give' ),
290
		$sitename
291
	);
292
	$default_text .= '</li>';
293
	$default_text .= '<li>';
294
	$default_text .= sprintf(
295
	/* translators: %s: site name */
296
		__( 'On the memo line of the check, please indicate that the donation is for "%s"', 'give' ),
297
		$sitename
298
	);
299
	$default_text .= '</li>';
300
	$default_text .= '<li>' . __( 'Please mail your check to:', 'give' ) . '</li>';
301
	$default_text .= '</ol>';
302
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>' . $sitename . '</em><br>';
303
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>123 G Street </em><br>';
304
	$default_text .= '&nbsp;&nbsp;&nbsp;&nbsp;<em>San Diego, CA 92101 </em><br>';
305
	$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>';
306
	$default_text .= '<p>' . __( 'Sincerely,', 'give' ) . '</p>';
307
	$default_text .= '<p>' . $sitename . '</p>';
308
309
	return apply_filters( 'give_default_offline_donation_content', $default_text );
310
311
}
312
313
/**
314
 * Set notice for offline donation.
315
 *
316
 * @since 1.7
317
 *
318
 * @param string $notice
319
 * @param int    $id
320
 *
321
 * @return string
322
 */
323
function give_offline_donation_receipt_status_notice( $notice, $id ) {
324
	$payment = new Give_Payment( $id );
325
326
	if ( 'offline' !== $payment->gateway ) {
327
		return $notice;
328
	}
329
330
	return give_output_error( 'Payment Pending: Please follow the instructions below to complete your donation.', false, 'warning' );
331
}
332
333
add_filter( 'give_receipt_status_notice', 'give_offline_donation_receipt_status_notice', 10, 2 );
334
335
/**
336
 * Add offline payment instruction on payment receipt.
337
 *
338
 * @since 1.7
339
 *
340
 * @param WP_Post $payment
341
 *
342
 * @return mixed
343
 */
344
function give_offline_payment_receipt_after( $payment ) {
345
	// Get payment object.
346
	$payment = new Give_Payment( $payment->ID );
347
348
	// Bailout.
349
	if ( 'offline' !== $payment->gateway ) {
350
		return false;
351
	}
352
353
	?>
354
	<tr>
355
		<td scope="row"><strong><?php esc_html_e( 'Offline Payment Instruction:', 'give' ); ?></strong></td>
356
		<td>
357
			<?php echo give_get_offline_payment_instruction( $payment->form_id, true ); ?>
358
		</td>
359
	</tr>
360
	<?php
361
}
362
363
add_filter( 'give_payment_receipt_after', 'give_offline_payment_receipt_after' );
364
365
/**
366
 * Get offline payment instructions.
367
 *
368
 * @since 1.7
369
 *
370
 * @param int  $form_id
371
 * @param bool $wpautop
372
 *
373
 * @return string
374
 */
375
function give_get_offline_payment_instruction( $form_id, $wpautop = false ) {
376
	// Bailout.
377
	if ( ! $form_id ) {
378
		return '';
379
	}
380
381
	$post_offline_customization_option = get_post_meta( $form_id, '_give_customize_offline_donations', true );
382
	$post_offline_instructions         = get_post_meta( $form_id, '_give_offline_checkout_notes', true );
383
	$global_offline_instruction        = give_get_option( 'global_offline_donation_content' );
384
	$offline_instructions              = $global_offline_instruction;
385
386
	if ( $post_offline_customization_option == 'yes' ) {
387
		$offline_instructions = $post_offline_instructions;
388
	}
389
390
	$settings_url = admin_url( 'post.php?post=' . $form_id . '&action=edit&message=1' );
391
392
	/* translators: %s: form settings url */
393
	$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 );
394
395
	return ( $wpautop ? wpautop( $offline_instructions ) : $offline_instructions );
396
}
397
398
399
/**
400
 * Remove offline gateway from gateway list of offline disable for form.
401
 *
402
 * @since  1.8
403
 *
404
 * @param  array $gateway_list
405
 * @param        $form_id
406
 *
407
 * @return array
408
 */
409
function give_filter_offline_gateway( $gateway_list, $form_id ) {
410
	if ( $form_id && ! give_is_setting_enabled( get_post_meta( $form_id, '_give_customize_offline_donations', true ), array(
411
			'enabled',
412
			'global',
413
		) )
414
	) {
415
		unset( $gateway_list['offline'] );
416
	}
417
418
	// Output.
419
	return $gateway_list;
420
}
421
422
add_filter( 'give_enabled_payment_gateways', 'give_filter_offline_gateway', 10, 2 );
423
424
/**
425
 * Set default gateway to global default payment gateway
426
 * if current default gateways selected offline and offline payment gateway is disabled.
427
 *
428
 * @since 1.8
429
 *
430
 * @param  string $meta_key   Meta key.
431
 * @param  string $meta_value Meta value.
432
 * @param  int    $postid     Form ID.
433
 *
434
 * @return void
435
 */
436
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...
437
	if ( ( 'no' === $meta_value ) && ( 'offline' === get_post_meta( $postid, '_give_default_gateway', true ) ) ) {
438
		update_post_meta( $postid, '_give_default_gateway', 'global' );
439
	}
440
}
441
442
add_filter( 'give_save__give_customize_offline_donations', '_give_customize_offline_donations_on_save_callback', 10, 3 );
443