Give_New_Offline_Donation_Email::init()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
/**
3
 * New Offline Donation Email
4
 *
5
 * This class handles all email notification settings.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, GiveWP
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.0
12
 */
13
14
// Exit if access directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
if ( ! class_exists( 'Give_New_Offline_Donation_Email' ) ) :
20
21
	/**
22
	 * Give_New_Offline_Donation_Email
23
	 *
24
	 * @abstract
25
	 * @since       2.0
26
	 */
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 &raquo;</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&section=offline-donations') ),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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() {
76
			/**
77
			 * Filter the default subject.
78
			 * Note: This filter will deprecate soon.
79
			 *
80
			 * @since 1.0
81
			 */
82
			$subject = apply_filters(
83
				'give_offline_admin_donation_notification_subject',
84
				__( 'New Pending Donation', 'give' )
85
			);
86
87
			/**
88
			 * Filter the default subject
89
			 *
90
			 * @since 2.0
91
			 */
92
			return apply_filters(
93
				"give_{$this->config['id']}_get_default_email_subject",
94
				$subject,
95
				$this
96
			);
97
		}
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() {
109
			$message = __( 'Dear Admin,', 'give' ) . "\n\n";
110
			$message .= __( 'An offline donation has been made on your website:', 'give' ) . ' ' . get_bloginfo( 'name' ) . ' ';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'get_bloginfo'
Loading history...
111
			$message .= __( 'Hooray! The donation is in a pending status and is awaiting payment. Donation instructions have been emailed to the donor. Once you receive payment, be sure to mark the donation as complete using the link below.', 'give' ) . "\n\n";
112
113
			$message .= '<strong>' . __( 'Donor:', 'give' ) . '</strong> {fullname}' . "\n";
114
			$message .= '<strong>' . __( 'Amount:', 'give' ) . '</strong> {amount}' . "\n\n";
115
116
			$message .= sprintf(
117
				'<a href="%1$s">%2$s</a>',
118
				admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&id=' . $this->payment->ID ),
119
				__( 'Click Here to View and/or Update Donation Details', 'give' )
120
			) . "\n\n";
121
122
			/**
123
			 * Filter the donation receipt email message
124
			 * Note: This filter will deprecate soon.
125
			 *
126
			 * @since 1.0
127
			 *
128
			 * @param string $message
129
			 */
130
			$message = apply_filters(
131
				'give_default_new_offline_donation_email',
132
				$message,
133
				$this->payment->ID
134
			);
135
136
			/**
137
			 * Filter the default message
138
			 *
139
			 * @since 2.0
140
			 */
141
			return apply_filters(
142
				"give_{$this->config['id']}_get_default_email_message",
143
				$message,
144
				$this
145
			);
146
		}
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
			$message = Give_Email_Notification_Util::get_value(
160
				$this,
161
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
162
				$form_id,
163
				$this->config['default_email_message']
164
			);
165
166
			/**
167
			 * Filter the email message.
168
			 * Note: This filter will deprecate soon.
169
			 *
170
			 * @since 1.0
171
			 */
172
			$message = apply_filters(
173
				'give_offline_admin_donation_notification',
174
				$message,
175
				$this->payment->ID
176
			);
177
178
			/**
179
			 * Filter the email message
180
			 *
181
			 * @since 2.0
182
			 */
183
			return apply_filters(
184
				"give_{$this->config['id']}_get_email_message",
185
				$message,
186
				$this,
187
				$form_id
188
			);
189
		}
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 ) {
202
			/**
203
			 * Filter the attachments.
204
			 * Note: This filter will deprecate soon.
205
			 *
206
			 * @since 1.0
207
			 */
208
			$attachment = apply_filters(
209
				'give_offline_admin_donation_notification_attachments',
210
				array(),
211
				$this->payment->ID
212
			);
213
214
			/**
215
			 * Filter the attachments.
216
			 *
217
			 * @since 2.0
218
			 */
219
			return apply_filters(
220
				"give_{$this->config['id']}_get_email_attachments",
221
				$attachment,
222
				$this
223
			);
224
		}
225
226
227
		/**
228
		 * Set email data.
229
		 *
230
		 * @since 2.0
231
		 */
232
		public function setup_email_data() {
233
			// Set header.
234
			Give()->emails->__set(
235
				'headers',
236
				apply_filters(
237
					'give_offline_admin_donation_notification_headers',
238
					Give()->emails->get_headers(),
239
					$this->payment->ID
240
				)
241
			);
242
		}
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
			$this->payment = new Give_Payment( $payment_id );
254
255
			// Exit if not donation was not with offline donation.
256
			if ( 'offline' !== $this->payment->gateway ) {
257
				return;
258
			}
259
260
			// Set email data.
261
			$this->setup_email_data();
262
263
			// Send email.
264
			$this->send_email_notification( array(
265
				'payment_id' => $this->payment->ID,
266
			) );
267
		}
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
			// Get updated settings.
280
			$update_options = give_get_settings();
281
282
			$notification_status = isset( $update_options['gateways']['offline'] ) ? 'enabled' : 'disabled';
283
284
			if (
285
				empty( $update_options[ "{$this->config['id']}_notification" ] )
286
				|| $notification_status !== $update_options[ "{$this->config['id']}_notification" ]
287
			) {
288
				$update_options[ "{$this->config['id']}_notification" ] = $notification_status;
289
				update_option( $option_name, $update_options, false );
290
			}
291
		}
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
305
306
			if ( in_array( 'offline', array_keys( give_get_enabled_payment_gateways($form_id) ) ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
307
				$settings[] = array(
308
					'id'     => $this->config['id'],
309
					'title'  => $this->config['label'],
310
					'fields' => $this->get_setting_fields( $form_id ),
311
				);
312
			}
313
314
			return $settings;
315
		}
316
	}
317
318
endif; // End class_exists check
319
320
return Give_New_Offline_Donation_Email::get_instance();
321