template.php ➔ give_get_preview_email_header()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 78
rs 8.48
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Email Template
4
 *
5
 * @package     Give
6
 * @subpackage  Emails
7
 * @copyright   Copyright (c) 2016, GiveWP
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
	$user             = wp_get_current_user();
63
	$gateway          = 'PayPal';
64
	$donation_id      = rand( 1, 100 );
65
	$receipt_link     = give_get_receipt_link( $donation_id );
66
	$receipt_link_url = give_get_receipt_url( $donation_id );
67
	$price            = give_currency_filter(
68
		give_format_amount(
69
			10.50,
70
			array(
71
				'sanitize' => false,
72
				)
73
		)
74
	);
75
76
	$message = str_replace( '{name}', $user->display_name, $message );
77
	$message = str_replace( '{fullname}', $user->display_name, $message );
78
	$message = str_replace( '{username}', $user->user_login, $message );
79
	$message = str_replace( '{user_email}', $user->user_email, $message );
80
	$message = str_replace( '{billing_address}', "123 Test Street, Unit 222\nSomewhere Town, CA, 92101", $message );
81
	$message = str_replace( '{date}', date( give_date_format(), current_time( 'timestamp' ) ), $message );
82
	$message = str_replace( '{amount}', $price, $message );
83
	$message = str_replace( '{price}', $price, $message );
84
	$message = str_replace( '{donation}', esc_html__( 'Sample Donation Form Title', 'give' ), $message );
85
	$message = str_replace( '{form_title}', esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ), $message );
86
	$message = str_replace( '{payment_method}', $gateway, $message );
87
	$message = str_replace( '{sitename}', get_bloginfo( 'name' ), $message );
88
	$message = str_replace( '{payment_id}', $donation_id, $message );
89
	$message = str_replace( '{receipt_link}', $receipt_link, $message );
90
	$message = str_replace( '{receipt_link_url}', $receipt_link_url, $message );
91
	$message = str_replace( '{pdf_receipt}', '<a href="#">Download Receipt</a>', $message );
92
93
	return wpautop( apply_filters( 'give_email_preview_template_tags', $message ) );
94
}
95
96
97
98
/**
99
 * Output Email Template Preview Buttons.
100
 *
101
 * @access private
102
 * @since  1.0
103
 * @since  1.8 Field arguments param added.
104
 *
105
 * @param array $field Field arguments.
106
 *
107
 * @return array
108
 */
109
function give_email_preview_buttons_callback( $field ) {
110
	$field_id = str_replace( '_preview_buttons', '', $field['id'] );
111
112
	ob_start();
113
114
	echo sprintf(
115
		'<a href="%1$s" class="button-secondary" target="_blank">%2$s</a>',
116
		wp_nonce_url(
117
			add_query_arg(
118
				array( 'give_action' => 'preview_email', 'email_type' => $field_id ),
119
				home_url()
120
			), 'give-preview-email'
121
		),
122
		$field['name']
123
	);
124
125
	echo sprintf(
126
		' <a href="%1$s" aria-label="%2$s" class="button-secondary">%3$s</a>',
127
		wp_nonce_url(
128
				add_query_arg( array(
129
			'give_action'  => 'send_preview_email',
130
			'email_type' => $field_id,
131
			'give-messages[]' => 'sent-test-email',
132
		) ), 'give-send-preview-email' ),
133
		esc_attr__( 'Send Test Email.', 'give' ),
134
		esc_html__( 'Send Test Email', 'give' )
135
	);
136
137
	echo ob_get_clean();
138
}
139
140
141
/**
142
 * Give Preview Email Header.
143
 *
144
 * Displays a header bar with the ability to change donations to preview actual data within the preview. Will not display if
145
 *
146
 * @since 1.6
147
 *
148
 */
149
function give_get_preview_email_header() {
150
151
	//Payment receipt switcher
152
	$payment_count = give_count_payments()->publish;
153
	$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...
154
155
	if ( $payment_count <= 0 ) {
156
		return false;
157
	}
158
159
	//Get payments.
160
	$donations = new Give_Payments_Query( array(
161
		'number' => 100,
162
		'output' => '',
163
		'fields' => 'ids'
164
	) );
165
	$donations = $donations->get_payments();
166
	$options  = array();
167
168
	// Default option.
169
	$options[0] = esc_html__( 'No donations found.', 'give' );
170
171
	//Provide nice human readable options.
172
	if ( $donations ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $donations 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...
173
		$options[0] = esc_html__( '- Select a donation -', 'give' );
174
		foreach ( $donations as $donation_id ) {
175
176
			$options[ $donation_id ] = sprintf(
177
				'#%1$s - %2$s - %3$s',
178
				$donation_id,
179
				give_get_donation_donor_email( $donation_id ),
180
				get_the_title( $donation_id )
181
			);
182
		}
183
	}
184
185
	//Start constructing HTML output.
186
	$transaction_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
187
188
	// Remove payment id query param if set from request url.
189
	$request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] );
190
	$query            = $request_url_data['query'];
191
	$query            = remove_query_arg( array( 'preview_id' ), $query );
192
193
	$request_url = home_url( '/?' . str_replace( '', '', $query ) );
194
195
	$transaction_header .= '<script>
196
				 function change_preview(){
197
				  var transactions = document.getElementById("give_preview_email_payment_id");
198
			        var selected_trans = transactions.options[transactions.selectedIndex];
199
				        if (selected_trans){
200
				            var url_string = "' . $request_url . '&preview_id=" + selected_trans.value;
201
				                window.location = url_string;
202
				        }
203
				    }
204
			    </script>';
205
206
	$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>';
207
208
	//The select field with 100 latest transactions
209
	$transaction_header .= Give()->html->select( array(
210
		'name'             => 'preview_email_payment_id',
211
		'selected'         => $payment_id,
212
		'id'               => 'give_preview_email_payment_id',
213
		'class'            => 'give-preview-email-payment-id',
214
		'options'          => $options,
215
		'chosen'           => false,
216
		'select_atts'      => 'onchange="change_preview()"',
217
		'show_option_all'  => false,
218
		'show_option_none' => false,
219
	) );
220
221
	//Closing tag
222
	$transaction_header .= '</div>';
223
224
	return apply_filters( 'give_preview_email_receipt_header', $transaction_header );
225
226
}
227