| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 6 |
* @since 1.1.9 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @var array $VARS |
| 15 |
*/ |
| 16 |
$slug = $VARS['slug']; |
| 17 |
$fs = freemius( $slug ); |
| 18 |
|
| 19 |
$cant_find_license_key_text = fs_text( 'cant-find-license-key', $slug ); |
| 20 |
$message_above_input_field = fs_text( 'activate-license-message', $slug ); |
| 21 |
$message_below_input_field = ''; |
| 22 |
|
| 23 |
$header_title = fs_text( $fs->is_free_plan() ? 'activate-license' : 'update-license', $slug ); |
| 24 |
|
| 25 |
if ( $fs->is_registered() ) { |
| 26 |
$activate_button_text = $header_title; |
| 27 |
} else { |
| 28 |
$freemius_site_url = $fs->has_paid_plan() ? |
| 29 |
'https://freemius.com/wordpress/' : |
| 30 |
// Insights platform information. |
| 31 |
'https://freemius.com/wordpress/usage-tracking/'; |
| 32 |
|
| 33 |
$freemius_link = '<a href="' . $freemius_site_url . '" target="_blank" tabindex="0">freemius.com</a>'; |
| 34 |
|
| 35 |
$message_below_input_field = sprintf( fs_text( 'license-sync-disclaimer', $slug ), $freemius_link ); |
| 36 |
|
| 37 |
$activate_button_text = fs_text( 'agree-activate-license', $slug ); |
| 38 |
} |
| 39 |
|
| 40 |
$license_key_text = fs_text( 'license-key' , $slug ); |
| 41 |
|
| 42 |
/** |
| 43 |
* IMPORTANT: |
| 44 |
* DO NOT ADD MAXLENGTH OR LIMIT THE LICENSE KEY LENGTH SINCE |
| 45 |
* WE DO WANT TO ALLOW INPUT OF LONGER KEYS (E.G. WooCommerce Keys) |
| 46 |
* FOR MIGRATED MODULES. |
| 47 |
*/ |
| 48 |
$modal_content_html = <<< HTML |
| 49 |
<div class="notice notice-error inline license-activation-message"><p></p></div> |
| 50 |
<p>{$message_above_input_field}</p> |
| 51 |
<input class="license_key" type="text" placeholder="{$license_key_text}" tabindex="1" /> |
| 52 |
<a class="show-license-resend-modal show-license-resend-modal-{$slug}" href="!#" tabindex="2">{$cant_find_license_key_text}</a> |
| 53 |
<p>{$message_below_input_field}</p> |
| 54 |
HTML; |
| 55 |
|
| 56 |
fs_enqueue_local_style( 'dialog-boxes', '/admin/dialog-boxes.css' ); |
| 57 |
?> |
| 58 |
<script type="text/javascript"> |
| 59 |
(function( $ ) { |
| 60 |
$( document ).ready(function() { |
| 61 |
var modalContentHtml = <?php echo json_encode($modal_content_html); ?>, |
| 62 |
modalHtml = |
| 63 |
'<div class="fs-modal fs-modal-license-activation">' |
| 64 |
+ ' <div class="fs-modal-dialog">' |
| 65 |
+ ' <div class="fs-modal-header">' |
| 66 |
+ ' <h4><?php echo esc_js($header_title) ?></h4>' |
| 67 |
+ ' <a href="!#" class="fs-close"><i class="dashicons dashicons-no" title="<?php fs_esc_attr_echo( 'dismiss', $slug ) ?>"></i></a>' |
| 68 |
+ ' </div>' |
| 69 |
+ ' <div class="fs-modal-body">' |
| 70 |
+ ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>' |
| 71 |
+ ' </div>' |
| 72 |
+ ' <div class="fs-modal-footer">' |
| 73 |
+ ' <button class="button button-secondary button-close" tabindex="4"><?php fs_esc_js_echo( 'cancel', $slug ) ?></button>' |
| 74 |
+ ' <button class="button button-primary button-activate-license" tabindex="3"><?php echo esc_js( $activate_button_text ) ?></button>' |
| 75 |
+ ' </div>' |
| 76 |
+ ' </div>' |
| 77 |
+ '</div>', |
| 78 |
$modal = $(modalHtml), |
| 79 |
$activateLicenseLink = $('span.activate-license.<?php echo $VARS['slug'] ?> a, .activate-license-trigger.<?php echo $VARS['slug'] ?>'), |
| 80 |
$activateLicenseButton = $modal.find('.button-activate-license'), |
| 81 |
$licenseKeyInput = $modal.find('input.license_key'), |
| 82 |
$licenseActivationMessage = $modal.find( '.license-activation-message' ), |
| 83 |
pluginSlug = '<?php echo $slug ?>'; |
| 84 |
|
| 85 |
$modal.appendTo($('body')); |
| 86 |
|
| 87 |
function registerEventHandlers() { |
| 88 |
$activateLicenseLink.click(function (evt) { |
| 89 |
evt.preventDefault(); |
| 90 |
|
| 91 |
showModal(); |
| 92 |
}); |
| 93 |
|
| 94 |
$modal.on('input propertychange', 'input.license_key', function () { |
| 95 |
|
| 96 |
var licenseKey = $(this).val().trim(); |
| 97 |
|
| 98 |
/** |
| 99 |
* If license key is not empty, enable the license activation button. |
| 100 |
*/ |
| 101 |
if (licenseKey.length > 0) { |
| 102 |
enableActivateLicenseButton(); |
| 103 |
} |
| 104 |
}); |
| 105 |
|
| 106 |
$modal.on('blur', 'input.license_key', function () { |
| 107 |
var licenseKey = $(this).val().trim(); |
| 108 |
|
| 109 |
/** |
| 110 |
* If license key is empty, disable the license activation button. |
| 111 |
*/ |
| 112 |
if (0 === licenseKey.length) { |
| 113 |
disableActivateLicenseButton(); |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
$modal.on('click', '.button-activate-license', function (evt) { |
| 118 |
evt.preventDefault(); |
| 119 |
|
| 120 |
if ($(this).hasClass('disabled')) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
var licenseKey = $licenseKeyInput.val().trim(); |
| 125 |
|
| 126 |
disableActivateLicenseButton(); |
| 127 |
|
| 128 |
if (0 === licenseKey.length) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$.ajax({ |
| 133 |
url: ajaxurl, |
| 134 |
method: 'POST', |
| 135 |
data: { |
| 136 |
action : '<?php echo $fs->get_ajax_action( 'activate_license' ) ?>', |
| 137 |
security : '<?php echo $fs->get_ajax_security( 'activate_license' ) ?>', |
| 138 |
slug : pluginSlug, |
| 139 |
license_key: licenseKey |
| 140 |
}, |
| 141 |
beforeSend: function () { |
| 142 |
$activateLicenseButton.text( <?php fs_json_encode_echo( 'activating-license', $slug ) ?> ); |
| 143 |
}, |
| 144 |
success: function( result ) { |
| 145 |
var resultObj = $.parseJSON( result ); |
| 146 |
if ( resultObj.success ) { |
| 147 |
closeModal(); |
| 148 |
|
| 149 |
// Redirect to the "Account" page and sync the license. |
| 150 |
window.location.href = resultObj.next_page; |
| 151 |
} else { |
| 152 |
showError( resultObj.error.message ? resultObj.error.message : resultObj.error ); |
| 153 |
resetActivateLicenseButton(); |
| 154 |
} |
| 155 |
} |
| 156 |
}); |
| 157 |
}); |
| 158 |
|
| 159 |
// If the user has clicked outside the window, close the modal. |
| 160 |
$modal.on('click', '.fs-close, .button-secondary', function () { |
| 161 |
closeModal(); |
| 162 |
return false; |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
registerEventHandlers(); |
| 167 |
|
| 168 |
function showModal() { |
| 169 |
resetModal(); |
| 170 |
|
| 171 |
// Display the dialog box. |
| 172 |
$modal.addClass('active'); |
| 173 |
$('body').addClass('has-fs-modal'); |
| 174 |
|
| 175 |
$licenseKeyInput.focus(); |
| 176 |
} |
| 177 |
|
| 178 |
function closeModal() { |
| 179 |
$modal.removeClass('active'); |
| 180 |
$('body').removeClass('has-fs-modal'); |
| 181 |
} |
| 182 |
|
| 183 |
function resetActivateLicenseButton() { |
| 184 |
enableActivateLicenseButton(); |
| 185 |
$activateLicenseButton.text( <?php echo json_encode( $activate_button_text ) ?> ); |
| 186 |
} |
| 187 |
|
| 188 |
function resetModal() { |
| 189 |
hideError(); |
| 190 |
resetActivateLicenseButton(); |
| 191 |
$licenseKeyInput.val( '' ); |
| 192 |
} |
| 193 |
|
| 194 |
function enableActivateLicenseButton() { |
| 195 |
$activateLicenseButton.removeClass( 'disabled' ); |
| 196 |
} |
| 197 |
|
| 198 |
function disableActivateLicenseButton() { |
| 199 |
$activateLicenseButton.addClass( 'disabled' ); |
| 200 |
} |
| 201 |
|
| 202 |
function hideError() { |
| 203 |
$licenseActivationMessage.hide(); |
| 204 |
} |
| 205 |
|
| 206 |
function showError( msg ) { |
| 207 |
$licenseActivationMessage.find( ' > p' ).html( msg ); |
| 208 |
$licenseActivationMessage.show(); |
| 209 |
} |
| 210 |
}); |
| 211 |
})( jQuery ); |
| 212 |
</script> |
| 213 |
|