Completed
Branch BUG-8957-add-countries (301f85)
by
unknown
32:21 queued 18:11
created

EE_Email_messenger   C

Complexity

Total Complexity 36

Size/Duplication

Total Lines 516
Duplicated Lines 2.13 %

Coupling/Cohesion

Components 8
Dependencies 4

Importance

Changes 0
Metric Value
dl 11
loc 516
rs 6.7692
c 0
b 0
f 0
wmc 36
lcom 8
cbo 4

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A _set_admin_pages() 0 5 1
A _set_valid_shortcodes() 0 7 1
B _set_validator_config() 0 36 1
A do_secondary_messenger_hooks() 0 5 2
A add_email_css() 0 8 1
B _set_test_settings_fields() 0 25 1
B _set_template_fields() 0 89 1
A _set_default_message_types() 0 9 1
A _set_valid_message_types() 0 14 1
A _set_admin_settings_fields() 0 1 1
A _send_message() 11 20 2
A _preview() 0 3 1
A _headers() 0 15 1
A _ensure_has_from_email_address() 0 5 2
A _parse_from() 0 17 3
A set_from_address() 0 6 3
A set_from_name() 0 11 4
B _body() 0 31 5
A get_existing_test_settings() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
if (!defined('EVENT_ESPRESSO_VERSION') )
4
	exit('NO direct script access allowed');
5
6
/**
7
 * Event Espresso
8
 *
9
 * Event Registration and Management Plugin for WordPress
10
 *
11
 * @ package			Event Espresso
12
 * @ author				Seth Shoultes
13
 * @ copyright			(c) 2008-2011 Event Espresso  All Rights Reserved.
14
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
15
 * @ link				http://www.eventespresso.com
16
 * @ version		 	4.0
17
 *
18
 * ------------------------------------------------------------------------
19
 *
20
 * Messenger class
21
 *
22
 * @package			Event Espresso
23
 * @subpackage		includes/core/messages
24
 * @author			Darren Ethier, Brent Christensen
25
 *
26
 * ------------------------------------------------------------------------
27
 */
28
29
/**
30
 * This sets up the email messenger for the EE_messages (notifications) subsystem in EE.
31
 */
32
class EE_Email_messenger extends EE_messenger  {
33
34
	/**
35
	 * The following are the properties that email requires for the message going out.
36
	 */
37
	protected $_to;
38
	protected $_from;
39
	protected $_subject;
40
	protected $_content;
41
42
43
44
	/**
45
	 * constructor
46
	 *
47
	 * @access public
48
	 */
49
	public function __construct() {
50
		//set name and description properties
51
		$this->name = 'email';
52
		$this->description = __('This messenger delivers messages via email using the built-in <code>wp_mail</code> function included with WordPress', 'event_espresso');
53
		$this->label = array(
54
			'singular' => __('email', 'event_espresso'),
55
			'plural' => __('emails', 'event_espresso')
56
			);
57
		$this->activate_on_install = TRUE;
58
59
		//we're using defaults so let's call parent constructor that will take care of setting up all the other properties
60
		parent::__construct();
61
	}
62
63
64
	/**
65
	 * see abstract declaration in parent class for details.
66
	 */
67
	protected function _set_admin_pages() {
68
		$this->admin_registered_pages = array(
69
			'events_edit' => true,
70
		);
71
	}
72
73
74
	/**
75
	 * see abstract declaration in parent class for details
76
	 */
77
	protected function _set_valid_shortcodes() {
78
		//remember by leaving the other fields not set, those fields will inherit the valid shortcodes from the message type.
79
		$this->_valid_shortcodes = array(
80
			'to' => array('email','event_author', 'primary_registration_details', 'recipient_details'),
81
			'from' => array('email', 'event_author', 'primary_registration_details', 'recipient_details')
82
			);
83
	}
84
85
86
87
88
89
90
91
	/**
92
	 * see abstract declaration in parent class for details
93
	 *
94
	 *
95
	 * @access protected
96
	 * @return void
97
	 */
98
	protected function _set_validator_config() {
99
		$valid_shortcodes = $this->get_valid_shortcodes();
100
101
		$this->_validator_config = array(
102
			'to' => array(
103
				'shortcodes' => $valid_shortcodes['to'],
104
				'type' => 'email'
105
				),
106
			'from' => array(
107
				'shortcodes' => $valid_shortcodes['from'],
108
				'type' => 'email'
109
				),
110
			'subject' => array(
111
				'shortcodes' => array('organization', 'primary_registration_details', 'event_author', 'primary_registration_details', 'recipient_details')
112
				),
113
			'content' => array(
114
				'shortcodes' => array('event_list','attendee_list', 'ticket_list', 'organization', 'primary_registration_details', 'primary_registration_list', 'event_author', 'recipient_details', 'recipient_list', 'transaction', 'messenger')
115
				),
116
			'attendee_list' => array(
117
				'shortcodes' => array('attendee', 'event_list', 'ticket_list'),
118
				'required' => array('[ATTENDEE_LIST]')
119
				),
120
			'event_list' => array(
121
				'shortcodes' => array('event', 'attendee_list', 'ticket_list', 'venue', 'datetime_list', 'attendee', 'primary_registration_details', 'primary_registration_list', 'event_author', 'recipient_details', 'recipient_list'),
122
				'required' => array('[EVENT_LIST]')
123
				),
124
			'ticket_list' => array(
125
				'shortcodes' => array('event_list', 'attendee_list', 'ticket', 'datetime_list','primary_registration_details', 'recipient_details'),
126
				'required' => array('[TICKET_LIST]')
127
				),
128
			'datetime_list' => array(
129
				'shortcodes' => array('datetime'),
130
				'required' => array('[DATETIME_LIST]')
131
				)
132
			);
133
	}
134
135
136
137
138
139
	/**
140
	 * @see parent EE_messenger class for docs
141
	 *
142
	 * @since 4.5.0
143
	 */
144
	public function do_secondary_messenger_hooks( $sending_messenger_name ) {
145
		if ( $sending_messenger_name = 'html' ) {
0 ignored issues
show
Unused Code introduced by
$sending_messenger_name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
			add_filter( 'FHEE__EE_Messages_Template_Pack__get_variation', array( $this, 'add_email_css' ), 10, 8 );
147
		}
148
	}
149
150
151
152
153
	public function add_email_css( $variation_path, $messenger, $message_type, $type, $variation, $file_extension, $url, EE_Messages_Template_Pack $template_pack ) {
154
		//prevent recursion on this callback.
155
		remove_filter( 'FHEE__EE_Messages_Template_Pack__get_variation', array( $this, 'add_email_css' ), 10 );
156
		$variation = $this->get_variation( $template_pack, $message_type, $url, 'main', $variation, FALSE  );
157
158
		add_filter( 'FHEE__EE_Messages_Template_Pack__get_variation', array( $this, 'add_email_css' ), 10, 8 );
159
		return $variation;
160
	}
161
162
163
164
165
	/**
166
	 * See parent for details
167
	 *
168
	 * @access protected
169
	 * @return void
170
	 */
171
	protected function _set_test_settings_fields() {
172
		$this->_test_settings_fields = array(
173
			'to' => array(
174
				'input' => 'text',
175
				'label' => __('Send a test email to', 'event_espresso'),
176
				'type' => 'email',
177
				'required' => TRUE,
178
				'validation' => TRUE,
179
				'css_class' => 'large-text',
180
				'format' => '%s',
181
				'default' => get_bloginfo('admin_email')
182
				),
183
			'subject' => array(
184
				'input' => 'hidden',
185
				'label' => '',
186
				'type' => 'string',
187
				'required' => FALSE,
188
				'validation' => FALSE,
189
				'format' => '%s',
190
				'value' => sprintf( __('Test email sent from %s', 'event_espresso'), get_bloginfo('name') ),
191
				'default'=> '',
192
				'css_class' => ''
193
				)
194
			);
195
	}
196
197
198
199
200
201
	/**
202
	 * _set_template_fields
203
	 * This sets up the fields that a messenger requires for the message to go out.
204
	 *
205
	 * @access  protected
206
	 * @return void
207
	 */
208
	protected function _set_template_fields() {
209
		// any extra template fields that are NOT used by the messenger but will get used by a messenger field for shortcode replacement get added to the 'extra' key in an associated array indexed by the messenger field they relate to.  This is important for the Messages_admin to know what fields to display to the user.  Also, notice that the "values" are equal to the field type that messages admin will use to know what kind of field to display. The values ALSO have one index labeled "shortcode".  the values in that array indicate which ACTUAL SHORTCODE (i.e. [SHORTCODE]) is required in order for this extra field to be displayed.  If the required shortcode isn't part of the shortcodes array then the field is not needed and will not be displayed/parsed.
210
		$this->_template_fields = array(
211
			'to' => array(
212
				'input' => 'text',
213
				'label' => __('To', 'event_espresso'),
214
				'type' => 'string',
215
				'required' => TRUE,
216
				'validation' => TRUE,
217
				'css_class' => 'large-text',
218
				'format' => '%s'
219
			),
220
			'from' => array(
221
				'input' => 'text',
222
				'label' => __('From', 'event_espresso'),
223
				'type' => 'string',
224
				'required' => TRUE,
225
				'validation' => TRUE,
226
				'css_class' => 'large-text',
227
				'format' => '%s'
228
			),
229
			'subject' => array(
230
				'input' => 'text',
231
				'label' => __('Subject', 'event_espresso'),
232
				'type' => 'string',
233
				'required' => TRUE,
234
				'validation' => TRUE,
235
				'css_class' => 'large-text',
236
				'format' => '%s'
237
			),
238
			'content' => '', //left empty b/c it is in the "extra array" but messenger still needs needs to know this is a field.
239
			'extra' => array(
240
				'content' => array(
241
					'main' => array(
242
						'input' => 'wp_editor',
243
						'label' => __('Main Content', 'event_espresso'),
244
						'type' => 'string',
245
						'required' => TRUE,
246
						'validation' => TRUE,
247
						'format' => '%s',
248
						'rows' => '15'
249
					),
250
					'event_list' => array(
251
						'input' => 'wp_editor',
252
						'label' => '[EVENT_LIST]',
253
						'type' => 'string',
254
						'required' => TRUE,
255
						'validation' => TRUE,
256
						'format' => '%s',
257
						'rows' => '15',
258
						'shortcodes_required' => array('[EVENT_LIST]')
259
						),
260
					'attendee_list' => array(
261
						'input' => 'textarea',
262
						'label' => '[ATTENDEE_LIST]',
263
						'type' => 'string',
264
						'required' => TRUE,
265
						'validation' => TRUE,
266
						'format' => '%s',
267
						'css_class' => 'large-text',
268
						'rows' => '5',
269
						'shortcodes_required' => array('[ATTENDEE_LIST]')
270
					),
271
					'ticket_list' => array(
272
						'input' => 'textarea',
273
						'label' => '[TICKET_LIST]',
274
						'type' => 'string',
275
						'required' => TRUE,
276
						'validation' => TRUE,
277
						'format' => '%s',
278
						'css_class' => 'large-text',
279
						'rows' => '10',
280
						'shortcodes_required' => array('[TICKET_LIST]')
281
						),
282
					'datetime_list' => array(
283
						'input' => 'textarea',
284
						'label' => '[DATETIME_LIST]',
285
						'type' => 'string',
286
						'required' => TRUE,
287
						'validation' => TRUE,
288
						'format' => '%s',
289
						'css_class' => 'large-text',
290
						'rows' => '10',
291
						'shortcodes_required' => array('[DATETIME_LIST]')
292
						)
293
				)
294
			)
295
		);
296
	}
297
298
299
300
301
302
	/**
303
	 * See definition of this class in parent
304
	 */
305
	protected function _set_default_message_types() {
306
		$this->_default_message_types = array(
307
			'payment',
308
			'payment_refund',
309
			'registration',
310
			'not_approved_registration',
311
			'pending_approval'
312
			);
313
	}
314
315
316
317
318
	/**
319
	 * @see definition of this class in parent
320
	 *
321
	 * @since 4.5.0
322
	 *
323
	 */
324
	protected function _set_valid_message_types() {
325
		$this->_valid_message_types = array(
326
			'payment',
327
			'registration',
328
			'not_approved_registration',
329
			'declined_registration',
330
			'cancelled_registration',
331
			'pending_approval',
332
			'registration_summary',
333
			'payment_reminder',
334
			'payment_declined',
335
			'payment_refund'
336
			);
337
	}
338
339
340
341
342
	/**
343
	 * setting up admin_settings_fields for messenger.
344
	 */
345
	protected function _set_admin_settings_fields() {}
346
347
	/**
348
	 * We just deliver the messages don't kill us!!
349
	 * @return bool | WP_Error  true if message delivered, false if it didn't deliver OR bubble up any error object if present.
350
	 */
351
	protected function _send_message() {
352
		$success =  wp_mail(
353
			html_entity_decode( $this->_to, ENT_QUOTES, "UTF-8" ),
354
			stripslashes( html_entity_decode( $this->_subject, ENT_QUOTES, "UTF-8" )),
355
			$this->_body(),
356
			$this->_headers()
357
		);
358 View Code Duplication
		if ( ! $success ) {
359
			EE_Error::add_error(
360
				sprintf(
361
					__( 'The email did not send successfully.%3$sThe WordPress wp_mail function is used for sending mails but does not give any useful information when an email fails to send.%3$sIt is possible the "to" address (%1$s) or "from" address (%2$s) is invalid.%3$s', 'event_espresso'),
362
					$this->_to,
363
					$this->_from,
364
					'<br />'
365
				),
366
				__FILE__, __FUNCTION__, __LINE__
367
			);
368
		}
369
		return $success;
370
	}
371
372
373
374
375
	/**
376
	 * see parent for definition
377
	 * @return string html body of the message content and the related css.
378
	 */
379
	protected function _preview() {
380
		return $this->_body( true );
381
	}
382
383
384
385
386
387
	/**
388
	 * Setup headers for email
389
	 *
390
	 * @access protected
391
	 * @return string formatted header for email
392
	 */
393
	protected function _headers() {
394
		$this->_ensure_has_from_email_address();
395
		$from = stripslashes_deep( html_entity_decode($this->_from,  ENT_QUOTES,"UTF-8" ) );
396
		$headers = array(
397
			'MIME-Version: 1.0',
398
			'From:' . $from,
399
			'Reply-To:' . $from,
400
			'Content-Type:text/html; charset=utf-8'
401
			);
402
403
		//but wait!  Header's for the from is NOT reliable because some plugins don't respect From: as set in the header.
404
		add_filter( 'wp_mail_from',  array( $this, 'set_from_address' ), 100 );
405
		add_filter( 'wp_mail_from_name', array( $this, 'set_from_name' ), 100 );
406
		return apply_filters( 'FHEE__EE_Email_messenger___headers', $headers, $this->_incoming_message_type, $this );
407
	}
408
409
410
	/**
411
	 * This simply ensures that the from address is not empty.  If it is, then we use whatever is set as the site email
412
	 * address for the from address to avoid problems with sending emails.
413
	 */
414
	protected function _ensure_has_from_email_address() {
415
		if ( empty( $this->_from ) ) {
416
			$this->_from = get_bloginfo('admin_email');
417
		}
418
	}
419
420
421
422
	/**
423
	 * This simply parses whatever is set as the $_from address and determines if it is in the format {name} <{email}> or just {email} and returns an array with the "from_name" and "from_email" as the values.
424
	 *
425
	 * Note from_name *MAY* be empty
426
	 *
427
	 * @since 4.3.1
428
	 *
429
	 * @return array
430
	 */
431
	private function _parse_from() {
432
		if ( strpos( $this->_from, '<' ) !== false ) {
433
			$from_name = substr( $this->_from, 0, strpos( $this->_from, '<' ) - 1 );
434
			$from_name = str_replace( '"', '', $from_name );
435
			$from_name = trim( $from_name );
436
437
			$from_email = substr( $this->_from, strpos( $this->_from, '<' ) + 1 );
438
			$from_email = str_replace( '>', '', $from_email );
439
			$from_email = trim( $from_email );
440
		} elseif ( trim( $this->_from ) !== '' ) {
441
			$from_name = '';
442
			$from_email = trim( $this->_from );
443
		} else {
444
			$from_name = $from_email = '';
445
		}
446
		return array( $from_name, $from_email );
447
	}
448
449
450
451
452
	/**
453
	 * Callback for the wp_mail_from filter.
454
	 *
455
	 * @since 4.3.1
456
	 *
457
	 * @param string $from_email What the original from_email is.
458
	 */
459
	public function set_from_address( $from_email ) {
0 ignored issues
show
Unused Code introduced by
The parameter $from_email 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...
460
		$parsed_from = $this->_parse_from();
461
		//includes fallback if the parsing failed.
462
		$from_email = is_array( $parsed_from ) && ! empty( $parsed_from[1] ) ? $parsed_from[1] : get_bloginfo( 'admin_email' );
463
		return $from_email;
464
	}
465
466
467
468
469
	/**
470
	 * Callback fro the wp_mail_from_name filter.
471
	 *
472
	 * @since 4.3.1
473
	 *
474
	 * @param string $from_name The original from_name.
475
	 */
476
	public function set_from_name( $from_name ) {
477
		$parsed_from = $this->_parse_from();
478
		if ( is_array( $parsed_from) && ! empty( $parsed_from[0] ) ) {
479
			$from_name =  $parsed_from[0];
480
		}
481
482
		//if from name is "WordPress" let's sub in the site name instead (more friendly!)
483
		$from_name = $from_name == 'WordPress' ? get_bloginfo() : $from_name;
484
485
		return stripslashes_deep( html_entity_decode($from_name,  ENT_QUOTES,"UTF-8" ) );
486
	}
487
488
489
490
	/**
491
	 * setup body for email
492
	 *
493
	 * @param bool $preview will determine whether this is preview template or not.
494
	 * @return string formatted body for email.
495
	 */
496
	protected function _body( $preview = false ) {
497
		//setup template args!
498
		$this->_template_args = array(
499
			'subject' => $this->_subject,
500
			'from' => $this->_from,
501
			'main_body' => wpautop(stripslashes_deep( html_entity_decode($this->_content,  ENT_QUOTES,"UTF-8" ) ))
502
			);
503
		$body =  $this->_get_main_template( $preview );
504
505
		/**
506
		 * This filter allows one to bypass the CSSToInlineStyles tool and leave the body untouched.
507
		 *
508
		 * @type    bool    $preview    Indicates whether a preview is being generated or not.
509
		 * @return  bool    true  indicates to use the inliner, false bypasses it.
510
		 */
511
		if ( apply_filters( 'FHEE__EE_Email_messenger__apply_CSSInliner ', true, $preview ) ) {
512
513
			//require CssToInlineStyles library and its dependencies via composer autoloader
514
			require_once EE_THIRD_PARTY . 'cssinliner/vendor/autoload.php';
515
516
			//now if this isn't a preview, let's setup the body so it has inline styles
517
			if ( ! $preview || ( $preview && defined( 'DOING_AJAX' ) ) ) {
518
				$style = file_get_contents( $this->get_variation( $this->_tmp_pack, $this->_incoming_message_type->name, FALSE, 'main', $this->_variation ), TRUE );
519
				$CSS = new TijsVerkoyen\CssToInlineStyles\CssToInlineStyles( $body, $style );
520
				$body = ltrim( $CSS->convert( true ), ">\n" ); //for some reason the library has a bracket and new line at the beginning.  This takes care of that.
521
				$body = ltrim( $body, "<?" ); //see https://events.codebasehq.com/projects/event-espresso/tickets/8609
522
			}
523
524
		}
525
		return $body;
526
	}
527
528
529
530
531
	/**
532
	 * This just returns any existing test settings that might be saved in the database
533
	 *
534
	 * @access public
535
	 * @return array
536
	 */
537
	public function get_existing_test_settings() {
538
		$settings = parent::get_existing_test_settings();
539
		//override subject if present because we always want it to be fresh.
540
		if ( is_array( $settings ) && ! empty( $settings['subject'] ) ) {
541
			$settings['subject'] = sprintf( __('Test email sent from %s', 'event_espresso'), get_bloginfo('name') );
542
		}
543
		return $settings;
544
	}
545
546
547
}
548
549
// end of file:	includes/core/messages/messengers/EE_Email_messenger.class.php
550