Completed
Push — release/2.0 ( 9c875b...4d845f )
by Ravinder
19:03
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
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 26 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 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 ) );
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
	$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( '{date}', date( give_date_format(), current_time( 'timestamp' ) ), $message );
83
	$message = str_replace( '{amount}', $price, $message );
84
	$message = str_replace( '{price}', $price, $message );
85
	$message = str_replace( '{donation}', esc_html__( 'Sample Donation Form Title', 'give' ), $message );
86
	$message = str_replace( '{form_title}', esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ), $message );
87
	$message = str_replace( '{receipt_id}', $receipt_id, $message );
88
	$message = str_replace( '{payment_method}', $gateway, $message );
89
	$message = str_replace( '{sitename}', get_bloginfo( 'name' ), $message );
90
	$message = str_replace( '{payment_id}', $payment_id, $message );
91
	$message = str_replace( '{receipt_link}', $receipt_link, $message );
92
	$message = str_replace( '{receipt_link_url}', $receipt_link_url, $message );
93
	$message = str_replace( '{pdf_receipt}', '<a href="#">Download Receipt</a>', $message );
94
95
	return wpautop( apply_filters( 'give_email_preview_template_tags', $message ) );
96
}
97
98
99
/**
100
 * Output Email Template Preview Buttons.
101
 *
102
 * @access private
103
 * @since  1.0
104
 * @since  1.8 Field arguments param added.
105
 *
106
 * @param array $field Field arguments.
107
 *
108
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
109
 */
110
function give_email_preview_buttons_callback( $field ) {
111
	$field_id = str_replace( '_preview_buttons', '', $field['id'] );
112
113
	ob_start();
114
115
	echo sprintf(
116
		'<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
117
		wp_nonce_url(
118
			add_query_arg(
119
				array( 'give_action' => 'preview_email', 'email_type' => $field_id ),
120
				home_url()
121
			), 'give-preview-email'
122
		),
123
		$field['name']
124
	);
125
126
	echo sprintf(
127
		' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
128
		wp_nonce_url(
129
				add_query_arg( array(
130
			'give_action'  => 'send_preview_email',
131
			'email_type' => $field_id,
132
			'give-message' => 'sent-test-email',
133
		) ), 'give-send-preview-email' ),
134
		esc_attr__( 'Send Test Email.', 'give' ),
135
		esc_html__( 'Send Test Email', 'give' )
136
	);
137
138
	echo ob_get_clean();
139
}
140
141
142
/**
143
 * Render Receipt in the Browser.
144
 *
145
 * A link is added to the Donation Receipt to view the email in the browser and
146
 * this function renders the Donation Receipt in the browser. It overrides the
147
 * Donation Receipt template and provides its only styling.
148
 *
149
 * @since  1.0
150
 */
151
function give_render_receipt_in_browser() {
152
	if ( ! isset( $_GET['payment_key'] ) ) {
153
		wp_die( esc_html__( 'Missing donation payment key.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
154
	}
155
156
	$key = urlencode( $_GET['payment_key'] );
157
158
	ob_start();
159
	//Disallows caching of the page
160
	header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
161
	header( "Cache-Control: no-store, no-cache, must-revalidate" ); // HTTP/1.1
162
	header( "Cache-Control: post-check=0, pre-check=0", false );
163
	header( "Pragma: no-cache" ); // HTTP/1.0
164
	header( "Expires: Sat, 23 Oct 1977 05:00:00 PST" ); // Date in the past
165
	?>
166
	<!DOCTYPE html>
167
	<html lang="en">
168
	<head>
169
		<?php
170
		/**
171
		 * Fires in the receipt HEAD.
172
		 *
173
		 * @since 1.0
174
		 */
175
		do_action( 'give_receipt_head' );
176
		?>
177
	</head>
178
	<body class="<?php echo apply_filters( 'give_receipt_page_body_class', 'give_receipt_page' ); ?>">
179
180
	<div id="give_receipt_wrapper">
181
		<?php
182
		/**
183
		 * Fires in the receipt template before the content.
184
		 *
185
		 * @since 1.0
186
		 */
187
		do_action( 'give_render_receipt_in_browser_before' );
188
189
		echo do_shortcode( '[give_receipt payment_key=' . $key . ']' );
190
191
		/**
192
		 * Fires in the receipt template after the content.
193
		 *
194
		 * @since 1.0
195
		 */
196
		do_action( 'give_render_receipt_in_browser_after' );
197
		?>
198
	</div>
199
200
	<?php
201
	/**
202
	 * Fires in the receipt footer.
203
	 *
204
	 * @since 1.0
205
	 */
206
	do_action( 'give_receipt_footer' );
207
	?>
208
	</body>
209
	</html>
210
	<?php
211
	echo ob_get_clean();
212
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_render_receipt_in_browser() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
213
}
214
215
add_action( 'give_view_receipt', 'give_render_receipt_in_browser' );
216
217
218
/**
219
 * Give Preview Email Header.
220
 *
221
 * Displays a header bar with the ability to change donations to preview actual data within the preview. Will not display if
222
 *
223
 * @since 1.6
224
 *
225
 */
226
function give_get_preview_email_header() {
227
228
	//Payment receipt switcher
229
	$payment_count = give_count_payments()->publish;
230
	$payment_id    = give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' );
231
232
	if ( $payment_count <= 0 ) {
233
		return false;
234
	}
235
236
	//Get payments.
237
	$payments = new Give_Payments_Query( array(
238
		'number' => 100
239
	) );
240
	$payments = $payments->get_payments();
241
	$options  = array();
242
243
	// Default option.
244
	$options[0] = esc_html__( 'No donations found.', 'give' );
245
246
	//Provide nice human readable options.
247
	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...
248
		$options[0] = esc_html__( '- Select a donation -', 'give' );
249
		foreach ( $payments as $payment ) {
250
251
			$options[ $payment->ID ] = esc_html( '#' . $payment->ID . ' - ' . $payment->email . ' - ' . $payment->form_title );
252
253
		}
254
	}
255
256
	//Start constructing HTML output.
257
	$transaction_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
258
259
	//Inline JS function for switching donations.
260
	$request_url = $_SERVER['REQUEST_URI'];
261
262
	// Remove payment id query param if set from request url.
263
	if ( $payment_id ) {
264
		$request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] );
265
		$query            = $request_url_data['query'];
266
		$query            = str_replace( "&preview_id={$payment_id}", '', $query );
267
268
		$request_url = home_url( '/?' . str_replace( '', '', $query ) );
269
	}
270
271
272
	$transaction_header .= '<script>
273
				 function change_preview(){
274
				  var transactions = document.getElementById("give_preview_email_payment_id");
275
			        var selected_trans = transactions.options[transactions.selectedIndex];
276
				        console.log(selected_trans);
277
				        if (selected_trans){
278
				            var url_string = "' . $request_url . '&preview_id=" + selected_trans.value;
279
				                window.location = url_string;
280
				        }
281
				    }
282
			    </script>';
283
284
	$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>';
285
286
	//The select field with 100 latest transactions
287
	$transaction_header .= Give()->html->select( array(
288
		'name'             => 'preview_email_payment_id',
289
		'selected'         => $payment_id,
290
		'id'               => 'give_preview_email_payment_id',
291
		'class'            => 'give-preview-email-payment-id',
292
		'options'          => $options,
293
		'chosen'           => false,
294
		'select_atts'      => 'onchange="change_preview()"',
295
		'show_option_all'  => false,
296
		'show_option_none' => false
297
	) );
298
299
	//Closing tag
300
	$transaction_header .= '</div>';
301
302
	return apply_filters( 'give_preview_email_receipt_header', $transaction_header );
303
304
}
305
306
307
/**
308
 * Give Receipt Head Content
309
 *
310
 * @since 1.6
311
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
312
 */
313
function give_receipt_head_content() {
314
315
	//Title.
316
	$output = '<title>' . esc_html__( 'Donation Receipt', 'give' ) . '</title>';
317
318
	//Meta.
319
	$output .= '<meta charset="utf-8"/>
320
		<!-- Further disallowing of caching of this page -->
321
		<meta charset="utf-8"/>
322
		<meta http-equiv="cache-control" content="max-age=0"/>
323
		<meta http-equiv="cache-control" content="no-cache"/>
324
		<meta http-equiv="expires" content="0"/>
325
		<meta http-equiv="expires" content="Tue, 23 Oct 1977 05:00:00 PST"/>
326
		<meta http-equiv="pragma" content="no-cache"/>
327
		<meta name="robots" content="noindex, nofollow"/>';
328
329
	//CSS
330
	$output .= '<link rel="stylesheet" href="' . give_get_stylesheet_uri() . '?ver=' . GIVE_VERSION . '">';
331
332
	echo apply_filters( 'give_receipt_head_content', $output );
333
334
}
335
336
add_action( 'give_receipt_head', 'give_receipt_head_content' );