Test Failed
Push — release/2.0 ( 6ea777...f7bd0b )
by Ravinder
05:21
created

Give_Emails::build_email()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 73
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 3
nop 1
dl 0
loc 73
rs 8.6829
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
 * Emails
4
 *
5
 * This class handles all emails sent through Give
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Give_Emails Class.
21
 *
22
 * @property $from_address
23
 * @property $from_name
24
 * @property $content_type
25
 * @property $headers
26
 * @property $html
27
 * @property $template
28
 * @property $heading
29
 *
30
 * @since 1.0
31
 */
32
class Give_Emails {
33
34
	/**
35
	 * Holds the from address.
36
	 *
37
	 * @since 1.0
38
	 */
39
	private $from_address;
40
41
	/**
42
	 * Holds the from name.
43
	 *
44
	 * @since 1.0
45
	 */
46
	private $from_name;
47
48
	/**
49
	 * Holds the email content type.
50
	 *
51
	 * @since 1.0
52
	 */
53
	private $content_type;
54
55
	/**
56
	 * Holds the email headers.
57
	 *
58
	 * @since 1.0
59
	 */
60
	private $headers;
61
62
	/**
63
	 * Whether to send email in HTML.
64
	 *
65
	 * @since 1.0
66
	 */
67
	private $html = true;
68
69
	/**
70
	 * The email template to use.
71
	 *
72
	 * @since 1.0
73
	 */
74
	private $template;
75
76
	/**
77
	 * The header text for the email.
78
	 *
79
	 * @since  1.0
80
	 */
81
	private $heading = '';
82
83
	/**
84
	 * Email template tags argument.
85
	 * This helps to decode email template tags,
86
	 *
87
	 * @since  1.0
88
	 */
89
	public $tag_args = array();
90
91
	/**
92
	 * Get things going.
93
	 *
94
	 * @since 1.0
95
	 */
96
	public function __construct() {
97
98
		if ( 'none' === $this->get_template() ) {
99
			$this->html = false;
100
		}
101
102
		add_action( 'give_email_send_before', array( $this, 'send_before' ) );
103
		add_action( 'give_email_send_after', array( $this, 'send_after' ) );
104
105
	}
106
107
	/**
108
	 * Set a property.
109
	 *
110
	 * @since 1.0
111
	 *
112
	 * @param $key
113
	 * @param $value
114
	 */
115
	public function __set( $key, $value ) {
116
		$this->$key = $value;
117
	}
118
119
	/**
120
	 * Get the email from name.
121
	 *
122
	 * @since 1.0
123
	 */
124
	public function get_from_name() {
125
		if ( ! $this->from_name ) {
126
			$this->from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
127
		}
128
129
		return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this );
130
	}
131
132
	/**
133
	 * Get the email from address.
134
	 *
135
	 * @since 1.0
136
	 */
137
	public function get_from_address() {
138
		if ( ! $this->from_address ) {
139
			$this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) );
140
		}
141
142
		return apply_filters( 'give_email_from_address', $this->from_address, $this );
143
	}
144
145
	/**
146
	 * Get the email content type.
147
	 *
148
	 * @since 1.0
149
	 */
150
	public function get_content_type() {
151
		if ( ! $this->content_type  ) {
152
			$this->content_type = $this->html
153
				? apply_filters( 'give_email_default_content_type', 'text/html', $this )
154
				: 'text/plain';
155
		}
156
157
		return apply_filters( 'give_email_content_type', $this->content_type, $this );
158
	}
159
160
	/**
161
	 * Get the email headers.
162
	 *
163
	 * @since 1.0
164
	 */
165
	public function get_headers() {
166
		if ( ! $this->headers ) {
167
			$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n";
168
			$this->headers .= "Reply-To: {$this->get_from_address()}\r\n";
169
			$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n";
170
		}
171
172
		return apply_filters( 'give_email_headers', $this->headers, $this );
173
	}
174
175
	/**
176
	 * Retrieve email templates.
177
	 *
178
	 * @since 1.0
179
	 */
180
	public function get_templates() {
181
		$templates = array(
182
			'default' => esc_html__( 'Default Template', 'give' ),
183
			'none'    => esc_html__( 'No template, plain text only', 'give' )
184
		);
185
186
		return apply_filters( 'give_email_templates', $templates );
187
	}
188
189
	/**
190
	 * Get the enabled email template.
191
	 *
192
	 * @since 1.0
193
	 */
194
	public function get_template() {
195
		if ( ! $this->template ) {
196
			$this->template = give_get_option( 'email_template', 'default' );
197
		}
198
199
		return apply_filters( 'give_email_template', $this->template );
200
	}
201
202
	/**
203
	 * Get the header text for the email.
204
	 *
205
	 * @since 1.0
206
	 */
207
	public function get_heading() {
208
		return apply_filters( 'give_email_heading', $this->heading );
209
	}
210
211
	/**
212
	 * Parse email template tags.
213
	 *
214
	 * @param $content
215
	 *
216
	 * @return mixed
217
	 */
218
	public function parse_tags( $content ) {
219
		return $content;
220
	}
221
222
	/**
223
	 * Build the final email.
224
	 *
225
	 * @since 1.0
226
	 *
227
	 * @param $message
228
	 *
229
	 * @return string
230
	 */
231
	public function build_email( $message ) {
232
233
		if ( false === $this->html ) {
234
235
			// Added Replacement check to simply behaviour of anchor tags.
236
			$pattern     = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i';
237
			$message     = preg_replace_callback(
238
				$pattern,
239
				function( $return ) {
240
					if ( $return[1] !== $return[2] ) {
241
						return "{$return[2]} ( {$return[1]} )";
242
					}
243
					return trailingslashit( $return[1] );
244
				},
245
				$message
246
			);
247
248
			return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this );
249
		}
250
251
		$message = $this->text_to_html( $message );
252
253
		$template = $this->get_template();
254
255
		ob_start();
256
257
		give_get_template_part( 'emails/header', $template, true );
258
259
		/**
260
		 * Fires in the email head.
261
		 *
262
		 * @since 1.0
263
		 *
264
		 * @param Give_Emails $this The email object.
265
		 */
266
		do_action( 'give_email_header', $this );
267
268
		if ( has_action( 'give_email_template_' . $template ) ) {
269
			/**
270
			 * Fires in a specific email template.
271
			 *
272
			 * @since 1.0
273
			 */
274
			do_action( "give_email_template_{$template}" );
275
		} else {
276
			give_get_template_part( 'emails/body', $template, true );
277
		}
278
279
		/**
280
		 * Fires in the email body.
281
		 *
282
		 * @since 1.0
283
		 *
284
		 * @param Give_Emails $this The email object.
285
		 */
286
		do_action( 'give_email_body', $this );
287
288
		give_get_template_part( 'emails/footer', $template, true );
289
290
		/**
291
		 * Fires in the email footer.
292
		 *
293
		 * @since 1.0
294
		 *
295
		 * @param Give_Emails $this The email object.
296
		 */
297
		do_action( 'give_email_footer', $this );
298
299
		$body    = ob_get_clean();
300
		$message = str_replace( '{email}', $message, $body );
301
302
		return apply_filters( 'give_email_message', $message, $this );
303
	}
304
305
	/**
306
	 * Send the email.
307
	 *
308
	 * @param  string       $to          The To address to send to.
309
	 * @param  string       $subject     The subject line of the email to send.
310
	 * @param  string       $message     The body of the email to send.
311
	 * @param  string|array $attachments Attachments to the email in a format supported by wp_mail().
312
	 *
313
	 * @return bool
314
	 */
315
	public function send( $to, $subject, $message, $attachments = '' ) {
316
317
		if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
318
			give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ), null );
319
320
			return false;
321
		}
322
323
		/**
324
		 * Fires before sending an email.
325
		 *
326
		 * @since 1.0
327
		 *
328
		 * @param Give_Emails $this The email object.
329
		 */
330
		do_action( 'give_email_send_before', $this );
331
332
		$subject = $this->parse_tags( $subject );
333
		$message = $this->parse_tags( $message );
334
335
		$message = $this->build_email( $message );
336
337
		$attachments = apply_filters( 'give_email_attachments', $attachments, $this );
338
339
		$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
340
341
		/**
342
		 * Fires after sending an email.
343
		 *
344
		 * @since 1.0
345
		 *
346
		 * @param Give_Emails $this The email object.
347
		 */
348
		do_action( 'give_email_send_after', $this );
349
350
		return $sent;
351
352
	}
353
354
	/**
355
	 * Add filters / actions before the email is sent.
356
	 *
357
	 * @since 1.0
358
	 */
359
	public function send_before() {
360
		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
361
		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
362
		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
363
	}
364
365
	/**
366
	 * Remove filters / actions after the email is sent.
367
	 *
368
	 * @since 1.0
369
	 */
370
	public function send_after() {
371
		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
372
		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
373
		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
374
375
		// Reset heading to an empty string
376
		$this->heading = '';
377
	}
378
379
	/**
380
	 * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags.
381
	 *
382
	 * @since 1.0
383
	 */
384
	public function text_to_html( $message ) {
385
386
		if ( 'text/html' == $this->content_type || true === $this->html ) {
387
			$message = wpautop( $message );
388
		}
389
390
		return $message;
391
	}
392
393
}