functions.php ➔ give_get_email_names()   C
last analyzed

Complexity

Conditions 14
Paths 37

Size

Total Lines 69

Duplication

Lines 6
Ratio 8.7 %

Code Coverage

Tests 16
CRAP Score 19.5228

Importance

Changes 0
Metric Value
cc 14
nc 37
nop 2
dl 6
loc 69
ccs 16
cts 23
cp 0.6957
crap 19.5228
rs 5.6896
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 42
	 * Fire the action
32
	 */
33 42
	do_action( 'give_donation-receipt_email_notification', $payment_id );
34 42
35
	// If admin notifications are on, send the admin notice.
36 42
	if ( $admin_notice && give_is_setting_enabled( Give_Email_Notification::get_instance('new-donation' )->get_notification_status() ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
37 42
		/**
38
		 * Fires in the donation email receipt.
39 42
		 *
40
		 * When admin email notices are not disabled, you can add new email notices.
41 42
		 *
42 42
		 * @since 1.0
43 42
		 *
44
		 * @param int   $payment_id   Payment id.
45 42
		 * @param mixed $payment_data Payment meta data.
46 42
		 */
47
		do_action( 'give_new-donation_email_notification', $payment_id, give_get_payment_meta( $payment_id ) );
48 42
	}
49
}
50 42
51 42
/**
52 42
 * Sends the Admin Sale Notification Email
53
 *
54
 * @since 1.0
55 42
 *
56 42
 * @param int $payment_id Payment ID (default: 0)
57
 *
58 42
 * @return void
59
 */
60 42
function give_admin_email_notice( $payment_id ) {
61 42
	/**
62 42
	 * Fires in the donation email receipt.
63 42
	 *
64
	 * When admin email notices are not disabled, you can add new email notices.
65
	 *
66
	 * @since 1.0
67
	 *
68
	 * @param int   $payment_id   Payment id.
69
	 * @param mixed $payment_data Payment meta data.
70
	 */
71
	do_action( 'give_new-donation_email_notification', $payment_id );
72
}
73
74
add_action( 'give_admin_donation_email', 'give_admin_email_notice' );
75
76
77
/**
78
 * Get default donation notification email text
79
 *
80
 * Returns the stored email text if available, the standard email text if not
81
 *
82
 * @since  1.0
83
 * @return string $message
84
 */
85
function give_get_default_donation_notification_email() {
86
87
	$default_email_body = __( 'Hi there,', 'give' ) . "\n\n";
88
	$default_email_body .= __( 'This email is to inform you that a new donation has been made on your website:', 'give' ) . ' {site_url}' . ".\n\n";
89
	$default_email_body .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
90
	$default_email_body .= '<strong>' . __( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
91
	$default_email_body .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
92
	$default_email_body .= '<strong>' . __( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
93
	$default_email_body .= __( 'Thank you,', 'give' ) . "\n\n";
94
	$default_email_body .= '{sitename}' . "\n";
95
96
	return apply_filters( 'give_default_donation_notification_email', $default_email_body );
97
}
98
99
100
/**
101
 * Get default donation receipt email text
102
 *
103
 * Returns the stored email text if available, the standard email text if not
104
 *
105
 * @since  1.3.7
106
 * @return string $message
107
 */
108
function give_get_default_donation_receipt_email() {
109
110
	$default_email_body = __( 'Dear', 'give' ) . " {name},\n\n";
111
	$default_email_body .= __( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
112 42
	$default_email_body .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
113
	$default_email_body .= '<strong>' . __( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
114 42
	$default_email_body .= '<strong>' . __( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
115
	$default_email_body .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
116
	$default_email_body .= '<strong>' . __( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
117
	$default_email_body .= '<strong>' . __( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
118 42
	$default_email_body .= '<strong>' . __( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
119
	$default_email_body .= '{receipt_link}' . "\n\n";
120
	$default_email_body .= "\n\n";
121
	$default_email_body .= __( 'Sincerely,', 'give' ) . "\n";
122 42
	$default_email_body .= '{sitename}' . "\n";
123 42
124
	return apply_filters( 'give_default_donation_receipt_email', $default_email_body );
125 42
}
126 42
127
/**
128
 * Get various correctly formatted names used in emails
129 42
 *
130 42
 * @since 1.0
131 42
 *
132
 * @param array             $user_info List of User Information.
133 42
 * @param Give_Payment|bool $payment   Payment Object.
134 42
 *
135
 * @return array $email_names
136 42
 */
137 42
function give_get_email_names( $user_info, $payment = false ) {
138
	$email_names = array();
139 42
140
	if ( is_a( $payment, 'Give_Payment' ) ) {
141 42
142
		if ( $payment->user_id > 0 ) {
143 42
144 42
			$user_data               = get_userdata( $payment->user_id );
145 42
			$email_names['name']     = $payment->first_name;
146 42
			$email_names['fullname'] = trim( $payment->first_name . ' ' . $payment->last_name );
147 42
			$email_names['username'] = $user_data->user_login;
148
149 42
		} elseif ( ! empty( $payment->first_name ) ) {
150
151 42
			$email_names['name']     = $payment->first_name;
152
			$email_names['fullname'] = trim( $payment->first_name . ' ' . $payment->last_name );
153
			$email_names['username'] = $payment->first_name;
154
155
		} else {
156
157
			$email_names['name']     = $payment->email;
158
			$email_names['username'] = $payment->email;
159
160
		}
161
	} else {
162
163
		// Support for old serialized data.
164 42
		if ( is_serialized( $user_info ) ) {
165
166 42
			// Security check.
167 42
			preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $user_info, $matches );
168
			if ( ! empty( $matches ) ) {
169 42
				return array(
170
					'name'     => '',
171
					'fullname' => '',
172
					'username' => '',
173
				);
174
			} else {
175
				$user_info = maybe_unserialize( $user_info );
176
			}
177
		}
178
179
		if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
180
			$user_data               = get_userdata( $user_info['id'] );
181
			$email_names['name']     = $user_info['first_name'];
182
			$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
183 42
			$email_names['username'] = $user_data->user_login;
184
		} elseif ( isset( $user_info['first_name'] ) ) {
185 42
			$email_names['name']     = $user_info['first_name'];
186
			$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
187
			$email_names['username'] = $user_info['first_name'];
188
		} else {
189
			$email_names['name']     = $user_info['email'];
190
			$email_names['username'] = $user_info['email'];
191
		}
192
	} // End if().
193
194
	// Set title prefix to name, if non empty.
195 View Code Duplication
	if ( ! empty( $user_info['title'] ) && ! empty( $user_info['last_name'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
		$email_names['name'] = give_get_donor_name_with_title_prefixes( $user_info['title'], $user_info['last_name'] );
197
	}
198
199
	// Set title prefix to fullname, if non empty.
200 View Code Duplication
	if ( ! empty( $user_info['title'] ) && ! empty( $email_names['fullname'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
		$email_names['fullname'] = give_get_donor_name_with_title_prefixes( $user_info['title'], $email_names['fullname'] );
202
	}
203
204
	return $email_names;
205
}
206
207
/**
208
 * Send email to admin when user tries to login and restricted due to user - donor disconnection.
209
 *
210
 * @param int $user_id  User ID.
211
 * @param int $donor_id Donor ID.
212
 *
213
 * @since 1.8.14
214
 */
215
function give_admin_email_user_donor_disconnection( $user_id, $donor_id ) {
216
217
	$user_id  = absint( $user_id );
218
	$donor_id = absint( $donor_id );
219
220
	// Bail Out, if user id doesn't exists.
221
	if ( empty( $user_id ) ) {
222 42
		return;
223 42
	}
224 42
225 42
	// Bail Out, if donor id doesn't exists.
226 42
	if ( empty( $donor_id ) ) {
227 42
		return;
228 42
	}
229 42
230 42
	$from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
231 42
232
	$from_email = give_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
233 42
234 42
	/* translators: %s: payment id */
235 42
	$subject = __( 'Attention: User tries to login whose Donor profile is disconnected!', 'give' );
236
237 42
	/**
238
	 * Filters the Donor-User Disconnection notification subject.
239 42
	 *
240
	 * @since 1.8.14
241 42
	 */
242
	$subject = apply_filters( 'give_admin_donor_user_disconnection_notification_subject', wp_strip_all_tags( $subject ) );
243
244
	$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...
245
	$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...
246
	$headers .= "Content-Type: text/html; charset=utf-8\r\n";
247
248
	/**
249
	 * Filters the Donor-User Disconnection notification email headers.
250
	 *
251
	 * @since 1.8.14
252
	 */
253
	$headers = apply_filters( 'give_admin_donor_user_disconnection_notification_headers', $headers );
254 42
255 42
	$message = __( 'Hi Admin,', 'give' ) . "\n\n";
256
	$message .= __( 'This email is to inform you that a user has tried logging in. But, User was unable to login due to User-Donor profile disconnection.', 'give' ) . "\n\n";
257 42
	$message .= __( 'Do you want to reconnect User and Donor profile again?', 'give' ) . "\n\n";
258 42
	$message .= sprintf(
259 41
		'<a href="%1$s">%2$s</a>',
260 41
		esc_url( admin_url() . 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&user_id=' . $user_id . '&give-messages[]=reconnect-user' ),
261 41
		__( 'Reconnect User', 'give' ) . "\n\n"
262 41
	);
263 42
	$message .= __( 'Thank you,', 'give' ) . "\n\n";
264 1
	$message .= '{sitename}' . "\n";
265 1
266 1
	$emails = Give()->emails;
267 1
	$emails->__set( 'from_name', $from_name );
268
	$emails->__set( 'from_email', $from_email );
269
	$emails->__set( 'headers', $headers );
270
	$emails->__set( 'heading', __( 'User - Donor Profile Disconnection', 'give' ) );
271
272 42
	$emails->send( give_get_admin_notice_emails(), $subject, give_do_email_tags( $message ) );
0 ignored issues
show
Bug introduced by
The call to give_do_email_tags() misses a required argument $tag_args.

This check looks for function calls that miss required arguments.

Loading history...
273
274
}
275