user-change.php
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 2.3.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @var array $VARS
*
* @var Freemius $fs
*/
$fs = freemius( $VARS['id'] );
$slug = $fs->get_slug();
/**
* @var object[] $license_owners
*/
$license_owners = $VARS['license_owners'];
$change_user_message = fs_text_inline( 'By changing the user, you agree to transfer the account ownership to:', 'change-user--message', $slug );
$header_title = fs_text_inline( 'Change User', 'change-user', $slug );
$user_change_button_text = fs_text_inline( 'I Agree - Change User', 'agree-change-user', $slug );
$other_text = fs_text_inline( 'Other', 'other', $slug );
$enter_email_address_placeholder_text = fs_text_inline( 'Enter email address', 'enter-email-address', $slug );
$user_change_options_html = <<< HTML
<div class="fs-user-change-options-container">
<table>
<tbody>
HTML;
foreach ( $license_owners as $license_owner ) {
$user_change_options_html .= <<< HTML
<tr class="fs-email-address-container">
<td><input id="fs_email_address_{$license_owner->id}" type="radio" name="fs_email_address" value="{$license_owner->id}"></td>
<td><label for="fs_email_address_{$license_owner->id}">{$license_owner->email}</label></td>
</tr>
HTML;
}
$user_change_options_html .= <<< HTML
<tr>
<td><input id="fs_other_email_address_radio" type="radio" name="fs_email_address" value="other"></td>
<td class="fs-other-email-address-container">
<div>
<label for="fs_email_address">{$other_text}: </label>
<div>
<input id="fs_other_email_address_text_field" class="fs-email-address" type="text" placeholder="{$enter_email_address_placeholder_text}" tabindex="1">
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
HTML;
$modal_content_html = <<< HTML
<div class="notice notice-error inline fs-change-user-result-message"><p></p></div>
<p>{$change_user_message}</p>
{$user_change_options_html}
HTML;
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' );
?>
<script type="text/javascript">
(function( $ ) {
$( document ).ready(function() {
var modalContentHtml = <?php echo json_encode( $modal_content_html ) ?>,
modalHtml =
'<div class="fs-modal fs-modal-change-user fs-modal-change-user-<?php echo $fs->get_unique_affix() ?>">'
+ ' <div class="fs-modal-dialog">'
+ ' <div class="fs-modal-header">'
+ ' <h4><?php echo esc_js( $header_title ) ?></h4>'
+ ' <a href="!#" class="fs-close"><i class="dashicons dashicons-no" title="<?php echo esc_js( fs_text_x_inline( 'Dismiss', 'close window', 'dismiss', $slug ) ) ?>"></i></a>'
+ ' </div>'
+ ' <div class="fs-modal-body">'
+ ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>'
+ ' </div>'
+ ' <div class="fs-modal-footer">'
+ ' <button class="button button-secondary button-close" tabindex="4"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel', $slug ) ?></button>'
+ ' <button class="button button-primary fs-user-change-button" tabindex="3"><?php echo esc_js( $user_change_button_text ) ?></button>'
+ ' </div>'
+ ' </div>'
+ '</div>',
$modal = $( modalHtml ),
$userChangeButton = $modal.find( '.fs-user-change-button' ),
$otherEmailAddressRadio = $modal.find( '#fs_other_email_address_radio' ),
$changeUserResultMessage = $modal.find( '.fs-change-user-result-message' ),
$otherEmailAddressContainer = $modal.find( '.fs-other-email-address-container' ),
$otherEmailAddressTextField = $modal.find( '#fs_other_email_address_text_field' ),
$licenseOwners = $modal.find( 'input[type="radio"][name="fs_email_address"]' );
$modal.appendTo( $( 'body' ) );
var previousEmailAddress = null;
function registerEventHandlers() {
$licenseOwners.change( function() {
var otherEmailAddress = $otherEmailAddressTextField.val().trim(),
otherEmailAddressIsSelected = isOtherEmailAddressSelected();
if ( otherEmailAddressIsSelected ) {
$otherEmailAddressTextField.focus();
}
if ( otherEmailAddress.length > 0 || ! otherEmailAddressIsSelected ) {
enableUserChangeButton();
} else {
disableUserChangeButton();
}
} );
$otherEmailAddressContainer.click( function () {
$otherEmailAddressRadio.click();
} );
// Handle for the "Change User" button on the "Account" page.
$( '#fs_change_user' ).click( function ( evt ) {
evt.preventDefault();
showModal( evt );
} );
/**
* Disables the "Change User" button when the email address is empty.
*/
$modal.on( 'keyup paste delete cut', 'input#fs_other_email_address_text_field', function () {
setTimeout( function () {
var emailAddress = $otherEmailAddressRadio.val().trim();
if ( emailAddress === previousEmailAddress ) {
return;
}
if ( '' === emailAddress ) {
disableUserChangeButton();
} else {
enableUserChangeButton();
}
previousEmailAddress = emailAddress;
}, 100 );
} ).focus();
$modal.on( 'input propertychange', 'input#fs_other_email_address_text_field', function () {
var emailAddress = $( this ).val().trim();
/**
* If email address is not empty, enable the "Change User" button.
*/
if ( emailAddress.length > 0 ) {
enableUserChangeButton();
}
} );
$modal.on( 'blur', 'input#fs_other_email_address_text_field', function( evt ) {
var emailAddress = $( this ).val().trim();
/**
* If email address is empty, disable the "Change User" button.
*/
if ( 0 === emailAddress.length ) {
disableUserChangeButton();
}
} );
$modal.on( 'click', '.fs-user-change-button', function ( evt ) {
evt.preventDefault();
if ( $( this ).hasClass( 'disabled' ) ) {
return;
}
var emailAddress = '',
licenseOwnerID = null;
if ( ! isOtherEmailAddressSelected() ) {
licenseOwnerID = $licenseOwners.filter( ':checked' ).val();
} else {
emailAddress = $otherEmailAddressTextField.val().trim();
if ( 0 === emailAddress.length ) {
return;
}
}
disableUserChangeButton();
$.ajax( {
url : ajaxurl,
method : 'POST',
data : {
action : '<?php echo $fs->get_ajax_action( 'change_user' ) ?>',
security : '<?php echo $fs->get_ajax_security( 'change_user' ) ?>',
email_address: emailAddress,
user_id : licenseOwnerID,
module_id : '<?php echo $fs->get_id() ?>'
},
beforeSend: function () {
$userChangeButton
.text( '<?php fs_esc_js_echo_inline( 'Changing user, please wait', 'changing-user-please-wait', $slug ) ?>...' )
.prepend('<i class="fs-ajax-spinner"></i>');
$(document.body).css({'cursor': 'wait'});
},
success : function( result ) {
if ( result.success ) {
// Redirect to the "Account" page.
window.location.reload();
} else {
$(document.body).css({'cursor': 'auto'});
showError( result.error.message ? result.error.message : result.error );
resetUserChangeButton();
}
},
error : function () {
$(document.body).css({'cursor': 'auto'});
showError( '<?php fs_esc_js_echo_inline( 'Unexpected error, try again in 5 minutes. If the error persists, please contact support.', 'unexpected-error', $slug ) ?>' );
resetUserChangeButton();
}
} );
} );
// If the user has clicked outside the window, close the modal.
$modal.on( 'click', '.fs-close, .button-secondary', function () {
closeModal();
return false;
} );
}
registerEventHandlers();
/**
* @returns {Boolean}
*/
function isOtherEmailAddressSelected() {
return ( 'other' === $licenseOwners.filter( ':checked' ).val() );
}
function showModal() {
resetModal();
// Display the dialog box.
$modal.addClass( 'active' );
$( 'body' ).addClass( 'has-fs-modal' );
// Select the first radio button.
$licenseOwners.get( 0 ).click();
$otherEmailAddressTextField.val( '' );
}
function closeModal() {
$modal.removeClass( 'active' );
$( 'body' ).removeClass( 'has-fs-modal' );
}
function resetUserChangeButton() {
enableUserChangeButton();
$userChangeButton.text( <?php echo json_encode( $user_change_button_text ) ?> );
}
function resetModal() {
hideError();
resetUserChangeButton();
}
function enableUserChangeButton() {
$userChangeButton.removeClass( 'disabled' );
}
function disableUserChangeButton() {
$userChangeButton.addClass( 'disabled' );
}
function hideError() {
$changeUserResultMessage.hide();
}
function showError( msg ) {
$changeUserResultMessage.find( ' > p' ).html( msg );
$changeUserResultMessage.show();
}
});
})( jQuery );
</script>