Completed
Push — issues/611 ( d123e1...0564a6 )
by Ravinder
20:29
created

Give_Donation_Receipt_Email::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
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
 * 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
45
			// Initialize empty payment.
46
			$this->payment = new Give_Payment(0);
47
48
			parent::load();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (load() instead of init()). Are you sure this is correct? If so, you might want to change this to $this->load().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

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