functions.php ➔ give_email_donation_receipt()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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