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 | * Array of notification settings. |
||
| 39 | * |
||
| 40 | * @since 2.0 |
||
| 41 | * @access public |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | public $config = array( |
||
| 45 | 'id' => '', |
||
| 46 | 'label' => '', |
||
| 47 | 'description' => '', |
||
| 48 | 'has_recipient_field' => false, |
||
| 49 | 'recipient_group_name' => '', |
||
| 50 | 'notification_status' => 'disabled', |
||
| 51 | 'notification_status_editable' => true, |
||
| 52 | 'notices' => array(), |
||
| 53 | 'content_type_editable' => true, |
||
| 54 | 'has_preview' => true, |
||
| 55 | 'has_preview_header' => true, |
||
| 56 | 'preview_email_tags_values' => array(), |
||
| 57 | 'email_tag_context' => 'all', |
||
| 58 | 'form_metabox_setting' => true, |
||
| 59 | 'content_type' => '', |
||
| 60 | 'email_template' => '', |
||
| 61 | 'default_email_subject' => '', |
||
| 62 | 'default_email_message' => '', |
||
| 63 | 'default_email_header' => '', |
||
| 64 | // This setting page will appear under core setting. |
||
| 65 | 'show_on_emails_setting_page' => true, |
||
| 66 | 'form_metabox_id' => 'give_email_notification_options_metabox_fields', |
||
| 67 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string $recipient_email Donor email. |
||
| 71 | * @access protected |
||
| 72 | * @since 2.0 |
||
| 73 | */ |
||
| 74 | protected $recipient_email = ''; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Setup email notification. |
||
| 79 | * |
||
| 80 | * @since 2.0 |
||
| 81 | */ |
||
| 82 | public function init() { |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Get instance. |
||
| 89 | * |
||
| 90 | * @since 2.0 |
||
| 91 | * @access public |
||
| 92 | * |
||
| 93 | * @param string $email_notification_id |
||
| 94 | * |
||
| 95 | * @return Give_Email_Notification |
||
| 96 | */ |
||
| 97 | public static function get_instance( $email_notification_id = '' ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Setup action and filters. |
||
| 121 | * |
||
| 122 | * @access public |
||
| 123 | * @since 2.0 |
||
| 124 | * |
||
| 125 | * @param array $config |
||
| 126 | */ |
||
| 127 | public function load( $config ) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Setup filters. |
||
| 175 | * |
||
| 176 | * @since 2.0 |
||
| 177 | * @access public |
||
| 178 | */ |
||
| 179 | private function setup_filters() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Add sections. |
||
| 249 | * |
||
| 250 | * @since 2.0 |
||
| 251 | * |
||
| 252 | * @param array $sections |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function add_section( $sections ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Add sections. |
||
| 264 | * |
||
| 265 | * @since 2.0 |
||
| 266 | * |
||
| 267 | * @param bool $hide_section |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function hide_section( $hide_section ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Register email settings. |
||
| 279 | * |
||
| 280 | * @since 2.0 |
||
| 281 | * @access public |
||
| 282 | * |
||
| 283 | * @param array $settings |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function add_setting_fields( $settings ) { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Get setting fields |
||
| 298 | * |
||
| 299 | * @since 2.0 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param int $form_id |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function get_setting_fields( $form_id = null ) { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Register email settings to form metabox. |
||
| 313 | * |
||
| 314 | * @since 2.0 |
||
| 315 | * @access public |
||
| 316 | * |
||
| 317 | * @param array $settings |
||
| 318 | * @param int $form_id |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function add_metabox_setting_field( $settings, $form_id ) { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Get extra setting field. |
||
| 338 | * |
||
| 339 | * @since 2.0 |
||
| 340 | * @access public |
||
| 341 | * |
||
| 342 | * @param int $form_id |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function get_extra_setting_fields( $form_id = null ) { |
||
| 349 | |||
| 350 | |||
| 351 | /** |
||
| 352 | * Get recipient(s). |
||
| 353 | * |
||
| 354 | * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
||
| 355 | * |
||
| 356 | * @since 2.0 |
||
| 357 | * @access public |
||
| 358 | * |
||
| 359 | * @param int $form_id |
||
| 360 | * |
||
| 361 | * @return string|array |
||
| 362 | */ |
||
| 363 | public function get_recipient( $form_id = null ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get notification status. |
||
| 400 | * |
||
| 401 | * @since 2.0 |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @param int $form_id |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function get_notification_status( $form_id = null ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get email subject. |
||
| 428 | * |
||
| 429 | * @since 2.0 |
||
| 430 | * @access public |
||
| 431 | * |
||
| 432 | * @param int $form_id |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | View Code Duplication | function get_email_subject( $form_id = null ) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Get email message. |
||
| 461 | * |
||
| 462 | * @since 2.0 |
||
| 463 | * @access public |
||
| 464 | * |
||
| 465 | * @param int $form_id |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function get_email_message( $form_id = null ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get email header. |
||
| 492 | * |
||
| 493 | * @param int $form_id The Form ID. |
||
| 494 | * |
||
| 495 | * @since 2.1.3 |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function get_email_header( $form_id = null ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get email content type. |
||
| 522 | * |
||
| 523 | * @since 2.0 |
||
| 524 | * @access public |
||
| 525 | * |
||
| 526 | * @param $form_id |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function get_email_content_type( $form_id ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get email template. |
||
| 553 | * |
||
| 554 | * @since 2.0 |
||
| 555 | * @access public |
||
| 556 | * |
||
| 557 | * @param $form_id |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function get_email_template( $form_id ) { |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Get allowed email tags for current email notification. |
||
| 588 | * |
||
| 589 | * @since 2.0 |
||
| 590 | * @access private |
||
| 591 | * |
||
| 592 | * @param bool $formatted |
||
| 593 | * |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | public function get_allowed_email_tags( $formatted = false ) { |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Get preview email recipients. |
||
| 642 | * |
||
| 643 | * @since 2.0 |
||
| 644 | * @access public |
||
| 645 | * |
||
| 646 | * @param int $form_id |
||
| 647 | * |
||
| 648 | * @return array|string |
||
| 649 | */ |
||
| 650 | public function get_preview_email_recipient( $form_id = null ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the recipient attachments. |
||
| 668 | * |
||
| 669 | * @since 2.0 |
||
| 670 | * @access public |
||
| 671 | * |
||
| 672 | * @param int $form_id |
||
| 673 | * |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | public function get_email_attachments( $form_id = null ) { |
||
| 684 | |||
| 685 | |||
| 686 | /** |
||
| 687 | * Send preview email. |
||
| 688 | * |
||
| 689 | * @since 2.0 |
||
| 690 | * @access public |
||
| 691 | * |
||
| 692 | * @param bool $send Flag to check if send email or not. |
||
| 693 | * |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | public function send_preview_email( $send = true ) { |
||
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * Send email notification. |
||
| 739 | * |
||
| 740 | * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails): |
||
| 741 | * 1. payment_id |
||
| 742 | * 2. user_id |
||
| 743 | * 3. form_id |
||
| 744 | * 4. donor_id |
||
| 745 | * 5. for third party email tags you can pass necessary param along above parameters other value replace by empty string. |
||
| 746 | * |
||
| 747 | * @since 2.0 |
||
| 748 | * @access public |
||
| 749 | * |
||
| 750 | * @param array $email_tag_args Arguments which helps to decode email template tags. |
||
| 751 | * |
||
| 752 | * @return bool |
||
| 753 | */ |
||
| 754 | public function send_email_notification( $email_tag_args = array() ) { |
||
| 755 | /** |
||
| 756 | * Fire the filter |
||
| 757 | * |
||
| 758 | * @since 2.2.3 |
||
| 759 | */ |
||
| 760 | if ( apply_filters( 'give_is_stop_email_notification', false, $this ) ) { |
||
| 761 | return false; |
||
| 762 | } |
||
| 763 | |||
| 764 | // Add email content type email tags. |
||
| 765 | $email_tag_args['email_content_type'] = $this->config['content_type']; |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Filter the email tag args |
||
| 769 | * |
||
| 770 | * @since 2.0 |
||
| 771 | */ |
||
| 772 | $email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this ); |
||
| 773 | |||
| 774 | // Get form id. |
||
| 775 | $form_id = ! empty( $email_tag_args['form_id'] ) |
||
| 776 | ? absint( $email_tag_args['form_id'] ) |
||
| 777 | : ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null ); |
||
| 778 | |||
| 779 | |||
| 780 | // Do not send email if notification is disable. |
||
| 781 | if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) { |
||
| 782 | return false; |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Fire action after before email send. |
||
| 787 | * |
||
| 788 | * @since 2.0 |
||
| 789 | */ |
||
| 790 | do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id ); |
||
| 791 | |||
| 792 | $attachments = $this->get_email_attachments(); |
||
| 793 | $message = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args ); |
||
| 794 | $subject = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args ); |
||
| 795 | $content_type = $this->get_email_content_type( $form_id ); |
||
| 796 | |||
| 797 | // Setup email content type. |
||
| 798 | Give()->emails->__set( 'content_type', $content_type ); |
||
| 799 | Give()->emails->__set( 'html', true ); |
||
| 800 | |||
| 801 | // Set email template. |
||
| 802 | Give()->emails->__set( 'template', $this->get_email_template( $form_id ) ); |
||
| 803 | |||
| 804 | // Set email header. |
||
| 805 | Give()->emails->__set( 'heading', give_do_email_tags( $this->get_email_header( $form_id ), $email_tag_args ) ); |
||
| 806 | |||
| 807 | View Code Duplication | if ( 'text/plain' === $content_type ) { |
|
| 808 | Give()->emails->__set( 'html', false ); |
||
| 809 | Give()->emails->__set( 'template', 'none' ); |
||
| 810 | $message = strip_tags( $message ); |
||
| 811 | } |
||
| 812 | |||
| 813 | View Code Duplication | if ( Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ) { |
|
| 814 | Give()->emails->form_id = $form_id; |
||
| 815 | Give()->emails->from_name = give_get_meta( $form_id, '_give_from_name', true ); |
||
| 816 | Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true ); |
||
| 817 | } |
||
| 818 | |||
| 819 | // Send email. |
||
| 820 | $email_status = Give()->emails->send( $this->get_recipient( $form_id ), $subject, $message, $attachments ); |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Fire action after after email send. |
||
| 824 | * |
||
| 825 | * @since 2.0 |
||
| 826 | */ |
||
| 827 | do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id ); |
||
| 828 | |||
| 829 | return $email_status; |
||
| 830 | } |
||
| 831 | |||
| 832 | |||
| 833 | /** |
||
| 834 | * Decode preview email template tags. |
||
| 835 | * |
||
| 836 | * @since 2.0 |
||
| 837 | * |
||
| 838 | * @param string $message |
||
| 839 | * |
||
| 840 | * @return string |
||
| 841 | */ |
||
| 842 | public function preview_email_template_tags( $message ) { |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Setup email data |
||
| 932 | * |
||
| 933 | * @since 2.0 |
||
| 934 | */ |
||
| 935 | public function setup_email_data() { |
||
| 937 | |||
| 938 | |||
| 939 | /** |
||
| 940 | * Validate email form setting |
||
| 941 | * |
||
| 942 | * Note: internal use only |
||
| 943 | * |
||
| 944 | * @since 2.0 |
||
| 945 | * @access public |
||
| 946 | * |
||
| 947 | * @param $form_meta_key |
||
| 948 | * @param $form_meta_value |
||
| 949 | * @param $post_id |
||
| 950 | * |
||
| 951 | */ |
||
| 952 | public function validate_form_recipient_field_value( $form_meta_key, $form_meta_value, $post_id ) { |
||
| 992 | } |
||
| 993 | |||
| 995 |