Test Failed
Push — master ( b86487...e5e5a2 )
by Ravinder
07:05
created

functions.php ➔ give_get_email_names()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 38
nc 10
nop 2
dl 0
loc 62
rs 6.4192
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
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
	$payment_data = give_get_payment_meta( $payment_id );
32
33
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
34
35
	/**
36
	 * Filters the from name.
37
	 *
38
	 * @param int   $payment_id   Payment id.
39
	 * @param mixed $payment_data Payment meta data.
40
	 *
41
	 * @since 1.0
42
	 */
43
	$from_name = apply_filters( 'give_donation_from_name', $from_name, $payment_id, $payment_data );
44
45
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
46
47
	/**
48
	 * Filters the from email.
49
	 *
50
	 * @param int   $payment_id   Payment id.
51
	 * @param mixed $payment_data Payment meta data.
52
	 *
53
	 * @since 1.0
54
	 */
55
	$from_email = apply_filters( 'give_donation_from_address', $from_email, $payment_id, $payment_data );
56
57
	$to_email = give_get_payment_user_email( $payment_id );
58
59
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
60
61
	/**
62
	 * Filters the donation email receipt subject.
63
	 *
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
		do_action( 'give_admin_donation_email', $payment_id, $payment_data );
113
	}
114
}
115
116
/**
117
 * Email the donation confirmation to the admin accounts for testing.
118
 *
119
 * @since 1.0
120
 *
121
 * @return void
122
 */
123
function give_email_test_donation_receipt() {
124
125
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
126
127
	/**
128
	 * Filters the from name.
129
	 *
130
	 * @since 1.7
131
	 */
132
	$from_name = apply_filters( 'give_donation_from_name', $from_name, 0, array() );
133
134
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
135
136
	/**
137
	 * Filters the from email.
138
	 *
139
	 * @since 1.7
140
	 */
141
	$from_email = apply_filters( 'give_donation_from_address', $from_email, 0, array() );
142
143
	$subject = give_get_option( 'donation_subject', esc_html__( 'Donation Receipt', 'give' ) );
144
	$subject = apply_filters( 'give_donation_subject', wp_strip_all_tags( $subject ), 0 );
145
	$subject = give_do_email_tags( $subject, 0 );
146
147
	$attachments = apply_filters( 'give_receipt_attachments', array(), 0, array() );
148
149
	$message = give_email_preview_template_tags( give_get_email_body_content( 0, array() ) );
150
151
	$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
 * Sends the Admin Sale Notification Email
165
 *
166
 * @since 1.0
167
 *
168
 * @param int   $payment_id   Payment ID (default: 0)
169
 * @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
	}
184
185
	$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";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal From: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
215
	$headers .= "Reply-To: " . $from_email . "\r\n";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Reply-To: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
216
	//$headers  .= "MIME-Version: 1.0\r\n";
217
	$headers .= "Content-Type: text/html; charset=utf-8\r\n";
218
219
	/**
220
	 * Filters the donation notification email headers.
221
	 *
222
	 * @since 1.0
223
	 */
224
	$headers = apply_filters( 'give_admin_donation_notification_headers', $headers, $payment_id, $payment_data );
225
226
	/**
227
	 * Filters the donation notification email attachments. By default, there is no attachment but plugins can hook in to provide one more multiple.
228
	 *
229
	 * @since 1.0
230
	 */
231
	$attachments = apply_filters( 'give_admin_donation_notification_attachments', array(), $payment_id, $payment_data );
232
233
	$message = give_get_donation_notification_body_content( $payment_id, $payment_data );
234
235
	$emails = Give()->emails;
236
	$emails->__set( 'from_name', $from_name );
237
	$emails->__set( 'from_email', $from_email );
238
	$emails->__set( 'headers', $headers );
239
	$emails->__set( 'heading', esc_html__( 'New Donation!', 'give' ) );
240
241
	$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
255
	$email_option = give_get_option( 'admin_notice_emails' );
256
257
	$emails = ! empty( $email_option ) && strlen( trim( $email_option ) ) > 0 ? $email_option : get_bloginfo( 'admin_email' );
258
	$emails = array_map( 'trim', explode( "\n", $emails ) );
259
260
	return apply_filters( 'give_admin_notice_emails', $emails );
261
}
262
263
/**
264
 * Checks whether admin donation notices are disabled
265
 *
266
 * @since 1.0
267
 *
268
 * @param int $payment_id
269
 *
270
 * @return mixed
271
 */
272
function give_admin_notices_disabled( $payment_id = 0 ) {
273
274
	return apply_filters(
275
		'give_admin_notices_disabled',
276
		! give_is_setting_enabled( give_get_option( 'admin_notices' ) ),
277
		$payment_id
278
	);
279
}
280
281
/**
282
 * Get default donation notification email text
283
 *
284
 * Returns the stored email text if available, the standard email text if not
285
 *
286
 * @since  1.0
287
 * @return string $message
288
 */
289
function give_get_default_donation_notification_email() {
290
291
	$default_email_body = esc_html__( 'Hi there,', 'give' ) . "\n\n";
292
	$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";
293
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
294
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
295
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
296
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
297
	$default_email_body .= esc_html__( 'Thank you,', 'give' ) . "\n\n";
298
	$default_email_body .= '{sitename}' . "\n";
299
300
	$custom_message = give_get_option( 'donation_notification' );
301
	$message        = ! empty( $custom_message ) ? $custom_message : $default_email_body;
302
303
	return apply_filters( 'give_default_donation_notification_email', $message );
304
}
305
306
307
/**
308
 * Get default donation receipt email text
309
 *
310
 * Returns the stored email text if available, the standard email text if not
311
 *
312
 * @since  1.3.7
313
 * @return string $message
314
 */
315
function give_get_default_donation_receipt_email() {
316
317
	$default_email_body = esc_html__( 'Dear', 'give' ) . " {name},\n\n";
318
	$default_email_body .= esc_html__( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
319
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
320
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
321
	$default_email_body .= '<strong>' . esc_html__( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
322
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
323
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
324
	$default_email_body .= '<strong>' . esc_html__( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
325
	$default_email_body .= '<strong>' . esc_html__( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
326
	$default_email_body .= '{receipt_link}' . "\n\n";
327
	$default_email_body .= "\n\n";
328
	$default_email_body .= esc_html__( 'Sincerely,', 'give' ) . "\n";
329
	$default_email_body .= '{sitename}' . "\n";
330
331
	$custom_message = give_get_option( 'donation_receipt' );
332
333
	$message = ! empty( $custom_message ) ? $custom_message : $default_email_body;
334
335
	return apply_filters( 'give_default_donation_receipt_email', $message );
336
}
337
338
/**
339
 * Get various correctly formatted names used in emails
340
 *
341
 * @since 1.0
342
 *
343
 * @param $user_info
344
 * @param $payment Give_Payment|bool for getting the names.
345
 *
346
 * @return array $email_names
347
 */
348
function give_get_email_names( $user_info, $payment = false ) {
349
	$email_names = array();
350
351
	if ( is_a( $payment, 'Give_Payment' ) ) {
352
353
		if ( $payment->user_id > 0 ) {
354
355
			$user_data               = get_userdata( $payment->user_id );
356
			$email_names['name']     = $payment->first_name;
357
			$email_names['fullname'] = trim( $payment->first_name . ' ' . $payment->last_name );
358
			$email_names['username'] = $user_data->user_login;
359
360
		} elseif ( ! empty( $payment->first_name ) ) {
361
362
			$email_names['name']     = $payment->first_name;
363
			$email_names['fullname'] = trim( $payment->first_name . ' ' . $payment->last_name );
364
			$email_names['username'] = $payment->first_name;
365
366
		} else {
367
368
			$email_names['name']     = $payment->email;
369
			$email_names['username'] = $payment->email;
370
371
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
372
373
	} else {
374
375
		// Support for old serialized data
376
		if ( is_serialized( $user_info ) ) {
377
378
			// Security check.
379
			preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $user_info, $matches );
380
			if ( ! empty( $matches ) ) {
381
				return array(
382
					'name'     => '',
383
					'fullname' => '',
384
					'username' => '',
385
				);
386
			} else {
387
				$user_info = maybe_unserialize( $user_info );
388
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
389
390
		}
391
392
		if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
393
			$user_data               = get_userdata( $user_info['id'] );
394
			$email_names['name']     = $user_info['first_name'];
395
			$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
396
			$email_names['username'] = $user_data->user_login;
397
		} elseif ( isset( $user_info['first_name'] ) ) {
398
			$email_names['name']     = $user_info['first_name'];
399
			$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
400
			$email_names['username'] = $user_info['first_name'];
401
		} else {
402
			$email_names['name']     = $user_info['email'];
403
			$email_names['username'] = $user_info['email'];
404
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
405
406
	}
407
408
	return $email_names;
409
}
410