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 ) { | 
            ||
| 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 | 			} else { | 
            ||
| 312 | |||
| 313 | 				foreach ( $fields as $field ) { | 
            ||
| 314 | 					if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) { | 
            ||
| 315 | echo '<p><strong>' . $field['label'] . ':</strong> ' . $field['value'] . '</p>';  | 
            ||
| 316 | }  | 
            ||
| 317 | }  | 
            ||
| 318 | }  | 
            ||
| 319 | }  | 
            ||
| 320 | }  | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * Is customer detail field valid?  | 
            ||
| 324 | * @param array $field  | 
            ||
| 325 | * @return boolean  | 
            ||
| 326 | */  | 
            ||
| 327 | 	public function customer_detail_field_is_valid( $field ) { | 
            ||
| 330 | |||
| 331 | /**  | 
            ||
| 332 | * Add customer details to email templates.  | 
            ||
| 333 | *  | 
            ||
| 334 | * @param mixed $order  | 
            ||
| 335 | * @param bool $sent_to_admin (default: false)  | 
            ||
| 336 | * @param bool $plain_text (default: false)  | 
            ||
| 337 | * @return string  | 
            ||
| 338 | */  | 
            ||
| 339 | 	public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) { | 
            ||
| 340 | $fields = array();  | 
            ||
| 341 | |||
| 342 | 		if ( $order->get_customer_note() ) { | 
            ||
| 343 | $fields['customer_note'] = array(  | 
            ||
| 344 | 'label' => __( 'Note', 'woocommerce' ),  | 
            ||
| 345 | 'value' => wptexturize( $order->get_customer_note() ),  | 
            ||
| 346 | );  | 
            ||
| 347 | }  | 
            ||
| 348 | |||
| 349 | 		if ( $order->get_billing_email() ) { | 
            ||
| 350 | $fields['billing_email'] = array(  | 
            ||
| 351 | 'label' => __( 'Email', 'woocommerce' ),  | 
            ||
| 352 | 'value' => wptexturize( $order->get_billing_email() ),  | 
            ||
| 353 | );  | 
            ||
| 354 | }  | 
            ||
| 355 | |||
| 356 | 	    if ( $order->get_billing_phone() ) { | 
            ||
| 357 | $fields['billing_phone'] = array(  | 
            ||
| 358 | 'label' => __( 'Tel', 'woocommerce' ),  | 
            ||
| 359 | 'value' => wptexturize( $order->get_billing_phone() ),  | 
            ||
| 360 | );  | 
            ||
| 361 | }  | 
            ||
| 362 | |||
| 363 | $fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', $fields, $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) );  | 
            ||
| 364 | |||
| 365 | 		if ( $plain_text ) { | 
            ||
| 366 | wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) );  | 
            ||
| 367 | 		} else { | 
            ||
| 368 | wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) );  | 
            ||
| 369 | }  | 
            ||
| 370 | }  | 
            ||
| 371 | |||
| 372 | /**  | 
            ||
| 373 | * Get the email addresses.  | 
            ||
| 374 | */  | 
            ||
| 375 | 	public function email_addresses( $order, $sent_to_admin = false, $plain_text = false ) { | 
            ||
| 382 | |||
| 383 | /**  | 
            ||
| 384 | * Get blog name formatted for emails.  | 
            ||
| 385 | * @return string  | 
            ||
| 386 | */  | 
            ||
| 387 | 	private function get_blogname() { | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Low stock notification email.  | 
            ||
| 393 | *  | 
            ||
| 394 | * @param WC_Product $product  | 
            ||
| 395 | */  | 
            ||
| 396 | 	public function low_stock( $product ) { | 
            ||
| 408 | |||
| 409 | /**  | 
            ||
| 410 | * No stock notification email.  | 
            ||
| 411 | *  | 
            ||
| 412 | * @param WC_Product $product  | 
            ||
| 413 | */  | 
            ||
| 414 | 	public function no_stock( $product ) { | 
            ||
| 426 | |||
| 427 | /**  | 
            ||
| 428 | * Backorder notification email.  | 
            ||
| 429 | *  | 
            ||
| 430 | * @param array $args  | 
            ||
| 431 | */  | 
            ||
| 432 | 	public function backorder( $args ) { | 
            ||
| 456 | |||
| 457 | /**  | 
            ||
| 458 | * Adds Schema.org markup for order in JSON-LD format.  | 
            ||
| 459 | *  | 
            ||
| 460 | * @deprecated 2.7.0  | 
            ||
| 461 | * @see WC_Structured_Data::generate_order_data()  | 
            ||
| 462 | *  | 
            ||
| 463 | * @since 2.6.0  | 
            ||
| 464 | * @param mixed $order  | 
            ||
| 465 | * @param bool $sent_to_admin (default: false)  | 
            ||
| 466 | * @param bool $plain_text (default: false)  | 
            ||
| 467 | */  | 
            ||
| 468 | 	public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) { | 
            ||
| 473 | }  | 
            ||
| 474 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.