| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* |
| 7 |
* @author Leo Fajardo (@leorw) |
| 8 |
* @since 2.5.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @var array $VARS |
| 17 |
*/ |
| 18 |
$fs = freemius( $VARS['id'] ); |
| 19 |
$slug = $fs->get_slug(); |
| 20 |
|
| 21 |
$user = $fs->get_user(); |
| 22 |
$current_email_address = $user->email; |
| 23 |
|
| 24 |
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
| 25 |
?> |
| 26 |
<script type="text/javascript"> |
| 27 |
( function ( $ ) { |
| 28 |
var modalHtml = |
| 29 |
'<div class="fs-modal fs-modal-email-address-update">' |
| 30 |
+ ' <div class="fs-modal-dialog">' |
| 31 |
+ ' <div class="fs-modal-header">' |
| 32 |
+ ' <h4><?php fs_esc_attr_echo_inline( 'Email address update', 'email-address-update', $slug ) ?></h4>' |
| 33 |
+ ' <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>' |
| 34 |
+ ' </div>' |
| 35 |
+ ' <div class="fs-modal-body">' |
| 36 |
+ ' <div class="fs-modal-panel active">' |
| 37 |
+ ' <div class="notice notice-error inline fs-update-error-message"><p></p></div>' |
| 38 |
+ ' <p><?php fs_echo_inline( 'Enter the new email address', 'enter-new-email-address' , $slug ) ?>:</p>' |
| 39 |
+ ' <input type="text" class="fs-new-email-address-input" />' |
| 40 |
+ ' <div class="fs-email-addresses-ownership-options" style="display: none">' |
| 41 |
+ ' <p><?php echo sprintf( |
| 42 |
fs_esc_html_inline( 'Are both %s and %s your email addresses?', 'email-addresses-ownership-confirmation' , $slug ), |
| 43 |
sprintf( '<strong>%s</strong>', $current_email_address ), |
| 44 |
'<strong class="fs-new-email-address"></strong>' |
| 45 |
) ?></p>' |
| 46 |
+ ' <ul>' |
| 47 |
+ ' <li>' |
| 48 |
+ ' <label>' |
| 49 |
+ ' <span><input type="radio" name="email-addresses-ownership" value="both"/></span>' |
| 50 |
+ ' <span><?php fs_echo_inline( 'Yes - both addresses are mine', 'both-addresses-are-mine', $slug ); ?></span>' |
| 51 |
+ ' </label>' |
| 52 |
+ ' </li>' |
| 53 |
+ ' <li>' |
| 54 |
+ ' <label>' |
| 55 |
+ ' <span><input type="radio" name="email-addresses-ownership" value="current"/></span>' |
| 56 |
+ ' <div><?php echo sprintf( |
| 57 |
fs_esc_html_inline( "%s is my client's email address", 'client-email-address-confirmation', $slug ), |
| 58 |
'<strong class="fs-new-email-address"></strong>' |
| 59 |
) ?></span>' |
| 60 |
+ ' </label>' |
| 61 |
+ ' </li>' |
| 62 |
+ ' <li>' |
| 63 |
+ ' <label>' |
| 64 |
+ ' <span><input type="radio" name="email-addresses-ownership" value="new"/></span>' |
| 65 |
+ ' <div><?php echo sprintf( |
| 66 |
fs_esc_html_inline( "%s is my email address", 'email-address-ownership-confirmation', $slug ), |
| 67 |
'<strong class="fs-new-email-address"></strong>' |
| 68 |
) ?></span>' |
| 69 |
+ ' </label>' |
| 70 |
+ ' </li>' |
| 71 |
+ ' </ul>' |
| 72 |
+ ' </div>' |
| 73 |
+ ' <div class="fs-assets-transfership-options" style="display: none">' |
| 74 |
+ ' <p><?php echo sprintf( |
| 75 |
fs_esc_html_inline( 'Would you like to merge %s into %s?', 'accounts-merge-confirmation' , $slug ), |
| 76 |
sprintf( '<strong>%s</strong>', $current_email_address ), |
| 77 |
'<strong class="fs-new-email-address"></strong>' |
| 78 |
) ?></p>' |
| 79 |
+ ' <ul>' |
| 80 |
+ ' <li>' |
| 81 |
+ ' <label>' |
| 82 |
+ ' <span><input type="radio" name="assets-transfer-type" value="all" /></span>' |
| 83 |
+ ' <span><?php echo sprintf( |
| 84 |
fs_esc_html_inline( 'Yes - move all my data and assets from %s to %s', 'move-all-data-and-assets-into-new-account', $slug ), |
| 85 |
sprintf( '<strong>%s</strong>', $current_email_address ), |
| 86 |
'<strong class="fs-new-email-address"></strong>' |
| 87 |
) ?></span>' |
| 88 |
+ ' </label>' |
| 89 |
+ ' </li>' |
| 90 |
+ ' <li>' |
| 91 |
+ ' <label>' |
| 92 |
+ ' <span><input type="radio" name="assets-transfer-type" value="plugin" /></span>' |
| 93 |
+ ' <span><?php echo sprintf( |
| 94 |
fs_esc_html_inline( "No - only move this site's data to %s", 'move-only-plugin-data-into-new-account', $slug ), |
| 95 |
'<strong class="fs-new-email-address"></strong>' |
| 96 |
) ?></span>' |
| 97 |
+ ' </label>' |
| 98 |
+ ' </li>' |
| 99 |
+ ' </ul>' |
| 100 |
+ ' </div>' |
| 101 |
+ ' </div>' |
| 102 |
+ ' </div>' |
| 103 |
+ ' <div class="fs-modal-footer">' |
| 104 |
+ ' <button class="button button-primary button-update" disabled><?php fs_esc_js_echo_inline( 'Update', 'update-email-address', $slug ) ?></button>' |
| 105 |
+ ' <button class="button button-secondary button-close"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel', $slug ) ?></button>' |
| 106 |
+ ' </div>' |
| 107 |
+ ' </div>' |
| 108 |
+ '</div>', |
| 109 |
$modal = $( modalHtml ), |
| 110 |
$updateButton = $modal.find( '.button-update' ), |
| 111 |
$updateResultMessage = $modal.find( '.fs-update-error-message' ), |
| 112 |
selectedEmailAddressesOwnershipOption = null, |
| 113 |
selectedAssetsTransfershipOption = null, |
| 114 |
previousEmailAddress = '', |
| 115 |
$body = $( 'body' ); |
| 116 |
|
| 117 |
$modal.appendTo( $body ); |
| 118 |
|
| 119 |
registerEventHandlers(); |
| 120 |
|
| 121 |
function registerEventHandlers() { |
| 122 |
$body.on( 'click', '#fs_account_details .button-edit-email-address', function ( evt ) { |
| 123 |
evt.preventDefault(); |
| 124 |
|
| 125 |
showModal( evt ); |
| 126 |
} ); |
| 127 |
|
| 128 |
$modal.on( 'input propertychange keyup paste delete cut', '.fs-new-email-address-input', function () { |
| 129 |
var emailAddress = $( this ).val().trim(); |
| 130 |
|
| 131 |
if ( emailAddress === previousEmailAddress ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
var isValidEmailAddressInput = isValidEmailAddress( emailAddress ); |
| 136 |
|
| 137 |
toggleOptions( isValidEmailAddressInput ); |
| 138 |
|
| 139 |
if ( ! isValidEmailAddressInput ) { |
| 140 |
disableUpdateButton(); |
| 141 |
} else { |
| 142 |
$modal.find( '.fs-new-email-address').text( emailAddress ); |
| 143 |
|
| 144 |
maybeEnableUpdateButton(); |
| 145 |
} |
| 146 |
|
| 147 |
previousEmailAddress = emailAddress; |
| 148 |
} ); |
| 149 |
|
| 150 |
$modal.on( 'blur', '.fs-new-email-address-input', function() { |
| 151 |
var emailAddress = $( this ).val().trim(), |
| 152 |
isValidEmailAddressInput = isValidEmailAddress( emailAddress ); |
| 153 |
|
| 154 |
toggleOptions( isValidEmailAddressInput ); |
| 155 |
|
| 156 |
if ( ! isValidEmailAddressInput ) { |
| 157 |
disableUpdateButton(); |
| 158 |
} |
| 159 |
} ); |
| 160 |
|
| 161 |
$modal.on( 'click', '.fs-close, .button-secondary', function () { |
| 162 |
closeModal(); |
| 163 |
return false; |
| 164 |
} ); |
| 165 |
|
| 166 |
$modal.on( 'click', '.fs-modal-footer .button-update', function ( evt ) { |
| 167 |
if ( ! isValidEmailAddress( previousEmailAddress ) ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if ( previousEmailAddress === '<?php echo $current_email_address ?>' ) { |
| 172 |
closeModal(); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
var transferType = 'transfer'; |
| 177 |
|
| 178 |
if ( 'current' === selectedEmailAddressesOwnershipOption ) { |
| 179 |
transferType = 'transfer_to_client'; |
| 180 |
} else if ( |
| 181 |
'both' === selectedEmailAddressesOwnershipOption && |
| 182 |
'all' === selectedAssetsTransfershipOption |
| 183 |
) { |
| 184 |
transferType = 'merge'; |
| 185 |
} |
| 186 |
|
| 187 |
$.ajax( { |
| 188 |
url : <?php echo Freemius::ajax_url() ?>, |
| 189 |
method : 'POST', |
| 190 |
data : { |
| 191 |
action : '<?php echo $fs->get_ajax_action( 'update_email_address' ) ?>', |
| 192 |
security : '<?php echo $fs->get_ajax_security( 'update_email_address' ) ?>', |
| 193 |
module_id : '<?php echo $fs->get_id() ?>', |
| 194 |
transfer_type: transferType, |
| 195 |
email_address: previousEmailAddress |
| 196 |
}, |
| 197 |
beforeSend: function () { |
| 198 |
disableUpdateButton(); |
| 199 |
|
| 200 |
$updateButton.find( '.fs-modal-footer .button' ).prop( 'disabled', true ); |
| 201 |
$updateButton.text( 'Processing...' ); |
| 202 |
}, |
| 203 |
success : function( result ) { |
| 204 |
if ( result.success ) { |
| 205 |
// Redirect to the "Account" page. |
| 206 |
window.location.reload(); |
| 207 |
} else { |
| 208 |
if ('change_ownership' === result.error.code) { |
| 209 |
window.location = result.error.url; |
| 210 |
} else { |
| 211 |
showError(result.error.message ? result.error.message : result.error); |
| 212 |
resetUpdateButton(); |
| 213 |
} |
| 214 |
} |
| 215 |
}, |
| 216 |
error : function () { |
| 217 |
showError( '<?php fs_esc_js_echo_inline( 'Unexpected error, try again in 5 minutes. If the error persists, please contact support.', 'unexpected-error', $slug ) ?>' ); |
| 218 |
|
| 219 |
resetUpdateButton(); |
| 220 |
} |
| 221 |
} ); |
| 222 |
} ); |
| 223 |
|
| 224 |
$modal.on( 'click', 'input[type="radio"]', function () { |
| 225 |
var $selectedOption = $( this ), |
| 226 |
selectedOptionValue = $selectedOption.val(); |
| 227 |
|
| 228 |
// If the selection has not changed, do not proceed. |
| 229 |
if ( |
| 230 |
selectedEmailAddressesOwnershipOption === selectedOptionValue || |
| 231 |
selectedAssetsTransfershipOption === selectedOptionValue |
| 232 |
) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
if ( 'assets-transfer-type' === $selectedOption.attr( 'name' ) ) { |
| 237 |
selectedAssetsTransfershipOption = selectedOptionValue; |
| 238 |
} else { |
| 239 |
selectedEmailAddressesOwnershipOption = selectedOptionValue; |
| 240 |
|
| 241 |
if ( 'both' !== selectedEmailAddressesOwnershipOption ) { |
| 242 |
$modal.find( '.fs-assets-transfership-options' ).hide(); |
| 243 |
} else { |
| 244 |
$modal.find( '.fs-assets-transfership-options' ).show(); |
| 245 |
$modal.find( '.fs-assets-transfership-options input[type="radio"]' ).prop('checked', false); |
| 246 |
|
| 247 |
selectedAssetsTransfershipOption = null; |
| 248 |
|
| 249 |
disableUpdateButton(); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if ( isValidEmailAddress( $( '.fs-new-email-address-input' ).val().trim() ) ) { |
| 254 |
maybeEnableUpdateButton(); |
| 255 |
} |
| 256 |
}); |
| 257 |
} |
| 258 |
|
| 259 |
function showModal() { |
| 260 |
resetModal(); |
| 261 |
|
| 262 |
// Display the dialog box. |
| 263 |
$modal.addClass( 'active' ); |
| 264 |
$modal.find( '.fs-new-email-address-input' ).focus(); |
| 265 |
|
| 266 |
$( 'body' ).addClass( 'has-fs-modal' ); |
| 267 |
} |
| 268 |
|
| 269 |
function closeModal() { |
| 270 |
selectedEmailAddressesOwnershipOption = null; |
| 271 |
|
| 272 |
disableUpdateButton(); |
| 273 |
|
| 274 |
$modal.removeClass( 'active' ); |
| 275 |
|
| 276 |
$( 'body' ).removeClass( 'has-fs-modal' ); |
| 277 |
} |
| 278 |
|
| 279 |
function resetModal() { |
| 280 |
hideError(); |
| 281 |
|
| 282 |
// Deselect all radio buttons. |
| 283 |
$modal.find( 'input[type="radio"]' ).prop( 'checked', false ); |
| 284 |
|
| 285 |
// Clear the value of the email address text field. |
| 286 |
$modal.find( 'input[type="text"]' ).val( '' ); |
| 287 |
|
| 288 |
toggleOptions( false ); |
| 289 |
|
| 290 |
disableUpdateButton(); |
| 291 |
|
| 292 |
$updateButton.text( <?php echo json_encode( fs_text_inline( 'Update', 'update-email-address', $slug ) ) ?> ); |
| 293 |
} |
| 294 |
|
| 295 |
function resetUpdateButton() { |
| 296 |
maybeEnableUpdateButton(); |
| 297 |
|
| 298 |
$updateButton.text( <?php echo json_encode( fs_text_inline( 'Update', 'update-email-address', $slug ) ) ?> ); |
| 299 |
} |
| 300 |
|
| 301 |
function maybeEnableUpdateButton() { |
| 302 |
if ( null === selectedEmailAddressesOwnershipOption ) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
if ( |
| 307 |
'both' === selectedEmailAddressesOwnershipOption && |
| 308 |
null === selectedAssetsTransfershipOption |
| 309 |
) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
$updateButton.prop( 'disabled', false ); |
| 314 |
} |
| 315 |
|
| 316 |
function disableUpdateButton() { |
| 317 |
$updateButton.prop( 'disabled', true ); |
| 318 |
} |
| 319 |
|
| 320 |
function hideError() { |
| 321 |
$updateResultMessage.hide(); |
| 322 |
} |
| 323 |
|
| 324 |
function showError( msg ) { |
| 325 |
$updateResultMessage.find( ' > p' ).html( msg ); |
| 326 |
$updateResultMessage.show(); |
| 327 |
} |
| 328 |
|
| 329 |
function isValidEmailAddress( emailAddress ) { |
| 330 |
if ( '' === emailAddress ) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
return /[0-9a-zA-Z][a-zA-Z\+0-9\.\_\-]*@[0-9a-zA-Z\-]+(\.[a-zA-Z]{2,24}){1,3}/.test( emailAddress ); |
| 335 |
} |
| 336 |
|
| 337 |
function toggleOptions( show ) { |
| 338 |
$modal.find( '.fs-email-addresses-ownership-options' ).toggle( show ); |
| 339 |
|
| 340 |
if ( ! show ) { |
| 341 |
$modal.find( '.fs-assets-transfership-options' ).hide(); |
| 342 |
} else if ( 'both' === selectedEmailAddressesOwnershipOption ) { |
| 343 |
$modal.find( '.fs-assets-transfership-options' ).show(); |
| 344 |
} |
| 345 |
} |
| 346 |
} )( jQuery ); |
| 347 |
</script> |