Completed
Push — issues/611 ( aec0d9 )
by Ravinder
28:06 queued 08:22
created

setup_email_notification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 15
rs 9.4285
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
 * New Donation 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_Email_Access_Email' ) ) :
20
21
	/**
22
	 * Give_Email_Access_Email
23
	 *
24
	 * @abstract
25
	 * @since       2.0
26
	 */
27
	class Give_Email_Access_Email extends Give_Email_Notification {
28
		/**
29
		 * Create a class instance.
30
		 *
31
		 * @access  public
32
		 * @since   2.0
33
		 */
34
		public function init() {
35
			$this->load( array(
36
				'id'                           => 'email-access',
37
				'label'                        => __( 'Email access', 'give' ),
38
				'description'                  => __( 'Email Access Notification will be sent to recipient(s) when want to access their donation history using only email.', 'give' ),
39
				'notification_status'          => 'enabled',
40
				'form_metabox_setting'         => true,
41
				'notification_status_editable' => false,
42
				'email_tag_context'            => 'donor',
43
				'recipient_group_name'         => __( 'Donor', 'give' ),
44
				'default_email_subject'        => sprintf( __( 'Your Access Link to %s', 'give' ), get_bloginfo( 'name' ) ),
45
				'default_email_message'        => $this->get_default_email_message(),
46
			) );
47
48
			add_action( "give_{$this->config['id']}_email_notification", array( $this, 'setup_email_notification' ), 10, 2 );
49
			add_action( 'give_save_settings_give_settings', array( $this, 'set_notification_status' ), 10, 2 );
50
			add_filter( 'give_email_preview_header', array( $this, 'email_preview_header' ), 10, 2 );
51
		}
52
53
54
		/**
55
		 * Get email subject.
56
		 *
57
		 * @since  2.0
58
		 * @access public
59
		 *
60
		 * @param int $form_id
61
		 *
62
		 * @return string
63
		 */
64
		public function get_email_subject( $form_id = null ) {
65
			$subject = wp_strip_all_tags( give_get_option( "{$this->config['id']}_email_subject", $this->config['default_email_subject'] ) );
66
67
			/**
68
			 * Filters the donation notification subject.
69
			 * Note: This filter will deprecate soon.
70
			 *
71
			 * @since 1.0
72
			 */
73
			$subject = apply_filters( 'give_email_access_token_subject', $subject );
74
75
			/**
76
			 * Filters the donation notification subject.
77
			 *
78
			 * @since 2.0
79
			 */
80
			$subject = apply_filters( "give_{$this->config['id']}_get_email_subject", $subject, $this, $form_id );
81
82
			return $subject;
83
		}
84
85
86
		/**
87
		 * Get email attachment.
88
		 *
89
		 * @since  2.0
90
		 * @access public
91
		 *
92
		 * @param int $form_id
93
		 *
94
		 * @return string
95
		 */
96
		public function get_email_message( $form_id = null ) {
97
			$message = give_get_option( "{$this->config['id']}_email_message", $this->get_default_email_message() );
98
99
			/**
100
			 * Filter the email message
101
			 * Note: This filter will deprecate soon.
102
			 *
103
			 * @since 1.0
104
			 */
105
			$message = apply_filters( 'give_email_access_token_message', $message );
106
107
			/**
108
			 * Filter the email message
109
			 *
110
			 * @since 2.0
111
			 */
112
			$message = apply_filters( "give_{$this->config['id']}_get_default_email_message", $message, $this, $form_id );
113
114
			return $message;
115
		}
116
117
118
		/**
119
		 * Get email attachment.
120
		 *
121
		 * @since  2.0
122
		 * @access public
123
		 *
124
		 * @param int $form_id
125
		 * @return array
126
		 */
127
		public function get_email_attachments( $form_id = null ) {
128
			/**
129
			 * Filters the donation notification email attachments.
130
			 * By default, there is no attachment but plugins can hook in to provide one more multiple.
131
			 * Note: This filter will deprecate soon.
132
			 *
133
			 * @since 1.0
134
			 */
135
			$attachments = apply_filters( 'give_admin_donation_notification_attachments', array() );
136
137
			/**
138
			 * Filters the donation notification email attachments.
139
			 * By default, there is no attachment but plugins can hook in to provide one more multiple.
140
			 *
141
			 * @since 2.0
142
			 */
143
			$attachments = apply_filters( "give_{$this->config['id']}_get_email_attachments", $attachments, $this, $form_id );
144
145
			return $attachments;
146
		}
147
148
149
		/**
150
		 * Get default email message.
151
		 *
152
		 * @since  2.0
153
		 * @access public
154
		 *
155
		 * @return string
156
		 */
157
		public function get_default_email_message() {
158
			$message = __( 'You or someone in your organization requested an access link be sent to this email address. This is a temporary access link for you to view your donation information. Click on the link below to view:', 'give' ) . "\n\n";
159
			$message .= '{email_access_link}' . "\n\n";
160
			$message .= "\n\n";
161
			$message .= __( 'Sincerely,', 'give' ) . "\n";
162
			$message .= get_bloginfo( 'name' ) . "\n";
163
164
			/**
165
			 * Filter the new donation email message
166
			 *
167
			 * @since 2.0
168
			 *
169
			 * @param string $message
170
			 */
171
			return apply_filters( "give_{$this->config['id']}_get_default_email_message", $message, $this );
172
		}
173
174
175
		/**
176
		 * Set email data
177
		 *
178
		 * @since 2.0
179
		 */
180
		public function setup_email_data() {
181
			/**
182
			 * Filters the from name.
183
			 * Note: This filter will deprecate soon.
184
			 *
185
			 * @since 1.0
186
			 */
187
			$from_name = apply_filters( 'give_donation_from_name', Give()->emails->get_from_name() );
188
189
			/**
190
			 * Filters the from email.
191
			 * Note: This filter will deprecate soon.
192
			 *
193
			 * @since 1.0
194
			 */
195
			$from_email = apply_filters( 'give_donation_from_address', Give()->emails->get_from_address() );
196
197
			Give()->emails->__set( 'from_name', $from_name );
198
			Give()->emails->__set( 'from_email', $from_email );
199
			Give()->emails->__set( 'heading', apply_filters( 'give_email_access_token_heading', __( 'Your Access Link', 'give' ) ) );
200
201
			/**
202
			 * Filters the donation notification email headers.
203
			 *
204
			 * @since 1.0
205
			 */
206
			$headers = apply_filters( 'give_admin_donation_notification_headers', Give()->emails->get_headers() );
207
208
			Give()->emails->__set( 'headers', $headers );
209
		}
210
211
		/**
212
		 * Setup email notification.
213
		 *
214
		 * @since  2.0
215
		 * @access public
216
		 *
217
		 * @param int    $donor_id
218
		 * @param string $email
219
		 */
220
		public function setup_email_notification( $donor_id, $email ) {
221
			$donor = Give()->customers->get_by( 'id', $donor_id );
222
			$this->recipient_email = $email;
223
224
			// Set email data.
225
			$this->setup_email_data();
226
227
			// Send email.
228
			$this->send_email_notification(
229
				array(
230
					'donor_id' => $donor_id,
231
					'user_id'  => $donor->user_id
232
				)
233
			);
234
		}
235
236
237
		/**
238
		 * Set notification status
239
		 *
240
		 * @since  2.0
241
		 * @access public
242
		 *
243
		 * @param $update_options
244
		 * @param $option_name
245
		 */
246
		public function set_notification_status( $update_options, $option_name ) {
247
			// Get updated settings.
248
			$update_options = give_get_settings();
249
250
			if (
251
				! empty( $update_options['email_access'] )
252
				&& $update_options['email_access'] !== $update_options[ "{$this->config['id']}_notification" ]
253
			) {
254
				$update_options[ "{$this->config['id']}_notification" ] = $update_options['email_access'];
255
				update_option( $option_name, $update_options );
256
			}
257
		}
258
259
260
		/**
261
		 * email preview header.
262
		 *
263
		 * @since  2.0
264
		 * @access public
265
		 *
266
		 * @param string                  $email_preview_header
267
		 * @param Give_Email_Access_Email $email
268
		 * @return string
269
		 */
270
		public function email_preview_header( $email_preview_header, $email ) {
271
			if( $this->config['id'] === $email->config['id'] ) {
272
				$email_preview_header = '';
273
			}
274
275
			return $email_preview_header;
276
		}
277
	}
278
279
endif; // End class_exists check
280
281
return Give_Email_Access_Email::get_instance();
282