Completed
Push — issues/611 ( 5f4e1c...15a365 )
by Ravinder
19:47
created

functions.php ➔ give_get_admin_notice_emails()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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
	$payment = new Give_Payment( $payment_id );
31
	/**
32
	 * Fire the action
33
	 */
34
	do_action( 'give_donation-receipt_email_notification', $payment_id );
35
36
	//If admin notifications are on, send the admin notice.
37
	if ( $admin_notice && ! give_admin_notices_disabled( $payment_id ) ) {
38
		/**
39
		 * Fires in the donation email receipt.
40
		 *
41
		 * When admin email notices are not disabled, you can add new email notices.
42
		 *
43
		 * @since 1.0
44
		 *
45
		 * @param int   $payment_id   Payment id.
46
		 * @param mixed $payment_data Payment meta data.
47
		 */
48
		do_action( 'give_new-donation_email_notification', $payment_id, $payment->payment_meta );
49
	}
50
}
51
52
/**
53
 * Sends the Admin Sale Notification Email
54
 *
55
 * @since 1.0
56
 *
57
 * @param int $payment_id Payment ID (default: 0)
58
 *
59
 * @return void
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
	 * @since 1.0
68
	 *
69
	 * @param int   $payment_id   Payment id.
70
	 * @param mixed $payment_data Payment meta data.
71
	 */
72
	do_action( 'give_new-donation_email_notification', $payment_id );
73
}
74
75
add_action( 'give_admin_donation_email', 'give_admin_email_notice' );
76
77
/**
78
 * Retrieves the emails for which admin notifications are sent to (these can be changed in the Give Settings).
79
 *
80
 * @since 1.0
81
 * @return mixed
82
 */
83
function give_get_admin_notice_emails() {
84
85
	$email_option = give_get_option( 'admin_notice_emails' );
86
87
	$emails = ! empty( $email_option ) && strlen( trim( $email_option ) ) > 0 ? $email_option : get_bloginfo( 'admin_email' );
88
	$emails = array_map( 'trim', explode( "\n", $emails ) );
89
90
	return apply_filters( 'give_admin_notice_emails', $emails );
91
}
92
93
/**
94
 * Checks whether admin donation notices are disabled
95
 *
96
 * @since 1.0
97
 *
98
 * @param int $payment_id
99
 *
100
 * @return mixed
101
 */
102
function give_admin_notices_disabled( $payment_id = 0 ) {
103
104
	return apply_filters(
105
		'give_admin_notices_disabled',
106
		! give_is_setting_enabled( give_get_option( 'new-donation_notification' ) ),
107
		$payment_id
108
	);
109
}
110
111
/**
112
 * Get default donation notification email text
113
 *
114
 * Returns the stored email text if available, the standard email text if not
115
 *
116
 * @since  1.0
117
 * @return string $message
118
 */
119
function give_get_default_donation_notification_email() {
120
121
	$default_email_body = esc_html__( 'Hi there,', 'give' ) . "\n\n";
122
	$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";
123
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
124
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
125
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
126
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
127
	$default_email_body .= esc_html__( 'Thank you,', 'give' ) . "\n\n";
128
	$default_email_body .= '{sitename}' . "\n";
129
130
	return apply_filters( 'give_default_donation_notification_email', $default_email_body );
131
}
132
133
134
/**
135
 * Get default donation receipt email text
136
 *
137
 * Returns the stored email text if available, the standard email text if not
138
 *
139
 * @since  1.3.7
140
 * @return string $message
141
 */
142
function give_get_default_donation_receipt_email() {
143
144
	$default_email_body = esc_html__( 'Dear', 'give' ) . " {name},\n\n";
145
	$default_email_body .= esc_html__( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
146
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
147
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
148
	$default_email_body .= '<strong>' . esc_html__( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
149
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
150
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
151
	$default_email_body .= '<strong>' . esc_html__( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
152
	$default_email_body .= '<strong>' . esc_html__( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
153
	$default_email_body .= '{receipt_link}' . "\n\n";
154
	$default_email_body .= "\n\n";
155
	$default_email_body .= esc_html__( 'Sincerely,', 'give' ) . "\n";
156
	$default_email_body .= '{sitename}' . "\n";
157
158
	return apply_filters( 'give_default_donation_receipt_email', $default_email_body );
159
}
160
161
/**
162
 * Get various correctly formatted names used in emails
163
 *
164
 * @since 1.0
165
 *
166
 * @param $user_info
167
 *
168
 * @return array $email_names
169
 */
170
function give_get_email_names( $user_info ) {
171
	$email_names = array();
172
	$user_info   = maybe_unserialize( $user_info );
173
174
	$email_names['fullname'] = '';
175
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
176
		$user_data               = get_userdata( $user_info['id'] );
177
		$email_names['name']     = $user_info['first_name'];
178
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
179
		$email_names['username'] = $user_data->user_login;
180
	} elseif ( isset( $user_info['first_name'] ) ) {
181
		$email_names['name']     = $user_info['first_name'];
182
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
183
		$email_names['username'] = $user_info['first_name'];
184
	} else {
185
		$email_names['name']     = $user_info['email'];
186
		$email_names['username'] = $user_info['email'];
187
	}
188
189
	return $email_names;
190
}
191