Completed
Push — issues/611 ( df05ad...753a69 )
by Ravinder
32:37 queued 11:53
created

get_email_message()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 23
rs 9.0856
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
 * Offline Donation Instruction 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_Offline_Donation_Instruction_Email' ) ) :
20
21
	/**
22
	 * Give_Offline_Donation_Instruction_Email
23
	 *
24
	 * @abstract
25
	 * @since       1.9
26
	 */
27
	class Give_Offline_Donation_Instruction_Email extends Give_Email_Notification {
28
		/* @var Give_Payment $payment */
29
		public $payment;
30
31
		/**
32
		 * Create a class instance.
33
		 *
34
		 * @param   mixed[] $objects
35
		 *
36
		 * @access  public
37
		 * @since   1.9
38
		 */
39
		public function __construct( $objects = array() ) {
40
			$this->id          = 'offline-donation-instruction';
41
			$this->label       = __( 'Offline Donation Instruction', 'give' );
42
			$this->description = __( 'Offline Donation Instruction will be sent to recipient(s) when offline donation received.', 'give' );
43
44
			$this->notification_status       = 'enabled';
45
			$this->recipient_group_name      = __( 'Donor', 'give' );
46
			$this->preview_email_tags_values = array(
47
				'payment_method' => esc_html__( 'Offline', 'give' ),
48
			);
49
50
			// Initialize empty payment.
51
			$this->payment = new Give_Payment( 0 );
52
53
			parent::__construct();
54
55
			add_action( 'give_insert_payment', array( $this, 'setup_email_notification' ) );
56
		}
57
58
59
		/**
60
		 * Get email message
61
		 *
62
		 * @since 1.9
63
		 * @return string
64
		 */
65
		public function get_email_message() {
66
			$post_offline_customization_option = get_post_meta(
67
				$this->payment->form_id,
68
				'_give_customize_offline_donations',
69
				true
70
			);
71
72
			//Customize email content depending on whether the single form has been customized
73
			$message = give_get_option( "{$this->id}_email_message", $this->get_default_email_message() );
74
75
			if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
76
				$message = get_post_meta( $this->payment->form_id, '_give_offline_donation_email', true );
77
			}
78
79
			/**
80
			 * Filter the email message.
81
			 *
82
			 * @since 1.9
83
			 */
84
			$message = apply_filters( "give_{$this->id}_get_email_message", $message, $this );
85
86
			return $message;
87
		}
88
89
		/**
90
		 * Get email message
91
		 *
92
		 * @since 1.9
93
		 * @return string
94
		 */
95
		public function get_email_subject() {
96
			$post_offline_customization_option = get_post_meta(
97
				$this->payment->form_id,
98
				'_give_customize_offline_donations',
99
				true
100
			);
101
102
			$subject = give_get_option(
103
				"{$this->id}_email_subject",
104
				$this->get_default_email_subject()
105
			);
106
107
			if ( give_is_setting_enabled( $post_offline_customization_option, 'enabled' ) ) {
108
				$subject = get_post_meta( $this->payment->form_id, '_give_offline_donation_subject', true );
109
			}
110
111
			/**
112
			 * Filter the email subject.
113
			 *
114
			 * @since 1.9
115
			 */
116
			$subject = apply_filters( "give_{$this->id}_get_email_subject", $subject, $this );
117
118
			return $subject;
119
		}
120
121
		/**
122
		 * Get attachments.
123
		 *
124
		 * @since 1.9
125
		 * @return array
126
		 */
127
		public function get_email_attachments() {
128
			/**
129
			 * Filter the attachments.
130
			 * Note: This filter will deprecate soon.
131
			 *
132
			 * @since 1.0
133
			 */
134
			$attachment = apply_filters(
135
				'give_offline_donation_attachments',
136
				array(),
137
				$this->payment->ID,
138
				$this->payment->payment_meta
139
			);
140
141
			/**
142
			 * Filter the email attachment.
143
			 *
144
			 * @since 1.9
145
			 */
146
			$attachment = apply_filters( "give_{$this->id}_get_email_attachment", $attachment, $this );
147
148
			return $attachment;
149
		}
150
151
		/**
152
		 * Get default email subject.
153
		 *
154
		 * @since  1.9
155
		 * @access public
156
		 * @return string
157
		 */
158
		public function get_default_email_subject() {
159
			/**
160
			 * Filter the default subject.
161
			 *
162
			 * @since 1.9
163
			 */
164
			return apply_filters( "give_{$this->id}_get_default_email_subject", esc_attr__( '{donation} - Offline Donation Instructions', 'give' ), $this );
165
		}
166
167
168
		/**
169
		 * Get default email message.
170
		 *
171
		 * @since  1.9
172
		 * @access public
173
		 *
174
		 * @return string
175
		 */
176
		public function get_default_email_message() {
177
			$message = give_get_default_offline_donation_email_content();
178
179
			/**
180
			 * Filter the email message
181
			 *
182
			 * @since 1.9
183
			 *
184
			 * @param string $message
185
			 */
186
			return apply_filters( "give_{$this->id}_get_default_email_message", $message, $this );
187
		}
188
189
190
		/**
191
		 * Set email data.
192
		 *
193
		 * @since 1.9
194
		 */
195
		public function setup_email_data() {
196
			// Set recipient email.
197
			$this->recipient_email = $this->payment->email;
198
199
			/**
200
			 * Filters the from name.
201
			 *
202
			 * @since 1.7
203
			 */
204
			$from_name = apply_filters( 'give_donation_from_name', Give()->emails->get_from_name(), $this->payment->ID, $this->payment->payment_meta );
205
206
			/**
207
			 * Filters the from email.
208
			 *
209
			 * @since 1.7
210
			 */
211
			$from_email = apply_filters( 'give_donation_from_address', Give()->emails->get_from_address(), $this->payment->ID, $this->payment->payment_meta );
212
213
214
			Give()->emails->__set( 'from_name', $from_name );
215
			Give()->emails->__set( 'from_email', $from_email );
216
			Give()->emails->__set( 'heading', __( 'Offline Donation Instructions', 'give' ) );
217
			Give()->emails->__set( 'headers', apply_filters( 'give_receipt_headers', Give()->emails->get_headers(), $this->payment->ID, $this->payment->payment_meta ) );
218
219
			error_log( print_r( Give()->emails, true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log' );
220
		}
221
222
		/**
223
		 * Setup email notification.
224
		 *
225
		 * @since  1.9
226
		 * @access public
227
		 *
228
		 * @param int $payment_id
229
		 */
230
		public function setup_email_notification( $payment_id ) {
231
			$this->payment = new Give_Payment( $payment_id );
232
233
			// Exit if not donation was not with offline donation.
234
			if ( 'offline' !== $this->payment->gateway ) {
235
				return;
236
			}
237
238
			// Set 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
endif; // End class_exists check
247
248
return new Give_Offline_Donation_Instruction_Email();
249