Completed
Push — backup/611 ( 661115 )
by Ravinder
1411:51 queued 1394:16
created

Give_Donation_Receipt_Email::setup_email_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 40
rs 9.28
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
 * Donation Receipt 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       1.9
12
 */
13
14
// Exit if access directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
if ( ! class_exists( 'Give_Donation_Receipt_Email' ) ) :
20
21
	/**
22
	 * Give_Donation_Receipt_Email
23
	 *
24
	 * @abstract
25
	 * @since       1.9
26
	 */
27
	class Give_Donation_Receipt_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   1.9
36
		 */
37
		public function init() {
38
			$this->id          = 'donation-receipt';
39
			$this->label       = __( 'Donation Receipt', 'give' );
40
			$this->description = __( 'Donation Receipt Notification will be sent to donor when new donation received.', 'give' );
41
42
			$this->notification_status  = 'enabled';
43
			$this->recipient_group_name = __( 'Donor', 'give' );
44
			$this->form_metabox_setting = true;
45
46
			// Initialize empty payment.
47
			$this->payment = new Give_Payment(0);
48
49
			$this->load();
50
51
			add_action( "give_{$this->id}_email_notification", array( $this, 'send_donation_receipt' ) );
52
			add_action( 'give_email_links', array( $this, 'resend_donation_receipt' ) );
53
		}
54
55
		/**
56
		 * Get default email subject.
57
		 *
58
		 * @since  1.9
59
		 * @access public
60
		 * @return string
61
		 */
62
		public function get_default_email_subject() {
63
			/**
64
			 * Filter the default subject.
65
			 *
66
			 * @since 1.9
67
			 */
68
			return apply_filters( "give_{$this->id}_get_default_email_subject", esc_attr__( 'Donation Receipt', 'give' ), $this );
69
		}
70
71
72
		/**
73
		 * Get default email message.
74
		 *
75
		 * @since  1.9
76
		 * @access public
77
		 *
78
		 * @return string
79
		 */
80
		public function get_default_email_message() {
81
			/**
82
			 * Filter the donation receipt email message
83
			 *
84
			 * @since 1.9
85
			 *
86
			 * @param string $message
87
			 */
88
			return apply_filters( "give_{$this->id}_get_default_email_message", give_get_default_donation_receipt_email(), $this );
89
		}
90
91
92
		/**
93
		 * Get email subject.
94
		 *
95
		 * @since 1.9
96
		 * @access public
97
		 * @return string
98
		 */
99
		public function get_email_subject() {
100
			$subject = wp_strip_all_tags( give_get_option( "{$this->id}_email_subject", $this->get_default_email_subject() ) );
101
102
			/**
103
			 * Filters the donation email receipt subject.
104
			 * Note: This filter will deprecate soon.
105
			 *
106
			 * @since 1.0
107
			 */
108
			$subject = apply_filters( 'give_donation_subject', $subject, $this->payment->ID );
109
110
			/**
111
			 * Filters the donation email receipt subject.
112
			 *
113
			 * @since 1.9
114
			 */
115
			$subject = apply_filters( "give_{$this->id}_get_email_subject", $subject, $this );
116
117
			return $subject;
118
		}
119
120
121
		/**
122
		 * Get email message.
123
		 *
124
		 * @since  1.9
125
		 * @access public
126
		 * @return string
127
		 */
128
		public function get_email_message() {
129
			$message = give_get_option( "{$this->id}_email_message", $this->get_default_email_message() );
130
131
			/**
132
			 * Filter message on basis of email template
133
			 * Note: This filter will deprecate soon.
134
			 *
135
			 * @since 1.0
136
			 */
137
			$message = apply_filters( 'give_donation_receipt_' . Give()->emails->get_template(), $message, $this->payment->ID, $this->payment->payment_meta );
138
139
			/**
140
			 * Filter the message
141
			 * Note: This filter will deprecate soon.
142
			 *
143
			 * @since 1.0
144
			 */
145
			$message = apply_filters( 'give_donation_receipt', $message, $this->payment->ID, $this->payment->payment_meta );
146
147
			/**
148
			 * Filter the message
149
			 *
150
			 * @since 1.9
151
			 */
152
			$message = apply_filters( "give_{$this->id}_get_email_message", $message, $this );
153
			return $message;
154
		}
155
156
		/**
157
		 * Get the recipient attachments.
158
		 *
159
		 * @since  1.9
160
		 * @access public
161
		 * @return array
162
		 */
163
		public function get_email_attachments() {
164
			/**
165
			 * Filter the attachments.
166
			 * Note: this filter will deprecate soon.
167
			 *
168
			 * @since 1.0
169
			 */
170
			$attachments = apply_filters( 'give_receipt_attachments', array(), $this->payment->ID, $this->payment->payment_meta );
171
172
			/**
173
			 * Filter the attachments.
174
			 *
175
			 * @since 1.9
176
			 */
177
			$attachments = apply_filters( "give_{$this->id}_get_email_attachments", $attachments, $this );
178
179
			return $attachments;
180
		}
181
182
183
		/**
184
		 * Set email data.
185
		 *
186
		 * @since 1.9
187
		 */
188
		public function setup_email_data() {
189
			// Set recipient email.
190
			$this->recipient_email = $this->payment->email;
191
192
			/**
193
			 * Filters the from name.
194
			 *
195
			 * @param int $payment_id Payment id.
196
			 * @param mixed $payment_data Payment meta data.
197
			 *
198
			 * @since 1.0
199
			 */
200
			$from_name = apply_filters( 'give_donation_from_name', Give()->emails->get_from_name(), $this->payment->ID, $this->payment->payment_meta );
201
202
			/**
203
			 * Filters the from email.
204
			 *
205
			 * @param int $payment_id Payment id.
206
			 * @param mixed $payment_data Payment meta data.
207
			 *
208
			 * @since 1.0
209
			 */
210
			$from_email = apply_filters( 'give_donation_from_address', Give()->emails->get_from_address(), $this->payment->ID, $this->payment->payment_meta );
211
212
			Give()->emails->__set( 'from_name', $from_name );
213
			Give()->emails->__set( 'from_email', $from_email );
214
			Give()->emails->__set( 'heading', esc_html__( 'Donation Receipt', 'give' ) );
215
216
			/**
217
			 * Filters the donation receipt's email headers.
218
			 *
219
			 * @param int $payment_id Payment id.
220
			 * @param mixed $payment_data Payment meta data.
221
			 *
222
			 * @since 1.0
223
			 */
224
			$headers = apply_filters( 'give_receipt_headers', Give()->emails->get_headers(), $this->payment->ID, $this->payment->payment_meta );
225
226
			Give()->emails->__set( 'headers', $headers );
227
		}
228
229
		/**
230
		 * Send donation receipt
231
		 * @since  1.9
232
		 * @access public
233
		 *
234
		 * @param $payment_id
235
		 */
236
		public function send_donation_receipt( $payment_id ) {
237
			$this->payment = new Give_Payment( $payment_id );
238
239
			// Setup email data.
240
			$this->setup_email_data();
241
242
			// Send email.
243
			$this->send_email_notification( array( 'payment_id' => $this->payment->ID ) );
244
		}
245
246
		/**
247
		 * Resend payment receipt by row action.
248
		 *
249
		 * @since  1.9
250
		 * @access public
251
		 *
252
		 * @param array $data
253
		 */
254
		public function resend_donation_receipt( $data ) {
255
			$purchase_id = absint( $data['purchase_id'] );
256
257
			if ( empty( $purchase_id ) ) {
258
				return;
259
			}
260
261
			// Get donation payment information.
262
			$this->payment = new Give_Payment( $purchase_id );
263
264
			if ( ! current_user_can( 'edit_give_payments', $this->payment->ID ) ) {
265
				wp_die( esc_html__( 'You do not have permission to edit payments.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
266
			}
267
268
			// Setup email data.
269
			$this->setup_email_data();
270
271
			// Send email.
272
			$this->send_email_notification( array( 'payment_id' => $this->payment->ID ) );
273
274
			wp_redirect( add_query_arg( array(
275
				'give-message' => 'email_sent',
276
				'give-action'  => false,
277
				'purchase_id'  => false,
278
			) ) );
279
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method resend_donation_receipt() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
280
		}
281
	}
282
283
endif; // End class_exists check
284
285
return Give_Donation_Receipt_Email::get_instance();