Completed
Push — issues/611 ( e1f393...0587e7 )
by Ravinder
21:03
created

setup_email_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 16.

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.

Loading history...
2
/**
3
 * Offline Donation Instruction Email
4
 *
5
 * This class handles all email notification settings.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
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_Offline_Donation_Instruction_Email' ) ) :
20
21
	/**
22
	 * Give_Offline_Donation_Instruction_Email
23
	 *
24
	 * @abstract
25
	 * @since       2.0
26
	 */
27
	class Give_Offline_Donation_Instruction_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
			$this->id          = 'offline-donation-instruction';
39
			$this->label       = __( 'Offline Donation Instruction', 'give' );
40
			$this->description = __( 'Offline Donation Instruction will be sent to recipient(s) when offline donation received.', 'give' );
41
42
			$this->notification_status             = 'enabled';
43
			$this->recipient_group_name            = __( 'Donor', 'give' );
44
			$this->form_metabox_setting            = true;
45
			$this->is_notification_status_editable = false;
46
			$this->preview_email_tags_values       = array(
47
				'payment_method' => esc_html__( 'Offline', 'give' ),
48
			);
49
50
			// Initialize empty payment.
51
			$this->payment = new Give_Payment( 0 );
52
53
			$this->load();
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
		/**
61
		 * Get email message
62
		 *
63
		 * @since 2.0
64
		 * @return string
65
		 */
66
		public function get_email_message() {
67
			$post_offline_customization_option = get_post_meta(
68
				$this->payment->form_id,
69
				'_give_customize_offline_donations',
70
				true
71
			);
72
73
			// Customize email content depending on whether the single form has been customized
74
			$message = give_get_option( "{$this->id}_email_message", $this->get_default_email_message() );
75
76
			if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
77
				$message = get_post_meta( $this->payment->form_id, '_give_offline_donation_email', true );
78
			}
79
80
			/**
81
			 * Filter the email message.
82
			 *
83
			 * @since 2.0
84
			 */
85
			$message = apply_filters( "give_{$this->id}_get_email_message", $message, $this );
86
87
			return $message;
88
		}
89
90
		/**
91
		 * Get email message
92
		 *
93
		 * @since 2.0
94
		 * @return string
95
		 */
96
		public function get_email_subject() {
97
			$post_offline_customization_option = get_post_meta(
98
				$this->payment->form_id,
99
				'_give_customize_offline_donations',
100
				true
101
			);
102
103
			$subject = give_get_option(
104
				"{$this->id}_email_subject",
105
				$this->get_default_email_subject()
106
			);
107
108
			if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
109
				$subject = get_post_meta( $this->payment->form_id, '_give_offline_donation_subject', true );
110
			}
111
112
			/**
113
			 * Filter the email subject.
114
			 *
115
			 * @since 2.0
116
			 */
117
			$subject = apply_filters( "give_{$this->id}_get_email_subject", $subject, $this );
118
119
			return $subject;
120
		}
121
122
		/**
123
		 * Get attachments.
124
		 *
125
		 * @since 2.0
126
		 * @return array
127
		 */
128
		public function get_email_attachments() {
129
			/**
130
			 * Filter the attachments.
131
			 * Note: This filter will deprecate soon.
132
			 *
133
			 * @since 1.0
134
			 */
135
			$attachment = apply_filters(
136
				'give_offline_donation_attachments',
137
				array(),
138
				$this->payment->ID,
139
				$this->payment->payment_meta
140
			);
141
142
			/**
143
			 * Filter the email attachment.
144
			 *
145
			 * @since 2.0
146
			 */
147
			$attachment = apply_filters( "give_{$this->id}_get_email_attachment", $attachment, $this );
148
149
			return $attachment;
150
		}
151
152
		/**
153
		 * Get default email subject.
154
		 *
155
		 * @since  2.0
156
		 * @access public
157
		 * @return string
158
		 */
159
		public function get_default_email_subject() {
160
			/**
161
			 * Filter the default subject.
162
			 *
163
			 * @since 2.0
164
			 */
165
			return apply_filters( "give_{$this->id}_get_default_email_subject", esc_attr__( '{donation} - Offline Donation Instructions', 'give' ), $this );
166
		}
167
168
169
		/**
170
		 * Get default email message.
171
		 *
172
		 * @since  2.0
173
		 * @access public
174
		 *
175
		 * @return string
176
		 */
177
		public function get_default_email_message() {
178
			$message = give_get_default_offline_donation_email_content();
179
180
			/**
181
			 * Filter the email message
182
			 *
183
			 * @since 2.0
184
			 *
185
			 * @param string $message
186
			 */
187
			return apply_filters( "give_{$this->id}_get_default_email_message", $message, $this );
188
		}
189
190
191
		/**
192
		 * Set email data.
193
		 *
194
		 * @since 2.0
195
		 */
196
		public function setup_email_data() {
197
			// Set recipient email.
198
			$this->recipient_email = $this->payment->email;
199
200
			/**
201
			 * Filters the from name.
202
			 *
203
			 * @since 1.7
204
			 */
205
			$from_name = apply_filters( 'give_donation_from_name', Give()->emails->get_from_name(), $this->payment->ID, $this->payment->payment_meta );
206
207
			/**
208
			 * Filters the from email.
209
			 *
210
			 * @since 1.7
211
			 */
212
			$from_email = apply_filters( 'give_donation_from_address', Give()->emails->get_from_address(), $this->payment->ID, $this->payment->payment_meta );
213
214
			Give()->emails->__set( 'from_name', $from_name );
215
			Give()->emails->__set( 'from_email', $from_email );
216
			Give()->emails->__set( 'heading', __( 'Offline Donation Instructions', 'give' ) );
217
			Give()->emails->__set( 'headers', apply_filters( 'give_receipt_headers', Give()->emails->get_headers(), $this->payment->ID, $this->payment->payment_meta ) );
218
		}
219
220
		/**
221
		 * Setup email notification.
222
		 *
223
		 * @since  2.0
224
		 * @access public
225
		 *
226
		 * @param int $payment_id
227
		 */
228
		public function setup_email_notification( $payment_id ) {
229
			$this->payment = new Give_Payment( $payment_id );
230
231
			// Exit if not donation was not with offline donation.
232
			if ( 'offline' !== $this->payment->gateway ) {
233
				return;
234
			}
235
236
			// Set email data.
237
			$this->setup_email_data();
238
239
			// Send email.
240
			$this->send_email_notification( array(
241
				'payment_id' => $this->payment->ID,
242
			) );
243
		}
244
245
		/**
246
		 * Set notification status
247
		 *
248
		 * @since  2.0
249
		 * @access public
250
		 *
251
		 * @param $update_options
252
		 * @param $option_name
253
		 */
254
		public function set_notification_status( $update_options, $option_name ) {
255
			// Get updated settings.
256
			$update_options = give_get_settings();
257
258
			$notification_status = isset( $update_options['gateways']['offline'] ) ? 'enabled' : 'disabled';
259
260
			if (
261
				empty( $update_options[ "{$this->id}_notification" ] )
262
				|| $notification_status !== $update_options[ "{$this->id}_notification" ]
263
			) {
264
				$update_options[ "{$this->id}_notification" ] = $notification_status;
265
				update_option( $option_name, $update_options );
266
			}
267
		}
268
	}
269
270
endif; // End class_exists check
271
272
return Give_Offline_Donation_Instruction_Email::get_instance();
273