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' => __( 'Sent to designated recipient(s) for a new (pending) offline donation.', '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' => ( false !== give_get_option( 'new-offline-donation_email_message' ) ) ? give_get_option( 'new-offline-donation_email_message' ) : give_get_default_donation_notification_email(), |
||
53 | 'default_email_header' => __( 'New Offline Donation!', 'give' ), |
||
54 | 'notices' => array( |
||
55 | 'non-notification-status-editable' => sprintf( |
||
56 | '%1$s <a href="%2$s">%3$s »</a>', |
||
57 | __( 'This notification is automatically toggled based on whether the gateway is enabled or not.', 'give' ), |
||
58 | esc_url( admin_url('edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=offline-donations') ), |
||
59 | __( 'Edit Setting', 'give' ) |
||
60 | ) |
||
61 | ), |
||
62 | ) ); |
||
63 | |||
64 | add_action( 'give_insert_payment', array( $this, 'setup_email_notification' ) ); |
||
65 | add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get default email subject. |
||
70 | * |
||
71 | * @since 2.0 |
||
72 | * @access public |
||
73 | * @return string |
||
74 | */ |
||
75 | public function get_default_email_subject() { |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Get default email message. |
||
102 | * |
||
103 | * @since 2.0 |
||
104 | * @access public |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function get_default_email_message() { |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Get message |
||
151 | * |
||
152 | * @since 2.0 |
||
153 | * |
||
154 | * @param int $form_id |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | View Code Duplication | public function get_email_message( $form_id = null ) { |
|
190 | |||
191 | |||
192 | /** |
||
193 | * Get attachments. |
||
194 | * |
||
195 | * @since 2.0 |
||
196 | * |
||
197 | * @param int $form_id |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function get_email_attachments( $form_id = null ) { |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Set email data. |
||
229 | * |
||
230 | * @since 2.0 |
||
231 | */ |
||
232 | public function setup_email_data() { |
||
243 | |||
244 | /** |
||
245 | * Setup email notification. |
||
246 | * |
||
247 | * @since 2.0 |
||
248 | * @access public |
||
249 | * |
||
250 | * @param int $payment_id |
||
251 | */ |
||
252 | View Code Duplication | public function setup_email_notification( $payment_id ) { |
|
268 | |||
269 | /** |
||
270 | * Set notification status |
||
271 | * |
||
272 | * @since 2.0 |
||
273 | * @access public |
||
274 | * |
||
275 | * @param $update_options |
||
276 | * @param $option_name |
||
277 | */ |
||
278 | View Code Duplication | public function set_notification_status( $update_options, $option_name ) { |
|
292 | |||
293 | /** |
||
294 | * Register email settings to form metabox. |
||
295 | * |
||
296 | * @since 2.0 |
||
297 | * @access public |
||
298 | * |
||
299 | * @param array $settings |
||
300 | * @param int $form_id |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | View Code Duplication | public function add_metabox_setting_field( $settings, $form_id ) { |
|
316 | } |
||
317 | |||
321 |