| 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.1.9 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @var Freemius $fs |
| 15 |
*/ |
| 16 |
$fs = freemius( $VARS['id'] ); |
| 17 |
$slug = $fs->get_slug(); |
| 18 |
|
| 19 |
$cant_find_license_key_text = fs_text_inline( "Can't find your license key?", 'cant-find-license-key', $slug ); |
| 20 |
$message_above_input_field = fs_text_inline( 'Please enter the license key that you received in the email right after the purchase:', 'activate-license-message', $slug ); |
| 21 |
$message_below_input_field = ''; |
| 22 |
|
| 23 |
$header_title = $fs->is_free_plan() ? |
| 24 |
fs_text_inline( 'Activate License', 'activate-license', $slug ) : |
| 25 |
fs_text_inline( 'Update License', 'update-license', $slug ); |
| 26 |
|
| 27 |
if ( $fs->is_registered() ) { |
| 28 |
$activate_button_text = $header_title; |
| 29 |
} else { |
| 30 |
$freemius_site_url = $fs->has_paid_plan() ? |
| 31 |
'https://freemius.com/wordpress/' : |
| 32 |
// Insights platform information. |
| 33 |
'https://freemius.com/wordpress/usage-tracking/'; |
| 34 |
|
| 35 |
$freemius_link = '<a href="' . $freemius_site_url . '" target="_blank" tabindex="0">freemius.com</a>'; |
| 36 |
|
| 37 |
$message_below_input_field = sprintf( |
| 38 |
fs_text_inline( 'The %1$s will be periodically sending data to %2$s to check for security and feature updates, and verify the validity of your license.', 'license-sync-disclaimer', $slug ), |
| 39 |
$fs->get_module_label( true ), |
| 40 |
$freemius_link |
| 41 |
); |
| 42 |
|
| 43 |
$activate_button_text = fs_text_inline( 'Agree & Activate License', 'agree-activate-license', $slug ); |
| 44 |
} |
| 45 |
|
| 46 |
$license_key_text = fs_text_inline( 'License key', 'license-key' , $slug ); |
| 47 |
|
| 48 |
$is_network_activation = ( |
| 49 |
$fs->is_network_active() && |
| 50 |
fs_is_network_admin() && |
| 51 |
! $fs->is_delegated_connection() |
| 52 |
); |
| 53 |
$network_activation_html = ''; |
| 54 |
|
| 55 |
$sites_details = array(); |
| 56 |
if ( $is_network_activation ) { |
| 57 |
$all_sites = Freemius::get_sites(); |
| 58 |
|
| 59 |
foreach ( $all_sites as $site ) { |
| 60 |
$site_details = $fs->get_site_info( $site ); |
| 61 |
|
| 62 |
$blog_id = Freemius::get_site_blog_id( $site ); |
| 63 |
$install = $fs->get_install_by_blog_id($blog_id); |
| 64 |
|
| 65 |
if ( is_object( $install ) && FS_Plugin_License::is_valid_id( $install->license_id ) ) { |
| 66 |
$site_details['license_id'] = $install->license_id; |
| 67 |
} |
| 68 |
|
| 69 |
$sites_details[] = $site_details; |
| 70 |
} |
| 71 |
|
| 72 |
if ( $is_network_activation ) { |
| 73 |
$vars = array( |
| 74 |
'id' => $fs->get_id(), |
| 75 |
'sites' => $sites_details, |
| 76 |
'require_license_key' => true |
| 77 |
); |
| 78 |
|
| 79 |
$network_activation_html = fs_get_template( 'partials/network-activation.php', $vars ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$premium_licenses = $fs->get_available_premium_licenses(); |
| 84 |
$available_licenses = array(); |
| 85 |
foreach ( $premium_licenses as $premium_license ) { |
| 86 |
$activations_left = $premium_license->left(); |
| 87 |
if ( ! ( $activations_left > 0 ) ) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
$available_licenses[ $activations_left . '_' . $premium_license->id ] = $premium_license; |
| 92 |
} |
| 93 |
|
| 94 |
$total_available_licenses = count( $available_licenses ); |
| 95 |
if ( $total_available_licenses > 0 ) { |
| 96 |
$license_input_html = <<< HTML |
| 97 |
<div id="license_options_container"> |
| 98 |
<table> |
| 99 |
<tbody> |
| 100 |
<tr id="available_license_key_container"> |
| 101 |
<td><input type="radio" name="license_type" value="available"></td> |
| 102 |
<td> |
| 103 |
HTML; |
| 104 |
|
| 105 |
if ( $total_available_licenses > 1 ) { |
| 106 |
// Sort the licenses by number of activations left in descending order. |
| 107 |
krsort( $available_licenses ); |
| 108 |
|
| 109 |
$license_input_html .= '<select id="licenses">'; |
| 110 |
|
| 111 |
/** |
| 112 |
* @var FS_Plugin_License $license |
| 113 |
*/ |
| 114 |
foreach ( $available_licenses as $license ) { |
| 115 |
$label = sprintf( |
| 116 |
"%s-Site %s License - %s", |
| 117 |
( 1 == $license->quota ? |
| 118 |
'Single' : |
| 119 |
$license->quota |
| 120 |
), |
| 121 |
$fs->_get_plan_by_id( $license->plan_id )->title, |
| 122 |
( htmlspecialchars( substr( $license->secret_key, 0, 6 ) ) . |
| 123 |
str_pad( '', 23 * 6, '•' ) . |
| 124 |
htmlspecialchars( substr( $license->secret_key, - 3 ) ) ) |
| 125 |
); |
| 126 |
|
| 127 |
$license_input_html .= "<option data-id='{$license->id}' value='{$license->secret_key}' data-left='{$license->left()}'>{$label}</option>"; |
| 128 |
} |
| 129 |
|
| 130 |
$license_input_html .= '</select>'; |
| 131 |
} else { |
| 132 |
$available_licenses = array_values( $available_licenses ); |
| 133 |
|
| 134 |
/** |
| 135 |
* @var FS_Plugin_License $available_license |
| 136 |
*/ |
| 137 |
$available_license = $available_licenses[0]; |
| 138 |
$value = sprintf( |
| 139 |
"%s-Site %s License - %s", |
| 140 |
( 1 == $available_license->quota ? |
| 141 |
'Single' : |
| 142 |
$available_license->quota |
| 143 |
), |
| 144 |
$fs->_get_plan_by_id( $available_license->plan_id )->title, |
| 145 |
( htmlspecialchars( substr( $available_license->secret_key, 0, 6 ) ) . |
| 146 |
str_pad( '', 23 * 6, '•' ) . |
| 147 |
htmlspecialchars( substr( $available_license->secret_key, - 3 ) ) ) |
| 148 |
); |
| 149 |
|
| 150 |
$license_input_html .= <<< HTML |
| 151 |
<input |
| 152 |
id="available_license_key" |
| 153 |
type="text" |
| 154 |
value="{$value}" |
| 155 |
data-id="{$available_license->id}" |
| 156 |
data-license-key="{$available_license->secret_key}" |
| 157 |
data-left="{$available_license->left()}" |
| 158 |
readonly /> |
| 159 |
HTML; |
| 160 |
} |
| 161 |
|
| 162 |
$license_input_html .= <<< HTML |
| 163 |
</td> |
| 164 |
</tr> |
| 165 |
<tr> |
| 166 |
<td><input type="radio" name="license_type" value="other"></td> |
| 167 |
<td id="other_license_key_container"> |
| 168 |
<label for="other_license_key">Other: </label> |
| 169 |
<div> |
| 170 |
<input id="other_license_key" class="license_key" type="text" placeholder="Enter license key" tabindex="1"> |
| 171 |
</div> |
| 172 |
</td> |
| 173 |
</tr> |
| 174 |
</tbody> |
| 175 |
</table> |
| 176 |
</div> |
| 177 |
HTML; |
| 178 |
} else { |
| 179 |
$license_input_html = "<input class='license_key' type='text' placeholder='{$license_key_text}' tabindex='1' />"; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* IMPORTANT: |
| 184 |
* DO NOT ADD MAXLENGTH OR LIMIT THE LICENSE KEY LENGTH SINCE |
| 185 |
* WE DO WANT TO ALLOW INPUT OF LONGER KEYS (E.G. WooCommerce Keys) |
| 186 |
* FOR MIGRATED MODULES. |
| 187 |
*/ |
| 188 |
$modal_content_html = <<< HTML |
| 189 |
<div class="notice notice-error inline license-activation-message"><p></p></div> |
| 190 |
<p>{$message_above_input_field}</p> |
| 191 |
{$license_input_html} |
| 192 |
<a class="show-license-resend-modal show-license-resend-modal-{$fs->get_unique_affix()}" href="!#" tabindex="2">{$cant_find_license_key_text}</a> |
| 193 |
{$network_activation_html} |
| 194 |
<p>{$message_below_input_field}</p> |
| 195 |
HTML; |
| 196 |
|
| 197 |
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
| 198 |
?> |
| 199 |
<script type="text/javascript"> |
| 200 |
(function( $ ) { |
| 201 |
$( document ).ready(function() { |
| 202 |
var modalContentHtml = <?php echo json_encode($modal_content_html); ?>, |
| 203 |
modalHtml = |
| 204 |
'<div class="fs-modal fs-modal-license-activation">' |
| 205 |
+ ' <div class="fs-modal-dialog">' |
| 206 |
+ ' <div class="fs-modal-header">' |
| 207 |
+ ' <h4><?php echo esc_js($header_title) ?></h4>' |
| 208 |
+ ' <a href="!#" class="fs-close"><i class="dashicons dashicons-no" title="<?php echo esc_js( fs_text_x_inline( 'Dismiss', 'as close a window', 'dismiss', $slug ) ) ?>"></i></a>' |
| 209 |
+ ' </div>' |
| 210 |
+ ' <div class="fs-modal-body">' |
| 211 |
+ ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>' |
| 212 |
+ ' </div>' |
| 213 |
+ ' <div class="fs-modal-footer">' |
| 214 |
+ ' <button class="button button-secondary button-close" tabindex="4"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel', $slug ) ?></button>' |
| 215 |
+ ' <button class="button button-primary button-activate-license" tabindex="3"><?php echo esc_js( $activate_button_text ) ?></button>' |
| 216 |
+ ' </div>' |
| 217 |
+ ' </div>' |
| 218 |
+ '</div>', |
| 219 |
$modal = $(modalHtml), |
| 220 |
$activateLicenseLink = $('span.activate-license.<?php echo $fs->get_unique_affix() ?> a, .activate-license-trigger.<?php echo $fs->get_unique_affix() ?>'), |
| 221 |
$activateLicenseButton = $modal.find('.button-activate-license'), |
| 222 |
$licenseKeyInput = $modal.find('input.license_key'), |
| 223 |
$licenseActivationMessage = $modal.find( '.license-activation-message' ), |
| 224 |
isNetworkActivation = <?php echo $is_network_activation ? 'true' : 'false' ?>; |
| 225 |
|
| 226 |
$modal.appendTo($('body')); |
| 227 |
|
| 228 |
var |
| 229 |
$licensesDropdown = $( '#licenses' ), |
| 230 |
$licenseTypes = $( 'input[type="radio"][name="license_type"]' ), |
| 231 |
$applyOnAllSites = $( '#apply_on_all_sites' ), |
| 232 |
$sitesListContainer = $( '#sites_list_container' ), |
| 233 |
$availableLicenseKey = $( '#available_license_key' ), |
| 234 |
$otherLicenseKey = $( '#other_license_key' ), |
| 235 |
$multisiteOptionsContainer = $( '#multisite_options_container' ), |
| 236 |
$activationsLeft = null, |
| 237 |
hasLicensesDropdown = ( $licensesDropdown.length > 0 ), |
| 238 |
hasLicenseTypes = ( $licenseTypes.length > 0 ), |
| 239 |
maxSitesListHeight = null, |
| 240 |
totalSites = <?php echo count( $sites_details ) ?>, |
| 241 |
singleBlogID = null; |
| 242 |
|
| 243 |
function registerEventHandlers() { |
| 244 |
var |
| 245 |
$otherLicenseKeyContainer = $( '#other_license_key_container' ); |
| 246 |
|
| 247 |
if ( isNetworkActivation ) { |
| 248 |
$applyOnAllSites.click(function() { |
| 249 |
var applyOnAllSites = $( this ).is( ':checked' ); |
| 250 |
|
| 251 |
$multisiteOptionsContainer.toggleClass( 'apply-on-all-sites', applyOnAllSites ); |
| 252 |
|
| 253 |
showSites( ! applyOnAllSites ); |
| 254 |
|
| 255 |
if ( hasValidLicenseKey() && ( applyOnAllSites || hasSelectedSite() ) ) { |
| 256 |
enableActivateLicenseButton(); |
| 257 |
} else { |
| 258 |
disableActivateLicenseButton(); |
| 259 |
} |
| 260 |
}); |
| 261 |
|
| 262 |
$sitesListContainer.delegate( 'td:not(:first-child)', 'click', function() { |
| 263 |
// If a site row is clicked, trigger a click on the checkbox. |
| 264 |
$( this ).parent().find( 'td:first-child input' ).click(); |
| 265 |
}); |
| 266 |
|
| 267 |
$sitesListContainer.delegate( 'input[type="checkbox"]', 'click', function() { |
| 268 |
enableDisableSitesSelection(); |
| 269 |
|
| 270 |
if ( hasValidLicenseKey() && hasSelectedSite() ) { |
| 271 |
enableActivateLicenseButton(); |
| 272 |
} else { |
| 273 |
disableActivateLicenseButton(); |
| 274 |
} |
| 275 |
}); |
| 276 |
} |
| 277 |
|
| 278 |
if ( hasLicensesDropdown ) { |
| 279 |
$licensesDropdown.change(function() { |
| 280 |
// When a license is selected, select the associated radio button. |
| 281 |
$licenseTypes.filter( '[value="available"]' ).attr( 'checked', true ); |
| 282 |
|
| 283 |
if ( ! isNetworkActivation || $modal.hasClass( 'is-single-site-activation' ) ) { |
| 284 |
enableActivateLicenseButton(); |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
toggleActivationOnAllSites(); |
| 289 |
}) |
| 290 |
} |
| 291 |
|
| 292 |
if ( hasLicenseTypes ) { |
| 293 |
$licenseTypes.change(function() { |
| 294 |
var |
| 295 |
licenseKey = $( 'input.license_key' ).val().trim(), |
| 296 |
otherLicenseKeySelected = isOtherLicenseKeySelected(); |
| 297 |
|
| 298 |
if ( ( licenseKey.length > 0 || ( hasLicenseTypes && ! otherLicenseKeySelected ) ) && |
| 299 |
( $modal.hasClass( 'is-single-site-activation' ) || ! isNetworkActivation || hasSelectedSite() ) |
| 300 |
) { |
| 301 |
/** |
| 302 |
* If the "other" license is not empty or an available license is selected, enable the activate |
| 303 |
* button. |
| 304 |
* |
| 305 |
* @author Leo Fajardo (@leorw) |
| 306 |
*/ |
| 307 |
enableActivateLicenseButton(); |
| 308 |
} else { |
| 309 |
disableActivateLicenseButton(); |
| 310 |
} |
| 311 |
|
| 312 |
if ( ! isNetworkActivation ) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
if ( otherLicenseKeySelected ) { |
| 317 |
$applyOnAllSites.attr( 'disabled', false ); |
| 318 |
enableDisableSitesSelection(); |
| 319 |
resetActivateLicenseCheckboxLabel(); |
| 320 |
} else if ( ! $modal.hasClass( 'is-single-site-activation' ) ) { |
| 321 |
toggleActivationOnAllSites(); |
| 322 |
} |
| 323 |
}); |
| 324 |
|
| 325 |
if ( ! hasLicensesDropdown ) { |
| 326 |
$availableLicenseKey.click(function() { |
| 327 |
$licenseTypes.filter( '[value="available"]' ).click(); |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
$otherLicenseKeyContainer.click(function() { |
| 332 |
$licenseTypes.filter( '[value="other"]' ).click(); |
| 333 |
}); |
| 334 |
} |
| 335 |
|
| 336 |
$activateLicenseLink.click(function (evt) { |
| 337 |
evt.preventDefault(); |
| 338 |
|
| 339 |
showModal( evt ); |
| 340 |
}); |
| 341 |
|
| 342 |
$modal.on('input propertychange', 'input.license_key', function () { |
| 343 |
|
| 344 |
var licenseKey = $(this).val().trim(); |
| 345 |
|
| 346 |
/** |
| 347 |
* If license key is not empty, enable the license activation button. |
| 348 |
*/ |
| 349 |
if ( licenseKey.length > 0 && ( $modal.hasClass( 'is-single-site-activation' ) || ! isNetworkActivation || hasSelectedSite() ) ) { |
| 350 |
enableActivateLicenseButton(); |
| 351 |
} |
| 352 |
}); |
| 353 |
|
| 354 |
$modal.on( 'blur', 'input.license_key', function( evt ) { |
| 355 |
var |
| 356 |
licenseKey = $(this).val().trim(), |
| 357 |
$focusedElement = $( evt.relatedTarget ), |
| 358 |
hasSelectedAvailableLicense = ( hasLicenseTypes && $focusedElement.parents( '#available_license_key_container' ).length > 0 ); |
| 359 |
|
| 360 |
/** |
| 361 |
* If license key is empty, disable the license activation button. |
| 362 |
*/ |
| 363 |
if ( ( 0 === licenseKey.length && ( ! hasLicenseTypes || ! hasSelectedAvailableLicense ) ) || |
| 364 |
( isNetworkActivation && ! hasSelectedSite() ) |
| 365 |
) { |
| 366 |
disableActivateLicenseButton(); |
| 367 |
} |
| 368 |
}); |
| 369 |
|
| 370 |
$modal.on('click', '.button-activate-license', function (evt) { |
| 371 |
evt.preventDefault(); |
| 372 |
|
| 373 |
if ($(this).hasClass('disabled')) { |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
var |
| 378 |
licenseKey = ''; |
| 379 |
|
| 380 |
if ( hasLicenseTypes ) { |
| 381 |
if ( isOtherLicenseKeySelected() ) { |
| 382 |
licenseKey = $otherLicenseKey.val(); |
| 383 |
} else { |
| 384 |
if ( ! hasLicensesDropdown ) { |
| 385 |
licenseKey = $availableLicenseKey.data( 'license-key' ); |
| 386 |
} else { |
| 387 |
licenseKey = $licensesDropdown.val(); |
| 388 |
} |
| 389 |
} |
| 390 |
} else { |
| 391 |
licenseKey = $licenseKeyInput.val().trim(); |
| 392 |
} |
| 393 |
|
| 394 |
disableActivateLicenseButton(); |
| 395 |
|
| 396 |
if (0 === licenseKey.length) { |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
var data = { |
| 401 |
action : '<?php echo $fs->get_ajax_action( 'activate_license' ) ?>', |
| 402 |
security : '<?php echo $fs->get_ajax_security( 'activate_license' ) ?>', |
| 403 |
license_key: licenseKey, |
| 404 |
module_id : '<?php echo $fs->get_id() ?>' |
| 405 |
}; |
| 406 |
|
| 407 |
if ( isNetworkActivation ) { |
| 408 |
var |
| 409 |
sites = []; |
| 410 |
|
| 411 |
if ( null === singleBlogID ) { |
| 412 |
var |
| 413 |
applyOnAllSites = $applyOnAllSites.is( ':checked' ); |
| 414 |
|
| 415 |
$sitesListContainer.find( 'tr' ).each(function() { |
| 416 |
var |
| 417 |
$this = $( this ), |
| 418 |
includeSite = ( applyOnAllSites || $this.find( 'input' ).is( ':checked' ) ); |
| 419 |
|
| 420 |
if ( ! includeSite ) |
| 421 |
return; |
| 422 |
|
| 423 |
var site = { |
| 424 |
uid : $this.find( '.uid' ).val(), |
| 425 |
url : $this.find( '.url' ).val(), |
| 426 |
title : $this.find( '.title' ).val(), |
| 427 |
language: $this.find( '.language' ).val(), |
| 428 |
charset : $this.find( '.charset' ).val(), |
| 429 |
blog_id : $this.find( '.blog-id' ).find( 'span' ).text() |
| 430 |
}; |
| 431 |
|
| 432 |
sites.push( site ); |
| 433 |
}); |
| 434 |
} else { |
| 435 |
data.blog_id = singleBlogID; |
| 436 |
} |
| 437 |
|
| 438 |
data.sites = sites; |
| 439 |
} |
| 440 |
|
| 441 |
$.ajax({ |
| 442 |
url: ajaxurl, |
| 443 |
method: 'POST', |
| 444 |
data: data, |
| 445 |
beforeSend: function () { |
| 446 |
$activateLicenseButton.text( '<?php fs_esc_js_echo_inline( 'Activating license', 'activating-license', $slug ) ?>...' ); |
| 447 |
}, |
| 448 |
success: function( result ) { |
| 449 |
var resultObj = $.parseJSON( result ); |
| 450 |
if ( resultObj.success ) { |
| 451 |
closeModal(); |
| 452 |
|
| 453 |
// Redirect to the "Account" page and sync the license. |
| 454 |
window.location.href = resultObj.next_page; |
| 455 |
} else { |
| 456 |
showError( resultObj.error.message ? resultObj.error.message : resultObj.error ); |
| 457 |
resetActivateLicenseButton(); |
| 458 |
} |
| 459 |
} |
| 460 |
}); |
| 461 |
}); |
| 462 |
|
| 463 |
// If the user has clicked outside the window, close the modal. |
| 464 |
$modal.on('click', '.fs-close, .button-secondary', function () { |
| 465 |
closeModal(); |
| 466 |
return false; |
| 467 |
}); |
| 468 |
} |
| 469 |
|
| 470 |
registerEventHandlers(); |
| 471 |
|
| 472 |
$('body').trigger('licenseActivationLoaded'); |
| 473 |
|
| 474 |
/** |
| 475 |
* @author Leo Fajardo (@leorw) |
| 476 |
* @since 2.0.0 |
| 477 |
*/ |
| 478 |
function enableDisableSitesSelection() { |
| 479 |
var |
| 480 |
canApplyOnAllSites = $applyOnAllSites.is( ':enabled' ), |
| 481 |
disableSitesSelection = null; |
| 482 |
|
| 483 |
if ( ! canApplyOnAllSites ) { |
| 484 |
var |
| 485 |
selectedSites = $sitesListContainer.find( 'input[type="checkbox"]:checked' ).length, |
| 486 |
activationsLeft = Math.max( 0, $activationsLeft.data( 'left' ) - selectedSites ); |
| 487 |
|
| 488 |
disableSitesSelection = ( 0 === activationsLeft ); |
| 489 |
|
| 490 |
$activationsLeft.text( activationsLeft ); |
| 491 |
} else { |
| 492 |
disableSitesSelection = false; |
| 493 |
} |
| 494 |
|
| 495 |
$sitesListContainer |
| 496 |
.find( 'input[type="checkbox"]:not(:checked)' ) |
| 497 |
.attr( 'disabled', disableSitesSelection ); |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* @author Leo Fajardo (@leorw) |
| 502 |
* @since 2.0.0 |
| 503 |
* |
| 504 |
* @returns {Boolean} |
| 505 |
*/ |
| 506 |
function isOtherLicenseKeySelected() { |
| 507 |
return ( hasLicenseTypes && 'other' === $licenseTypes.filter( ':checked' ).val() ); |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* @author Leo Fajardo (@leorw) |
| 512 |
* @since 2.0.0 |
| 513 |
* |
| 514 |
* @returns {Boolean} |
| 515 |
*/ |
| 516 |
function hasValidLicenseKey() { |
| 517 |
var licenseKey = ''; |
| 518 |
if ( hasLicenseTypes ) { |
| 519 |
if ( 'available' === $licenseTypes.filter( ':checked' ).val() ) { |
| 520 |
return true; |
| 521 |
} else { |
| 522 |
licenseKey = $otherLicenseKey.val(); |
| 523 |
} |
| 524 |
} else { |
| 525 |
licenseKey = $( 'input.license_key' ).val(); |
| 526 |
} |
| 527 |
|
| 528 |
return ( licenseKey.trim().length > 0 ); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* @author Leo Fajardo (@leorw) |
| 533 |
* @since 2.0.0 |
| 534 |
* |
| 535 |
* @returns {Boolean} |
| 536 |
*/ |
| 537 |
function hasSelectedSite() { |
| 538 |
return ( $applyOnAllSites.is( ':checked' ) || |
| 539 |
$sitesListContainer.find( 'input[type="checkbox"]:checked:not(:disabled)' ).length > 0 ); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* @author Leo Fajardo (@leorw) |
| 544 |
* @since 2.0.0 |
| 545 |
*/ |
| 546 |
function toggleActivationOnAllSites() { |
| 547 |
var activationsLeft, |
| 548 |
licenseID; |
| 549 |
|
| 550 |
if (hasLicensesDropdown) { |
| 551 |
var $selectedOption = $licensesDropdown.find( ':selected' ); |
| 552 |
activationsLeft = $selectedOption.data('left'); |
| 553 |
licenseID = $selectedOption.data('id'); |
| 554 |
} else { |
| 555 |
activationsLeft = $availableLicenseKey.data('left'); |
| 556 |
licenseID = $availableLicenseKey.data('id'); |
| 557 |
} |
| 558 |
|
| 559 |
// Cleanup previously auto-selected site. |
| 560 |
$('#sites_list_container input[type=checkbox]:disabled') |
| 561 |
.attr('disabled', false) |
| 562 |
.attr('checked', false); |
| 563 |
|
| 564 |
var $blogsWithActiveLicense = $('#sites_list_container tr[data-license-id=' + licenseID + '] input[type=checkbox]'); |
| 565 |
|
| 566 |
if ($blogsWithActiveLicense.length > 0) { |
| 567 |
$blogsWithActiveLicense.attr('checked', true) |
| 568 |
.attr('disabled', true); |
| 569 |
|
| 570 |
activationsLeft += $blogsWithActiveLicense.length; |
| 571 |
} |
| 572 |
|
| 573 |
if ( activationsLeft >= totalSites ) { |
| 574 |
$applyOnAllSites.attr( 'disabled', false ); |
| 575 |
enableDisableSitesSelection(); |
| 576 |
|
| 577 |
resetActivateLicenseCheckboxLabel(); |
| 578 |
|
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
$applyOnAllSites.attr( 'checked', false ); |
| 583 |
$applyOnAllSites.attr( 'disabled', true ); |
| 584 |
|
| 585 |
showSites( true ); |
| 586 |
|
| 587 |
var |
| 588 |
activateLicenseCheckboxLabel = '<?php fs_esc_js_echo_inline( 'Choose up to %s site(s) to activate the license on.', 'choose-up-to-n-sites-to-activate-the-license-on', $slug ) ?>'; |
| 589 |
|
| 590 |
activateLicenseCheckboxLabel = activateLicenseCheckboxLabel.replace( '%s', '<span data-left="' + activationsLeft + '" class="activations-left">' + activationsLeft + '</span>' ); |
| 591 |
|
| 592 |
// Update the label of the "Activate license on all sites" checkbox. |
| 593 |
$applyOnAllSites.parent().find( 'span' ).html( activateLicenseCheckboxLabel ); |
| 594 |
$activationsLeft = $( '.activations-left' ); |
| 595 |
|
| 596 |
if ( hasSelectedSite() ) { |
| 597 |
enableActivateLicenseButton(); |
| 598 |
enableDisableSitesSelection(); |
| 599 |
} else { |
| 600 |
disableActivateLicenseButton(); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
/** |
| 605 |
* @author Leo Fajardo (@leorw) |
| 606 |
* @since 2.0.0 |
| 607 |
*/ |
| 608 |
function resetActivateLicenseCheckboxLabel() { |
| 609 |
var activateLicenseCheckboxLabel = '<?php fs_esc_js_echo_inline( 'Activate license on all sites in the network.', 'activate-license-on-all-sites-in-the-network', $slug ) ?>'; |
| 610 |
$applyOnAllSites.parent().find( 'span' ).text( activateLicenseCheckboxLabel ); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* @author Leo Fajardo (@leorw) |
| 615 |
* @since 2.0.0 |
| 616 |
* |
| 617 |
* @param {Boolean} show |
| 618 |
*/ |
| 619 |
function showSites( show ) { |
| 620 |
$sitesListContainer.toggle( show ); |
| 621 |
if ( show && null === maxSitesListHeight ) { |
| 622 |
/** |
| 623 |
* Set the visible number of rows to 5 (5 * height of the first row). |
| 624 |
* |
| 625 |
* @author Leo Fajardo (@leorw) |
| 626 |
*/ |
| 627 |
maxSitesListHeight = ( 5 * $sitesListContainer.find( 'tr:first' ).height() ); |
| 628 |
$sitesListContainer.css( 'max-height', maxSitesListHeight ); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
function showModal( evt ) { |
| 633 |
resetModal(); |
| 634 |
|
| 635 |
// Display the dialog box. |
| 636 |
$modal.addClass('active'); |
| 637 |
$('body').addClass('has-fs-modal'); |
| 638 |
|
| 639 |
var |
| 640 |
$singleInstallDetails = $( evt.target ).parents( 'tr.fs-install-details' ), |
| 641 |
isSingleSiteActivation = ( $singleInstallDetails.length > 0 ); |
| 642 |
|
| 643 |
$modal.toggleClass( 'is-single-site-activation', isSingleSiteActivation ); |
| 644 |
|
| 645 |
singleBlogID = isSingleSiteActivation ? |
| 646 |
$singleInstallDetails.prev().data( 'blog-id' ) : |
| 647 |
null; |
| 648 |
|
| 649 |
$multisiteOptionsContainer.toggle( isNetworkActivation && ! isSingleSiteActivation ); |
| 650 |
|
| 651 |
if ( hasLicenseTypes ) { |
| 652 |
$licenseTypes.attr( 'checked', false ); |
| 653 |
|
| 654 |
if ( hasLicensesDropdown ) { |
| 655 |
$licensesDropdown.find( 'option:first' ).attr( 'selected', true ).trigger( 'change' ); |
| 656 |
} else { |
| 657 |
$licenseTypes.filter( '[value="available"]' ).click(); |
| 658 |
} |
| 659 |
|
| 660 |
$otherLicenseKey.val( '' ); |
| 661 |
} else { |
| 662 |
$licenseKeyInput.val( '' ); |
| 663 |
$licenseKeyInput.focus(); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
function closeModal() { |
| 668 |
$modal.removeClass('active'); |
| 669 |
$('body').removeClass('has-fs-modal'); |
| 670 |
} |
| 671 |
|
| 672 |
function resetActivateLicenseButton() { |
| 673 |
enableActivateLicenseButton(); |
| 674 |
$activateLicenseButton.text( <?php echo json_encode( $activate_button_text ) ?> ); |
| 675 |
} |
| 676 |
|
| 677 |
function resetModal() { |
| 678 |
hideError(); |
| 679 |
resetActivateLicenseButton(); |
| 680 |
} |
| 681 |
|
| 682 |
function enableActivateLicenseButton() { |
| 683 |
$activateLicenseButton.removeClass( 'disabled' ); |
| 684 |
} |
| 685 |
|
| 686 |
function disableActivateLicenseButton() { |
| 687 |
$activateLicenseButton.addClass( 'disabled' ); |
| 688 |
} |
| 689 |
|
| 690 |
function hideError() { |
| 691 |
$licenseActivationMessage.hide(); |
| 692 |
} |
| 693 |
|
| 694 |
function showError( msg ) { |
| 695 |
$licenseActivationMessage.find( ' > p' ).html( msg ); |
| 696 |
$licenseActivationMessage.show(); |
| 697 |
} |
| 698 |
}); |
| 699 |
})( jQuery ); |
| 700 |
</script> |