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:
Complex classes like Give_Email_Notification 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 Give_Email_Notification, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class Give_Email_Notification { |
||
| 28 | /** |
||
| 29 | * Array of instances |
||
| 30 | * |
||
| 31 | * @since 2.0 |
||
| 32 | * @access private |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private static $singleton = array(); |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Array of notification settings. |
||
| 40 | * |
||
| 41 | * @since 2.0 |
||
| 42 | * @access public |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public $config = array( |
||
| 46 | 'id' => '', |
||
| 47 | 'label' => '', |
||
| 48 | 'description' => '', |
||
| 49 | 'has_recipient_field' => false, |
||
| 50 | 'recipient_group_name' => '', |
||
| 51 | 'notification_status' => 'disabled', |
||
| 52 | 'notification_status_editable' => true, |
||
| 53 | 'notices' => array(), |
||
| 54 | 'content_type_editable' => true, |
||
| 55 | 'has_preview' => true, |
||
| 56 | 'has_preview_header' => true, |
||
| 57 | 'preview_email_tags_values' => array(), |
||
| 58 | 'email_tag_context' => 'all', |
||
| 59 | 'form_metabox_setting' => true, |
||
| 60 | 'content_type' => '', |
||
| 61 | 'email_template' => '', |
||
| 62 | 'default_email_subject' => '', |
||
| 63 | 'default_email_message' => '', |
||
| 64 | // This setting page will appear under core setting. |
||
| 65 | 'show_on_emails_setting_page' => true, |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string $recipient_email Donor email. |
||
| 70 | * @access protected |
||
| 71 | * @since 2.0 |
||
| 72 | */ |
||
| 73 | protected $recipient_email = ''; |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Setup email notification. |
||
| 78 | * |
||
| 79 | * @since 2.0 |
||
| 80 | */ |
||
| 81 | public function init() { |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Get instance. |
||
| 88 | * |
||
| 89 | * @since 2.0 |
||
| 90 | * @access public |
||
| 91 | * |
||
| 92 | * @param string $email_notification_id |
||
| 93 | * |
||
| 94 | * @return Give_Email_Notification |
||
| 95 | */ |
||
| 96 | public static function get_instance( $email_notification_id = '' ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Setup action and filters. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * @since 2.0 |
||
| 123 | * |
||
| 124 | * @param array $config |
||
| 125 | */ |
||
| 126 | public function load( $config ) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Setup filters. |
||
| 174 | * |
||
| 175 | * @since 2.0 |
||
| 176 | * @access public |
||
| 177 | */ |
||
| 178 | private function setup_filters() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Add sections. |
||
| 228 | * |
||
| 229 | * @since 2.0 |
||
| 230 | * |
||
| 231 | * @param array $sections |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function add_section( $sections ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Add sections. |
||
| 243 | * |
||
| 244 | * @since 2.0 |
||
| 245 | * |
||
| 246 | * @param bool $hide_section |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function hide_section( $hide_section ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Register email settings. |
||
| 258 | * |
||
| 259 | * @since 2.0 |
||
| 260 | * @access public |
||
| 261 | * |
||
| 262 | * @param array $settings |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function add_setting_fields( $settings ) { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Get setting fields |
||
| 277 | * |
||
| 278 | * @since 2.0 |
||
| 279 | * @access public |
||
| 280 | * |
||
| 281 | * @param int $form_id |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function get_setting_fields( $form_id = null ) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Register email settings to form metabox. |
||
| 292 | * |
||
| 293 | * @since 2.0 |
||
| 294 | * @access public |
||
| 295 | * |
||
| 296 | * @param array $settings |
||
| 297 | * @param int $form_id |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function add_metabox_setting_field( $settings, $form_id ) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Get extra setting field. |
||
| 315 | * |
||
| 316 | * @since 2.0 |
||
| 317 | * @access public |
||
| 318 | * |
||
| 319 | * @param int $form_id |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | public function get_extra_setting_fields( $form_id = null ) { |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * Get recipient(s). |
||
| 330 | * |
||
| 331 | * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
||
| 332 | * |
||
| 333 | * @since 2.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @param int $form_id |
||
| 337 | * |
||
| 338 | * @return string|array |
||
| 339 | */ |
||
| 340 | public function get_recipient( $form_id = null ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get notification status. |
||
| 373 | * |
||
| 374 | * @since 2.0 |
||
| 375 | * @access public |
||
| 376 | * |
||
| 377 | * @param int $form_id |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function get_notification_status( $form_id = null ) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get email subject. |
||
| 401 | * |
||
| 402 | * @since 2.0 |
||
| 403 | * @access public |
||
| 404 | * |
||
| 405 | * @param int $form_id |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | View Code Duplication | function get_email_subject( $form_id = null ) { |
|
| 410 | $subject = wp_strip_all_tags( |
||
| 411 | Give_Email_Notification_Util::get_value( |
||
| 412 | $this, |
||
| 413 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
||
| 414 | $form_id, |
||
| 415 | $this->config['default_email_subject'] |
||
| 416 | ) |
||
| 417 | ); |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Filter the subject. |
||
| 421 | * |
||
| 422 | * @since 2.0 |
||
| 423 | */ |
||
| 424 | return apply_filters( |
||
| 425 | "give_{$this->config['id']}_get_email_subject", |
||
| 426 | $subject, |
||
| 427 | $this, |
||
| 428 | $form_id |
||
| 429 | ); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get email message. |
||
| 434 | * |
||
| 435 | * @since 2.0 |
||
| 436 | * @access public |
||
| 437 | * |
||
| 438 | * @param int $form_id |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function get_email_message( $form_id = null ) { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Get email content type. |
||
| 466 | * |
||
| 467 | * @since 2.0 |
||
| 468 | * @access public |
||
| 469 | * |
||
| 470 | * @param $form_id |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function get_email_content_type( $form_id ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get email template. |
||
| 497 | * |
||
| 498 | * @since 2.0 |
||
| 499 | * @access public |
||
| 500 | * |
||
| 501 | * @param $form_id |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function get_email_template( $form_id ) { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Get allowed email tags for current email notification. |
||
| 529 | * |
||
| 530 | * @since 2.0 |
||
| 531 | * @access private |
||
| 532 | * |
||
| 533 | * @param bool $formatted |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | public function get_allowed_email_tags( $formatted = false ) { |
||
| 538 | // Get all email tags. |
||
| 539 | $email_tags = Give()->email_tags->get_tags(); |
||
| 540 | |||
| 541 | // Skip if all email template tags context setup exit. |
||
| 542 | if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) { |
||
| 543 | if ( is_array( $this->config['email_tag_context'] ) ) { |
||
| 544 | View Code Duplication | foreach ( $email_tags as $index => $email_tag ) { |
|
| 545 | if ( in_array( $email_tag['context'], $this->config['email_tag_context'] ) ) { |
||
| 546 | continue; |
||
| 547 | } |
||
| 548 | |||
| 549 | unset( $email_tags[ $index ] ); |
||
| 550 | } |
||
| 551 | } else { |
||
| 552 | View Code Duplication | foreach ( $email_tags as $index => $email_tag ) { |
|
| 553 | if ( $this->config['email_tag_context'] === $email_tag['context'] ) { |
||
| 554 | continue; |
||
| 555 | } |
||
| 556 | |||
| 557 | unset( $email_tags[ $index ] ); |
||
| 558 | } |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | if ( count( $email_tags ) && $formatted ) : ob_start() ?> |
||
| 563 | <div class="give-email-tags-wrap"> |
||
| 564 | View Code Duplication | <?php foreach ( $email_tags as $email_tag ) : ?> |
|
| 565 | <span class="give_<?php echo $email_tag['tag']; ?>_tag"> |
||
| 566 | <code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?> |
||
| 567 | </span> |
||
| 568 | <?php endforeach; ?> |
||
| 569 | </div> |
||
| 570 | <?php |
||
| 571 | $email_tags = ob_get_clean(); |
||
| 572 | endif; |
||
| 573 | |||
| 574 | return $email_tags; |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get preview email recipients. |
||
| 579 | * |
||
| 580 | * @since 2.0 |
||
| 581 | * @access public |
||
| 582 | * |
||
| 583 | * @param int $form_id |
||
| 584 | * |
||
| 585 | * @return array|string |
||
| 586 | */ |
||
| 587 | public function get_preview_email_recipient( $form_id = null ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Get the recipient attachments. |
||
| 605 | * |
||
| 606 | * @since 2.0 |
||
| 607 | * @access public |
||
| 608 | * |
||
| 609 | * @param int $form_id |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | public function get_email_attachments( $form_id = null ) { |
||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * Send preview email. |
||
| 625 | * |
||
| 626 | * @since 2.0 |
||
| 627 | * @access public |
||
| 628 | */ |
||
| 629 | public function send_preview_email() { |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * Send email notification. |
||
| 661 | * |
||
| 662 | * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails): |
||
| 663 | * 1. payment_id |
||
| 664 | * 2. user_id |
||
| 665 | * 3. form_id |
||
| 666 | * 4. donor_id |
||
| 667 | * 5. for third party email tags you can pass necessary param along above parameters other value replace by empty string. |
||
| 668 | * |
||
| 669 | * @since 2.0 |
||
| 670 | * @access public |
||
| 671 | * |
||
| 672 | * @param array $email_tag_args Arguments which helps to decode email template tags. |
||
| 673 | * |
||
| 674 | * @return bool |
||
| 675 | */ |
||
| 676 | public function send_email_notification( $email_tag_args = array() ) { |
||
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * Decode preview email template tags. |
||
| 739 | * |
||
| 740 | * @since 2.0 |
||
| 741 | * |
||
| 742 | * @param string $message |
||
| 743 | * |
||
| 744 | * @return string |
||
| 745 | */ |
||
| 746 | public function preview_email_template_tags( $message ) { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Setup email data |
||
| 831 | * |
||
| 832 | * @since 2.0 |
||
| 833 | */ |
||
| 834 | public function setup_email_data() { |
||
| 836 | } |
||
| 837 | |||
| 839 |