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 | 'notification_status' => 'disabled', |
||
| 51 | 'notification_status_editable' => true, |
||
| 52 | 'has_preview' => true, |
||
| 53 | 'has_preview_header' => true, |
||
| 54 | 'preview_email_tags_values' => array(), |
||
| 55 | 'email_tag_context' => 'all', |
||
| 56 | 'form_metabox_setting' => true, |
||
| 57 | 'content_type' => '', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string $recipient_email Donor email. |
||
| 62 | * @access protected |
||
| 63 | * @since 2.0 |
||
| 64 | */ |
||
| 65 | protected $recipient_email = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string $recipient_group_name Categories single or group of recipient. |
||
| 69 | * @access protected |
||
| 70 | * @since 2.0 |
||
| 71 | */ |
||
| 72 | protected $recipient_group_name = ''; |
||
| 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( 'text/html', 'text/plain') ) |
||
| 117 | ? 'text/html' // @todo: use Give()->emails->get_content_Type() get email content type. |
||
| 118 | : $this->config['content_type']; |
||
| 119 | $this->config['content_type'] = give_get_option( "{$this->config['id']}_email_content_type", $this->config['content_type'] ); |
||
| 120 | |||
| 121 | // Setup filters. |
||
| 122 | $this->setup_filters(); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Setup filters. |
||
| 128 | * |
||
| 129 | * @since 2.0 |
||
| 130 | * @access public |
||
| 131 | */ |
||
| 132 | private function setup_filters() { |
||
| 133 | // Apply filter only for current email notification section. |
||
| 134 | if ( give_get_current_setting_section() === $this->config['id'] ) { |
||
| 135 | // Initialize email context for email notification. |
||
| 136 | $this->config['email_tag_context'] = apply_filters( |
||
| 137 | "give_{$this->config['id']}_email_tag_context", |
||
| 138 | $this->config['email_tag_context'], |
||
| 139 | $this |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Setup setting fields. |
||
| 144 | add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 ); |
||
| 145 | |||
| 146 | if ( $this->config['form_metabox_setting'] ) { |
||
| 147 | add_filter( |
||
| 148 | 'give_email_notification_options_metabox_fields', |
||
| 149 | array( $this, 'add_metabox_setting_field' ), |
||
| 150 | 10, |
||
| 151 | 2 |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add sections. |
||
| 158 | * |
||
| 159 | * @since 2.0 |
||
| 160 | * |
||
| 161 | * @param array $sections |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function add_section( $sections ) { |
||
| 166 | $sections[ $this->config['id'] ] = $this->config['label']; |
||
| 167 | |||
| 168 | return $sections; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Add sections. |
||
| 173 | * |
||
| 174 | * @since 2.0 |
||
| 175 | * |
||
| 176 | * @param bool $hide_section |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hide_section( $hide_section ) { |
||
| 181 | $hide_section = true; |
||
| 182 | |||
| 183 | return $hide_section; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Register email settings. |
||
| 188 | * |
||
| 189 | * @since 2.0 |
||
| 190 | * @access public |
||
| 191 | * |
||
| 192 | * @param array $settings |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function add_setting_fields( $settings ) { |
||
| 197 | if ( $this->config['id'] === give_get_current_setting_section() ) { |
||
| 198 | $settings = $this->get_setting_fields(); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $settings; |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Get setting fields |
||
| 207 | * |
||
| 208 | * @since 2.0 |
||
| 209 | * @access public |
||
| 210 | * |
||
| 211 | * @param int $form_id |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function get_setting_fields( $form_id = 0 ) { |
||
| 216 | return Give_Email_Setting_Field::get_setting_fields( $this, $form_id ); |
||
| 217 | } |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * Register email settings to form metabox. |
||
| 222 | * |
||
| 223 | * @since 2.0 |
||
| 224 | * @access public |
||
| 225 | * |
||
| 226 | * @param array $settings |
||
| 227 | * @param int $post_id |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function add_metabox_setting_field( $settings, $post_id ) { |
||
| 232 | |||
| 233 | $settings[] = array( |
||
| 234 | 'id' => $this->config['id'], |
||
| 235 | 'title' => $this->config['label'], |
||
| 236 | 'fields' => $this->get_setting_fields( $post_id ), |
||
| 237 | ); |
||
| 238 | |||
| 239 | return $settings; |
||
| 240 | } |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * Get extra setting field. |
||
| 245 | * |
||
| 246 | * @since 2.0 |
||
| 247 | * @access public |
||
| 248 | * |
||
| 249 | * @param int $form_id |
||
| 250 | * |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function get_extra_setting_fields( $form_id = 0 ) { |
||
| 254 | return array(); |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Get recipient(s). |
||
| 260 | * |
||
| 261 | * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
||
| 262 | * |
||
| 263 | * @since 2.0 |
||
| 264 | * @access public |
||
| 265 | * @return string|array |
||
| 266 | */ |
||
| 267 | public function get_recipient() { |
||
| 268 | $recipient = $this->recipient_email; |
||
| 269 | |||
| 270 | if ( ! $recipient && $this->config['has_recipient_field'] ) { |
||
| 271 | $recipient = give_get_option( "{$this->config['id']}_recipient" ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Filter the recipients |
||
| 276 | * |
||
| 277 | * @since 2.0 |
||
| 278 | */ |
||
| 279 | return apply_filters( "give_{$this->config['id']}_get_recipients", give_check_variable( $recipient, 'empty', Give()->emails->get_from_address() ), $this ); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get recipient(s) group name. |
||
| 284 | * * |
||
| 285 | * |
||
| 286 | * @since 2.0 |
||
| 287 | * @access public |
||
| 288 | * @return string|array |
||
| 289 | */ |
||
| 290 | public function get_recipient_group_name() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get notification status. |
||
| 296 | * |
||
| 297 | * @since 2.0 |
||
| 298 | * @access public |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function get_notification_status() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Filter the notification status. |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get email subject. |
||
| 313 | * |
||
| 314 | * @since 2.0 |
||
| 315 | * @access public |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | function get_email_subject() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get email message. |
||
| 331 | * |
||
| 332 | * @since 2.0 |
||
| 333 | * @access public |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function get_email_message() { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * Get email message field description |
||
| 350 | * |
||
| 351 | * @since 2.0 |
||
| 352 | * @acess public |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function get_email_message_field_description() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get a formatted HTML list of all available email tags |
||
| 371 | * |
||
| 372 | * @since 1.0 |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | function get_emails_tags_list_html() { |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Get allowed email tags for current email notification. |
||
| 399 | * |
||
| 400 | * @since 2.0 |
||
| 401 | * @access private |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | private function get_allowed_email_tags() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get default email subject. |
||
| 434 | * |
||
| 435 | * @since 2.0 |
||
| 436 | * @access public |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | function get_default_email_subject() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Get default email message. |
||
| 445 | * |
||
| 446 | * @since 2.0 |
||
| 447 | * @access public |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | function get_default_email_message() { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Get preview email recipients. |
||
| 458 | * |
||
| 459 | * @since 2.0 |
||
| 460 | * @access public |
||
| 461 | * @return array|string |
||
| 462 | */ |
||
| 463 | public function get_preview_email_recipient() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the recipient attachments. |
||
| 481 | * |
||
| 482 | * @since 2.0 |
||
| 483 | * @access public |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | public function get_email_attachments() { |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * Send preview email. |
||
| 498 | * |
||
| 499 | * @since 2.0 |
||
| 500 | * @access public |
||
| 501 | */ |
||
| 502 | public function send_preview_email() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Send email notification |
||
| 538 | * |
||
| 539 | * @since 2.0 |
||
| 540 | * @access public |
||
| 541 | * |
||
| 542 | * @param array $email_tag_args Arguments which helps to decode email template tags. |
||
| 543 | * |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | public function send_email_notification( $email_tag_args = array() ) { |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Decode preview email template tags. |
||
| 588 | * |
||
| 589 | * @since 2.0 |
||
| 590 | * |
||
| 591 | * @param string $message |
||
| 592 | * |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function preview_email_template_tags( $message ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Setup email data |
||
| 658 | * |
||
| 659 | * @since 2.0 |
||
| 660 | */ |
||
| 661 | public function setup_email_data() { |
||
| 663 | } |
||
| 664 | |||
| 666 |
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.