| 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 2.5.1 |
| 7 |
*/ |
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
?> |
| 12 |
<script type="text/javascript"> |
| 13 |
( function ( $ ) { |
| 14 |
var global = this; |
| 15 |
|
| 16 |
// Namespace. |
| 17 |
global.FS = global.FS || {}; |
| 18 |
|
| 19 |
//region Switches |
| 20 |
|
| 21 |
function toggleSwitches( $switches, isOn ) { |
| 22 |
$switches |
| 23 |
.toggleClass( 'fs-on', ( null != isOn ? ( true === isOn ) : isOn ) ) |
| 24 |
.toggleClass( 'fs-off', ( null != isOn ? ( false === isOn ) : isOn ) ); |
| 25 |
} |
| 26 |
|
| 27 |
function isSwitch( $switch, isOn ) { |
| 28 |
return $switch.hasClass( isOn ? 'fs-on' : 'fs-off' ); |
| 29 |
} |
| 30 |
|
| 31 |
function getSwitchesStates( $switches, isEnabled ) { |
| 32 |
var switchStates = []; |
| 33 |
for ( var i = 0; i < $switches.length; i++ ) { |
| 34 |
switchStates.push( isSwitch( $( $switches[ i ] ), isEnabled ) ); |
| 35 |
} |
| 36 |
|
| 37 |
return switchStates; |
| 38 |
} |
| 39 |
|
| 40 |
//endregion |
| 41 |
|
| 42 |
function toggleGroupOptOut( $button, isEnabled ) { |
| 43 |
setOptInLabel( $button, ! isEnabled ); |
| 44 |
|
| 45 |
$button.data( 'is-enabled', isEnabled ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param {object} $permissionsSection |
| 50 |
* |
| 51 |
* @returns {string[]} |
| 52 |
*/ |
| 53 |
function getGroupPermissionIDs( $permissionsSection ) { |
| 54 |
var permissions = []; |
| 55 |
$permissionsSection.find( 'ul li').each( function() { |
| 56 |
permissions.push( $( this ).data( 'permission-id' ) ); |
| 57 |
}); |
| 58 |
|
| 59 |
return permissions; |
| 60 |
} |
| 61 |
|
| 62 |
function getGroupOptOutButton( $section ) { |
| 63 |
return $section.find( '.fs-group-opt-out-button' ); |
| 64 |
} |
| 65 |
|
| 66 |
//region Opt-in/out Labels |
| 67 |
|
| 68 |
function setUpdatingLabel( $button, isEnabled ) { |
| 69 |
$button.text( isEnabled ? |
| 70 |
'<?php fs_esc_js_echo_inline( 'Opting in', 'opting-in' ) ?>...' : |
| 71 |
'<?php fs_esc_js_echo_inline( 'Opting out', 'opting-out' ) ?>...' |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
function getOptInLabel( isEnabled ) { |
| 76 |
return isEnabled ? |
| 77 |
'<?php echo esc_js( fs_text_x_inline( 'Opt In', 'verb', 'opt-in' ) ) ?>' : |
| 78 |
'<?php echo esc_js( fs_text_x_inline( 'Opt Out', 'verb', 'opt-out' ) ) ?>'; |
| 79 |
} |
| 80 |
|
| 81 |
function setOptInLabel( $button, isEnabled ) { |
| 82 |
$button.text( getOptInLabel( isEnabled ) ); |
| 83 |
} |
| 84 |
|
| 85 |
//endregion |
| 86 |
|
| 87 |
global.FS.Permissions = function () { |
| 88 |
var isUpdating = false; |
| 89 |
|
| 90 |
function updateStarted() { |
| 91 |
isUpdating = true; |
| 92 |
$body.addClass( 'fs-loading' ); |
| 93 |
} |
| 94 |
|
| 95 |
function updateCompleted() { |
| 96 |
isUpdating = false; |
| 97 |
$body.removeClass( 'fs-loading' ); |
| 98 |
} |
| 99 |
|
| 100 |
return { |
| 101 |
isUpdating: function() { |
| 102 |
return isUpdating; |
| 103 |
}, |
| 104 |
/** |
| 105 |
* @param {Number} pluginID |
| 106 |
* @param {Array} permissions |
| 107 |
* @param {Boolean} isEnabled |
| 108 |
* @param {Callback} [success] |
| 109 |
* @param {Callback} [failure] |
| 110 |
* @param {Callback} [complete] |
| 111 |
*/ |
| 112 |
updatePermissions: function( |
| 113 |
pluginID, |
| 114 |
permissions, |
| 115 |
isEnabled, |
| 116 |
success, |
| 117 |
failure, |
| 118 |
complete |
| 119 |
) { |
| 120 |
if ( isUpdating ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
updateStarted(); |
| 125 |
|
| 126 |
var |
| 127 |
$permissionsContainer = $( '#fs_opt_out_' + pluginID ); |
| 128 |
|
| 129 |
$.ajax( { |
| 130 |
url : <?php echo Freemius::ajax_url() ?>, |
| 131 |
method : 'POST', |
| 132 |
data : { |
| 133 |
action : $permissionsContainer.data( 'action' ), |
| 134 |
security : $permissionsContainer.data( 'security' ), |
| 135 |
module_id : pluginID, |
| 136 |
_wp_http_referer: '<?php echo Freemius::current_page_url() ?>', |
| 137 |
permissions : permissions.join( ',' ), |
| 138 |
is_enabled : isEnabled |
| 139 |
}, |
| 140 |
success : function ( resultObj ) { |
| 141 |
if ( resultObj.success ) { |
| 142 |
if ( success ) { |
| 143 |
success( resultObj ); |
| 144 |
} |
| 145 |
} else { |
| 146 |
if ( failure ) { |
| 147 |
failure( resultObj ); |
| 148 |
} |
| 149 |
} |
| 150 |
}, |
| 151 |
error : failure, |
| 152 |
complete: function () { |
| 153 |
if ( complete ) { |
| 154 |
complete(); |
| 155 |
} |
| 156 |
|
| 157 |
updateCompleted(); |
| 158 |
} |
| 159 |
}); |
| 160 |
}, |
| 161 |
updateGroupPermissions: function( |
| 162 |
pluginID, |
| 163 |
groupID, |
| 164 |
isEnabled, |
| 165 |
success, |
| 166 |
failure, |
| 167 |
complete |
| 168 |
) { |
| 169 |
if ( isUpdating ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
var |
| 174 |
$modal = $( '#fs_opt_out_' + pluginID ), |
| 175 |
$permissionsSection = $modal.find( '.fs-permissions-section.fs-' + groupID + '-permissions' ), |
| 176 |
$optOutButton = getGroupOptOutButton( $permissionsSection ), |
| 177 |
$permissions = $permissionsSection.find( 'ul li'), |
| 178 |
permissions = []; |
| 179 |
|
| 180 |
$permissions.each( function() { |
| 181 |
permissions.push( $( this ).data( 'permission-id' ) ); |
| 182 |
}); |
| 183 |
|
| 184 |
setUpdatingLabel( $optOutButton, isEnabled ); |
| 185 |
|
| 186 |
this.updatePermissions( |
| 187 |
pluginID, |
| 188 |
permissions, |
| 189 |
isEnabled, |
| 190 |
function( resultObj ) { |
| 191 |
if ( resultObj.success ) { |
| 192 |
toggleGroupOptOut( $optOutButton, isEnabled ); |
| 193 |
|
| 194 |
// Update permissions state. |
| 195 |
$permissions.toggleClass( 'fs-disabled', ! isEnabled ); |
| 196 |
|
| 197 |
// Update switches state, if there are any. |
| 198 |
toggleSwitches( $permissions.find( '.fs-switch' ), isEnabled ); |
| 199 |
|
| 200 |
if ( success ) { |
| 201 |
success(); |
| 202 |
} |
| 203 |
} |
| 204 |
}, |
| 205 |
function ( resultObj ) { |
| 206 |
setOptInLabel( $optOutButton, isEnabled ); |
| 207 |
|
| 208 |
if ( failure ) { |
| 209 |
failure( resultObj ); |
| 210 |
} |
| 211 |
}, |
| 212 |
complete |
| 213 |
); |
| 214 |
} |
| 215 |
}; |
| 216 |
}(); |
| 217 |
|
| 218 |
var $body = $( 'body' ) |
| 219 |
|
| 220 |
global.FS.OptOut = function ( |
| 221 |
pluginID, |
| 222 |
slug, |
| 223 |
type, |
| 224 |
isRegistered, |
| 225 |
isTrackingAllowed, |
| 226 |
reconnectUrl |
| 227 |
) { |
| 228 |
var $modal = $( '#fs_opt_out_' + pluginID ), |
| 229 |
actionLinkSelector = ('theme' === type ? '#fs_theme_opt_in_out' : 'span.opt-in-or-opt-out.' + slug + ' a' ); |
| 230 |
|
| 231 |
//region Error Handling |
| 232 |
|
| 233 |
function hideError( $optOutErrorMessage ) { |
| 234 |
$optOutErrorMessage = $optOutErrorMessage || $modal.find( '.opt-out-error-message' ); |
| 235 |
$optOutErrorMessage.hide(); |
| 236 |
} |
| 237 |
|
| 238 |
function showError( $optOutErrorMessage, msg ) { |
| 239 |
$optOutErrorMessage.find( ' > p' ).html( msg ); |
| 240 |
$optOutErrorMessage.show(); |
| 241 |
} |
| 242 |
|
| 243 |
//endregion |
| 244 |
|
| 245 |
function backToPermissionsList() { |
| 246 |
$modal.find( '.fs-opt-out-disclaimer' ) |
| 247 |
.hide(); |
| 248 |
|
| 249 |
$modal.find( '.fs-opt-out-permissions' ) |
| 250 |
.show(); |
| 251 |
} |
| 252 |
|
| 253 |
function removeFeedbackIndicators() { |
| 254 |
$modal.find( '.fs-switch-feedback' ) |
| 255 |
.remove(); |
| 256 |
} |
| 257 |
|
| 258 |
//region Modal Dialog |
| 259 |
|
| 260 |
function closeModal() { |
| 261 |
$modal.removeClass( 'active' ); |
| 262 |
$body.removeClass( 'has-fs-modal' ); |
| 263 |
$modal.hide(); |
| 264 |
} |
| 265 |
|
| 266 |
function resetModal() { |
| 267 |
hideError(); |
| 268 |
removeFeedbackIndicators(); |
| 269 |
backToPermissionsList(); |
| 270 |
} |
| 271 |
|
| 272 |
function showModal() { |
| 273 |
resetModal(); |
| 274 |
|
| 275 |
// Display the dialog box. |
| 276 |
$modal.show(); |
| 277 |
$modal.addClass( 'active' ); |
| 278 |
$body.addClass( 'has-fs-modal' ); |
| 279 |
} |
| 280 |
|
| 281 |
//endregion |
| 282 |
|
| 283 |
function registerActionLinkClick() { |
| 284 |
$body.on( 'click', actionLinkSelector, function( evt ) { |
| 285 |
evt.preventDefault(); |
| 286 |
|
| 287 |
showModal(); |
| 288 |
|
| 289 |
return false; |
| 290 |
}); |
| 291 |
} |
| 292 |
|
| 293 |
function registerEventHandlers() { |
| 294 |
// If the user has clicked outside the window, close the modal. |
| 295 |
$modal.on( 'click', '.fs-close, .button-close', function() { |
| 296 |
closeModal(); |
| 297 |
return false; |
| 298 |
} ); |
| 299 |
|
| 300 |
$modal.on( 'click', '.fs-permissions .fs-switch', function () { |
| 301 |
if ( FS.Permissions.isUpdating() ) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
var $switch = $( this ), |
| 306 |
$permission = $switch.closest( '.fs-permission' ); |
| 307 |
|
| 308 |
toggleSwitches( $switch ); |
| 309 |
|
| 310 |
$permission.toggleClass( 'fs-disabled' ); |
| 311 |
|
| 312 |
var $optOutContainer = $switch.closest( '.fs-modal-opt-out' ); |
| 313 |
|
| 314 |
if ( 0 === $optOutContainer.length ) { |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
// Remove previously added feedback element. |
| 319 |
$switch.closest( '.fs-modal-dialog' ) |
| 320 |
.find( '.fs-switch-feedback' ) |
| 321 |
.remove(); |
| 322 |
|
| 323 |
var $switchFeedback = $( '<span class="fs-switch-feedback"><i class="fs-ajax-spinner"></i></span>' ); |
| 324 |
|
| 325 |
$switch.after( $switchFeedback ) |
| 326 |
|
| 327 |
var |
| 328 |
permissionID = $permission.data( 'permission-id' ), |
| 329 |
isEnabled = isSwitch( $switch, true ); |
| 330 |
|
| 331 |
FS.Permissions.updatePermissions( |
| 332 |
$optOutContainer.data( 'plugin-id' ), |
| 333 |
[ permissionID ], |
| 334 |
isEnabled, |
| 335 |
function () { |
| 336 |
$switchFeedback.addClass( 'success' ); |
| 337 |
$switchFeedback.html( '<i class="dashicons dashicons-yes"></i> <?php echo esc_js( fs_text_inline( 'Saved', 'saved' ) ) ?>' ); |
| 338 |
|
| 339 |
var |
| 340 |
$permissionsGroup = $switch.closest( '.fs-permissions-section' ), |
| 341 |
$groupPermissions = $permissionsGroup.find( 'ul li' ); |
| 342 |
|
| 343 |
var allGroupPermissionsUseSameValue = false; |
| 344 |
|
| 345 |
if ( |
| 346 |
isEnabled && |
| 347 |
0 === $groupPermissions.filter( '.fs-disabled' ).length ) |
| 348 |
{ |
| 349 |
allGroupPermissionsUseSameValue = true; |
| 350 |
} else if ( |
| 351 |
! isEnabled && |
| 352 |
$groupPermissions.length === $groupPermissions.filter( '.fs-disabled' ).length |
| 353 |
) { |
| 354 |
allGroupPermissionsUseSameValue = true; |
| 355 |
} |
| 356 |
|
| 357 |
if ( allGroupPermissionsUseSameValue ) { |
| 358 |
toggleGroupOptOut( getGroupOptOutButton( $permissionsGroup ), isEnabled ); |
| 359 |
} |
| 360 |
}, |
| 361 |
function () { |
| 362 |
// Revert switch. |
| 363 |
toggleSwitches( $switch ); |
| 364 |
|
| 365 |
$switchFeedback.remove(); |
| 366 |
} |
| 367 |
) |
| 368 |
}); |
| 369 |
|
| 370 |
// Move back to the permissions list if cancelling opt-out. |
| 371 |
$modal.on( 'click', '.fs-opt-out-disclaimer .fs-opt-out-cancel-button', function ( evt ) { |
| 372 |
backToPermissionsList(); |
| 373 |
}); |
| 374 |
|
| 375 |
$modal.on( 'click', '.fs-opt-out-disclaimer .fs-modal-footer .fs-opt-out-button', function ( evt ) { |
| 376 |
var |
| 377 |
$optOutButton = $( this ), |
| 378 |
$actionLink = $( actionLinkSelector ), |
| 379 |
isEnabled = true, |
| 380 |
$optOutDisclaimer = $( $optOutButton.closest( '.fs-opt-out-disclaimer' )[ 0 ] ), |
| 381 |
groupID = $optOutDisclaimer.data( 'group-id' ), |
| 382 |
$errorMessage = $optOutDisclaimer.find( '.opt-out-error-message' ); |
| 383 |
|
| 384 |
setUpdatingLabel( $optOutButton, ! isEnabled ); |
| 385 |
|
| 386 |
$optOutDisclaimer.find( '.button-primary' ).prop( 'disabled', true ); |
| 387 |
|
| 388 |
hideError( $errorMessage ); |
| 389 |
|
| 390 |
FS.Permissions.updateGroupPermissions( |
| 391 |
pluginID, |
| 392 |
groupID, |
| 393 |
! isEnabled, |
| 394 |
function () { |
| 395 |
if ( 'communication' === groupID ) { |
| 396 |
window.location.reload(); |
| 397 |
} else { |
| 398 |
setOptInLabel( $actionLink, ! isEnabled ); |
| 399 |
|
| 400 |
backToPermissionsList(); |
| 401 |
} |
| 402 |
}, |
| 403 |
function ( resultObj ) { |
| 404 |
setOptInLabel( $optOutButton, false ); |
| 405 |
|
| 406 |
showError( $errorMessage, resultObj.error ); |
| 407 |
}, |
| 408 |
function () { |
| 409 |
if ( 'communication' !== groupID ) { |
| 410 |
setOptInLabel( $optOutButton, false ); |
| 411 |
} |
| 412 |
|
| 413 |
$optOutDisclaimer.find( '.button-primary' ).prop( 'disabled', false ); |
| 414 |
} |
| 415 |
); |
| 416 |
} ); |
| 417 |
|
| 418 |
$modal.on( 'click', '.fs-group-opt-out-button', function ( evt ) { |
| 419 |
evt.preventDefault(); |
| 420 |
|
| 421 |
if ( FS.Permissions.isUpdating() ) { |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
var |
| 426 |
$optOutButton = $( this ), |
| 427 |
groupID = $optOutButton.data( 'group-id' ), |
| 428 |
isEnabled = $optOutButton.data( 'is-enabled' ), |
| 429 |
$optOutDisclaimer = $modal.find( '.fs-' + groupID + '-opt-out' ), |
| 430 |
isConfirmRequired = ( 0 < $optOutDisclaimer.length ), |
| 431 |
$errorMessage = $modal.find( '.fs-opt-out-permissions .opt-out-error-message' ); |
| 432 |
|
| 433 |
$errorMessage.hide(); |
| 434 |
|
| 435 |
if ( isConfirmRequired ) { |
| 436 |
if ( isEnabled ) { |
| 437 |
// Move to disclaimer window. |
| 438 |
$modal.find( '.fs-opt-out-permissions' ) |
| 439 |
.hide(); |
| 440 |
|
| 441 |
$optOutDisclaimer.show(); |
| 442 |
} else { |
| 443 |
// Opt-in. |
| 444 |
FS.Permissions.updateGroupPermissions( |
| 445 |
pluginID, |
| 446 |
groupID, |
| 447 |
! isEnabled, |
| 448 |
( 'communication' !== groupID ) ? |
| 449 |
null : |
| 450 |
function () { |
| 451 |
window.location.reload(); |
| 452 |
}, |
| 453 |
function ( resultObj ) { |
| 454 |
showError( $errorMessage, resultObj.error ); |
| 455 |
} |
| 456 |
); |
| 457 |
} |
| 458 |
} else { |
| 459 |
// Remove previously added feedback element. |
| 460 |
$modal.find( '.fs-switch-feedback' ) |
| 461 |
.remove(); |
| 462 |
|
| 463 |
var $switches = $optOutButton.closest( '.fs-permissions-section' ) |
| 464 |
.find( '.fs-permission .fs-switch' ); |
| 465 |
|
| 466 |
var switchStates = getSwitchesStates( $switches, isEnabled ); |
| 467 |
|
| 468 |
toggleSwitches( $switches, ! isEnabled ); |
| 469 |
|
| 470 |
$switches.closest( '.fs-permission' ) |
| 471 |
.toggleClass( 'fs-disabled', isEnabled ); |
| 472 |
|
| 473 |
var $switchFeedback = $( '<span class="fs-switch-feedback"><i class="fs-ajax-spinner"></i></span>' ); |
| 474 |
|
| 475 |
$optOutButton.after( $switchFeedback ) |
| 476 |
|
| 477 |
setUpdatingLabel( $optOutButton, ! isEnabled ); |
| 478 |
|
| 479 |
FS.Permissions.updatePermissions( |
| 480 |
pluginID, |
| 481 |
getGroupPermissionIDs( $modal.find( '.fs-permissions-section.fs-' + groupID + '-permissions' ) ), |
| 482 |
! isEnabled, |
| 483 |
function () { |
| 484 |
$switchFeedback.addClass( 'success' ); |
| 485 |
$switchFeedback.html( '<i class="dashicons dashicons-yes"></i> <?php echo esc_js( fs_text_inline( 'Saved', 'saved' ) ) ?>' ); |
| 486 |
|
| 487 |
toggleGroupOptOut( $optOutButton, ! isEnabled ); |
| 488 |
}, |
| 489 |
function () { |
| 490 |
// Revert switches to their previous state. |
| 491 |
for ( var i = 0; i < switchStates.length; i++ ) { |
| 492 |
if ( switchStates[ i ] ) { |
| 493 |
toggleSwitches( $( $switches[ i ] ), isEnabled ); |
| 494 |
$( $switches[ i ] ).removeClass( 'fs-disabled' ); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
toggleGroupOptOut( $optOutButton, isEnabled ); |
| 499 |
} |
| 500 |
) |
| 501 |
} |
| 502 |
}); |
| 503 |
} |
| 504 |
|
| 505 |
if ( 'theme' === type ) { |
| 506 |
/** |
| 507 |
* Add opt-in/out button to the active theme's buttons collection |
| 508 |
* in the theme's extended details overlay. |
| 509 |
* |
| 510 |
* @author Vova Feldman (@svovaf) |
| 511 |
* @since 1.2.2.7 |
| 512 |
*/ |
| 513 |
$( '.theme-overlay' ).contentChange( function () { |
| 514 |
if ( 0 === $( '.theme-overlay.active' ).length ) { |
| 515 |
// Add opt-in/out button only to the currently active theme. |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
if ( $( '#fs_theme_opt_in_out' ).length > 0 ) { |
| 520 |
// Button already there. |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
var label = getOptInLabel( ! isTrackingAllowed ), |
| 525 |
href = ( isTrackingAllowed || isRegistered ) ? '' : reconnectUrl, |
| 526 |
$actionLink = $( '<a id="fs_theme_opt_in_out" href="' + encodeURI( href ) + '" class="button">' + label + '</a>' ); |
| 527 |
|
| 528 |
$( '.theme-wrap .theme-actions .active-theme' ).append( $actionLink ); |
| 529 |
|
| 530 |
if ( isRegistered && '' === href ) { |
| 531 |
registerActionLinkClick(); |
| 532 |
} |
| 533 |
}); |
| 534 |
} |
| 535 |
|
| 536 |
if ( isRegistered ) { |
| 537 |
if ( 'theme' !== type ) { |
| 538 |
registerActionLinkClick(); |
| 539 |
} |
| 540 |
|
| 541 |
registerEventHandlers(); |
| 542 |
} |
| 543 |
|
| 544 |
}; |
| 545 |
} )( jQuery ); |
| 546 |
</script> |