Completed
Push — release/2.0 ( 9c875b...4d845f )
by Ravinder
19:03
created

Give_Email_Notifications::preview_email()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 69
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 21
nop 0
dl 0
loc 69
rs 6.5437
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 359.

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
			// Add section.
83
			add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) );
84
			add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) );
85
86
			if ( ! Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) {
87
				continue;
88
			}
89
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
	 * Add setting to metabox.
99
	 *
100
	 * @since  2.0
101
	 * @access public
102
	 *
103
	 * @param array $settings
104
	 * @param int   $post_id
105
	 *
106
	 * @return array
107
	 */
108
	public function add_metabox_setting_fields( $settings, $post_id ) {
109
		$emails = $this->get_email_notifications();
110
111
		// Bailout.
112
		if ( empty( $emails ) ) {
113
			return $settings;
114
		}
115
116
		// Email notification setting.
117
		$settings['email_notification_options'] = array(
118
			'id'         => 'email_notification_options',
119
			'title'      => __( 'Email Notification', 'give' ),
120
121
			/**
122
			 * Filter the email notification settings.
123
			 *
124
			 * @since 2.0
125
			 */
126
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
127
		);
128
129
		return $settings;
130
	}
131
132
	/**
133
	 * Add email notifications
134
	 *
135
	 * @since  2.0
136
	 * @access private
137
	 */
138
	private function add_emails_notifications() {
139
		$this->emails = array(
140
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
141
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
142
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
144
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
145
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
146
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
147
		);
148
149
		/**
150
		 * Filter the email notifications.
151
		 *
152
		 * @since 2.0
153
		 */
154
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
155
156
		// Bailout.
157
		if ( empty( $this->emails ) ) {
158
			return;
159
		}
160
161
		// Initiate email notifications.
162
		foreach ( $this->emails as $email ) {
163
			$email->init();
164
		}
165
	}
166
167
168
	/**
169
	 * Get list of email notifications.
170
	 *
171
	 * @since  2.0
172
	 * @access public
173
	 * @return array
174
	 */
175
	public function get_email_notifications() {
176
		return $this->emails;
177
	}
178
179
180
	/**
181
	 * Displays the email preview
182
	 *
183
	 * @since  2.0
184
	 * @access public
185
	 * @return bool|null
186
	 */
187
	public function preview_email() {
188
		// Bailout.
189
		if ( ! Give_Email_Notification_Util::can_preview_email() ) {
190
			return false;
191
		}
192
193
		// Security check.
194
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
195
196
		// Get email type.
197
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
198
199
		/* @var Give_Email_Notification $email */
200
		foreach ( $this->get_email_notifications() as $email ) {
201
			if ( $email_type !== $email->config['id'] ) {
202
				continue;
203
			}
204
205
			// Set form id.
206
			$form_id = empty( $_GET['form_id']  ) ? null : absint( $_GET['form_id'] );
207
208
			// Call setup email data to apply filter and other thing to email.
209
			$email->setup_email_data();
210
211
			// Decode message.
212
			$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) );
213
214
			// Set email template.
215
			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...
216
			Give()->emails->__set( 'template', $email->get_email_template( $form_id ) );
217
218
			if ( 'text/plain' === $email->config['content_type'] ) {
219
				// 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...
220
				Give()->emails->__set( 'template', 'none' );
221
			}
222
223
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
224
225
				/**
226
				 * Filter the email preview data
227
				 *
228
				 * @since 2.0
229
				 *
230
				 * @param array
231
				 */
232
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
233
234
				/**
235
				 * Fire the give_{$email_type}_email_preview action
236
				 *
237
				 * @since 2.0
238
				 */
239
				do_action( "give_{$email_type}_email_preview", $email );
240
241
				/**
242
				 * Filter the email message
243
				 *
244
				 * @since 2.0
245
				 *
246
				 * @param string                  $email_message
247
				 * @param array                   $email_preview_data
248
				 * @param Give_Email_Notification $email
249
				 */
250
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
251
252
				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...
253
			}
254
		}// 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...
255
	}
256
257
258
	/**
259
	 * Add header to donation receipt email preview
260
	 *
261
	 * @since   2.0
262
	 * @access  public
263
	 *
264
	 * @param Give_Email_Notification $email
265
	 */
266
	public function email_preview_header( $email ) {
267
		/**
268
		 * Filter the all email preview headers.
269
		 *
270
		 * @since 2.0
271
		 *
272
		 * @param Give_Email_Notification $email
273
		 */
274
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
275
276
		echo $email_preview_header;
277
	}
278
279
	/**
280
	 * Add email preview data
281
	 *
282
	 * @since   2.0
283
	 * @access  public
284
	 *
285
	 * @param array $email_preview_data
286
	 *
287
	 * @return array
288
	 */
289
	public function email_preview_data( $email_preview_data ) {
290
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) );
291
		$email_preview_data['user_id']    = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) );
292
293
		return $email_preview_data;
294
	}
295
296
	/**
297
	 * Replace email template tags.
298
	 *
299
	 * @since   2.0
300
	 * @access  public
301
	 *
302
	 * @param string $email_message
303
	 * @param array  $email_preview_data
304
	 *
305
	 * @return string
306
	 */
307
	public function email_preview_message( $email_message, $email_preview_data ) {
308
		if (
309
			! empty( $email_preview_data['payment_id'] )
310
			|| ! empty( $email_preview_data['user_id'] )
311
		) {
312
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
313
		}
314
315
		return $email_message;
316
	}
317
318
	/**
319
	 * Displays the email preview
320
	 *
321
	 * @since  2.0
322
	 * @access public
323
	 * @return bool|null
324
	 */
325
	public function send_preview_email() {
326
		// Bailout.
327
		if ( ! Give_Email_Notification_Util::can_send_preview_email() ) {
328
			return false;
329
		}
330
331
		// Security check.
332
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
333
334
		// Get email type.
335
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
336
337
		/* @var Give_Email_Notification $email */
338
		foreach ( $this->get_email_notifications() as $email ) {
339
			if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) {
340
				$email->send_preview_email();
341
				break;
342
			}
343
		}
344
	}
345
346
347
	/**
348
	 * Load Give_Email_Notifications
349
	 *
350
	 * @since  2.0
351
	 * @access public
352
	 */
353
	public function load() {
354
		add_action( 'init', array( $this, 'init' ), -1 );
355
	}
356
}
357
358
// Helper class.
359
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
360
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
361
362
// Add backward compatibility.
363
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php';
364
365
/**
366
 * Initialize functionality.
367
 */
368
Give_Email_Notifications::get_instance()->load();
369