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

admin/emails/class-donor-register-email.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * 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_Donor_Register_Email' ) ) :
18
19
	/**
20
	 * Give_Donor_Register_Email
21
	 *
22
	 * @abstract
23
	 * @since       2.0
24
	 */
25
	class Give_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'                    => 'donor-register',
36
				'label'                 => __( 'Donor Register', 'give' ),
37
				'description'           => __( 'Donor Register Notification will be sent to donor when new donor registered.', 'give' ),
38
				'notification_status'   => 'enabled',
39
				'email_tag_contex'      => 'donor',
40
				'form_metabox_setting'  => false,
41
				'recipient_group_name'  => __( 'Donor', 'give' ),
42
				'email_tag_context'     => array( 'donor', 'general' ),
43
				'default_email_subject' => sprintf(
44
				/* translators: %s: site name */
45
					esc_attr__( '[%s] Your username and password', 'give' ),
46
					get_bloginfo( 'name' )
47
				),
48
				'default_email_message' => $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() {
76
			$message = esc_attr__( 'Username: {username}', 'give' ) . "\r\n";
77
			$message .= sprintf(
78
				            esc_attr__( 'Password: %s', 'give' ),
79
				            esc_attr__( '[Password entered during donation]', 'give' )
80
			            ) . "\r\n";
81
82
			$message .= '<a href="' . wp_login_url() . '"> ' . esc_attr__( 'Click Here to Login &raquo;', 'give' ) . '</a>' . "\r\n";
83
84
			/**
85
			 * Filter the default email message
86
			 *
87
			 * @since 2.0
88
			 */
89
			return apply_filters(
90
				"give_{$this->config['id']}_get_default_email_message",
91
				$message, $this
92
			);
93
		}
94
95
96
		/**
97
		 * Setup and send new donor register notifications.
98
		 *
99
		 * @since  2.0
100
		 * @access public
101
		 *
102
		 * @param int   $user_id   User ID.
103
		 * @param array $user_data User Information.
104
		 *
105
		 * @return string
106
		 */
107
		public function setup_email_notification( $user_id, $user_data ) {
108
			$this->recipient_email = $user_data['user_email'];
109
			$this->send_email_notification( array(
110
				'user_id' => $user_id,
111
			) );
112
		}
113
114
		/**
115
		 * email preview header.
116
		 *
117
		 * @since  2.0
118
		 * @access public
119
		 *
120
		 * @param string                    $email_preview_header
121
		 * @param Give_Donor_Register_Email $email
122
		 *
123
		 * @return bool
124
		 */
125
		public function email_preview_header( $email_preview_header, $email ) {
126
			// Bailout.
127
			if ( $this->config['id'] !== $email->config['id'] ) {
128
				return $email_preview_header;
129
			}
130
131
			// Payment receipt switcher
132
			$user_id = give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' );
133
134
			// Get payments.
135
			$donors  = new Give_API();
136
			$donors  = give_check_variable( $donors->get_customers(), 'empty', array(), 'donors' );
0 ignored issues
show
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...
137
			$options = array();
138
139
			// Default option.
140
			$options[0] = esc_html__( 'No donor(s) found.', 'give' );
141
142
			// Provide nice human readable options.
143
			if ( $donors ) {
144
				$options[0] = esc_html__( '- Select a donor -', 'give' );
145
				foreach ( $donors as $donor ) {
146
					// Exclude customers for which wp user not exist.
147
					if ( ! $donor['info']['user_id'] ) {
148
						continue;
149
					}
150
					$options[ $donor['info']['user_id'] ] = esc_html( '#' . $donor['info']['customer_id'] . ' - ' . $donor['info']['email'] );
151
				}
152
			}
153
154
			// Start constructing HTML output.
155
			$email_preview_header = '<div style="margin:0;padding:10px 0;width:100%;background-color:#FFF;border-bottom:1px solid #eee; text-align:center;">';
156
157
			// Inline JS function for switching donations.
158
			$request_url = $_SERVER['REQUEST_URI'];
159
160
			// Remove payment id query param if set from request url.
161
			if ( $user_id ) {
162
				$request_url_data = wp_parse_url( $_SERVER['REQUEST_URI'] );
163
				$query            = $request_url_data['query'];
164
				$query            = str_replace( "&user_id={$user_id}", '', $query );
165
166
				$request_url = home_url( '/?' . str_replace( '', '', $query ) );
167
			}
168
169
			$email_preview_header .= '<script>
170
				 function change_preview(){
171
				  var transactions = document.getElementById("give_preview_email_user_id");
172
			        var selected_trans = transactions.options[transactions.selectedIndex];
173
				        if (selected_trans){
174
				            var url_string = "' . $request_url . '&user_id=" + selected_trans.value;
175
				                window.location = url_string;
176
				        }
177
				    }
178
			    </script>';
179
180
			$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>';
181
182
			// The select field with 100 latest transactions
183
			$email_preview_header .= Give()->html->select( array(
184
				'name'             => 'preview_email_user_id',
185
				'selected'         => $user_id,
186
				'id'               => 'give_preview_email_user_id',
187
				'class'            => 'give-preview-email-donor-id',
188
				'options'          => $options,
189
				'chosen'           => false,
190
				'select_atts'      => 'onchange="change_preview()"',
191
				'show_option_all'  => false,
192
				'show_option_none' => false,
193
			) );
194
195
			// Closing tag
196
			$email_preview_header .= '</div>';
197
198
			echo $email_preview_header;
199
		}
200
	}
201
202
endif; // End class_exists check
203
204
return Give_Donor_Register_Email::get_instance();
205