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 |
||
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() { |
||
41 | |||
42 | /** |
||
43 | * Cloning is forbidden. |
||
44 | * |
||
45 | * @since 2.1 |
||
46 | */ |
||
47 | public function __clone() { |
||
50 | |||
51 | /** |
||
52 | * Unserializing instances of this class is forbidden. |
||
53 | * |
||
54 | * @since 2.1 |
||
55 | */ |
||
56 | public function __wakeup() { |
||
59 | |||
60 | /** |
||
61 | * Hook in all transactional emails. |
||
62 | */ |
||
63 | public static function init_transactional_emails() { |
||
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() { |
||
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() { |
||
152 | |||
153 | /** |
||
154 | * Return the email classes - used in admin to load settings. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function get_emails() { |
||
161 | |||
162 | /** |
||
163 | * Get from name for email. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function get_from_name() { |
||
170 | |||
171 | /** |
||
172 | * Get from email address. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function get_from_address() { |
||
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 ) { |
||
188 | |||
189 | /** |
||
190 | * Get the email footer. |
||
191 | */ |
||
192 | public function email_footer() { |
||
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 ) { |
||
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 = "" ) { |
||
234 | |||
235 | /** |
||
236 | * Prepare and send the customer invoice email on demand. |
||
237 | */ |
||
238 | public function customer_invoice( $order ) { |
||
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 ) { |
||
259 | |||
260 | /** |
||
261 | * Show the order details table |
||
262 | */ |
||
263 | public function order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
372 | |||
373 | /** |
||
374 | * Get the email addresses. |
||
375 | */ |
||
376 | public function email_addresses( $order, $sent_to_admin = false, $plain_text = false ) { |
||
383 | |||
384 | /** |
||
385 | * Get blog name formatted for emails. |
||
386 | * @return string |
||
387 | */ |
||
388 | private function get_blogname() { |
||
391 | |||
392 | /** |
||
393 | * Low stock notification email. |
||
394 | * |
||
395 | * @param WC_Product $product |
||
396 | */ |
||
397 | public function low_stock( $product ) { |
||
409 | |||
410 | /** |
||
411 | * No stock notification email. |
||
412 | * |
||
413 | * @param WC_Product $product |
||
414 | */ |
||
415 | public function no_stock( $product ) { |
||
427 | |||
428 | /** |
||
429 | * Backorder notification email. |
||
430 | * |
||
431 | * @param array $args |
||
432 | */ |
||
433 | public function backorder( $args ) { |
||
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 ) { |
||
474 | } |
||
475 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.