Completed
Pull Request — master (#11455)
by
unknown
11:57
created

WC_Emails   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 457
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 9
Bugs 1 Features 1
Metric Value
dl 0
loc 457
rs 6.8
c 9
b 1
f 1
wmc 55
lcom 3
cbo 4

26 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 6 2
A __clone() 0 3 1
A __wakeup() 0 3 1
B init_transactional_emails() 0 27 2
A send_transactional_email() 0 5 1
A init() 0 23 3
A get_emails() 0 3 1
A get_from_name() 0 3 1
A get_from_address() 0 3 1
A email_header() 0 3 1
A email_footer() 0 3 1
A wrap_message() 0 15 1
A send() 0 5 1
A customer_invoice() 0 4 1
A customer_new_account() 0 10 3
A order_details() 0 7 2
A __construct() 0 20 1
C order_meta() 0 43 14
A customer_detail_field_is_valid() 0 3 2
B customer_details() 0 32 5
A email_addresses() 0 7 2
A get_blogname() 0 3 1
A low_stock() 0 12 1
A no_stock() 0 12 1
B backorder() 0 24 4
A order_schema_markup() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like WC_Emails often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WC_Emails, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Transactional Emails Controller
9
 *
10
 * WooCommerce Emails Class which handles the sending on transactional emails and email templates. This class loads in available emails.
11
 *
12
 * @class 		WC_Emails
13
 * @version		2.3.0
14
 * @package		WooCommerce/Classes/Emails
15
 * @category	Class
16
 * @author 		WooThemes
17
 */
18
class WC_Emails {
19
20
	/** @var array Array of email notification classes */
21
	public $emails;
22
23
	/** @var WC_Emails The single instance of the class */
24
	protected static $_instance = null;
25
26
	/**
27
	 * Main WC_Emails Instance.
28
	 *
29
	 * Ensures only one instance of WC_Emails is loaded or can be loaded.
30
	 *
31
	 * @since 2.1
32
	 * @static
33
	 * @return WC_Emails Main instance
34
	 */
35
	public static function instance() {
36
		if ( is_null( self::$_instance ) ) {
37
			self::$_instance = new self();
38
		}
39
		return self::$_instance;
40
	}
41
42
	/**
43
	 * Cloning is forbidden.
44
	 *
45
	 * @since 2.1
46
	 */
47
	public function __clone() {
48
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
49
	}
50
51
	/**
52
	 * Unserializing instances of this class is forbidden.
53
	 *
54
	 * @since 2.1
55
	 */
56
	public function __wakeup() {
57
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
58
	}
59
60
	/**
61
	 * Hook in all transactional emails.
62
	 */
63
	public static function init_transactional_emails() {
64
		$email_actions = apply_filters( 'woocommerce_email_actions', array(
65
			'woocommerce_low_stock',
66
			'woocommerce_no_stock',
67
			'woocommerce_product_on_backorder',
68
			'woocommerce_order_status_pending_to_processing',
69
			'woocommerce_order_status_pending_to_completed',
70
			'woocommerce_order_status_pending_to_cancelled',
71
			'woocommerce_order_status_pending_to_failed',
72
			'woocommerce_order_status_pending_to_on-hold',
73
			'woocommerce_order_status_failed_to_processing',
74
			'woocommerce_order_status_failed_to_completed',
75
			'woocommerce_order_status_failed_to_on-hold',
76
			'woocommerce_order_status_on-hold_to_processing',
77
			'woocommerce_order_status_on-hold_to_cancelled',
78
			'woocommerce_order_status_on-hold_to_failed',
79
			'woocommerce_order_status_completed',
80
			'woocommerce_order_fully_refunded',
81
			'woocommerce_order_partially_refunded',
82
			'woocommerce_new_customer_note',
83
			'woocommerce_created_customer'
84
		) );
85
86
		foreach ( $email_actions as $action ) {
87
			add_action( $action, array( __CLASS__, 'send_transactional_email' ), 10, 10 );
88
		}
89
	}
90
91
	/**
92
	 * Init the mailer instance and call the notifications for the current filter.
93
	 * @internal param array $args (default: array())
94
	 */
95
	public static function send_transactional_email() {
96
		$args = func_get_args();
97
		self::instance();
98
		do_action_ref_array( current_filter() . '_notification', $args );
99
	}
100
101
	/**
102
	 * Constructor for the email class hooks in all emails that can be sent.
103
	 *
104
	 */
105
	public function __construct() {
106
		$this->init();
107
108
		// Email Header, Footer and content hooks
109
		add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
110
		add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
111
		add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 );
112
		add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 );
113
		add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 );
114
		add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
115
116
		// Hooks for sending emails during store events
117
		add_action( 'woocommerce_low_stock_notification', array( $this, 'low_stock' ) );
118
		add_action( 'woocommerce_no_stock_notification', array( $this, 'no_stock' ) );
119
		add_action( 'woocommerce_product_on_backorder_notification', array( $this, 'backorder' ) );
120
		add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account' ), 10, 3 );
121
122
		// Let 3rd parties unhook the above via this hook
123
		do_action( 'woocommerce_email', $this );
124
	}
125
126
	/**
127
	 * Init email classes.
128
	 */
129
	public function init() {
130
		// Include email classes
131
		include_once( dirname( __FILE__ ) . '/emails/class-wc-email.php' );
132
133
		$this->emails['WC_Email_New_Order'] 		                 = include( 'emails/class-wc-email-new-order.php' );
134
		$this->emails['WC_Email_Cancelled_Order'] 		             = include( 'emails/class-wc-email-cancelled-order.php' );
135
		$this->emails['WC_Email_Failed_Order'] 		                 = include( 'emails/class-wc-email-failed-order.php' );
136
		$this->emails['WC_Email_Customer_On_Hold_Order'] 		     = include( 'emails/class-wc-email-customer-on-hold-order.php' );
137
		$this->emails['WC_Email_Customer_Processing_Order'] 		 = include( 'emails/class-wc-email-customer-processing-order.php' );
138
		$this->emails['WC_Email_Customer_Completed_Order'] 		     = include( 'emails/class-wc-email-customer-completed-order.php' );
139
		$this->emails['WC_Email_Customer_Refunded_Order'] 		     = include( 'emails/class-wc-email-customer-refunded-order.php' );
140
		$this->emails['WC_Email_Customer_Invoice'] 		             = include( 'emails/class-wc-email-customer-invoice.php' );
141
		$this->emails['WC_Email_Customer_Note'] 		             = include( 'emails/class-wc-email-customer-note.php' );
142
		$this->emails['WC_Email_Customer_Reset_Password'] 		     = include( 'emails/class-wc-email-customer-reset-password.php' );
143
		$this->emails['WC_Email_Customer_New_Account'] 		         = include( 'emails/class-wc-email-customer-new-account.php' );
144
145
		$this->emails = apply_filters( 'woocommerce_email_classes', $this->emails );
146
147
		// include css inliner
148
		if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
149
			include_once( dirname( __FILE__ ) . '/libraries/class-emogrifier.php' );
150
		}
151
	}
152
153
	/**
154
	 * Return the email classes - used in admin to load settings.
155
	 *
156
	 * @return array
157
	 */
158
	public function get_emails() {
159
		return $this->emails;
160
	}
161
162
	/**
163
	 * Get from name for email.
164
	 *
165
	 * @return string
166
	 */
167
	public function get_from_name() {
168
		return wp_specialchars_decode( get_option( 'woocommerce_email_from_name' ), ENT_QUOTES );
169
	}
170
171
	/**
172
	 * Get from email address.
173
	 *
174
	 * @return string
175
	 */
176
	public function get_from_address() {
177
		return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
178
	}
179
180
	/**
181
	 * Get the email header.
182
	 *
183
	 * @param mixed $email_heading heading for the email
184
	 */
185
	public function email_header( $email_heading ) {
186
		wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
187
	}
188
189
	/**
190
	 * Get the email footer.
191
	 */
192
	public function email_footer() {
193
		wc_get_template( 'emails/email-footer.php' );
194
	}
195
196
	/**
197
	 * Wraps a message in the woocommerce mail template.
198
	 *
199
	 * @param mixed $email_heading
200
	 * @param string $message
201
	 * @return string
202
	 */
203
	public function wrap_message( $email_heading, $message, $plain_text = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plain_text 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...
204
		// Buffer
205
		ob_start();
206
207
		do_action( 'woocommerce_email_header', $email_heading );
208
209
		echo wpautop( wptexturize( $message ) );
210
211
		do_action( 'woocommerce_email_footer' );
212
213
		// Get contents
214
		$message = ob_get_clean();
215
216
		return $message;
217
	}
218
219
	/**
220
	 * Send the email.
221
	 *
222
	 * @param mixed $to
223
	 * @param mixed $subject
224
	 * @param mixed $message
225
	 * @param string $headers (default: "Content-Type: text/html\r\n")
226
	 * @param string $attachments (default: "")
227
	 * @return bool
228
	 */
229
	public function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
230
		// Send
231
		$email = new WC_Email();
232
		return $email->send( $to, $subject, $message, $headers, $attachments );
233
	}
234
235
	/**
236
	 * Prepare and send the customer invoice email on demand.
237
	 */
238
	public function customer_invoice( $order ) {
239
		$email = $this->emails['WC_Email_Customer_Invoice'];
240
		$email->trigger( $order );
241
	}
242
243
	/**
244
	 * Customer new account welcome email.
245
	 *
246
	 * @param int $customer_id
247
	 * @param array $new_customer_data
248
	 */
249
	public function customer_new_account( $customer_id, $new_customer_data = array(), $password_generated = false ) {
250
		if ( ! $customer_id ) {
251
			return;
252
		}
253
254
		$user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : '';
255
256
		$email = $this->emails['WC_Email_Customer_New_Account'];
257
		$email->trigger( $customer_id, $user_pass, $password_generated );
258
	}
259
260
	/**
261
	 * Show the order details table
262
	 */
263
	public function order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
264
		if ( $plain_text ) {
265
			wc_get_template( 'emails/plain/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) );
266
		} else {
267
			wc_get_template( 'emails/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) );
268
		}
269
	}
270
271
	/**
272
	 * Add order meta to email templates.
273
	 *
274
	 * @param mixed $order
275
	 * @param bool $sent_to_admin (default: false)
276
	 * @param bool $plain_text (default: false)
277
	 * @return string
278
	 */
279
	public function order_meta( $order, $sent_to_admin = false, $plain_text = false ) {
280
		$fields = apply_filters( 'woocommerce_email_order_meta_fields', array(), $sent_to_admin, $order );
281
282
		/**
283
		 * Deprecated woocommerce_email_order_meta_keys filter.
284
		 *
285
		 * @since 2.3.0
286
		 */
287
		$_fields = apply_filters( 'woocommerce_email_order_meta_keys', array(), $sent_to_admin );
288
289
		if ( $_fields ) {
290
			foreach ( $_fields as $key => $field ) {
291
				if ( is_numeric( $key ) ) {
292
					$key = $field;
293
				}
294
295
				$fields[ $key ] = array(
296
					'label' => wptexturize( $key ),
297
					'value' => wptexturize( get_post_meta( $order->get_id(), $field, true ) )
298
				);
299
			}
300
		}
301
302
		if ( $fields ) {
303
304
			if ( $plain_text ) {
305
306
				foreach ( $fields as $field ) {
307
					if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) {
308
						echo $field['label'] . ': ' . $field['value'] . "\n";
309
					}
310
				}
311
312
			} else {
313
314
				foreach ( $fields as $field ) {
315
					if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) {
316
						echo '<p><strong>' . $field['label'] . ':</strong> ' . $field['value'] . '</p>';
317
					}
318
				}
319
			}
320
		}
321
	}
322
323
	/**
324
	 * Is customer detail field valid?
325
	 * @param  array  $field
326
	 * @return boolean
327
	 */
328
	public function customer_detail_field_is_valid( $field ) {
329
		return isset( $field['label'] ) && ! empty( $field['value'] );
330
	}
331
332
	/**
333
	 * Add customer details to email templates.
334
	 *
335
	 * @param mixed $order
336
	 * @param bool $sent_to_admin (default: false)
337
	 * @param bool $plain_text (default: false)
338
	 * @return string
339
	 */
340
	public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) {
341
		$fields = array();
342
343
		if ( $order->get_customer_note() ) {
344
			$fields['customer_note'] = array(
345
				'label' => __( 'Note', 'woocommerce' ),
346
				'value' => wptexturize( $order->get_customer_note() )
347
			);
348
		}
349
350
		if ( $order->get_billing_email() ) {
351
			$fields['billing_email'] = array(
352
				'label' => __( 'Email', 'woocommerce' ),
353
				'value' => wptexturize( $order->get_billing_email() )
354
			);
355
	    }
356
357
	    if ( $order->get_billing_phone() ) {
358
			$fields['billing_phone'] = array(
359
				'label' => __( 'Tel', 'woocommerce' ),
360
				'value' => wptexturize( $order->get_billing_phone() )
361
			);
362
	    }
363
364
		$fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', $fields, $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) );
365
366
		if ( $plain_text ) {
367
			wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) );
368
		} else {
369
			wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) );
370
		}
371
	}
372
373
	/**
374
	 * Get the email addresses.
375
	 */
376
	public function email_addresses( $order, $sent_to_admin = false, $plain_text = false ) {
377
		if ( $plain_text ) {
378
			wc_get_template( 'emails/plain/email-addresses.php', array( 'order' => $order ) );
379
		} else {
380
			wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) );
381
		}
382
	}
383
384
	/**
385
	 * Get blog name formatted for emails.
386
	 * @return string
387
	 */
388
	private function get_blogname() {
389
		return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
390
	}
391
392
	/**
393
	 * Low stock notification email.
394
	 *
395
	 * @param WC_Product $product
396
	 */
397
	public function low_stock( $product ) {
398
		$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product low in stock', 'woocommerce' ) );
399
		$message = sprintf( __( '%s is low in stock.', 'woocommerce' ), html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) ) . ' ' . sprintf( __( 'There are %d left', 'woocommerce' ), html_entity_decode( strip_tags( $product->get_total_stock() ) ) );
400
401
		wp_mail(
402
			apply_filters( 'woocommerce_email_recipient_low_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),
403
			apply_filters( 'woocommerce_email_subject_low_stock', $subject, $product ),
404
			apply_filters( 'woocommerce_email_content_low_stock', $message, $product ),
405
			apply_filters( 'woocommerce_email_headers', '', 'low_stock', $product ),
406
			apply_filters( 'woocommerce_email_attachments', array(), 'low_stock', $product )
407
		);
408
	}
409
410
	/**
411
	 * No stock notification email.
412
	 *
413
	 * @param WC_Product $product
414
	 */
415
	public function no_stock( $product ) {
416
		$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product out of stock', 'woocommerce' ) );
417
		$message = sprintf( __( '%s is out of stock.', 'woocommerce' ), html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) );
418
419
		wp_mail(
420
			apply_filters( 'woocommerce_email_recipient_no_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),
421
			apply_filters( 'woocommerce_email_subject_no_stock', $subject, $product ),
422
			apply_filters( 'woocommerce_email_content_no_stock', $message, $product ),
423
			apply_filters( 'woocommerce_email_headers', '', 'no_stock', $product ),
424
			apply_filters( 'woocommerce_email_attachments', array(), 'no_stock', $product )
425
		);
426
	}
427
428
	/**
429
	 * Backorder notification email.
430
	 *
431
	 * @param array $args
432
	 */
433
	public function backorder( $args ) {
434
		$args = wp_parse_args( $args, array(
435
			'product'  => '',
436
			'quantity' => '',
437
			'order_id' => ''
438
		) );
439
440
		extract( $args );
441
442
		if ( ! $product || ! $quantity || ! ( $order = wc_get_order( $order_id ) ) ) {
443
			return;
444
		}
445
446
		$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product Backorder', 'woocommerce' ) );
447
		$message = sprintf( __( '%1$s units of %2$s have been backordered in order #%3$s.', 'woocommerce' ), $quantity, html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ), $order->get_order_number() );
448
449
		wp_mail(
450
			apply_filters( 'woocommerce_email_recipient_backorder', get_option( 'woocommerce_stock_email_recipient' ), $args ),
451
			apply_filters( 'woocommerce_email_subject_backorder', $subject, $args ),
452
			apply_filters( 'woocommerce_email_content_backorder', $message, $args ),
453
			apply_filters( 'woocommerce_email_headers', '', 'backorder', $args ),
454
			apply_filters( 'woocommerce_email_attachments', array(), 'backorder', $args )
455
		);
456
	}
457
458
	/**
459
	 * Adds Schema.org markup for order in JSON-LD format.
460
	 *
461
	 * @deprecated 2.7.0
462
	 * @see WC_Structured_Data::generate_order_data()
463
	 *
464
	 * @since 2.6.0
465
	 * @param mixed $order
466
	 * @param bool $sent_to_admin (default: false)
467
	 * @param bool $plain_text (default: false)
468
	 */
469
	public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) {
470
		_deprecated_function( 'WC_Emails::order_schema_markup', '2.7', 'WC_Structured_Data::generate_order_data' );
471
472
		WC()->structured_data->generate_output_structured_data( 'order', $order, $sent_to_admin, $plain_text );
473
	}
474
}
475