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

functions.php ➔ give_email_donation_receipt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 57
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.1707

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
nc 2
nop 2
dl 0
loc 57
ccs 22
cts 30
cp 0.7332
crap 3.1707
rs 9.6818
c 5
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 29 and the first side effect is on line 14.

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
 * Email Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Emails
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
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Email Donation Receipt
19
 *
20
 * Email the donation confirmation to the donor via the customizable "Donation Receipt" settings
21
 *
22
 * @since 1.0
23
 *
24
 * @param int $payment_id Payment ID
25
 * @param bool $admin_notice Whether to send the admin email notification or not (default: true)
26
 *
27
 * @return void
28
 */
29
function give_email_donation_receipt( $payment_id, $admin_notice = true ) {
30
31 42
	$payment_data = give_get_payment_meta( $payment_id );
32
33 42
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
34 42
35
	/**
36 42
	 * Filters the from name.
37 42
	 *
38
	 * @since 1.7
39 42
	 */
40
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
41 42
42 42
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
43 42
44
	/**
45 42
	 * Filters the from email.
46 42
	 *
47
	 * @since 1.7
48 42
	 */
49
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
50 42
51 42
	$to_email = give_get_payment_user_email( $payment_id );
52 42
53
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
54
	$subject = apply_filters( 'give_donation_subject', wp_strip_all_tags( $subject ), $payment_id );
55 42
	$subject = give_do_email_tags( $subject, $payment_id );
56 42
57
	$attachments = apply_filters( 'give_receipt_attachments', array(), $payment_id, $payment_data );
58 42
	$message     = give_do_email_tags( give_get_email_body_content( $payment_id, $payment_data ), $payment_id );
59
60 42
	$emails = Give()->emails;
61 42
62 42
	$emails->__set( 'from_name', $from_name );
63 42
	$emails->__set( 'from_email', $from_email );
64
	$emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
65
66
67
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), $payment_id, $payment_data );
68
	$emails->__set( 'headers', $headers );
69
70
	$emails->send( $to_email, $subject, $message, $attachments );
71
72
	if ( $admin_notice && ! give_admin_notices_disabled( $payment_id ) ) {
73
		/**
74
		 * Fires in the email donation receipt.
75
		 *
76
		 * When admin notices are not disabled, you can add new sale notices.
77
		 *
78
		 * @since 1.0
79
		 *
80
		 * @param int   $payment_id   Payment id.
81
		 * @param mixed $payment_data Payment meta data.
82
		 */
83
		do_action( 'give_admin_sale_notice', $payment_id, $payment_data );
84
	}
85
}
86
87
/**
88
 * Email the donation confirmation to the admin accounts for testing.
89
 *
90
 * @since 1.0
91
 *
92
 * @return void
93
 */
94
function give_email_test_donation_receipt() {
95
96
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
97
98
	/**
99
	 * Filters the from name.
100
	 *
101
	 * @since 1.7
102
	 */
103
	$from_name = apply_filters( 'give_donation_from_name', $from_name, 0, array() );
104
105
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
106
107
	/**
108
	 * Filters the from email.
109
	 *
110
	 * @since 1.7
111
	 */
112 42
	$from_email = apply_filters( 'give_donation_from_address', $from_email, 0, array() );
113
114 42
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
115
	$subject = apply_filters( 'give_donation_subject', wp_strip_all_tags( $subject ), 0 );
116
	$subject = give_do_email_tags( $subject, 0 );
117
118 42
	$attachments = apply_filters( 'give_receipt_attachments', array(), 0, array() );
119
120
	$message = give_email_preview_template_tags( give_get_email_body_content( 0, array() ) );
121
122 42
	$emails = Give()->emails;
123 42
	$emails->__set( 'from_name', $from_name );
124
	$emails->__set( 'from_email', $from_email );
125 42
	$emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
126 42
127
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), 0, array() );
128
	$emails->__set( 'headers', $headers );
129 42
130 42
	$emails->send( give_get_admin_notice_emails(), $subject, $message, $attachments );
131 42
132
}
133 42
134 42
/**
135
 * Sends the Admin Sale Notification Email
136 42
 *
137 42
 * @since 1.0
138
 *
139 42
 * @param int $payment_id Payment ID (default: 0)
140
 * @param array $payment_data Payment Meta and Data
141 42
 *
142
 * @return void
143 42
 */
144 42
function give_admin_email_notice( $payment_id = 0, $payment_data = array() ) {
145 42
146 42
	$payment_id = absint( $payment_id );
147 42
148
	if ( empty( $payment_id ) ) {
149 42
		return;
150
	}
151 42
152
	if ( ! give_get_payment_by( 'id', $payment_id ) ) {
153
		return;
154
	}
155
156
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
157
158
	/**
159
	 * Filters the from name.
160
	 *
161
	 * @since 1.7
162
	 */
163
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
164 42
165
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
166 42
167 42
	/**
168
	 * Filters the from email.
169 42
	 *
170
	 * @since 1.7
171
	 */
172
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
173
174
	/* translators: %s: payment id */
175
	$subject = give_get_option( 'donation_notification_subject', sprintf( esc_html__( 'New Donation - Payment #%s', 'give' ), $payment_id ) );
176
	$subject = apply_filters( 'give_admin_donation_notification_subject', wp_strip_all_tags( $subject ), $payment_id );
177
	$subject = give_do_email_tags( $subject, $payment_id );
178
179
	$headers = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>\r\n";
180
	$headers .= "Reply-To: " . $from_email . "\r\n";
181
	//$headers  .= "MIME-Version: 1.0\r\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
182
	$headers .= "Content-Type: text/html; charset=utf-8\r\n";
183 42
	$headers = apply_filters( 'give_admin_donation_notification_headers', $headers, $payment_id, $payment_data );
184
185 42
	$attachments = apply_filters( 'give_admin_donation_notification_attachments', array(), $payment_id, $payment_data );
186
187
	$message = give_get_donation_notification_body_content( $payment_id, $payment_data );
188
189
	$emails = Give()->emails;
190
	$emails->__set( 'from_name', $from_name );
191
	$emails->__set( 'from_email', $from_email );
192
	$emails->__set( 'headers', $headers );
193
	$emails->__set( 'heading', esc_html__( 'New Donation!', 'give' ) );
194
195
	$emails->send( give_get_admin_notice_emails(), $subject, $message, $attachments );
196
197
}
198
199
add_action( 'give_admin_sale_notice', 'give_admin_email_notice', 10, 2 );
200
201
/**
202
 * Retrieves the emails for which admin notifications are sent to (these can be changed in the Give Settings).
203
 *
204
 * @since 1.0
205
 * @return mixed
206
 */
207
function give_get_admin_notice_emails() {
208
209
	$email_option = give_get_option('admin_notice_emails');
210
211
	$emails = !empty( $email_option ) && strlen( trim( $email_option ) ) > 0 ? $email_option : get_bloginfo( 'admin_email' );
212
	$emails = array_map( 'trim', explode( "\n", $emails ) );
213
214
	return apply_filters( 'give_admin_notice_emails', $emails );
215
}
216
217
/**
218
 * Checks whether admin donation notices are disabled
219
 *
220
 * @since 1.0
221
 *
222 42
 * @param int $payment_id
223 42
 *
224 42
 * @return mixed
225 42
 */
226 42
function give_admin_notices_disabled( $payment_id = 0 ) {
227 42
228 42
	$retval = give_get_option( 'disable_admin_notices' );
229 42
230 42
	return apply_filters( 'give_admin_notices_disabled', $retval, $payment_id );
231 42
}
232
233 42
/**
234 42
 * Get default donation notification email text
235 42
 *
236
 * Returns the stored email text if available, the standard email text if not
237 42
 *
238
 * @since  1.0
239 42
 * @return string $message
240
 */
241 42
function give_get_default_donation_notification_email() {
242
243
	$default_email_body  = esc_html__( 'Hi there,', 'give' ) . "\n\n";
244
	$default_email_body .= esc_html__( 'This email is to inform you that a new donation has been made on your website:', 'give' ) . ' <a href="' . get_bloginfo( 'url' ) . '" target="_blank">' . get_bloginfo( 'url' ) . '</a>' . ".\n\n";
245
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
246
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
247
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {price}' . "\n";
248
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
249
	$default_email_body .= esc_html__( 'Thank you,', 'give' ) . "\n\n";
250
	$default_email_body .= '{sitename}' . "\n";
251
252
	$custom_message = give_get_option( 'donation_notification' );
253
	$message        = ! empty( $custom_message ) ? $custom_message : $default_email_body;
254 42
255 42
	return apply_filters( 'give_default_donation_notification_email', $message );
256
}
257 42
258 42
259 41
/**
260 41
 * Get default donation receipt email text
261 41
 *
262 41
 * Returns the stored email text if available, the standard email text if not
263 42
 *
264 1
 * @since  1.3.7
265 1
 * @return string $message
266 1
 */
267 1
function give_get_default_donation_receipt_email() {
268
269
	$default_email_body  = esc_html__( 'Dear', 'give' ) . " {name},\n\n";
270
	$default_email_body .= esc_html__( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
271
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
272 42
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
273
	$default_email_body .= '<strong>' . esc_html__( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
274
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {price}' . "\n";
275
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
276
	$default_email_body .= '<strong>' . esc_html__( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
277
	$default_email_body .= '<strong>' . esc_html__( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
278
	$default_email_body .= '{receipt_link}' . "\n\n";
279
	$default_email_body .= "\n\n";
280
	$default_email_body .= esc_html__( 'Sincerely,', 'give' ) . "\n";
281
	$default_email_body .= '{sitename}' . "\n";
282
283
	$custom_message = give_get_option( 'donation_receipt' );
284
285
	$message = ! empty( $custom_message ) ? $custom_message : $default_email_body;
286
287
	return apply_filters( 'give_default_donation_receipt_email', $message );
288
}
289
290
/**
291
 * Get various correctly formatted names used in emails
292
 *
293
 * @since 1.0
294
 *
295
 * @param $user_info
296
 *
297
 * @return array $email_names
298
 */
299
function give_get_email_names( $user_info ) {
300
	$email_names = array();
301
	$user_info   = maybe_unserialize( $user_info );
302
303
	$email_names['fullname'] = '';
304
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
305
		$user_data               = get_userdata( $user_info['id'] );
306
		$email_names['name']     = $user_info['first_name'];
307
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
308
		$email_names['username'] = $user_data->user_login;
309
	} elseif ( isset( $user_info['first_name'] ) ) {
310
		$email_names['name']     = $user_info['first_name'];
311
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
312
		$email_names['username'] = $user_info['first_name'];
313
	} else {
314
		$email_names['name']     = $user_info['email'];
315
		$email_names['username'] = $user_info['email'];
316
	}
317
318
	return $email_names;
319
}
320