Completed
Push — issues/611 ( 0564a6...f51557 )
by Ravinder
20:55
created

functions.php ➔ give_get_admin_notice_emails()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 10
nop 1
dl 0
loc 16
rs 8.8571
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
 * @since 1.9 Adds $email_notification_id param.
82
 *
83
 * @param string $email_notitfication_id Email ID.
84
 *
85
 * @return mixed
86
 */
87
function give_get_admin_notice_emails( $email_notitfication_id = '' ) {
88
	$emails = array();
89
90
	if( ! empty( $email_notitfication_id ) ) {
91
		$emails = give_check_variable( give_get_option( "{$email_notitfication_id}_recipient", '' ), 'empty', array() );
92
	}
93
94
	// Backward compatibility.
95
	if( empty( $emails) ) {
96
		$email_option = give_get_option( 'admin_notice_emails' );
97
		$emails = ! empty( $email_option ) && strlen( trim( $email_option ) ) > 0 ? $email_option : get_bloginfo( 'admin_email' );
98
		$emails = array_map( 'trim', explode( "\n", $emails ) );
99
	}
100
101
	return apply_filters( 'give_admin_notice_emails', $emails );
102
}
103
104
/**
105
 * Checks whether admin donation notices are disabled
106
 *
107
 * @since 1.0
108
 *
109
 * @param int $payment_id
110
 *
111
 * @return mixed
112
 */
113
function give_admin_notices_disabled( $payment_id = 0 ) {
114
115
	return apply_filters(
116
		'give_admin_notices_disabled',
117
		! give_is_setting_enabled( give_get_option( 'new-donation_notification' ) ),
118
		$payment_id
119
	);
120
}
121
122
/**
123
 * Get default donation notification email text
124
 *
125
 * Returns the stored email text if available, the standard email text if not
126
 *
127
 * @since  1.0
128
 * @return string $message
129
 */
130
function give_get_default_donation_notification_email() {
131
132
	$default_email_body = esc_html__( 'Hi there,', 'give' ) . "\n\n";
133
	$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";
134
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {name}' . "\n";
135
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
136
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
137
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n\n";
138
	$default_email_body .= esc_html__( 'Thank you,', 'give' ) . "\n\n";
139
	$default_email_body .= '{sitename}' . "\n";
140
141
	return apply_filters( 'give_default_donation_notification_email', $default_email_body );
142
}
143
144
145
/**
146
 * Get default donation receipt email text
147
 *
148
 * Returns the stored email text if available, the standard email text if not
149
 *
150
 * @since  1.3.7
151
 * @return string $message
152
 */
153
function give_get_default_donation_receipt_email() {
154
155
	$default_email_body = esc_html__( 'Dear', 'give' ) . " {name},\n\n";
156
	$default_email_body .= esc_html__( 'Thank you for your donation. Your generosity is appreciated! Here are the details of your donation:', 'give' ) . "\n\n";
157
	$default_email_body .= '<strong>' . esc_html__( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
158
	$default_email_body .= '<strong>' . esc_html__( 'Donation:', 'give' ) . '</strong> {donation}' . "\n";
159
	$default_email_body .= '<strong>' . esc_html__( 'Donation Date:', 'give' ) . '</strong> {date}' . "\n";
160
	$default_email_body .= '<strong>' . esc_html__( 'Amount:', 'give' ) . '</strong> {amount}' . "\n";
161
	$default_email_body .= '<strong>' . esc_html__( 'Payment Method:', 'give' ) . '</strong> {payment_method}' . "\n";
162
	$default_email_body .= '<strong>' . esc_html__( 'Payment ID:', 'give' ) . '</strong> {payment_id}' . "\n";
163
	$default_email_body .= '<strong>' . esc_html__( 'Receipt ID:', 'give' ) . '</strong> {receipt_id}' . "\n\n";
164
	$default_email_body .= '{receipt_link}' . "\n\n";
165
	$default_email_body .= "\n\n";
166
	$default_email_body .= esc_html__( 'Sincerely,', 'give' ) . "\n";
167
	$default_email_body .= '{sitename}' . "\n";
168
169
	return apply_filters( 'give_default_donation_receipt_email', $default_email_body );
170
}
171
172
/**
173
 * Get various correctly formatted names used in emails
174
 *
175
 * @since 1.0
176
 *
177
 * @param $user_info
178
 *
179
 * @return array $email_names
180
 */
181
function give_get_email_names( $user_info ) {
182
	$email_names = array();
183
	$user_info   = maybe_unserialize( $user_info );
184
185
	$email_names['fullname'] = '';
186
	if ( isset( $user_info['id'] ) && $user_info['id'] > 0 && isset( $user_info['first_name'] ) ) {
187
		$user_data               = get_userdata( $user_info['id'] );
188
		$email_names['name']     = $user_info['first_name'];
189
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
190
		$email_names['username'] = $user_data->user_login;
191
	} elseif ( isset( $user_info['first_name'] ) ) {
192
		$email_names['name']     = $user_info['first_name'];
193
		$email_names['fullname'] = $user_info['first_name'] . ' ' . $user_info['last_name'];
194
		$email_names['username'] = $user_info['first_name'];
195
	} else {
196
		$email_names['name']     = $user_info['email'];
197
		$email_names['username'] = $user_info['email'];
198
	}
199
200
	return $email_names;
201
}
202