Completed
Push — issues/1796 ( 616c54 )
by Ravinder
20:12
created

Give_Email_Notifications::email_preview_header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 12
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 17 and the first side effect is on line 360.

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 Notification
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
/**
15
 * Class Give_Email_Notifications
16
 */
17
class Give_Email_Notifications {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
18
	/**
19
	 * Instance.
20
	 *
21
	 * @since  2.0
22
	 * @access static
23
	 * @var
24
	 */
25
	static private $instance;
26
27
	/**
28
	 * Array of email notifications.
29
	 *
30
	 * @since  2.0
31
	 * @access private
32
	 * @var array
33
	 */
34
	private $emails = array();
35
36
	/**
37
	 * Singleton pattern.
38
	 *
39
	 * @since  2.0
40
	 * @access private
41
	 * Give_Payumoney_API constructor.
42
	 */
43
	private function __construct() {
44
	}
45
46
47
	/**
48
	 * Get instance.
49
	 *
50
	 * @since  2.0
51
	 * @access static
52
	 * @return static
53
	 */
54
	static function get_instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
		if ( null === static::$instance ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
56
			self::$instance = new static();
57
		}
58
59
		return self::$instance;
60
	}
61
62
	/**
63
	 * Setup dependencies
64
	 *
65
	 * @since 2.0
66
	 */
67
	public function init() {
68
		// Load files.
69
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/ajax-handler.php';
70
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-setting-field.php';
71
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/filters.php';
72
73
		// Load email notifications.
74
		$this->add_emails_notifications();
75
76
		add_filter( 'give_metabox_form_data_settings', array( $this, 'add_metabox_setting_fields' ), 10, 2 );
77
		add_action( 'init', array( $this, 'preview_email' ) );
78
		add_action( 'init', array( $this, 'send_preview_email' ) );
79
80
		/* @var Give_Email_Notification $email */
81
		foreach ( $this->get_email_notifications() as $email ) {
82
			// Setup email section.
83
			if( Give_Email_Notification_Util::is_show_on_emails_setting_page( $email ) ) {
84
				add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) );
85
				add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) );
86
			}
87
88
			// Setup email preview.
89
			if ( Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) {
90
				add_action( "give_{$email->config['id']}_email_preview", array( $this, 'email_preview_header' ) );
91
				add_filter( "give_{$email->config['id']}_email_preview_data", array( $this, 'email_preview_data' ) );
92
				add_filter( "give_{$email->config['id']}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 );
93
			}
94
		}
95
	}
96
97
98
	/**
99
	 * Add setting to metabox.
100
	 *
101
	 * @since  2.0
102
	 * @access public
103
	 *
104
	 * @param array $settings
105
	 * @param int   $post_id
106
	 *
107
	 * @return array
108
	 */
109
	public function add_metabox_setting_fields( $settings, $post_id ) {
110
		$emails = $this->get_email_notifications();
111
112
		// Bailout.
113
		if ( empty( $emails ) ) {
114
			return $settings;
115
		}
116
117
		// Email notification setting.
118
		$settings['email_notification_options'] = array(
119
			'id'         => 'email_notification_options',
120
			'title'      => __( 'Email Notification', 'give' ),
121
122
			/**
123
			 * Filter the email notification settings.
124
			 *
125
			 * @since 2.0
126
			 */
127
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
128
		);
129
130
		return $settings;
131
	}
132
133
	/**
134
	 * Add email notifications
135
	 *
136
	 * @since  2.0
137
	 * @access private
138
	 */
139
	private function add_emails_notifications() {
140
		$this->emails = array(
141
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
142
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
144
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
145
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
146
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
147
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
148
		);
149
150
		/**
151
		 * Filter the email notifications.
152
		 *
153
		 * @since 2.0
154
		 */
155
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
156
157
		// Bailout.
158
		if ( empty( $this->emails ) ) {
159
			return;
160
		}
161
162
		// Initiate email notifications.
163
		foreach ( $this->emails as $email ) {
164
			$email->init();
165
		}
166
	}
167
168
169
	/**
170
	 * Get list of email notifications.
171
	 *
172
	 * @since  2.0
173
	 * @access public
174
	 * @return array
175
	 */
176
	public function get_email_notifications() {
177
		return $this->emails;
178
	}
179
180
181
	/**
182
	 * Displays the email preview
183
	 *
184
	 * @since  2.0
185
	 * @access public
186
	 * @return bool|null
187
	 */
188
	public function preview_email() {
189
		// Bailout.
190
		if ( ! Give_Email_Notification_Util::can_preview_email() ) {
191
			return false;
192
		}
193
194
		// Security check.
195
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
196
197
		// Get email type.
198
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
199
200
		/* @var Give_Email_Notification $email */
201
		foreach ( $this->get_email_notifications() as $email ) {
202
			if ( $email_type !== $email->config['id'] ) {
203
				continue;
204
			}
205
206
			// Set form id.
207
			$form_id = empty( $_GET['form_id']  ) ? null : absint( $_GET['form_id'] );
208
209
			// Call setup email data to apply filter and other thing to email.
210
			$email->setup_email_data();
211
212
			// Decode message.
213
			$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) );
214
215
			// Set email template.
216
			Give()->emails->html    = true;
0 ignored issues
show
Documentation introduced by
The property $html is declared private in Give_Emails. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
217
			Give()->emails->__set( 'template', $email->get_email_template( $form_id ) );
218
219
			if ( 'text/plain' === $email->config['content_type'] ) {
220
				// Give()->emails->__set( 'html', false );
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
221
				Give()->emails->__set( 'template', 'none' );
222
			}
223
224
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
225
226
				/**
227
				 * Filter the email preview data
228
				 *
229
				 * @since 2.0
230
				 *
231
				 * @param array
232
				 */
233
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
234
235
				/**
236
				 * Fire the give_{$email_type}_email_preview action
237
				 *
238
				 * @since 2.0
239
				 */
240
				do_action( "give_{$email_type}_email_preview", $email );
241
242
				/**
243
				 * Filter the email message
244
				 *
245
				 * @since 2.0
246
				 *
247
				 * @param string                  $email_message
248
				 * @param array                   $email_preview_data
249
				 * @param Give_Email_Notification $email
250
				 */
251
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
252
253
				exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method preview_email() 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...
254
			}
255
		}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
256
	}
257
258
259
	/**
260
	 * Add header to donation receipt email preview
261
	 *
262
	 * @since   2.0
263
	 * @access  public
264
	 *
265
	 * @param Give_Email_Notification $email
266
	 */
267
	public function email_preview_header( $email ) {
268
		/**
269
		 * Filter the all email preview headers.
270
		 *
271
		 * @since 2.0
272
		 *
273
		 * @param Give_Email_Notification $email
274
		 */
275
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
276
277
		echo $email_preview_header;
278
	}
279
280
	/**
281
	 * Add email preview data
282
	 *
283
	 * @since   2.0
284
	 * @access  public
285
	 *
286
	 * @param array $email_preview_data
287
	 *
288
	 * @return array
289
	 */
290
	public function email_preview_data( $email_preview_data ) {
291
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) );
292
		$email_preview_data['user_id']    = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) );
293
294
		return $email_preview_data;
295
	}
296
297
	/**
298
	 * Replace email template tags.
299
	 *
300
	 * @since   2.0
301
	 * @access  public
302
	 *
303
	 * @param string $email_message
304
	 * @param array  $email_preview_data
305
	 *
306
	 * @return string
307
	 */
308
	public function email_preview_message( $email_message, $email_preview_data ) {
309
		if (
310
			! empty( $email_preview_data['payment_id'] )
311
			|| ! empty( $email_preview_data['user_id'] )
312
		) {
313
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
314
		}
315
316
		return $email_message;
317
	}
318
319
	/**
320
	 * Displays the email preview
321
	 *
322
	 * @since  2.0
323
	 * @access public
324
	 * @return bool|null
325
	 */
326
	public function send_preview_email() {
327
		// Bailout.
328
		if ( ! Give_Email_Notification_Util::can_send_preview_email() ) {
329
			return false;
330
		}
331
332
		// Security check.
333
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
334
335
		// Get email type.
336
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
337
338
		/* @var Give_Email_Notification $email */
339
		foreach ( $this->get_email_notifications() as $email ) {
340
			if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) {
341
				$email->send_preview_email();
342
				break;
343
			}
344
		}
345
	}
346
347
348
	/**
349
	 * Load Give_Email_Notifications
350
	 *
351
	 * @since  2.0
352
	 * @access public
353
	 */
354
	public function load() {
355
		add_action( 'init', array( $this, 'init' ), -1 );
356
	}
357
}
358
359
// Helper class.
360
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
361
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
362
363
// Add backward compatibility.
364
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php';
365
366
/**
367
 * Initialize functionality.
368
 */
369
Give_Email_Notifications::get_instance()->load();
370