PluginProbe ʕ •ᴥ•ʔ
Auto Post Cleaner / 3.3.0
Auto Post Cleaner v3.3.0
3.12.0 3.13.1 3.2.4 3.2.5 3.3.0 3.3.10 3.3.11 3.3.8 3.4.2 3.5.3 3.6.0 3.7.0 3.7.1 3.7.2 3.7.3 3.7.5 3.7.6 3.8.0 3.9.0 3.9.4 3.9.6 3.9.7 trunk 3.0.0 3.1.0 3.10.1 3.10.2 3.11.4
delete-old-posts-programmatically / freemius / templates / forms / license-activation.php
delete-old-posts-programmatically / freemius / templates / forms Last commit date
deactivation 3 years ago affiliation.php 3 years ago data-debug-mode.php 3 years ago email-address-update.php 3 years ago index.php 5 years ago license-activation.php 3 years ago optout.php 3 years ago premium-versions-upgrade-handler.php 5 years ago premium-versions-upgrade-metadata.php 5 years ago resend-key.php 3 years ago subscription-cancellation.php 5 years ago trial-start.php 3 years ago user-change.php 3 years ago
license-activation.php
891 lines
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 array $VARS
15 *
16 * @var Freemius $fs
17 */
18 $fs = freemius( $VARS['id'] );
19 $slug = $fs->get_slug();
20 $unique_affix = $fs->get_unique_affix();
21
22 $cant_find_license_key_text = fs_text_inline( "Can't find your license key?", 'cant-find-license-key', $slug );
23 $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 );
24 $message_below_input_field = '';
25
26 $header_title = $fs->is_free_plan() ?
27 fs_text_inline( 'Activate License', 'activate-license', $slug ) :
28 fs_text_inline( 'Update License', 'update-license', $slug );
29
30 if ( $fs->is_registered() ) {
31 $activate_button_text = $header_title;
32 } else {
33 $message_below_input_field = sprintf(
34 fs_text_inline( 'The %1$s will be periodically sending essential license data to %2$s to check for security and feature updates, and verify the validity of your license.', 'license-sync-disclaimer', $slug ),
35 $fs->get_module_label( true ),
36 "<b>{$fs->get_plugin_title()}</b>"
37 );
38
39 $activate_button_text = fs_text_inline( 'Agree & Activate License', 'agree-activate-license', $slug );
40 }
41
42 $license_key_text = fs_text_inline( 'License key', 'license-key' , $slug );
43
44 $is_network_activation = (
45 $fs->is_network_active() &&
46 fs_is_network_admin() &&
47 ! $fs->is_delegated_connection()
48 );
49 $network_activation_html = '';
50
51 $sites_details = array();
52 if ( $is_network_activation ) {
53 $all_sites = Freemius::get_sites();
54
55 $subsite_data_by_install_id = array();
56 $install_url_by_install_id = array();
57
58 foreach ( $all_sites as $site ) {
59 $site_details = $fs->get_site_info( $site );
60
61 if ( FS_Clone_Manager::instance()->is_temporary_duplicate_by_blog_id( $site_details['blog_id'] ) ) {
62 continue;
63 }
64
65 $blog_id = Freemius::get_site_blog_id( $site );
66 $install = $fs->get_install_by_blog_id($blog_id);
67
68 if ( is_object( $install ) ) {
69 if ( isset( $subsite_data_by_install_id[ $install->id ] ) ) {
70 $clone_subsite_data = $subsite_data_by_install_id[ $install->id ];
71 $clone_install_url = $install_url_by_install_id[ $install->id ];
72
73 if (
74 /**
75 * If we already have an install with the same URL as the subsite it's stored in, skip the current subsite. Otherwise, replace the existing install's data with the current subsite's install's data if the URLs match.
76 *
77 * @author Leo Fajardo (@leorw)
78 * @since 2.5.0
79 */
80 fs_strip_url_protocol( untrailingslashit( $clone_install_url ) ) === fs_strip_url_protocol( untrailingslashit( $clone_subsite_data['url'] ) ) ||
81 fs_strip_url_protocol( untrailingslashit( $install->url ) ) !== fs_strip_url_protocol( untrailingslashit( $site_details['url'] ) )
82 ) {
83 continue;
84 }
85 }
86
87 if ( FS_Plugin_License::is_valid_id( $install->license_id ) ) {
88 $site_details['license_id'] = $install->license_id;
89 }
90
91 $subsite_data_by_install_id[ $install->id ] = $site_details;
92 $install_url_by_install_id[ $install->id ] = $install->url;
93 }
94 }
95
96 if ( $is_network_activation ) {
97 $vars = array(
98 'id' => $fs->get_id(),
99 'sites' => array_values( $subsite_data_by_install_id ),
100 'require_license_key' => true
101 );
102
103 $network_activation_html = fs_get_template( 'partials/network-activation.php', $vars );
104 }
105 }
106
107 $premium_licenses = $fs->get_available_premium_licenses();
108 $available_licenses = array();
109 foreach ( $premium_licenses as $premium_license ) {
110 $activations_left = $premium_license->left();
111 if ( ! ( $activations_left > 0 ) ) {
112 continue;
113 }
114
115 $available_licenses[ $activations_left . '_' . $premium_license->id ] = $premium_license;
116 }
117
118 $total_available_licenses = count( $available_licenses );
119 if ( $total_available_licenses > 0 ) {
120 $license_input_html = <<< HTML
121 <div class="fs-license-options-container">
122 <table>
123 <tbody>
124 <tr class="fs-available-license-key-container">
125 <td><input type="radio" name="license_type" value="available"></td>
126 <td>
127 HTML;
128
129 if ( $total_available_licenses > 1 ) {
130 // Sort the licenses by number of activations left in descending order.
131 krsort( $available_licenses );
132
133 $license_input_html .= '<select class="fs-licenses">';
134
135 /**
136 * @var FS_Plugin_License $license
137 */
138 foreach ( $available_licenses as $license ) {
139 $plan = $fs->_get_plan_by_id( $license->plan_id );
140
141 $label = sprintf(
142 "%s-Site %s License - %s",
143 ( 1 == $license->quota ?
144 'Single' :
145 ( $license->is_unlimited() ? 'Unlimited' : $license->quota )
146 ),
147 ( is_object( $plan ) ? $plan->title : '' ),
148 $license->get_html_escaped_masked_secret_key()
149 );
150
151 $license_input_html .= "<option data-id='{$license->id}' value='{$license->secret_key}' data-left='{$license->left()}'>{$label}</option>";
152 }
153
154 $license_input_html .= '</select>';
155 } else {
156 $available_licenses = array_values( $available_licenses );
157
158 /**
159 * @var FS_Plugin_License $available_license
160 */
161 $available_license = $available_licenses[0];
162 $value = sprintf(
163 "%s-Site %s License - %s",
164 ( 1 == $available_license->quota ?
165 'Single' :
166 ( $available_license->is_unlimited() ? 'Unlimited' : $available_license->quota )
167 ),
168 $fs->_get_plan_by_id( $available_license->plan_id )->title,
169 $available_license->get_html_escaped_masked_secret_key()
170 );
171
172 $license_input_html .= <<< HTML
173 <input
174 class="fs-available-license-key"
175 type="text"
176 value="{$value}"
177 data-id="{$available_license->id}"
178 data-license-key="{$available_license->secret_key}"
179 data-left="{$available_license->left()}"
180 readonly />
181 HTML;
182 }
183
184 $license_input_html .= <<< HTML
185 </td>
186 </tr>
187 <tr>
188 <td><input type="radio" name="license_type" value="other"></td>
189 <td class="fs-other-license-key-container">
190 <label for="other_license_key_{$unique_affix}">Other: </label>
191 <div>
192 <input id="other_license_key_{$unique_affix}" class="fs-license-key" type="text" placeholder="Enter license key" tabindex="1">
193 </div>
194 </td>
195 </tr>
196 </tbody>
197 </table>
198 </div>
199 HTML;
200 } else {
201 $license_input_html = "<input class='fs-license-key' type='text' placeholder='{$license_key_text}' tabindex='1' />";
202 }
203
204 $ownership_change_option_text = fs_text_inline( "Associate with the license owner's account.", 'associate-account-with-license-owner', $slug );
205 $ownership_change_option_html = "<div class='ownership-change-option-container' style='display: none'><label><input type='checkbox' /> <strong>{$ownership_change_option_text}</strong></label></div>";
206
207 /**
208 * IMPORTANT:
209 * DO NOT ADD MAXLENGTH OR LIMIT THE LICENSE KEY LENGTH SINCE
210 * WE DO WANT TO ALLOW INPUT OF LONGER KEYS (E.G. WooCommerce Keys)
211 * FOR MIGRATED MODULES.
212 */
213 $modal_content_html = <<< HTML
214 <div class="notice notice-error inline license-activation-message"><p></p></div>
215 <p>{$message_above_input_field}</p>
216 {$license_input_html}
217 <a class="show-license-resend-modal show-license-resend-modal-{$fs->get_unique_affix()}" href="!#" tabindex="2">{$cant_find_license_key_text}</a>
218 {$network_activation_html}
219 <p>{$message_below_input_field}</p>
220 {$ownership_change_option_html}
221 HTML;
222
223 /**
224 * Handle the ownership change option if not an add-on or if no license yet is activated for the
225 * parent product in case of an add-on.
226 *
227 * @author Leo Fajardo (@leorw)
228 * @since 2.3.2
229 */
230 $is_user_change_supported = ( ! $fs->is_addon() || ! $fs->get_parent_instance()->has_active_valid_license() );
231
232 fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' );
233 ?>
234 <script type="text/javascript">
235 (function( $ ) {
236 $( document ).ready(function() {
237 var modalContentHtml = <?php echo json_encode($modal_content_html); ?>,
238 modalHtml =
239 '<div class="fs-modal fs-modal-license-activation fs-modal-license-activation-<?php echo $unique_affix ?>">'
240 + ' <div class="fs-modal-dialog">'
241 + ' <div class="fs-modal-header">'
242 + ' <h4><?php echo esc_js($header_title) ?></h4>'
243 + ' <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>'
244 + ' </div>'
245 + ' <div class="fs-modal-body">'
246 + ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>'
247 + ' </div>'
248 + ' <div class="fs-modal-footer">'
249 + ' <button class="button button-secondary button-close" tabindex="4"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel', $slug ) ?></button>'
250 + ' <button class="button button-primary button-activate-license" tabindex="3"><?php echo esc_js( $activate_button_text ) ?></button>'
251 + ' </div>'
252 + ' </div>'
253 + '</div>',
254 $modal = $(modalHtml),
255 $activateLicenseButton = $modal.find('.button-activate-license'),
256 $licenseKeyInput = $modal.find( 'input.fs-license-key' ),
257 $licenseActivationMessage = $modal.find( '.license-activation-message' ),
258 isNetworkActivation = <?php echo $is_network_activation ? 'true' : 'false' ?>,
259 isUserChangeSupported = <?php echo $is_user_change_supported ? 'true' : 'false' ?>,
260 isSingleSiteActivation = false,
261 $ownershipChangeOptionContainer = $modal.find( '.ownership-change-option-container' ),
262 $body = $( 'body' );
263
264 $modal.appendTo( $body );
265
266 var
267 $licensesDropdown = $modal.find( '.fs-licenses' ),
268 $licenseTypes = $modal.find( 'input[type="radio"][name="license_type"]' ),
269 $applyOnAllSites = $modal.find( '.fs-apply-on-all-sites-checkbox' ),
270 $sitesListContainer = $modal.find( '.fs-sites-list-container' ),
271 $availableLicenseKey = $modal.find( '.fs-available-license-key' ),
272 $otherLicenseKey = $modal.find( '#other_license_key_<?php echo $unique_affix ?>' ),
273 $multisiteOptionsContainer = $modal.find( '.fs-multisite-options-container' ),
274 $activationsLeft = null,
275 hasLicensesDropdown = ( $licensesDropdown.length > 0 ),
276 hasLicenseTypes = ( $licenseTypes.length > 0 ),
277 maxSitesListHeight = null,
278 totalSites = <?php echo count( $sites_details ) ?>,
279 singleBlogID = null;
280
281 var
282 previousLicenseKey = null,
283 otherLicenseOwnerID = null,
284 /**
285 * @author Leo Fajardo (@leorw)
286 * @since 2.3.2
287 */
288 resetLoadingMode = function () {
289 // Reset loading mode.
290 $activateLicenseButton.text( <?php echo json_encode( $activate_button_text ) ?> );
291 $activateLicenseButton.prop( 'disabled', false );
292 $( document.body ).css( { 'cursor': 'auto' } );
293 $( '.fs-loading' ).removeClass( 'fs-loading' );
294
295 console.log( 'resetLoadingMode - Primary button was enabled' );
296 },
297 /**
298 * @author Leo Fajardo (@leorw)
299 * @since 2.3.2
300 */
301 setLoadingMode = function () {
302 $( document.body ).css( { 'cursor': 'wait' } );
303 },
304 /**
305 * @author Leo Fajardo (@leorw)
306 * @since 2.3.2
307 */
308 afterLicenseUserDataLoaded = function () {
309 if (
310 null !== otherLicenseOwnerID &&
311 otherLicenseOwnerID != <?php echo $fs->is_registered() ? $fs->get_user()->id : 'null' ?>
312 ) {
313 $ownershipChangeOptionContainer.show();
314 } else {
315 $ownershipChangeOptionContainer.hide();
316 $activateLicenseButton.focus();
317 }
318 },
319 /**
320 * @author Leo Fajardo (@leorw)
321 * @since 2.3.2
322 */
323 fetchLicenseUserData = function () {
324 var hideAndUncheckUserChangeCheckbox = ( ! isUserChangeSupported ),
325 otherLicenseKeyIsSelected = isOtherLicenseKeySelected();
326
327 if ( ! hideAndUncheckUserChangeCheckbox ) {
328 // User change is supported only on the site level.
329 hideAndUncheckUserChangeCheckbox = ( isNetworkActivation || isSingleSiteActivation );
330 }
331
332 if ( ! hideAndUncheckUserChangeCheckbox ) {
333 hideAndUncheckUserChangeCheckbox = ( hasLicenseTypes && ! otherLicenseKeyIsSelected );
334 }
335
336 var licenseKey = $licenseKeyInput.val().trim();
337
338 if ( ! hideAndUncheckUserChangeCheckbox && otherLicenseKeyIsSelected ) {
339 hideAndUncheckUserChangeCheckbox = ( licenseKey.length < 32 );
340 }
341
342 if ( licenseKey !== previousLicenseKey ) {
343 // If the license key has not been changed, keep the owner ID in order to prevent another API call.
344 otherLicenseOwnerID = null;
345 }
346
347 if ( hideAndUncheckUserChangeCheckbox ) {
348 $ownershipChangeOptionContainer.hide().find( 'input' ).attr( 'checked', false );
349
350 return;
351 }
352
353 if ( null !== otherLicenseOwnerID ) {
354 afterLicenseUserDataLoaded();
355 return;
356 }
357
358 setLoadingMode();
359
360 $activateLicenseButton.addClass( 'fs-loading' );
361 $activateLicenseButton.attr( 'disabled', 'disabled' );
362 $activateLicenseButton.html( '<?php fs_esc_js_echo_inline( 'Please wait', 'please-wait', $slug ) ?>...' );
363
364 $.ajax( {
365 url : <?php echo Freemius::ajax_url() ?>,
366 method : 'POST',
367 data : {
368 action : '<?php echo $fs->get_ajax_action( 'fetch_is_marketing_required_flag_value' ) ?>',
369 security : '<?php echo $fs->get_ajax_security( 'fetch_is_marketing_required_flag_value' ) ?>',
370 license_key: licenseKey,
371 module_id : '<?php echo $fs->get_id() ?>'
372 },
373 success: function ( result ) {
374 resetLoadingMode();
375
376 if ( result.success ) {
377 result = result.data;
378
379 // Cache license owner's ID.
380 otherLicenseOwnerID = result.license_owner_id;
381 }
382
383 afterLicenseUserDataLoaded();
384 }
385 } );
386 };
387
388 function registerEventHandlers() {
389 var
390 $otherLicenseKeyContainer = $modal.find( '.fs-other-license-key-container' );
391
392 if ( isNetworkActivation ) {
393 $applyOnAllSites.click(function() {
394 var applyOnAllSites = $( this ).is( ':checked' );
395
396 $multisiteOptionsContainer.toggleClass( 'fs-apply-on-all-sites', applyOnAllSites );
397
398 showSites( ! applyOnAllSites );
399
400 if ( hasValidLicenseKey() && ( applyOnAllSites || hasSelectedSite() ) ) {
401 enableActivateLicenseButton();
402 } else {
403 disableActivateLicenseButton();
404 }
405 });
406
407 $sitesListContainer.delegate( 'td:not(:first-child)', 'click', function() {
408 // If a site row is clicked, trigger a click on the checkbox.
409 $( this ).parent().find( 'td:first-child input' ).click();
410 });
411
412 $sitesListContainer.delegate( 'input[type="checkbox"]', 'click', function() {
413 enableDisableSitesSelection();
414
415 if ( hasValidLicenseKey() && hasSelectedSite() ) {
416 enableActivateLicenseButton();
417 } else {
418 disableActivateLicenseButton();
419 }
420 });
421 }
422
423 if ( hasLicensesDropdown ) {
424 $licensesDropdown.change(function() {
425 // When a license is selected, select the associated radio button.
426 $licenseTypes.filter( '[value="available"]' ).attr( 'checked', true );
427
428 if ( ! isNetworkActivation || $modal.hasClass( 'is-single-site-activation' ) ) {
429 enableActivateLicenseButton();
430 return true;
431 }
432
433 toggleActivationOnAllSites();
434 })
435 }
436
437 if ( hasLicenseTypes ) {
438 $licenseTypes.change(function() {
439 var
440 licenseKey = $modal.find( 'input.fs-license-key' ).val().trim(),
441 otherLicenseKeySelected = isOtherLicenseKeySelected();
442
443 if ( ( licenseKey.length > 0 || ( hasLicenseTypes && ! otherLicenseKeySelected ) ) &&
444 ( $modal.hasClass( 'is-single-site-activation' ) || ! isNetworkActivation || hasSelectedSite() )
445 ) {
446 /**
447 * If the "other" license is not empty or an available license is selected, enable the activate
448 * button.
449 *
450 * @author Leo Fajardo (@leorw)
451 */
452 enableActivateLicenseButton();
453 } else {
454 disableActivateLicenseButton();
455 }
456
457 if ( '' !== licenseKey ) {
458 fetchLicenseUserData();
459 }
460
461 if ( ! isNetworkActivation ) {
462 return;
463 }
464
465 if ( otherLicenseKeySelected ) {
466 $applyOnAllSites.attr( 'disabled', false );
467 enableDisableSitesSelection();
468 resetActivateLicenseCheckboxLabel();
469 } else if ( ! $modal.hasClass( 'is-single-site-activation' ) ) {
470 toggleActivationOnAllSites();
471 }
472 });
473
474 if ( ! hasLicensesDropdown ) {
475 $availableLicenseKey.click(function() {
476 $licenseTypes.filter( '[value="available"]' ).click();
477 });
478 }
479
480 $otherLicenseKeyContainer.click(function() {
481 $licenseTypes.filter( '[value="other"]' ).click();
482 });
483 }
484
485 $body.on( 'click', 'span.activate-license.<?php echo $unique_affix ?> a, .activate-license-trigger.<?php echo $unique_affix ?>', function (evt) {
486 evt.preventDefault();
487
488 showModal( evt );
489 });
490
491 var licenseTimeout = null;
492
493 /**
494 * Disable activation button when license key is empty.
495 *
496 * @author Leo Fajardo (@leorw)
497 * @since 2.3.2
498 */
499 $modal.on( 'keyup paste delete cut', 'input.fs-license-key', function () {
500 clearTimeout(licenseTimeout);
501
502 licenseTimeout = setTimeout( function () {
503 var licenseKey = $licenseKeyInput.val().trim();
504
505 if ( licenseKey == previousLicenseKey ) {
506 afterLicenseUserDataLoaded();
507 return;
508 }
509
510 if ( '' === licenseKey ) {
511 disableActivateLicenseButton();
512 $ownershipChangeOptionContainer.hide();
513 } else {
514 enableActivateLicenseButton();
515
516 if ( 32 <= licenseKey.length ) {
517 fetchLicenseUserData();
518 } else {
519 $ownershipChangeOptionContainer.hide();
520 }
521 }
522
523 previousLicenseKey = licenseKey;
524 }, 200 );
525 } ).focus();
526
527 $modal.on('input propertychange', 'input.fs-license-key', function () {
528
529 var licenseKey = $(this).val().trim();
530
531 /**
532 * If license key is not empty, enable the license activation button.
533 */
534 if ( licenseKey.length > 0 && ( $modal.hasClass( 'is-single-site-activation' ) || ! isNetworkActivation || hasSelectedSite() ) ) {
535 enableActivateLicenseButton();
536 }
537 });
538
539 $modal.on( 'blur', 'input.fs-license-key', function( evt ) {
540 var
541 licenseKey = $(this).val().trim(),
542 $focusedElement = $( evt.relatedTarget ),
543 hasSelectedAvailableLicense = ( hasLicenseTypes && $focusedElement.parents( '.fs-available-license-key-container' ).length > 0 );
544
545 /**
546 * If license key is empty, disable the license activation button.
547 */
548 if ( ( 0 === licenseKey.length && ( ! hasLicenseTypes || ! hasSelectedAvailableLicense ) ) ||
549 ( isNetworkActivation && ! hasSelectedSite() )
550 ) {
551 disableActivateLicenseButton();
552 }
553 });
554
555 $modal.on('click', '.button-activate-license', function (evt) {
556 evt.preventDefault();
557
558 if ($(this).hasClass('disabled')) {
559 return;
560 }
561
562 var
563 licenseKey = '';
564
565 if ( hasLicenseTypes ) {
566 if ( isOtherLicenseKeySelected() ) {
567 licenseKey = $otherLicenseKey.val();
568 } else {
569 if ( ! hasLicensesDropdown ) {
570 licenseKey = $availableLicenseKey.data( 'license-key' );
571 } else {
572 licenseKey = $licensesDropdown.val();
573 }
574 }
575 } else {
576 licenseKey = $licenseKeyInput.val().trim();
577 }
578
579 disableActivateLicenseButton();
580
581 if (0 === licenseKey.length) {
582 return;
583 }
584
585 var data = {
586 action : '<?php echo $fs->get_ajax_action( 'activate_license' ) ?>',
587 security : '<?php echo $fs->get_ajax_security( 'activate_license' ) ?>',
588 license_key: licenseKey,
589 module_id : '<?php echo $fs->get_id() ?>'
590 };
591
592 if ( isNetworkActivation ) {
593 var
594 sites = [];
595
596 if ( null === singleBlogID ) {
597 var
598 applyOnAllSites = $applyOnAllSites.is( ':checked' );
599
600 $sitesListContainer.find( 'tr' ).each(function() {
601 var
602 $this = $( this ),
603 includeSite = ( applyOnAllSites || $this.find( 'input' ).is( ':checked' ) );
604
605 if ( ! includeSite )
606 return;
607
608 var site = {
609 uid : $this.find( '.uid' ).val(),
610 url : $this.find( '.url' ).val(),
611 title : $this.find( '.title' ).val(),
612 language: $this.find( '.language' ).val(),
613 blog_id : $this.find( '.blog-id' ).find( 'span' ).text()
614 };
615
616 sites.push( site );
617 });
618 } else {
619 data.blog_id = singleBlogID;
620 }
621
622 data.sites = sites;
623 }
624
625 if ( $ownershipChangeOptionContainer.find( 'input:checked' ).length > 0 ) {
626 data.user_id = otherLicenseOwnerID;
627 }
628
629 $.ajax({
630 url: <?php echo Freemius::ajax_url() ?>,
631 method: 'POST',
632 data: data,
633 beforeSend: function () {
634 $activateLicenseButton.text( '<?php fs_esc_js_echo_inline( 'Activating license', 'activating-license', $slug ) ?>...' );
635 },
636 success: function( result ) {
637 var resultObj = $.parseJSON( result );
638 if ( resultObj.success ) {
639 closeModal();
640
641 // Redirect to the "Account" page and sync the license.
642 window.location.href = resultObj.next_page;
643 } else {
644 showError( resultObj.error.message ? resultObj.error.message : resultObj.error );
645 resetActivateLicenseButton();
646 }
647 }
648 });
649 });
650
651 // If the user has clicked outside the window, close the modal.
652 $modal.on('click', '.fs-close, .button-secondary', function () {
653 closeModal();
654 return false;
655 });
656 }
657
658 registerEventHandlers();
659
660 $body.trigger('licenseActivationLoaded');
661
662 /**
663 * @author Leo Fajardo (@leorw)
664 * @since 2.0.0
665 */
666 function enableDisableSitesSelection() {
667 var
668 canApplyOnAllSites = $applyOnAllSites.is( ':enabled' ),
669 disableSitesSelection = null;
670
671 if ( ! canApplyOnAllSites ) {
672 var
673 selectedSites = $sitesListContainer.find( 'input[type="checkbox"]:checked' ).length,
674 activationsLeft = Math.max( 0, $activationsLeft.data( 'left' ) - selectedSites );
675
676 disableSitesSelection = ( 0 === activationsLeft );
677
678 $activationsLeft.text( activationsLeft );
679 } else {
680 disableSitesSelection = false;
681 }
682
683 $sitesListContainer
684 .find( 'input[type="checkbox"]:not(:checked)' )
685 .attr( 'disabled', disableSitesSelection );
686 }
687
688 /**
689 * @author Leo Fajardo (@leorw)
690 * @since 2.0.0
691 *
692 * @returns {Boolean}
693 */
694 function isOtherLicenseKeySelected() {
695 return ( hasLicenseTypes && 'other' === $licenseTypes.filter( ':checked' ).val() );
696 }
697
698 /**
699 * @author Leo Fajardo (@leorw)
700 * @since 2.0.0
701 *
702 * @returns {Boolean}
703 */
704 function hasValidLicenseKey() {
705 var licenseKey = '';
706 if ( hasLicenseTypes ) {
707 if ( 'available' === $licenseTypes.filter( ':checked' ).val() ) {
708 return true;
709 } else {
710 licenseKey = $otherLicenseKey.val();
711 }
712 } else {
713 licenseKey = $modal.find( 'input.fs-license-key' ).val();
714 }
715
716 return ( licenseKey.trim().length > 0 );
717 }
718
719 /**
720 * @author Leo Fajardo (@leorw)
721 * @since 2.0.0
722 *
723 * @returns {Boolean}
724 */
725 function hasSelectedSite() {
726 return ( $applyOnAllSites.is( ':checked' ) ||
727 $sitesListContainer.find( 'input[type="checkbox"]:checked:not(:disabled)' ).length > 0 );
728 }
729
730 /**
731 * @author Leo Fajardo (@leorw)
732 * @since 2.0.0
733 */
734 function toggleActivationOnAllSites() {
735 var activationsLeft,
736 licenseID;
737
738 if (hasLicensesDropdown) {
739 var $selectedOption = $licensesDropdown.find( ':selected' );
740 activationsLeft = $selectedOption.data('left');
741 licenseID = $selectedOption.data('id');
742 } else {
743 activationsLeft = $availableLicenseKey.data('left');
744 licenseID = $availableLicenseKey.data('id');
745 }
746
747 // Cleanup previously auto-selected site.
748 $modal.find( '.fs-sites-list-container input[type=checkbox]:disabled' )
749 .attr('disabled', false)
750 .attr('checked', false);
751
752 var $blogsWithActiveLicense = $modal.find( '.fs-sites-list-container tr[data-license-id=' + licenseID + '] input[type=checkbox]' );
753
754 if ($blogsWithActiveLicense.length > 0) {
755 $blogsWithActiveLicense.attr('checked', true)
756 .attr('disabled', true);
757
758 activationsLeft += $blogsWithActiveLicense.length;
759 }
760
761 if ( activationsLeft >= totalSites ) {
762 $applyOnAllSites.attr( 'disabled', false );
763 enableDisableSitesSelection();
764
765 resetActivateLicenseCheckboxLabel();
766
767 return;
768 }
769
770 $applyOnAllSites.attr( 'checked', false );
771 $applyOnAllSites.attr( 'disabled', true );
772
773 showSites( true );
774
775 var
776 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 ) ?>';
777
778 activateLicenseCheckboxLabel = activateLicenseCheckboxLabel.replace( '%s', '<span data-left="' + activationsLeft + '" class="activations-left">' + activationsLeft + '</span>' );
779
780 // Update the label of the "Activate license on all sites" checkbox.
781 $applyOnAllSites.parent().find( 'span' ).html( activateLicenseCheckboxLabel );
782 $activationsLeft = $modal.find( '.activations-left' );
783
784 if ( hasSelectedSite() ) {
785 enableActivateLicenseButton();
786 enableDisableSitesSelection();
787 } else {
788 disableActivateLicenseButton();
789 }
790 }
791
792 /**
793 * @author Leo Fajardo (@leorw)
794 * @since 2.0.0
795 */
796 function resetActivateLicenseCheckboxLabel() {
797 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 ) ?>';
798 $applyOnAllSites.parent().find( 'span' ).text( activateLicenseCheckboxLabel );
799 }
800
801 /**
802 * @author Leo Fajardo (@leorw)
803 * @since 2.0.0
804 *
805 * @param {Boolean} show
806 */
807 function showSites( show ) {
808 $sitesListContainer.toggle( show );
809 if ( show && null === maxSitesListHeight ) {
810 /**
811 * Set the visible number of rows to 5 (5 * height of the first row).
812 *
813 * @author Leo Fajardo (@leorw)
814 */
815 maxSitesListHeight = ( 5 * $sitesListContainer.find( 'tr:first' ).height() );
816 $sitesListContainer.css( 'max-height', maxSitesListHeight );
817 }
818 }
819
820 function showModal( evt ) {
821 resetModal();
822
823 // Display the dialog box.
824 $modal.addClass('active');
825 $body.addClass('has-fs-modal');
826
827 var
828 $singleInstallDetails = $( evt.target ).parents( 'tr.fs-install-details' ),
829 isSingleSiteActivation = ( $singleInstallDetails.length > 0 );
830
831 $modal.toggleClass( 'is-single-site-activation', isSingleSiteActivation );
832
833 singleBlogID = isSingleSiteActivation ?
834 $singleInstallDetails.prev().data( 'blog-id' ) :
835 null;
836
837 <?php if ( $fs->apply_filters( 'enable_per_site_activation', true ) ) : ?>
838 $multisiteOptionsContainer.toggle( isNetworkActivation && ! isSingleSiteActivation );
839 <?php endif ?>
840
841 if ( hasLicenseTypes ) {
842 $licenseTypes.attr( 'checked', false );
843
844 if ( hasLicensesDropdown ) {
845 $licensesDropdown.find( 'option:first' ).attr( 'selected', true ).trigger( 'change' );
846 } else {
847 $licenseTypes.filter( '[value="available"]' ).click();
848 }
849
850 $otherLicenseKey.val( '' );
851 } else {
852 $licenseKeyInput.val( '' );
853 $licenseKeyInput.focus();
854 }
855 }
856
857 function closeModal() {
858 $modal.removeClass('active');
859 $body.removeClass('has-fs-modal');
860 }
861
862 function resetActivateLicenseButton() {
863 enableActivateLicenseButton();
864 $activateLicenseButton.text( <?php echo json_encode( $activate_button_text ) ?> );
865 }
866
867 function resetModal() {
868 hideError();
869 resetActivateLicenseButton();
870 }
871
872 function enableActivateLicenseButton() {
873 $activateLicenseButton.removeClass( 'disabled' );
874 }
875
876 function disableActivateLicenseButton() {
877 $activateLicenseButton.addClass( 'disabled' );
878 }
879
880 function hideError() {
881 $licenseActivationMessage.hide();
882 }
883
884 function showError( msg ) {
885 $licenseActivationMessage.find( ' > p' ).html( msg );
886 $licenseActivationMessage.show();
887 }
888 });
889 })( jQuery );
890 </script>
891