| Total Complexity | 58 |
| Total Lines | 487 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Invoice_Notification_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.
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 GetPaid_Invoice_Notification_Emails, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GetPaid_Invoice_Notification_Emails { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The array of invoice email actions. |
||
| 17 | * |
||
| 18 | * @param array |
||
| 19 | */ |
||
| 20 | public $invoice_actions; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class constructor |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 27 | |||
| 28 | $this->invoice_actions = apply_filters( |
||
| 29 | 'getpaid_notification_email_invoice_triggers', |
||
| 30 | array( |
||
| 31 | 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
||
| 32 | 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
||
| 33 | 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
||
| 34 | 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
||
| 35 | 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
||
| 36 | 'getpaid_invoice_status_publish' => 'completed_invoice', |
||
| 37 | 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
||
| 38 | 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
||
| 39 | 'getpaid_new_customer_note' => 'user_note', |
||
| 40 | 'getpaid_daily_maintenance' => 'overdue', |
||
| 41 | ) |
||
| 42 | ); |
||
| 43 | |||
| 44 | $this->init_hooks(); |
||
| 45 | |||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Registers email hooks. |
||
| 50 | */ |
||
| 51 | public function init_hooks() { |
||
| 52 | |||
| 53 | add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
||
| 54 | add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
||
| 55 | |||
| 56 | foreach ( $this->invoice_actions as $hook => $email_type ) { |
||
| 57 | $this->init_email_type_hook( $hook, $email_type ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Registers an email hook for an invoice action. |
||
| 63 | * |
||
| 64 | * @param string $hook |
||
| 65 | * @param string|array $email_type |
||
| 66 | */ |
||
| 67 | public function init_email_type_hook( $hook, $email_type ) { |
||
| 86 | } |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Filters invoice merge tags. |
||
| 92 | * |
||
| 93 | * @param array $merge_tags |
||
| 94 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
||
| 95 | */ |
||
| 96 | public function invoice_merge_tags( $merge_tags, $object ) { |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Generates invoice merge tags. |
||
| 118 | * |
||
| 119 | * @param WPInv_Invoice $invoice |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function get_invoice_merge_tags( $invoice ) { |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Helper function to send an email. |
||
| 172 | * |
||
| 173 | * @param WPInv_Invoice $invoice |
||
| 174 | * @param GetPaid_Notification_Email $email |
||
| 175 | * @param string $type |
||
| 176 | * @param string|array $recipients |
||
| 177 | * @param array $extra_args Extra template args. |
||
| 178 | */ |
||
| 179 | public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Also send emails to any cc users. |
||
| 233 | * |
||
| 234 | * @param array $recipients |
||
| 235 | * @param GetPaid_Notification_Email $email |
||
| 236 | */ |
||
| 237 | public function filter_email_recipients( $recipients, $email ) { |
||
| 238 | |||
| 239 | if ( ! $email->is_admin_email() ) { |
||
| 240 | $cc = $email->object->get_email_cc(); |
||
| 241 | $cc_2 = get_user_meta( $email->object->get_user_id(), '_wpinv_email_cc', true ); |
||
| 242 | |||
| 243 | if ( ! empty( $cc ) ) { |
||
| 244 | $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
||
| 245 | $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
||
| 246 | } |
||
| 247 | |||
| 248 | if ( ! empty( $cc_2 ) ) { |
||
| 249 | $cc_2 = array_map( 'sanitize_email', wpinv_parse_list( $cc_2 ) ); |
||
| 250 | $recipients = array_filter( array_unique( array_merge( $recipients, $cc_2 ) ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | } |
||
| 254 | |||
| 255 | return $recipients; |
||
| 256 | |||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sends a new invoice notification. |
||
| 261 | * |
||
| 262 | * @param WPInv_Invoice $invoice |
||
| 263 | */ |
||
| 264 | public function new_invoice( $invoice ) { |
||
| 265 | |||
| 266 | // Only send this email for invoices created via the admin page. |
||
| 267 | if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
||
| 268 | return; |
||
| 269 | } |
||
| 270 | |||
| 271 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 272 | $recipient = wpinv_get_admin_email(); |
||
| 273 | |||
| 274 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 275 | |||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Sends a cancelled invoice notification. |
||
| 280 | * |
||
| 281 | * @param WPInv_Invoice $invoice |
||
| 282 | */ |
||
| 283 | public function cancelled_invoice( $invoice ) { |
||
| 284 | |||
| 285 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 286 | $recipient = wpinv_get_admin_email(); |
||
| 287 | |||
| 288 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sends a failed invoice notification. |
||
| 294 | * |
||
| 295 | * @param WPInv_Invoice $invoice |
||
| 296 | */ |
||
| 297 | public function failed_invoice( $invoice ) { |
||
| 298 | |||
| 299 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 300 | $recipient = wpinv_get_admin_email(); |
||
| 301 | |||
| 302 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 303 | |||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sends a notification whenever an invoice is put on hold. |
||
| 308 | * |
||
| 309 | * @param WPInv_Invoice $invoice |
||
| 310 | */ |
||
| 311 | public function onhold_invoice( $invoice ) { |
||
| 312 | |||
| 313 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 314 | $recipient = $invoice->get_email(); |
||
| 315 | |||
| 316 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 317 | |||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Sends a notification whenever an invoice is marked as processing payment. |
||
| 322 | * |
||
| 323 | * @param WPInv_Invoice $invoice |
||
| 324 | */ |
||
| 325 | public function processing_invoice( $invoice ) { |
||
| 326 | |||
| 327 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 328 | $recipient = $invoice->get_email(); |
||
| 329 | |||
| 330 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 331 | |||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Sends a notification whenever an invoice is paid. |
||
| 336 | * |
||
| 337 | * @param WPInv_Invoice $invoice |
||
| 338 | */ |
||
| 339 | public function completed_invoice( $invoice ) { |
||
| 340 | |||
| 341 | // (Maybe) abort if it is a renewal invoice. |
||
| 342 | if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
||
| 343 | return; |
||
| 344 | } |
||
| 345 | |||
| 346 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 347 | $recipient = $invoice->get_email(); |
||
| 348 | |||
| 349 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sends a notification whenever an invoice is refunded. |
||
| 355 | * |
||
| 356 | * @param WPInv_Invoice $invoice |
||
| 357 | */ |
||
| 358 | public function refunded_invoice( $invoice ) { |
||
| 359 | |||
| 360 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 361 | $recipient = $invoice->get_email(); |
||
| 362 | |||
| 363 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 364 | |||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Notifies a user about new invoices |
||
| 369 | * |
||
| 370 | * @param WPInv_Invoice $invoice |
||
| 371 | * @param bool $force |
||
| 372 | */ |
||
| 373 | public function user_invoice( $invoice, $force = false ) { |
||
| 374 | |||
| 375 | if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
||
| 376 | return; |
||
| 377 | } |
||
| 378 | |||
| 379 | // Only send this email for invoices created via the admin page. |
||
| 380 | if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
||
| 381 | return; |
||
| 382 | } |
||
| 383 | |||
| 384 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 385 | $recipient = $invoice->get_email(); |
||
| 386 | |||
| 387 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
||
| 388 | |||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Checks if an invoice is a payment form invoice. |
||
| 393 | * |
||
| 394 | * @param int $invoice |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function is_payment_form_invoice( $invoice ) { |
||
| 398 | $is_payment_form_invoice = empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
||
| 399 | return apply_filters( 'getpaid_invoice_notifications_is_payment_form_invoice', $is_payment_form_invoice, $invoice ); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Notifies admin about new invoice notes |
||
| 404 | * |
||
| 405 | * @param WPInv_Invoice $invoice |
||
| 406 | * @param string $note |
||
| 407 | */ |
||
| 408 | public function user_note( $invoice, $note ) { |
||
| 409 | |||
| 410 | $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
||
| 411 | $recipient = $invoice->get_email(); |
||
| 412 | |||
| 413 | return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
||
| 414 | |||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * (Force) Sends overdue notices. |
||
| 419 | * |
||
| 420 | * @param WPInv_Invoice $invoice |
||
| 421 | */ |
||
| 422 | public function force_send_overdue_notice( $invoice ) { |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Sends overdue notices. |
||
| 429 | * |
||
| 430 | * @TODO: Create an invoices query class. |
||
| 431 | */ |
||
| 432 | public function overdue() { |
||
| 466 | } |
||
| 467 | |||
| 468 | } |
||
| 469 | |||
| 470 | } |
||
| 471 | |||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Calculates the date query for an invoices query |
||
| 476 | * |
||
| 477 | * @param array $reminder_days |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function get_date_query( $reminder_days ) { |
||
| 500 | |||
| 501 | } |
||
| 502 | |||
| 503 | } |
||
| 504 |