Test Failed
Push — issues/1227 ( 92c1d9 )
by Ravinder
06:04
created

template.php ➔ give_email_template_preview()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
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
 * @todo Modify this function to remove payment id dependency.
35
 *
36
 * @since 1.0
37
 *
38
 * @param string $message Message with the template tags.
39
 * @param array  $payment_data Payment Data.
40
 * @param int    $payment_id Payment ID.
41
 * @param bool   $admin_notice Whether or not this is a notification email.
42
 *
43
 * @return string $message Fully formatted message
44
 */
45
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...
46
	return give_do_email_tags( $message, $payment_id );
47
}
48
49
/**
50
 * Email Preview Template Tags.
51
 *
52
 * Provides sample content for the preview email functionality within settings > email.
53
 *
54
 * @since 1.0
55
 *
56
 * @param string $message Email message with template tags
57
 *
58
 * @return string $message Fully formatted message
59
 */
60
function give_email_preview_template_tags( $message ) {
61
62
	$price = give_currency_filter( give_format_amount( 10.50, array( 'sanitize' => false ) ) );
63
64
	$gateway = 'PayPal';
65
66
	$receipt_id = strtolower( md5( uniqid() ) );
67
68
	$payment_id = rand( 1, 100 );
69
70
	$receipt_link_url = esc_url( add_query_arg( array( 'payment_key' => $receipt_id, 'give_action' => 'view_receipt' ), home_url() ) );
71
	$receipt_link = sprintf(
72
		'<a href="%1$s">%2$s</a>',
73
		$receipt_link_url,
74
		esc_html__( 'View the receipt in your browser &raquo;', 'give' )
75
	);
76
77
	// Set user.
78
	$user = wp_get_current_user();
79
80
	$message = str_replace( '{name}', $user->display_name, $message );
81
	$message = str_replace( '{fullname}', $user->display_name, $message );
82
	$message = str_replace( '{username}', $user->user_login, $message );
83
	$message = str_replace( '{user_email}', $user->user_email, $message );
84
	$message = str_replace( '{billing_address}', "123 Test Street, Unit 222\nSomewhere Town, CA, 92101", $message );
85
	$message = str_replace( '{date}', date( give_date_format(), current_time( 'timestamp' ) ), $message );
86
	$message = str_replace( '{amount}', $price, $message );
87
	$message = str_replace( '{price}', $price, $message );
88
	$message = str_replace( '{donation}', esc_html__( 'Sample Donation Form Title', 'give' ), $message );
89
	$message = str_replace( '{form_title}', esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ), $message );
90
	$message = str_replace( '{receipt_id}', $receipt_id, $message );
91
	$message = str_replace( '{payment_method}', $gateway, $message );
92
	$message = str_replace( '{sitename}', get_bloginfo( 'name' ), $message );
93
	$message = str_replace( '{payment_id}', $payment_id, $message );
94
	$message = str_replace( '{receipt_link}', $receipt_link, $message );
95
	$message = str_replace( '{receipt_link_url}', $receipt_link_url, $message );
96
	$message = str_replace( '{pdf_receipt}', '<a href="#">Download Receipt</a>', $message );
97
98
	return wpautop( apply_filters( 'give_email_preview_template_tags', $message ) );
99
}
100
101
102
/**
103
 * Output Email Template Preview Buttons.
104
 *
105
 * @access private
106
 * @since  1.0
107
 * @since  1.8 Field arguments param added.
108
 *
109
 * @param array $field Field arguments.
110
 *
111
 * @return array
112
 */
113
function give_email_preview_buttons_callback( $field ) {
114
	$field_id = str_replace( '_preview_buttons', '', $field['id'] );
115
116
	ob_start();
117
118
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
119
		'<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
120
		wp_nonce_url(
121
			add_query_arg(
122
				array( 'give_action' => 'preview_email', 'email_type' => $field_id ),
123
				home_url()
124
			), 'give-preview-email'
125
		),
126
		$field['name']
127
	);
128
129
	echo sprintf(
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
130
		' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
131
		wp_nonce_url(
132
				add_query_arg( array(
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
133
			'give_action'  => 'send_preview_email',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 12.
Loading history...
134
			'email_type' => $field_id,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 12.
Loading history...
135
			'give-message' => 'sent-test-email',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 12.
Loading history...
136
		) ), 'give-send-preview-email' ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 8.
Loading history...
137
		esc_attr__( 'Send Test Email.', 'give' ),
138
		esc_html__( 'Send Test Email', 'give' )
139
	);
140
141
	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...
142
}
143
144
145
/**
146
 * Render Receipt in the Browser.
147
 *
148
 * A link is added to the Donation Receipt to view the email in the browser and
149
 * this function renders the Donation Receipt in the browser. It overrides the
150
 * Donation Receipt template and provides its only styling.
151
 *
152
 * @since  1.0
153
 */
154
function give_render_receipt_in_browser() {
155 View Code Duplication
	if ( ! isset( $_GET['payment_key'] ) ) {
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...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
156
		wp_die( esc_html__( 'Missing donation payment key.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
157
	}
158
159
	$key = urlencode( $_GET['payment_key'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
160
161
	ob_start();
162
	//Disallows caching of the page
163
	header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Last-Modified: 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...
Coding Style Comprehensibility introduced by
The string literal D, d M Y H:i:s 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...
Coding Style Comprehensibility introduced by
The string literal GMT 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...
164
	header( "Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cache-Control: no-store, no-cache, must-revalidate 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...
165
	header( "Cache-Control: post-check=0, pre-check=0", false );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Cache-Control: post-check=0, pre-check=0 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...
166
	header( "Pragma: no-cache" ); // HTTP/1.0
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Pragma: no-cache 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...
167
	header( "Expires: Sat, 23 Oct 1977 05:00:00 PST" ); // Date in the past
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Expires: Sat, 23 Oct 1977 05:00:00 PST 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...
168
	?>
169
	<!DOCTYPE html>
170
	<html lang="en">
171
	<head>
172
		<?php
173
		/**
174
		 * Fires in the receipt HEAD.
175
		 *
176
		 * @since 1.0
177
		 */
178
		do_action( 'give_receipt_head' );
179
		?>
180
	</head>
181
	<body class="<?php echo apply_filters( 'give_receipt_page_body_class', 'give_receipt_page' ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
182
183
	<div id="give_receipt_wrapper">
184
		<?php
185
		/**
186
		 * Fires in the receipt template before the content.
187
		 *
188
		 * @since 1.0
189
		 */
190
		do_action( 'give_render_receipt_in_browser_before' );
191
192
		echo do_shortcode( '[give_receipt payment_key=' . $key . ']' );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'do_shortcode'
Loading history...
193
194
		/**
195
		 * Fires in the receipt template after the content.
196
		 *
197
		 * @since 1.0
198
		 */
199
		do_action( 'give_render_receipt_in_browser_after' );
200
		?>
201
	</div>
202
203
	<?php
204
	/**
205
	 * Fires in the receipt footer.
206
	 *
207
	 * @since 1.0
208
	 */
209
	do_action( 'give_receipt_footer' );
210
	?>
211
	</body>
212
	</html>
213
	<?php
214
	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...
215
	die();
216
}
217
218
add_action( 'give_view_receipt', 'give_render_receipt_in_browser' );
219
220
221
/**
222
 * Give Preview Email Header.
223
 *
224
 * Displays a header bar with the ability to change donations to preview actual data within the preview. Will not display if
225
 *
226
 * @since 1.6
227
 *
228
 */
229
function give_get_preview_email_header() {
230
231
	//Payment receipt switcher
232
	$payment_count = give_count_payments()->publish;
233
	$payment_id    = give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
234
235
	if ( $payment_count <= 0 ) {
236
		return false;
237
	}
238
239
	//Get payments.
240
	$payments = new Give_Payments_Query( array(
241
		'number' => 100
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
242
	) );
243
	$payments = $payments->get_payments();
244
	$options  = array();
245
246
	// Default option.
247
	$options[0] = esc_html__( 'No donations found.', 'give' );
248
249
	//Provide nice human readable options.
250
	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...
251
		$options[0] = esc_html__( '- Select a donation -', 'give' );
252
		foreach ( $payments as $payment ) {
253
254
			$options[ $payment->ID ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title );
255
256
		}
257
	}
258
259
	//Start constructing HTML output.
260
	$transaction_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
261
262
	//Inline JS function for switching donations.
263
	$request_url = $_SERVER['REQUEST_URI'];
0 ignored issues
show
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
264
265
	// Remove payment id query param if set from request url.
266
	if ( $payment_id ) {
267
		$request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] );
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_SERVER
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
268
		$query            = $request_url_data['query'];
269
		$query            = str_replace( "&preview_id={$payment_id}", '', $query );
270
271
		$request_url = home_url( '/?' . str_replace( '', '', $query ) );
272
	}
273
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
274
275
	$transaction_header .= '<script>
276
				 function change_preview(){
277
				  var transactions = document.getElementById("give_preview_email_payment_id");
278
			        var selected_trans = transactions.options[transactions.selectedIndex];
279
				        console.log(selected_trans);
280
				        if (selected_trans){
281
				            var url_string = "' . $request_url . '&preview_id=" + selected_trans.value;
282
				                window.location = url_string;
283
				        }
284
				    }
285
			    </script>';
286
287
	$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>';
288
289
	//The select field with 100 latest transactions
290
	$transaction_header .= Give()->html->select( array(
291
		'name'             => 'preview_email_payment_id',
292
		'selected'         => $payment_id,
293
		'id'               => 'give_preview_email_payment_id',
294
		'class'            => 'give-preview-email-payment-id',
295
		'options'          => $options,
296
		'chosen'           => false,
297
		'select_atts'      => 'onchange="change_preview()"',
298
		'show_option_all'  => false,
299
		'show_option_none' => false
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
300
	) );
301
302
	//Closing tag
303
	$transaction_header .= '</div>';
304
305
	return apply_filters( 'give_preview_email_receipt_header', $transaction_header );
306
307
}
308
309
310
/**
311
 * Give Receipt Head Content
312
 *
313
 * @since 1.6
314
 * @return string
315
 */
316
function give_receipt_head_content() {
317
318
	//Title.
319
	$output = '<title>' . esc_html__( 'Donation Receipt', 'give' ) . '</title>';
320
321
	//Meta.
322
	$output .= '<meta charset="utf-8"/>
323
		<!-- Further disallowing of caching of this page -->
324
		<meta charset="utf-8"/>
325
		<meta http-equiv="cache-control" content="max-age=0"/>
326
		<meta http-equiv="cache-control" content="no-cache"/>
327
		<meta http-equiv="expires" content="0"/>
328
		<meta http-equiv="expires" content="Tue, 23 Oct 1977 05:00:00 PST"/>
329
		<meta http-equiv="pragma" content="no-cache"/>
330
		<meta name="robots" content="noindex, nofollow"/>';
331
332
	//CSS
333
	$output .= '<link rel="stylesheet" href="' . give_get_stylesheet_uri() . '?ver=' . GIVE_VERSION . '">';
0 ignored issues
show
introduced by
Stylesheets must be registered/enqueued via wp_enqueue_style
Loading history...
334
335
	echo apply_filters( 'give_receipt_head_content', $output );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
336
337
}
338
339
add_action( 'give_receipt_head', 'give_receipt_head_content' );