Completed
Push — issues/611 ( 3dcd15...955512 )
by Ravinder
18:08
created

Give_Email_Access_Email::email_preview_header()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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