Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

functions.php ➔ give_admin_email_notice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 71
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 3
nop 2
dl 0
loc 71
ccs 18
cts 27
cp 0.6667
crap 3.3332
rs 9.1369
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 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     https://opensource.org/licenses/gpl-license 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
	 * @param int $payment_id Payment id.
39 42
	 * @param mixed $payment_data Payment meta data.
40
	 *
41 42
	 * @since 1.0
42 42
	 */
43 42
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
44
45 42
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
46 42
47
	/**
48 42
	 * Filters the from email.
49
	 *
50 42
	 * @param int $payment_id Payment id.
51 42
	 * @param mixed $payment_data Payment meta data.
52 42
	 *
53
	 * @since 1.0
54
	 */
55 42
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
56 42
57
	$to_email = give_get_payment_user_email( $payment_id );
58 42
59
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
60 42
61 42
	/**
62 42
	 * Filters the donation email receipt subject.
63 42
	 *
64
	 * @since 1.0
65
	 */
66
	$subject = apply_filters( 'give_donation_subject', wp_strip_all_tags( $subject ), $payment_id );
67
	$subject = give_do_email_tags( $subject, $payment_id );
68
69
	/**
70
	 * Filters the donation email receipt attachments. By default, there is no attachment but plugins can hook in to provide one more multiple for the donor. Examples would be a printable ticket or PDF receipt.
71
	 *
72
	 * @param int $payment_id Payment id.
73
	 * @param mixed $payment_data Payment meta data.
74
	 *
75
	 * @since 1.0
76
	 */
77
	$attachments = apply_filters( 'give_receipt_attachments', array(), $payment_id, $payment_data );
78
	$message     = give_do_email_tags( give_get_email_body_content( $payment_id, $payment_data ), $payment_id );
79
80
	$emails = Give()->emails;
81
82
	$emails->__set( 'from_name', $from_name );
83
	$emails->__set( 'from_email', $from_email );
84
	$emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
85
86
	/**
87
	 * Filters the donation receipt's email headers.
88
	 *
89
	 * @param int $payment_id Payment id.
90
	 * @param mixed $payment_data Payment meta data.
91
	 *
92
	 * @since 1.0
93
	 */
94
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), $payment_id, $payment_data );
95
	$emails->__set( 'headers', $headers );
96
97
	//Send the donation receipt.
98
	$emails->send( $to_email, $subject, $message, $attachments );
99
100
	//If admin notifications are on, send the admin notice.
101
	if ( $admin_notice && ! give_admin_notices_disabled( $payment_id ) ) {
102
		/**
103
		 * Fires in the donation email receipt.
104
		 *
105
		 * When admin email notices are not disabled, you can add new email notices.
106
		 *
107
		 * @since 1.0
108
		 *
109
		 * @param int $payment_id Payment id.
110
		 * @param mixed $payment_data Payment meta data.
111
		 */
112 42
		do_action( 'give_admin_donation_email', $payment_id, $payment_data );
113
	}
114 42
}
115
116
/**
117
 * Email the donation confirmation to the admin accounts for testing.
118 42
 *
119
 * @since 1.0
120
 *
121
 * @return void
122 42
 */
123 42
function give_email_test_donation_receipt() {
124
125 42
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
126 42
127
	/**
128
	 * Filters the from name.
129 42
	 *
130 42
	 * @since 1.7
131 42
	 */
132
	$from_name = apply_filters( 'give_donation_from_name', $from_name, 0, array() );
133 42
134 42
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
135
136 42
	/**
137 42
	 * Filters the from email.
138
	 *
139 42
	 * @since 1.7
140
	 */
141 42
	$from_email = apply_filters( 'give_donation_from_address', $from_email, 0, array() );
142
143 42
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
144 42
	$subject = apply_filters( 'give_donation_subject', wp_strip_all_tags( $subject ), 0 );
145 42
	$subject = give_do_email_tags( $subject, 0 );
146 42
147 42
	$attachments = apply_filters( 'give_receipt_attachments', array(), 0, array() );
148
149 42
	$message = give_email_preview_template_tags( give_get_email_body_content( 0, array() ) );
150
151 42
	$emails = Give()->emails;
152
	$emails->__set( 'from_name', $from_name );
153
	$emails->__set( 'from_email', $from_email );
154
	$emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
155
156
	$headers = apply_filters( 'give_receipt_headers', $emails->get_headers(), 0, array() );
157
	$emails->__set( 'headers', $headers );
158
159
	$emails->send( give_get_admin_notice_emails(), $subject, $message, $attachments );
160
161
}
162
163
/**
164 42
 * Sends the Admin Sale Notification Email
165
 *
166 42
 * @since 1.0
167 42
 *
168
 * @param int $payment_id Payment ID (default: 0)
169 42
 * @param array $payment_data Payment Meta and Data
170
 *
171
 * @return void
172
 */
173
function give_admin_email_notice( $payment_id = 0, $payment_data = array() ) {
174
175
	$payment_id = absint( $payment_id );
176
177
	if ( empty( $payment_id ) ) {
178
		return;
179
	}
180
181
	if ( ! give_get_payment_by( 'id', $payment_id ) ) {
182
		return;
183 42
	}
184
185 42
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
186
187
	/**
188
	 * Filters the from name.
189
	 *
190
	 * @since 1.0
191
	 */
192
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
193
194
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
195
196
	/**
197
	 * Filters the from email.
198
	 *
199
	 * @since 1.0
200
	 */
201
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
202
203
	/* translators: %s: payment id */
204
	$subject = give_get_option( 'donation_notification_subject', sprintf( esc_html__( 'New Donation - Payment #%s', 'give' ), $payment_id ) );
205
206
	/**
207
	 * Filters the donation notification subject.
208
	 *
209
	 * @since 1.0
210
	 */
211
	$subject = apply_filters( 'give_admin_donation_notification_subject', wp_strip_all_tags( $subject ), $payment_id );
212
	$subject = give_do_email_tags( $subject, $payment_id );
213
214
	$headers = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>\r\n";
215
	$headers .= "Reply-To: " . $from_email . "\r\n";
216
	//$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...
217
	$headers .= "Content-Type: text/html; charset=utf-8\r\n";
218
219
	/**
220
	 * Filters the donation notification email headers.
221
	 *
222 42
	 * @since 1.0
223 42
	 */
224 42
	$headers = apply_filters( 'give_admin_donation_notification_headers', $headers, $payment_id, $payment_data );
225 42
226 42
	/**
227 42
	 * Filters the donation notification email attachments. By default, there is no attachment but plugins can hook in to provide one more multiple.
228 42
	 *
229 42
	 * @since 1.0
230 42
	 */
231 42
	$attachments = apply_filters( 'give_admin_donation_notification_attachments', array(), $payment_id, $payment_data );
232
233 42
	$message = give_get_donation_notification_body_content( $payment_id, $payment_data );
234 42
235 42
	$emails = Give()->emails;
236
	$emails->__set( 'from_name', $from_name );
237 42
	$emails->__set( 'from_email', $from_email );
238
	$emails->__set( 'headers', $headers );
239 42
	$emails->__set( 'heading', esc_html__( 'New Donation!', 'give' ) );
240
241 42
	$emails->send( give_get_admin_notice_emails(), $subject, $message, $attachments );
242
243
}
244
245
add_action( 'give_admin_donation_email', 'give_admin_email_notice', 10, 2 );
246
247
/**
248
 * Retrieves the emails for which admin notifications are sent to (these can be changed in the Give Settings).
249
 *
250
 * @since 1.0
251
 * @return mixed
252
 */
253
function give_get_admin_notice_emails() {
254 42
255 42
	$email_option = give_get_option( 'admin_notice_emails' );
256
257 42
	$emails = ! empty( $email_option ) && strlen( trim( $email_option ) ) > 0 ? $email_option : get_bloginfo( 'admin_email' );
258 42
	$emails = array_map( 'trim', explode( "\n", $emails ) );
259 41
260 41
	return apply_filters( 'give_admin_notice_emails', $emails );
261 41
}
262 41
263 42
/**
264 1
 * Checks whether admin donation notices are disabled
265 1
 *
266 1
 * @since 1.0
267 1
 *
268
 * @param int $payment_id
269
 *
270
 * @return mixed
271
 */
272 42
function give_admin_notices_disabled( $payment_id = 0 ) {
273
274
	$retval = give_get_option( 'disable_admin_notices' );
275
276
	return apply_filters( 'give_admin_notices_disabled', $retval, $payment_id );
277
}
278
279
/**
280
 * Get default donation notification email text
281
 *
282
 * Returns the stored email text if available, the standard email text if not
283
 *
284
 * @since  1.0
285
 * @return string $message
286
 */
287
function give_get_default_donation_notification_email() {
288
289
	$default_email_body = esc_html__( 'Hi there,', 'give' ) . "\n\n";
290
	$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";
291
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
292
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
293
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {price}' . "\n";
294
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
295
	$default_email_body .= esc_html__( 'Thank you,', 'give' ) . "\n\n";
296
	$default_email_body .= '{sitename}' . "\n";
297
298
	$custom_message = give_get_option( 'donation_notification' );
299
	$message        = ! empty( $custom_message ) ? $custom_message : $default_email_body;
300
301
	return apply_filters( 'give_default_donation_notification_email', $message );
302
}
303
304
305
/**
306
 * Get default donation receipt email text
307
 *
308
 * Returns the stored email text if available, the standard email text if not
309
 *
310
 * @since  1.3.7
311
 * @return string $message
312
 */
313
function give_get_default_donation_receipt_email() {
314
315
	$default_email_body = esc_html__( 'Dear', 'give' ) . " {name},\n\n";
316
	$default_email_body .= esc_html__( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
317
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
318
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
319
	$default_email_body .= '<strong>' . esc_html__( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
320
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {price}' . "\n";
321
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
322
	$default_email_body .= '<strong>' . esc_html__( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
323
	$default_email_body .= '<strong>' . esc_html__( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
324
	$default_email_body .= '{receipt_link}' . "\n\n";
325
	$default_email_body .= "\n\n";
326
	$default_email_body .= esc_html__( 'Sincerely,', 'give' ) . "\n";
327
	$default_email_body .= '{sitename}' . "\n";
328
329
	$custom_message = give_get_option( 'donation_receipt' );
330
331
	$message = ! empty( $custom_message ) ? $custom_message : $default_email_body;
332
333
	return apply_filters( 'give_default_donation_receipt_email', $message );
334
}
335
336
/**
337
 * Get various correctly formatted names used in emails
338
 *
339
 * @since 1.0
340
 *
341
 * @param $user_info
342
 *
343
 * @return array $email_names
344
 */
345
function give_get_email_names( $user_info ) {
346
	$email_names = array();
347
	$user_info   = maybe_unserialize( $user_info );
348
349
	$email_names['fullname'] = '';
350
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
351
		$user_data               = get_userdata( $user_info['id'] );
352
		$email_names['name']     = $user_info['first_name'];
353
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
354
		$email_names['username'] = $user_data->user_login;
355
	} elseif ( isset( $user_info['first_name'] ) ) {
356
		$email_names['name']     = $user_info['first_name'];
357
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
358
		$email_names['username'] = $user_info['first_name'];
359
	} else {
360
		$email_names['name']     = $user_info['email'];
361
		$email_names['username'] = $user_info['email'];
362
	}
363
364
	return $email_names;
365
}
366