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 ) { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Setup filters. |
||
| 177 | * |
||
| 178 | * @since 2.0 |
||
| 179 | * @access public |
||
| 180 | */ |
||
| 181 | private function setup_filters() { |
||
| 182 | // Do not setup filters if not admin. |
||
| 183 | if ( ! is_admin() ) { |
||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | // Apply filter only for current email notification section. |
||
| 188 | if ( isset( $_GET['section'] ) && give_get_current_setting_section() === $this->config['id'] ) { |
||
| 189 | // Initialize email context for email notification. |
||
| 190 | $this->config['email_tag_context'] = apply_filters( |
||
| 191 | "give_{$this->config['id']}_email_tag_context", |
||
| 192 | $this->config['email_tag_context'], |
||
| 193 | $this |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | // Setup setting fields. |
||
| 198 | if ( $this->config['show_on_emails_setting_page'] ) { |
||
| 199 | add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 ); |
||
| 200 | } |
||
| 201 | |||
| 202 | if ( $this->config['form_metabox_setting'] && ! empty( $this->config['form_metabox_id'] ) ) { |
||
| 203 | add_filter( |
||
| 204 | $this->config['form_metabox_id'], |
||
| 205 | array( $this, 'add_metabox_setting_field' ), |
||
| 206 | 10, |
||
| 207 | 2 |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ( $this->config['has_recipient_field'] ) { |
||
| 212 | add_action( |
||
| 213 | "give_save__give_{$this->config['id']}_recipient", |
||
| 214 | array( $this, 'validate_form_recipient_field_value' ), |
||
| 215 | 10, |
||
| 216 | 3 |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Filter the default email subject. |
||
| 222 | * |
||
| 223 | * @since 2.0 |
||
| 224 | */ |
||
| 225 | $this->config['default_email_subject'] = apply_filters( |
||
| 226 | "give_{$this->config['id']}_get_default_email_subject", |
||
| 227 | $this->config['default_email_subject'], |
||
| 228 | $this |
||
| 229 | ); |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Filter the default email message. |
||
| 233 | * |
||
| 234 | * @since 2.0 |
||
| 235 | */ |
||
| 236 | $this->config['default_email_message'] = apply_filters( |
||
| 237 | "give_{$this->config['id']}_get_default_email_message", |
||
| 238 | $this->config['default_email_message'], |
||
| 239 | $this |
||
| 240 | ); |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Filter the default email header. |
||
| 244 | * |
||
| 245 | * @since 2.1.3 |
||
| 246 | */ |
||
| 247 | $this->config['default_email_header'] = apply_filters( |
||
| 248 | "give_{$this->config['id']}_get_default_email_header", |
||
| 249 | $this->config['default_email_header'], |
||
| 250 | $this |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Add sections. |
||
| 256 | * |
||
| 257 | * @since 2.0 |
||
| 258 | * |
||
| 259 | * @param array $sections |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function add_section( $sections ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Add sections. |
||
| 271 | * |
||
| 272 | * @since 2.0 |
||
| 273 | * |
||
| 274 | * @param bool $hide_section |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function hide_section( $hide_section ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Register email settings. |
||
| 286 | * |
||
| 287 | * @since 2.0 |
||
| 288 | * @access public |
||
| 289 | * |
||
| 290 | * @param array $settings |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function add_setting_fields( $settings ) { |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Get setting fields |
||
| 305 | * |
||
| 306 | * @since 2.0 |
||
| 307 | * @access public |
||
| 308 | * |
||
| 309 | * @param int $form_id |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function get_setting_fields( $form_id = null ) { |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Register email settings to form metabox. |
||
| 320 | * |
||
| 321 | * @since 2.0 |
||
| 322 | * @access public |
||
| 323 | * |
||
| 324 | * @param array $settings |
||
| 325 | * @param int $form_id |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function add_metabox_setting_field( $settings, $form_id ) { |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Get extra setting field. |
||
| 345 | * |
||
| 346 | * @since 2.0 |
||
| 347 | * @access public |
||
| 348 | * |
||
| 349 | * @param int $form_id |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function get_extra_setting_fields( $form_id = null ) { |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * Get recipient(s). |
||
| 360 | * |
||
| 361 | * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
||
| 362 | * |
||
| 363 | * @since 2.0 |
||
| 364 | * @access public |
||
| 365 | * |
||
| 366 | * @param int $form_id |
||
| 367 | * |
||
| 368 | * @return string|array |
||
| 369 | */ |
||
| 370 | public function get_recipient( $form_id = null ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get notification status. |
||
| 406 | * |
||
| 407 | * @since 2.0 |
||
| 408 | * @access public |
||
| 409 | * |
||
| 410 | * @param int $form_id |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | */ |
||
| 414 | public function get_notification_status( $form_id = null ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get email subject. |
||
| 434 | * |
||
| 435 | * @since 2.0 |
||
| 436 | * @access public |
||
| 437 | * |
||
| 438 | * @param int $form_id |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | View Code Duplication | function get_email_subject( $form_id = null ) { |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Get email message. |
||
| 467 | * |
||
| 468 | * @since 2.0 |
||
| 469 | * @access public |
||
| 470 | * |
||
| 471 | * @param int $form_id |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function get_email_message( $form_id = null ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get email header. |
||
| 498 | * |
||
| 499 | * @param int $form_id The Form ID. |
||
| 500 | * |
||
| 501 | * @since 2.1.3 |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function get_email_header( $form_id = null ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get email content type. |
||
| 528 | * |
||
| 529 | * @since 2.0 |
||
| 530 | * @access public |
||
| 531 | * |
||
| 532 | * @param $form_id |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | public function get_email_content_type( $form_id ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get email template. |
||
| 559 | * |
||
| 560 | * @since 2.0 |
||
| 561 | * @access public |
||
| 562 | * |
||
| 563 | * @param $form_id |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function get_email_template( $form_id ) { |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Get allowed email tags for current email notification. |
||
| 594 | * |
||
| 595 | * @since 2.0 |
||
| 596 | * @access private |
||
| 597 | * |
||
| 598 | * @param bool $formatted |
||
| 599 | * |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | public function get_allowed_email_tags( $formatted = false ) { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Get preview email recipients. |
||
| 649 | * |
||
| 650 | * @since 2.0 |
||
| 651 | * @access public |
||
| 652 | * |
||
| 653 | * @param int $form_id |
||
| 654 | * |
||
| 655 | * @return array|string |
||
| 656 | */ |
||
| 657 | public function get_preview_email_recipient( $form_id = null ) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Get the recipient attachments. |
||
| 675 | * |
||
| 676 | * @since 2.0 |
||
| 677 | * @access public |
||
| 678 | * |
||
| 679 | * @param int $form_id |
||
| 680 | * |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | public function get_email_attachments( $form_id = null ) { |
||
| 691 | |||
| 692 | |||
| 693 | /** |
||
| 694 | * Send preview email. |
||
| 695 | * |
||
| 696 | * @since 2.0 |
||
| 697 | * @access public |
||
| 698 | * |
||
| 699 | * @param bool $send Flag to check if send email or not. |
||
| 700 | * |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | public function send_preview_email( $send = true ) { |
||
| 742 | |||
| 743 | |||
| 744 | /** |
||
| 745 | * Send email notification. |
||
| 746 | * |
||
| 747 | * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails): |
||
| 748 | * 1. payment_id |
||
| 749 | * 2. user_id |
||
| 750 | * 3. form_id |
||
| 751 | * 4. donor_id |
||
| 752 | * 5. for third party email tags you can pass necessary param along above parameters other value replace by empty string. |
||
| 753 | * |
||
| 754 | * @since 2.0 |
||
| 755 | * @access public |
||
| 756 | * |
||
| 757 | * @param array $email_tag_args Arguments which helps to decode email template tags. |
||
| 758 | * |
||
| 759 | * @return bool |
||
| 760 | */ |
||
| 761 | public function send_email_notification( $email_tag_args = array() ) { |
||
| 837 | |||
| 838 | |||
| 839 | /** |
||
| 840 | * Decode preview email template tags. |
||
| 841 | * |
||
| 842 | * @since 2.0 |
||
| 843 | * |
||
| 844 | * @param string $message Email Template Message. |
||
| 845 | * |
||
| 846 | * @return string |
||
| 847 | */ |
||
| 848 | public function preview_email_template_tags( $message ) { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Setup email data |
||
| 949 | * |
||
| 950 | * @since 2.0 |
||
| 951 | */ |
||
| 952 | public function setup_email_data() { |
||
| 954 | |||
| 955 | |||
| 956 | /** |
||
| 957 | * Validate email form setting |
||
| 958 | * |
||
| 959 | * Note: internal use only |
||
| 960 | * |
||
| 961 | * @since 2.0 |
||
| 962 | * @access public |
||
| 963 | * |
||
| 964 | * @param $form_meta_key |
||
| 965 | * @param $form_meta_value |
||
| 966 | * @param $post_id |
||
| 967 | */ |
||
| 968 | public function validate_form_recipient_field_value( $form_meta_key, $form_meta_value, $post_id ) { |
||
| 1010 | } |
||
| 1011 | |||
| 1013 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.