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:
| 1 | <?php |
||
| 27 | class Give_New_Offline_Donation_Email extends Give_Email_Notification { |
||
| 28 | /* @var Give_Payment $payment */ |
||
| 29 | public $payment; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a class instance. |
||
| 33 | * |
||
| 34 | * @access public |
||
| 35 | * @since 2.0 |
||
| 36 | */ |
||
| 37 | public function init() { |
||
| 38 | // Initialize empty payment. |
||
| 39 | $this->payment = new Give_Payment( 0 ); |
||
| 40 | |||
| 41 | $this->load( array( |
||
| 42 | 'id' => 'new-offline-donation', |
||
| 43 | 'label' => __( 'New Offline Donation', 'give' ), |
||
| 44 | 'description' => __( 'Donation Notification will be sent to admin when new offline donation received.', 'give' ), |
||
| 45 | 'has_recipient_field' => true, |
||
| 46 | 'notification_status' => give_is_gateway_active( 'offline' ) ? 'enabled' : 'disabled', |
||
| 47 | 'notification_status_editable' => false, |
||
| 48 | 'preview_email_tags_values' => array( |
||
| 49 | 'payment_method' => esc_html__( 'Offline', 'give' ), |
||
| 50 | ), |
||
| 51 | 'default_email_subject' => $this->get_default_email_subject(), |
||
| 52 | 'default_email_message' => $this->get_default_email_message(), |
||
| 53 | ) ); |
||
| 54 | |||
| 55 | add_action( 'give_insert_payment', array( $this, 'setup_email_notification' ) ); |
||
| 56 | add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Get default email subject. |
||
| 61 | * |
||
| 62 | * @since 2.0 |
||
| 63 | * @access public |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function get_default_email_subject() { |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Get default email message. |
||
| 93 | * |
||
| 94 | * @since 2.0 |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | public function get_default_email_message() { |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Get message |
||
| 142 | * |
||
| 143 | * @since 2.0 |
||
| 144 | * |
||
| 145 | * @param int $form_id |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function get_email_message( $form_id = null ) { |
|
| 150 | $message = Give_Email_Notification_Util::get_value( |
||
| 151 | $this, |
||
| 152 | Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
||
| 153 | $form_id, |
||
| 154 | $this->config['default_email_message'] |
||
| 155 | ); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Filter the email message. |
||
| 159 | * Note: This filter will deprecate soon. |
||
| 160 | * |
||
| 161 | * @since 1.0 |
||
| 162 | */ |
||
| 163 | $message = apply_filters( |
||
| 164 | 'give_offline_admin_donation_notification', |
||
| 165 | $message, |
||
| 166 | $this->payment->ID |
||
| 167 | ); |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Filter the email message |
||
| 171 | * |
||
| 172 | * @since 2.0 |
||
| 173 | */ |
||
| 174 | return apply_filters( |
||
| 175 | "give_{$this->config['id']}_get_email_message", |
||
| 176 | $message, |
||
| 177 | $this, |
||
| 178 | $form_id |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Get attachments. |
||
| 185 | * |
||
| 186 | * @since 2.0 |
||
| 187 | * |
||
| 188 | * @param int $form_id |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function get_email_attachments( $form_id = null ) { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Set email data. |
||
| 220 | * |
||
| 221 | * @since 2.0 |
||
| 222 | */ |
||
| 223 | public function setup_email_data() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Setup email notification. |
||
| 237 | * |
||
| 238 | * @since 2.0 |
||
| 239 | * @access public |
||
| 240 | * |
||
| 241 | * @param int $payment_id |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function setup_email_notification( $payment_id ) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Set notification status |
||
| 262 | * |
||
| 263 | * @since 2.0 |
||
| 264 | * @access public |
||
| 265 | * |
||
| 266 | * @param $update_options |
||
| 267 | * @param $option_name |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function set_notification_status( $update_options, $option_name ) { |
|
| 283 | } |
||
| 284 | |||
| 288 |