Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

Give_New_Donor_Register_Email   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 33 1
A get_default_email_message() 0 16 1
A setup_email_notification() 0 6 1
B email_preview_header() 0 75 6
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 25 and the first side effect is on line 14.

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
 * New Donor Register Email
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Emails
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       2.0
10
 */
11
12
// Exit if access directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
if ( ! class_exists( 'Give_New_Donor_Register_Email' ) ) :
18
19
	/**
20
	 * Give_New_Donor_Register_Email
21
	 *
22
	 * @abstract
23
	 * @since       2.0
24
	 */
25
	class Give_New_Donor_Register_Email extends Give_Email_Notification {
26
27
		/**
28
		 * Create a class instance.
29
		 *
30
		 * @access  public
31
		 * @since   2.0
32
		 */
33
		public function init() {
34
			$this->load( array(
35
				'id'                    => 'new-donor-register',
36
				'label'                 => __( 'New Donor Register', 'give' ),
37
				'description'           => __( 'New Donor Register Notification will be sent to recipient(s) when new donor registered.', 'give' ),
38
				'has_recipient_field'   => true,
39
				'notification_status'   => 'enabled',
40
				'has_preview_header'    => true,
41
				'email_tag_context'     => array( 'donor', 'general' ),
42
				'form_metabox_setting'  => false,
43
				'default_email_subject' => sprintf(
44
					/* translators: %s: site name */
45
					esc_attr__( '[%s] New User Registration', 'give' ),
46
					get_bloginfo( 'name' )
47
				),
48
				'default_email_massage' => $this->get_default_email_message(),
49
			) );
50
51
			// Setup action hook.
52
			add_action(
53
				"give_{$this->config['id']}_email_notification",
54
				array( $this, 'setup_email_notification' ),
55
				10,
56
				2
57
			);
58
59
			add_filter(
60
				'give_email_preview_header',
61
				array( $this, 'email_preview_header' ),
62
				10,
63
				2
64
			);
65
		}
66
67
		/**
68
		 * Get default email message.
69
		 *
70
		 * @since  2.0
71
		 * @access public
72
		 *
73
		 * @return string
74
		 */
75
		function get_default_email_message() {
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...
76
			$message = esc_attr__( 'New user registration on your site {sitename}:', 'give' ) . "\r\n\r\n";
77
			$message .= esc_attr__( 'Username: {username}', 'give' ) . "\r\n\r\n";
78
			$message .= esc_attr__( 'E-mail: {user_email}', 'give' ) . "\r\n";
79
80
			/**
81
			 * Filter the default email message
82
			 *
83
			 * @since 2.0
84
			 */
85
			return apply_filters(
86
				"give_{$this->config['id']}_get_default_email_message",
87
				$message,
88
				$this
89
			);
90
		}
91
92
93
		/**
94
		 * Send new donor register notifications.
95
		 *
96
		 * @since  2.0
97
		 * @access public
98
		 *
99
		 * @param int   $user_id   User ID.
100
		 * @param array $user_data User Information.
101
		 *
102
		 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
		 */
104
		public function setup_email_notification( $user_id, $user_data ) {
105
			$this->recipient_email = $user_data['user_email'];
106
			$this->send_email_notification( array(
107
				'user_id' => $user_id,
108
			) );
109
		}
110
111
112
		/**
113
		 * email preview header.
114
		 *
115
		 * @since  2.0
116
		 * @access public
117
		 *
118
		 * @param string                        $email_preview_header
119
		 * @param Give_New_Donor_Register_Email $email
120
		 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
121
		 */
122
		public function email_preview_header( $email_preview_header, $email ) {
123
			// Bailout.
124
			if ( $this->config['id'] !== $email->config['id'] ) {
125
				return $email_preview_header;
126
			}
127
128
			// Payment receipt switcher
129
			$user_id = give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' );
130
131
			// Get payments.
132
			$donors  = new Give_API();
133
			$donors  = give_check_variable( $donors->get_customers(), 'empty', array(), 'donors' );
0 ignored issues
show
Bug introduced by
The method get_customers() does not seem to exist on object<Give_API>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
			$options = array();
135
136
			// Default option.
137
			$options[0] = esc_html__( 'No donor(s) found.', 'give' );
138
139
			// Provide nice human readable options.
140
			if ( $donors ) {
141
				$options[0] = esc_html__( '- Select a donor -', 'give' );
142
				foreach ( $donors as $donor ) {
143
					// Exclude customers for which wp user not exist.
144
					if ( ! $donor['info']['user_id'] ) {
145
						continue;
146
					}
147
					$options[ $donor['info']['user_id'] ] = esc_html( '#' . $donor['info']['customer_id'] . ' - ' . $donor['info']['email'] );
148
				}
149
			}
150
151
			// Start constructing HTML output.
152
			$email_preview_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
153
154
			// Inline JS function for switching donations.
155
			$request_url = $_SERVER['REQUEST_URI'];
156
157
			// Remove payment id query param if set from request url.
158
			if ( $user_id ) {
159
				$request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] );
160
				$query            = $request_url_data['query'];
161
				$query            = str_replace( "&user_id={$user_id}", '', $query );
162
163
				$request_url = home_url( '/?' . str_replace( '', '', $query ) );
164
			}
165
166
			$email_preview_header .= '<script>
167
				 function change_preview(){
168
				  var transactions = document.getElementById("give_preview_email_user_id");
169
			        var selected_trans = transactions.options[transactions.selectedIndex];
170
				        if (selected_trans){
171
				            var url_string = "' . $request_url . '&user_id=" + selected_trans.value;
172
				                window.location = url_string;
173
				        }
174
				    }
175
			    </script>';
176
177
			$email_preview_header .= '<label for="give_preview_email_user_id" style="font-size:12px;color:#333;margin:0 4px 0 0;">' . esc_html__( 'Preview email with a donor:', 'give' ) . '</label>';
178
179
			// The select field with 100 latest transactions
180
			$email_preview_header .= Give()->html->select( array(
181
				'name'             => 'preview_email_user_id',
182
				'selected'         => $user_id,
183
				'id'               => 'give_preview_email_user_id',
184
				'class'            => 'give-preview-email-donor-id',
185
				'options'          => $options,
186
				'chosen'           => false,
187
				'select_atts'      => 'onchange="change_preview()"',
188
				'show_option_all'  => false,
189
				'show_option_none' => false,
190
			) );
191
192
			// Closing tag
193
			$email_preview_header .= '</div>';
194
195
			echo $email_preview_header;
196
		}
197
	}
198
199
endif; // End class_exists check
200
201
return Give_New_Donor_Register_Email::get_instance();
202