Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

template.php ➔ give_receipt_head_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 22
ccs 0
cts 0
cp 0
crap 2
rs 9.2
1
<?php
2
/**
3
 * Email Template
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
 * Gets all the email templates that have been registered. The list is extendable
19
 * and more templates can be added.
20
 *
21
 * This is simply a wrapper to Give_Email_Templates->get_templates()
22
 *
23
 * @since 1.0
24
 * @return array $templates All the registered email templates.
25
 */
26
function give_get_email_templates() {
27
	$templates = new Give_Emails;
28
29
	return $templates->get_templates();
30
}
31
32
/**
33
 * Email Template Tags.
34
 *
35
 * @since 1.0
36
 *
37
 * @param string $message      Message with the template tags.
38
 * @param array  $payment_data Payment Data.
39
 * @param int    $payment_id   Payment ID.
40
 * @param bool   $admin_notice Whether or not this is a notification email.
41
 *
42
 * @return string $message Fully formatted message
43
 */
44
function give_email_template_tags( $message, $payment_data, $payment_id, $admin_notice = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $admin_notice is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
	return give_do_email_tags( $message, $payment_id );
46
}
47
48
/**
49
 * Email Preview Template Tags.
50
 *
51
 * Provides sample content for the preview email functionality within settings > email.
52
 *
53
 * @since 1.0
54
 *
55
 * @param string $message Email message with template tags
56
 *
57
 * @return string $message Fully formatted message
58
 */
59
function give_email_preview_template_tags( $message ) {
60
61
	$price = give_currency_filter( give_format_amount( 10.50, array( 'sanitize' => false ) ) );
62
63
	$gateway = 'PayPal';
64
65
	$receipt_id = strtolower( md5( uniqid() ) );
66
67
	$payment_id = rand( 1, 100 );
68
	$receipt_link_url = esc_url( add_query_arg( array( 'payment_key' => $receipt_id ), give_get_history_page_uri() ) );
69
70
	$receipt_link     = sprintf(
71
		'<a href="%1$s">%2$s</a>',
72
		$receipt_link_url,
73
		esc_html__( 'View the receipt in your browser &raquo;', 'give' )
74
	);
75
76
	// Set user.
77
	$user = wp_get_current_user();
78
79
	$message = str_replace( '{name}', $user->display_name, $message );
80
	$message = str_replace( '{fullname}', $user->display_name, $message );
81
	$message = str_replace( '{username}', $user->user_login, $message );
82
	$message = str_replace( '{user_email}', $user->user_email, $message );
83
	$message = str_replace( '{billing_address}', "123 Test Street, Unit 222\nSomewhere Town, CA, 92101", $message );
84
	$message = str_replace( '{date}', date( give_date_format(), current_time( 'timestamp' ) ), $message );
85
	$message = str_replace( '{amount}', $price, $message );
86
	$message = str_replace( '{price}', $price, $message );
87
	$message = str_replace( '{donation}', esc_html__( 'Sample Donation Form Title', 'give' ), $message );
88
	$message = str_replace( '{form_title}', esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ), $message );
89
	$message = str_replace( '{receipt_id}', $receipt_id, $message );
90
	$message = str_replace( '{payment_method}', $gateway, $message );
91
	$message = str_replace( '{sitename}', get_bloginfo( 'name' ), $message );
92
	$message = str_replace( '{payment_id}', $payment_id, $message );
93
	$message = str_replace( '{receipt_link}', $receipt_link, $message );
94
	$message = str_replace( '{receipt_link_url}', $receipt_link_url, $message );
95
	$message = str_replace( '{pdf_receipt}', '<a href="#">Download Receipt</a>', $message );
96
97
	return wpautop( apply_filters( 'give_email_preview_template_tags', $message ) );
98
}
99
100
/**
101
 * Filter for Email Template Preview Buttons.
102
 *
103
 * @param array $array
104
 *
105
 * @access private
106
 * @since  1.0
107
 * @return array|bool
108
 */
109
function give_email_template_preview( $array ) {
110
111
	if ( ! current_user_can( 'manage_give_settings' ) ) {
112
		return false;
113
	}
114
	$custom_field = array(
115
		'name' => esc_html__( 'Preview Email', 'give' ),
116
		'desc' => esc_html__( 'Click the buttons to preview or send test emails.', 'give' ),
117
		'id'   => 'give_email_preview_buttons',
118
		'type' => 'email_preview_buttons',
119
	);
120
121
	return give_settings_array_insert( $array, 'donation_subject', array( $custom_field ) );
122
123
}
124
125
add_filter( 'give_settings_emails', 'give_email_template_preview' );
126
127
/**
128
 * Output Email Template Preview Buttons.
129
 *
130
 * @access private
131
 * @since  1.0
132
 * @return array
133
 */
134
function give_email_preview_buttons_callback() {
135
	ob_start();
136
	?>
137
	<a href="<?php echo esc_url( add_query_arg( array( 'give_action' => 'preview_email' ), home_url() ) ); ?>" class="button-secondary"
138
	   target="_blank"><?php esc_html_e( 'Preview Donation Receipt', 'give' ); ?></a>
139
	<a href="<?php echo wp_nonce_url( add_query_arg( array(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'wp_nonce_url'
Loading history...
140
		'give_action'  => 'send_test_email',
141
		'give-message' => 'sent-test-email',
142
		'tag'          => 'emails',
143
	) ), 'give-test-email' ); ?>" aria-label="<?php esc_attr_e( 'Send demo donation receipt to the emails listed below.', 'give' ); ?>"
144
	   class="button-secondary"><?php esc_html_e( 'Send Test Email', 'give' ); ?></a>
145
	<?php
146
	echo ob_get_clean();
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'ob_get_clean'
Loading history...
147
}
148
149
/**
150
 * Displays the email preview
151
 *
152
 * @since 1.0
153
 * @return void
154
 */
155
function give_display_email_template_preview() {
156
157
	if ( empty( $_GET['give_action'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
158
		return;
159
	}
160
161
	if ( 'preview_email' !== $_GET['give_action'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
162
		return;
163
	}
164
165
	if ( ! current_user_can( 'manage_give_settings' ) ) {
166
		return;
167
	}
168
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
169
170
	Give()->emails->heading = esc_html__( 'Donation Receipt', 'give' );
171
172
	$payment_id = (int) isset( $_GET['preview_id'] ) ? $_GET['preview_id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
173
174
	echo give_get_preview_email_header();
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_get_preview_email_header'
Loading history...
175
176
	//Are we previewing an actual payment?
177
	if ( ! empty( $payment_id ) ) {
178
179
		$content = give_get_email_body_content( $payment_id );
180
181
		$preview_content = give_do_email_tags( $content, $payment_id );
182
183 42
	} else {
184
185 42
		//No payment ID, use sample preview content
186
		$preview_content = give_email_preview_template_tags( give_get_email_body_content( 0, array() ) );
187 42
	}
188
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
189 42
190
	echo Give()->emails->build_email( $preview_content );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'Give'
Loading history...
191 42
192
	exit;
193 42
194
}
195
196
add_action( 'init', 'give_display_email_template_preview' );
197
198
/**
199
 * Email Template Body.
200
 *
201
 * @since 1.0
202
 *
203
 * @param int   $payment_id   Payment ID
204
 * @param array $payment_data Payment Data
205
 *
206
 * @return string $email_body Body of the email
207
 */
208
function give_get_email_body_content( $payment_id = 0, $payment_data = array() ) {
209
210 42
	$default_email_body = give_get_default_donation_receipt_email();
211
212 42
	$email_content = give_get_option( 'donation_receipt' );
213 42
	$email_content = ( $email_content ) ? stripslashes( $email_content ) : $default_email_body;
214
215 42
	$email_body = wpautop( $email_content );
216 41
217 41
	$email_body = apply_filters( 'give_donation_receipt_' . Give()->emails->get_template(), $email_body, $payment_id, $payment_data );
218 42
219 1
	return apply_filters( 'give_donation_receipt', $email_body, $payment_id, $payment_data );
220 1
}
221
222
/**
223
 * Donation Notification Template Body.
224 42
 *
225
 * @since  1.0
226 42
 *
227 42
 * @param int   $payment_id   Payment ID
228
 * @param array $payment_data Payment Data
229 42
 *
230 42
 * @return string $email_body Body of the email
231 42
 */
232
function give_get_donation_notification_body_content( $payment_id = 0, $payment_data = array() ) {
233 42
234 42
	$payment = new Give_Payment( $payment_id );
235 42
236
	if ( $payment->user_id > 0 ) {
237 42
		$user_data = get_userdata( $payment->user_id );
238
		$name      = $user_data->display_name;
239 42
	} elseif ( ! empty( $payment->first_name ) && ! empty( $payment->last_name ) ) {
240
		$name = $payment->first_name . ' ' . $payment->last_name;
241 42
	} else {
242
		$name = $payment->email;
243 42
	}
244
245
	$gateway = give_get_gateway_admin_label( $payment->gateway );
246
247
	$default_email_body = esc_html__( 'Hello', 'give' ) . "\n\n";
248
	$default_email_body .= esc_html__( 'A donation has been made.', 'give' ) . "\n\n";
249
	$default_email_body .= esc_html__( 'Donation:', 'give' ) . "\n\n";
250
	$default_email_body .= esc_html__( 'Donor:', 'give' ) . ' ' . html_entity_decode( $name, ENT_COMPAT, 'UTF-8' ) . "\n";
251
	$default_email_body .= esc_html__( 'Amount:', 'give' ) . ' ' . html_entity_decode( give_currency_filter( give_format_amount( $payment->total, array( 'sanitize' => false ) ) ), ENT_COMPAT, 'UTF-8' ) . "\n";
252
	$default_email_body .= esc_html__( 'Payment Method:', 'give' ) . ' ' . $gateway . "\n\n";
253
	$default_email_body .= esc_html__( 'Thank you', 'give' );
254
255
	$message = give_get_option( 'donation_notification' );
256
	$message = isset( $message ) ? stripslashes( $message ) : $default_email_body;
257
258
	$email_body = give_do_email_tags( $message, $payment_id );
259
260
	return apply_filters( 'give_donation_notification', wpautop( $email_body ), $payment_id, $payment_data );
261
}
262
263
/**
264
 * Give Preview Email Header.
265
 *
266
 * Displays a header bar with the ability to change donations to preview actual data within the preview. Will not display if
267
 *
268
 * @since 1.6
269
 *
270
 */
271
function give_get_preview_email_header() {
272
273
	//Payment receipt switcher
274
	$payment_count = give_count_payments()->publish;
275
	$payment_id    = (int) isset( $_GET['preview_id'] ) ? $_GET['preview_id'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
276
277
	if ( $payment_count <= 0 ) {
278
		return false;
279
	}
280
281
	//Get payments.
282
	$payments = new Give_Payments_Query( array(
283
		'number' => 100,
284
	) );
285
	$payments = $payments->get_payments();
286
	$options  = array();
287
288
	//Provide nice human readable options.
289 View Code Duplication
	if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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...
290
		$options[0] = esc_html__( '- Select a donation -', 'give' );
291
		foreach ( $payments as $payment ) {
292
293
			$options[ $payment->ID ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title );
294
295
		}
296
	} else {
297
		$options[0] = esc_html__( 'No donations found.', 'give' );
298
	}
299
300
	//Start constructing HTML output.
301
	$transaction_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
302
303
	//Inline JS function for switching donations.
304
	$transaction_header .= '<script>
305
				 function change_preview(){
306
				  var transactions = document.getElementById("give_preview_email_payment_id");
307
			        var selected_trans = transactions.options[transactions.selectedIndex];
308
				        console.log(selected_trans);
309
				        if (selected_trans){
310
				            var url_string = "' . get_bloginfo( 'url' ) . '?give_action=preview_email&preview_id=" + selected_trans.value;
311
				                window.location = url_string;
312
				        }
313
				    }
314
			    </script>';
315
316
	$transaction_header .= '<label for="give_preview_email_payment_id" style="font-size:12px;color:#333;margin:0 4px 0 0;">' . esc_html__( 'Preview email with a donation:', 'give' ) . '</label>';
317
318
	//The select field with 100 latest transactions
319
	$transaction_header .= Give()->html->select( array(
320
		'name'             => 'preview_email_payment_id',
321
		'selected'         => $payment_id,
322
		'id'               => 'give_preview_email_payment_id',
323
		'class'            => 'give-preview-email-payment-id',
324
		'options'          => $options,
325
		'chosen'           => false,
326
		'select_atts'      => 'onchange="change_preview()">',
327
		'show_option_all'  => false,
328
		'show_option_none' => false,
329
	) );
330
331
	//Closing tag
332
	$transaction_header .= '</div>';
333
334
	return apply_filters( 'give_preview_email_receipt_header', $transaction_header );
335
336
}