| 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 |
* @since 1.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @var array $VARS |
| 15 |
* @var Freemius $fs |
| 16 |
*/ |
| 17 |
$fs = freemius( $VARS['id'] ); |
| 18 |
|
| 19 |
$slug = $fs->get_slug(); |
| 20 |
|
| 21 |
$send_button_text = fs_text_inline( 'Send License Key', 'send-license-key', $slug ); |
| 22 |
$cancel_button_text = fs_text_inline( 'Cancel', 'cancel', $slug ); |
| 23 |
$email_address_placeholder = fs_esc_attr_inline( 'Email address', 'email-address', $slug ); |
| 24 |
$other_text = fs_text_inline( 'Other', 'other', $slug ); |
| 25 |
|
| 26 |
$is_freemium = $fs->is_freemium(); |
| 27 |
|
| 28 |
$send_button_text_html = esc_html($send_button_text); |
| 29 |
|
| 30 |
$button_html = <<< HTML |
| 31 |
<div class="button-container"> |
| 32 |
<a href="#" class="button button-primary button-send-license-key" tabindex="2">{$send_button_text_html}</a> |
| 33 |
</div> |
| 34 |
HTML; |
| 35 |
|
| 36 |
if ( $is_freemium ) { |
| 37 |
$current_user = Freemius::_get_current_wp_user(); |
| 38 |
$email = $current_user->user_email; |
| 39 |
$esc_email = esc_attr( $email ); |
| 40 |
$form_html = <<< HTML |
| 41 |
<div class="email-address-container"> |
| 42 |
<label><input name="email-address" type="radio" checked="checked" tabindex="1" value="{$esc_email}"> {$email}</label> |
| 43 |
<label><input name="email-address" type="radio" tabindex="1" value="other">{$other_text}: <input class="email-address" type="text" placeholder="{$email_address_placeholder}"></label> |
| 44 |
</div> |
| 45 |
{$button_html} |
| 46 |
HTML; |
| 47 |
} else { |
| 48 |
$email = ''; |
| 49 |
$form_html = <<< HTML |
| 50 |
{$button_html} |
| 51 |
<div class="email-address-container"> |
| 52 |
<input class="email-address" type="text" placeholder="{$email_address_placeholder}" tabindex="1"> |
| 53 |
</div> |
| 54 |
HTML; |
| 55 |
} |
| 56 |
|
| 57 |
$message_above_input_field = $fs->is_only_premium() ? |
| 58 |
fs_esc_html_inline( "Enter the email address you've used during the purchase and we will resend you the license key.", 'ask-for-upgrade-email-address-premium-only', $slug ) : |
| 59 |
fs_esc_html_inline( "Enter the email address you've used for the upgrade below and we will resend you the license key.", 'ask-for-upgrade-email-address', $slug ); |
| 60 |
|
| 61 |
$modal_content_html = <<< HTML |
| 62 |
<div class="notice notice-error inline license-resend-message"><p></p></div> |
| 63 |
<p>{$message_above_input_field}</p> |
| 64 |
<div class="input-container"> |
| 65 |
{$form_html} |
| 66 |
</div> |
| 67 |
HTML; |
| 68 |
|
| 69 |
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
| 70 |
?> |
| 71 |
<script type="text/javascript"> |
| 72 |
(function ($) { |
| 73 |
$(document).ready(function () { |
| 74 |
var contentHtml = <?php echo json_encode( $modal_content_html ); ?>, |
| 75 |
modalHtml = |
| 76 |
'<div class="fs-modal fs-modal-license-key-resend <?php echo $is_freemium ? 'fs-freemium' : 'fs-premium' ?>">' |
| 77 |
+ ' <div class="fs-modal-dialog">' |
| 78 |
+ ' <div class="fs-modal-header">' |
| 79 |
+ ' <h4><?php echo esc_js( $send_button_text ) ?></h4>' |
| 80 |
+ ' <a href="#!" class="fs-close" tabindex="3" title="Close"><i class="dashicons dashicons-no" title="<?php echo esc_js( fs_text_x_inline( 'Dismiss', 'as close a window', 'dismiss', $slug ) ) ?>"></i></a>' |
| 81 |
+ ' </div>' |
| 82 |
+ ' <div class="fs-modal-body">' |
| 83 |
+ ' <div class="fs-modal-panel active">' + contentHtml + '</div>' |
| 84 |
+ ' </div>' |
| 85 |
+ ' </div>' |
| 86 |
+ '</div>', |
| 87 |
$modal = $(modalHtml), |
| 88 |
$sendButton = $modal.find('.button-send-license-key'), |
| 89 |
$emailInput = $modal.find('input.email-address'), |
| 90 |
$feedbackMessage = $modal.find('.license-resend-message'), |
| 91 |
isFreemium = <?php echo json_encode( $is_freemium ) ?>, |
| 92 |
userEmail = <?php echo json_encode( $email ) ?>, |
| 93 |
moduleID = '<?php echo $fs->get_id() ?>', |
| 94 |
isChild = false; |
| 95 |
|
| 96 |
|
| 97 |
$modal.appendTo($('body')); |
| 98 |
|
| 99 |
function registerEventHandlers() { |
| 100 |
$('a.show-license-resend-modal-<?php echo $fs->get_unique_affix() ?>').click(function (evt) { |
| 101 |
evt.preventDefault(); |
| 102 |
|
| 103 |
showModal(); |
| 104 |
}); |
| 105 |
|
| 106 |
if (isFreemium) { |
| 107 |
$modal.on('change', 'input[type=radio][name=email-address]', function () { |
| 108 |
updateButtonState(); |
| 109 |
}); |
| 110 |
|
| 111 |
$modal.on('focus', 'input.email-address', function () { |
| 112 |
// Check custom email radio button on email input focus. |
| 113 |
$($modal.find('input[type=radio]')[1]).prop('checked', true); |
| 114 |
|
| 115 |
updateButtonState(); |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
$modal.on('input propertychange', 'input.email-address', function () { |
| 120 |
updateButtonState(); |
| 121 |
}); |
| 122 |
|
| 123 |
$modal.on('blur', 'input.email-address', function () { |
| 124 |
updateButtonState(); |
| 125 |
}); |
| 126 |
|
| 127 |
$modal.on('click', '.fs-close', function (){ |
| 128 |
closeModal(); |
| 129 |
return false; |
| 130 |
}); |
| 131 |
|
| 132 |
$modal.on('click', '.button', function (evt) { |
| 133 |
evt.preventDefault(); |
| 134 |
|
| 135 |
if ($(this).hasClass('disabled')) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
var email = getEmail(); |
| 140 |
|
| 141 |
disableButton(); |
| 142 |
|
| 143 |
if (!(-1 < email.indexOf('@'))) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
$.ajax({ |
| 148 |
url : <?php echo Freemius::ajax_url() ?>, |
| 149 |
method : 'POST', |
| 150 |
data : { |
| 151 |
action : '<?php echo $fs->get_ajax_action( 'resend_license_key' ) ?>', |
| 152 |
security : '<?php echo $fs->get_ajax_security( 'resend_license_key' ) ?>', |
| 153 |
module_id : moduleID, |
| 154 |
email : email |
| 155 |
}, |
| 156 |
beforeSend: function () { |
| 157 |
$sendButton.text('<?php fs_esc_js_echo_inline( 'Sending license key', 'sending-license-key', $slug ) ?>...'); |
| 158 |
}, |
| 159 |
success : function (result) { |
| 160 |
var resultObj = $.parseJSON(result); |
| 161 |
if (resultObj.success) { |
| 162 |
closeModal(); |
| 163 |
} else { |
| 164 |
showError(resultObj.error); |
| 165 |
resetButton(); |
| 166 |
} |
| 167 |
} |
| 168 |
}); |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
registerEventHandlers(); |
| 173 |
|
| 174 |
resetButton(); |
| 175 |
|
| 176 |
function showModal() { |
| 177 |
resetModal(); |
| 178 |
|
| 179 |
// Display the dialog box. |
| 180 |
$modal.addClass('active'); |
| 181 |
|
| 182 |
if (!isFreemium) |
| 183 |
$emailInput.focus(); |
| 184 |
|
| 185 |
var $body = $('body'); |
| 186 |
|
| 187 |
isChild = $body.hasClass('has-fs-modal'); |
| 188 |
if (isChild) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$body.addClass('has-fs-modal'); |
| 193 |
} |
| 194 |
|
| 195 |
function closeModal() { |
| 196 |
$modal.removeClass('active'); |
| 197 |
|
| 198 |
// If child modal, do not remove the "has-fs-modal" class of the <body> element to keep its scrollbars hidden. |
| 199 |
if (isChild) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
$('body').removeClass('has-fs-modal'); |
| 204 |
} |
| 205 |
|
| 206 |
function resetButton() { |
| 207 |
updateButtonState(); |
| 208 |
$sendButton.text(<?php echo json_encode($send_button_text) ?>); |
| 209 |
} |
| 210 |
|
| 211 |
function resetModal() { |
| 212 |
hideError(); |
| 213 |
resetButton(); |
| 214 |
$emailInput.val(''); |
| 215 |
} |
| 216 |
|
| 217 |
function getEmail() { |
| 218 |
var email = $emailInput.val().trim(); |
| 219 |
|
| 220 |
if (isFreemium) { |
| 221 |
if ('other' != $modal.find('input[type=radio][name=email-address]:checked').val()) { |
| 222 |
email = userEmail; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return email; |
| 227 |
} |
| 228 |
|
| 229 |
function updateButtonState() { |
| 230 |
/** |
| 231 |
* If email address is not empty, enable the send license key button. |
| 232 |
*/ |
| 233 |
$sendButton.toggleClass('disabled', !( -1 < getEmail().indexOf('@') )); |
| 234 |
} |
| 235 |
|
| 236 |
function disableButton() { |
| 237 |
$sendButton.addClass('disabled'); |
| 238 |
} |
| 239 |
|
| 240 |
function hideError() { |
| 241 |
$feedbackMessage.hide(); |
| 242 |
} |
| 243 |
|
| 244 |
function showError(msg) { |
| 245 |
$feedbackMessage.find(' > p').html(msg); |
| 246 |
$feedbackMessage.show(); |
| 247 |
} |
| 248 |
}); |
| 249 |
})(jQuery); |
| 250 |
</script> |
| 251 |
|