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 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string $recipient_email Donor email. |
||
| 68 | * @access protected |
||
| 69 | * @since 2.0 |
||
| 70 | */ |
||
| 71 | protected $recipient_email = ''; |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Setup email notification. |
||
| 76 | * |
||
| 77 | * @since 2.0 |
||
| 78 | */ |
||
| 79 | public function init() { |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Get instance. |
||
| 86 | * |
||
| 87 | * @since 2.0 |
||
| 88 | * @access public |
||
| 89 | * @return Give_Email_Notification |
||
| 90 | */ |
||
| 91 | public static function get_instance() { |
||
| 92 | $class = get_called_class(); |
||
| 93 | if ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) { |
||
| 94 | self::$singleton[ $class ] = new $class(); |
||
| 95 | } |
||
| 96 | |||
| 97 | return self::$singleton[ $class ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Setup action and filters. |
||
| 102 | * |
||
| 103 | * @access public |
||
| 104 | * @since 2.0 |
||
| 105 | * |
||
| 106 | * @param array $config |
||
| 107 | */ |
||
| 108 | public function load( $config ) { |
||
| 109 | // Set notification configuration. |
||
| 110 | $this->config = wp_parse_args( $config, $this->config ); |
||
| 111 | |||
| 112 | // Set email preview header status. |
||
| 113 | $this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false; |
||
| 114 | |||
| 115 | // Set email content type |
||
| 116 | $this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array( $this->config['content_type'], array( |
||
| 117 | 'text/html', |
||
| 118 | 'text/plain', |
||
| 119 | ) ) |
||
| 120 | ? Give()->emails->get_content_type() |
||
| 121 | : $this->config['content_type']; |
||
| 122 | $this->config['content_type'] = give_get_option( "{$this->config['id']}_email_content_type", $this->config['content_type'] ); |
||
| 123 | |||
| 124 | // Set email template type. |
||
| 125 | $this->config['email_template'] = empty( $this->config['email_template'] ) |
||
| 126 | ? give_get_option( 'email_template' ) |
||
| 127 | : $this->config['email_template']; |
||
| 128 | |||
| 129 | // Set recipient group name. |
||
| 130 | $this->config['recipient_group_name'] = empty( $this->config['recipient_group_name'] ) |
||
| 131 | ? ( ! Give_Email_Notification_Util::has_recipient_field( $this ) ? __( 'Donor', 'give' ) : '' ) |
||
| 132 | : $this->config['recipient_group_name']; |
||
| 133 | |||
| 134 | // Non notification status editable notice. |
||
| 135 | $this->config['notices']['non-notification-status-editable'] = empty( $this->config['notices']['non-notification-status-editable'] ) |
||
| 136 | ? __( 'You can not edit this notification directly. This will be enable or disable automatically on basis of plugin settings.', 'give' ) |
||
| 137 | : $this->config['notices']['non-notification-status-editable']; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Filter the notification config. |
||
| 141 | * |
||
| 142 | * @since 2.0 |
||
| 143 | * |
||
| 144 | * @param array Give_Email_Notification::config |
||
| 145 | * @param Give_Email_Notification $this |
||
| 146 | */ |
||
| 147 | $this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this ); |
||
| 148 | |||
| 149 | // Setup filters. |
||
| 150 | $this->setup_filters(); |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Setup filters. |
||
| 156 | * |
||
| 157 | * @since 2.0 |
||
| 158 | * @access public |
||
| 159 | */ |
||
| 160 | private function setup_filters() { |
||
| 161 | // Apply filter only for current email notification section. |
||
| 162 | if ( give_get_current_setting_section() === $this->config['id'] ) { |
||
| 163 | // Initialize email context for email notification. |
||
| 164 | $this->config['email_tag_context'] = apply_filters( |
||
| 165 | "give_{$this->config['id']}_email_tag_context", |
||
| 166 | $this->config['email_tag_context'], |
||
| 167 | $this |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | // Setup setting fields. |
||
| 172 | add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 ); |
||
| 173 | |||
| 174 | if ( $this->config['form_metabox_setting'] ) { |
||
| 175 | add_filter( |
||
| 176 | 'give_email_notification_options_metabox_fields', |
||
| 177 | array( $this, 'add_metabox_setting_field' ), |
||
| 178 | 10, |
||
| 179 | 2 |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Filter the default email subject. |
||
| 185 | * |
||
| 186 | * @since 2.0 |
||
| 187 | */ |
||
| 188 | $this->config['default_email_subject'] = apply_filters( |
||
| 189 | "give_{$this->config['id']}_get_default_email_subject", |
||
| 190 | $this->config['default_email_subject'], |
||
| 191 | $this |
||
| 192 | ); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Filter the default email message. |
||
| 196 | * |
||
| 197 | * @since 2.0 |
||
| 198 | */ |
||
| 199 | $this->config['default_email_message'] = apply_filters( |
||
| 200 | "give_{$this->config['id']}_get_default_email_message", |
||
| 201 | $this->config['default_email_message'], |
||
| 202 | $this |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add sections. |
||
| 208 | * |
||
| 209 | * @since 2.0 |
||
| 210 | * |
||
| 211 | * @param array $sections |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function add_section( $sections ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add sections. |
||
| 223 | * |
||
| 224 | * @since 2.0 |
||
| 225 | * |
||
| 226 | * @param bool $hide_section |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function hide_section( $hide_section ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Register email settings. |
||
| 238 | * |
||
| 239 | * @since 2.0 |
||
| 240 | * @access public |
||
| 241 | * |
||
| 242 | * @param array $settings |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function add_setting_fields( $settings ) { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Get setting fields |
||
| 257 | * |
||
| 258 | * @since 2.0 |
||
| 259 | * @access public |
||
| 260 | * |
||
| 261 | * @param int $form_id |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function get_setting_fields( $form_id = null ) { |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Register email settings to form metabox. |
||
| 272 | * |
||
| 273 | * @since 2.0 |
||
| 274 | * @access public |
||
| 275 | * |
||
| 276 | * @param array $settings |
||
| 277 | * @param int $form_id |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function add_metabox_setting_field( $settings, $form_id ) { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Get extra setting field. |
||
| 295 | * |
||
| 296 | * @since 2.0 |
||
| 297 | * @access public |
||
| 298 | * |
||
| 299 | * @param int $form_id |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function get_extra_setting_fields( $form_id = null ) { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Get recipient(s). |
||
| 310 | * |
||
| 311 | * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
||
| 312 | * |
||
| 313 | * @since 2.0 |
||
| 314 | * @access public |
||
| 315 | * |
||
| 316 | * @param int $form_id |
||
| 317 | * |
||
| 318 | * @return string|array |
||
| 319 | */ |
||
| 320 | public function get_recipient( $form_id = null ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get notification status. |
||
| 344 | * |
||
| 345 | * @since 2.0 |
||
| 346 | * @access public |
||
| 347 | * |
||
| 348 | * @param int $form_id |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function get_notification_status( $form_id = null ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get email subject. |
||
| 372 | * |
||
| 373 | * @since 2.0 |
||
| 374 | * @access public |
||
| 375 | * |
||
| 376 | * @param int $form_id |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | function get_email_subject( $form_id = null ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get email message. |
||
| 405 | * |
||
| 406 | * @since 2.0 |
||
| 407 | * @access public |
||
| 408 | * |
||
| 409 | * @param int $form_id |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function get_email_message( $form_id = null ) { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Get email content type. |
||
| 437 | * |
||
| 438 | * @since 2.0 |
||
| 439 | * @access public |
||
| 440 | * |
||
| 441 | * @param $form_id |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function get_email_content_type( $form_id ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get email template. |
||
| 468 | * |
||
| 469 | * @since 2.0 |
||
| 470 | * @access public |
||
| 471 | * |
||
| 472 | * @param $form_id |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function get_email_template( $form_id ) { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Get allowed email tags for current email notification. |
||
| 500 | * |
||
| 501 | * @since 2.0 |
||
| 502 | * @access private |
||
| 503 | * |
||
| 504 | * @param bool $formatted |
||
| 505 | * |
||
| 506 | * @return array |
||
| 507 | */ |
||
| 508 | public function get_allowed_email_tags( $formatted = false ) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get preview email recipients. |
||
| 550 | * |
||
| 551 | * @since 2.0 |
||
| 552 | * @access public |
||
| 553 | * |
||
| 554 | * @param int $form_id |
||
| 555 | * |
||
| 556 | * @return array|string |
||
| 557 | */ |
||
| 558 | public function get_preview_email_recipient( $form_id = null ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Get the recipient attachments. |
||
| 576 | * |
||
| 577 | * @since 2.0 |
||
| 578 | * @access public |
||
| 579 | * |
||
| 580 | * @param int $form_id |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | public function get_email_attachments( $form_id = null ) { |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * Send preview email. |
||
| 596 | * |
||
| 597 | * @since 2.0 |
||
| 598 | * @access public |
||
| 599 | */ |
||
| 600 | public function send_preview_email() { |
||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * Send email notification. |
||
| 632 | * |
||
| 633 | * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails): |
||
| 634 | * 1. payment_id |
||
| 635 | * 2. user_id |
||
| 636 | * 3. form_id |
||
| 637 | * 4. donor_id |
||
| 638 | * 5. for third party email tags you can pass necessary param along above parameters other value replace by empty string. |
||
| 639 | * |
||
| 640 | * @since 2.0 |
||
| 641 | * @access public |
||
| 642 | * |
||
| 643 | * @param array $email_tag_args Arguments which helps to decode email template tags. |
||
| 644 | * |
||
| 645 | * @return bool |
||
| 646 | */ |
||
| 647 | public function send_email_notification( $email_tag_args = array() ) { |
||
| 706 | |||
| 707 | |||
| 708 | /** |
||
| 709 | * Decode preview email template tags. |
||
| 710 | * |
||
| 711 | * @since 2.0 |
||
| 712 | * |
||
| 713 | * @param string $message |
||
| 714 | * |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | public function preview_email_template_tags( $message ) { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Setup email data |
||
| 801 | * |
||
| 802 | * @since 2.0 |
||
| 803 | */ |
||
| 804 | public function setup_email_data() { |
||
| 806 | } |
||
| 807 | |||
| 809 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.