Completed
Push — issues/611 ( f5eca5...6d6b48 )
by Ravinder
21:13
created

Give_Email_Access_Email::get_email_attachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 20
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 26 and the first side effect is on line 15.

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