| 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.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
$fs = freemius( $VARS['id'] ); |
| 14 |
$slug = $fs->get_slug(); |
| 15 |
|
| 16 |
$confirmation_message = $fs->apply_filters( 'uninstall_confirmation_message', '' ); |
| 17 |
|
| 18 |
$reasons = $VARS['reasons']; |
| 19 |
|
| 20 |
$reasons_list_items_html = ''; |
| 21 |
|
| 22 |
foreach ( $reasons as $reason ) { |
| 23 |
$list_item_classes = 'reason' . ( ! empty( $reason['input_type'] ) ? ' has-input' : '' ); |
| 24 |
|
| 25 |
if ( isset( $reason['internal_message'] ) && ! empty( $reason['internal_message'] ) ) { |
| 26 |
$list_item_classes .= ' has-internal-message'; |
| 27 |
$reason_internal_message = $reason['internal_message']; |
| 28 |
} else { |
| 29 |
$reason_internal_message = ''; |
| 30 |
} |
| 31 |
|
| 32 |
$reason_input_type = ( ! empty( $reason['input_type'] ) ? $reason['input_type'] : '' ); |
| 33 |
$reason_input_placeholder = ( ! empty( $reason['input_placeholder'] ) ? $reason['input_placeholder'] : '' ); |
| 34 |
|
| 35 |
$reason_list_item_html = <<< HTML |
| 36 |
<li class="{$list_item_classes}" |
| 37 |
data-input-type="{$reason_input_type}" |
| 38 |
data-input-placeholder="{$reason_input_placeholder}"> |
| 39 |
<label> |
| 40 |
<span> |
| 41 |
<input type="radio" name="selected-reason" value="{$reason['id']}"/> |
| 42 |
</span> |
| 43 |
<span>{$reason['text']}</span> |
| 44 |
</label> |
| 45 |
<div class="internal-message">{$reason_internal_message}</div> |
| 46 |
</li> |
| 47 |
HTML; |
| 48 |
|
| 49 |
$reasons_list_items_html .= $reason_list_item_html; |
| 50 |
} |
| 51 |
|
| 52 |
$is_anonymous = ( ! $fs->is_registered() ); |
| 53 |
if ( $is_anonymous ) { |
| 54 |
$anonymous_feedback_checkbox_html = sprintf( |
| 55 |
'<label class="anonymous-feedback-label"><input type="checkbox" class="anonymous-feedback-checkbox"> %s</label>', |
| 56 |
fs_esc_html_inline( 'Anonymous feedback', 'anonymous-feedback', $slug ) |
| 57 |
); |
| 58 |
} else { |
| 59 |
$anonymous_feedback_checkbox_html = ''; |
| 60 |
} |
| 61 |
|
| 62 |
// Aliases. |
| 63 |
$deactivate_text = fs_text_inline( 'Deactivate', 'deactivate', $slug ); |
| 64 |
$theme_text = fs_text_inline( 'Theme', 'theme', $slug ); |
| 65 |
$activate_x_text = fs_text_inline( 'Activate %s', 'activate-x', $slug ); |
| 66 |
|
| 67 |
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
| 68 |
?> |
| 69 |
<script type="text/javascript"> |
| 70 |
(function ($) { |
| 71 |
var reasonsHtml = <?php echo json_encode( $reasons_list_items_html ); ?>, |
| 72 |
modalHtml = |
| 73 |
'<div class="fs-modal fs-modal-deactivation-feedback<?php echo empty( $confirmation_message ) ? ' no-confirmation-message' : ''; ?>">' |
| 74 |
+ ' <div class="fs-modal-dialog">' |
| 75 |
+ ' <div class="fs-modal-header">' |
| 76 |
+ ' <h4><?php fs_esc_attr_echo_inline( 'Quick feedback', 'quick-feedback' , $slug ) ?></h4>' |
| 77 |
+ ' </div>' |
| 78 |
+ ' <div class="fs-modal-body">' |
| 79 |
+ ' <div class="fs-modal-panel" data-panel-id="confirm"><p><?php echo $confirmation_message; ?></p></div>' |
| 80 |
+ ' <div class="fs-modal-panel active" data-panel-id="reasons"><h3><strong><?php echo esc_js( sprintf( fs_text_inline( 'If you have a moment, please let us know why you are %s', 'deactivation-share-reason' , $slug ), ( $fs->is_plugin() ? fs_text_inline( 'deactivating', 'deactivating', $slug ) : fs_text_inline( 'switching', 'switching', $slug ) ) ) ) ?>:</strong></h3><ul id="reasons-list">' + reasonsHtml + '</ul></div>' |
| 81 |
+ ' </div>' |
| 82 |
+ ' <div class="fs-modal-footer">' |
| 83 |
+ ' <?php echo $anonymous_feedback_checkbox_html ?>' |
| 84 |
+ ' <a href="#" class="button button-secondary button-deactivate"></a>' |
| 85 |
+ ' <a href="#" class="button button-primary button-close"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel' , $slug ) ?></a>' |
| 86 |
+ ' </div>' |
| 87 |
+ ' </div>' |
| 88 |
+ '</div>', |
| 89 |
$modal = $(modalHtml), |
| 90 |
$deactivateLink = $('#the-list .deactivate > [data-module-id=<?php echo $fs->get_id() ?>].fs-module-id').prev(), |
| 91 |
selectedReasonID = false, |
| 92 |
redirectLink = '', |
| 93 |
$anonymousFeedback = $modal.find( '.anonymous-feedback-label' ), |
| 94 |
isAnonymous = <?php echo ( $is_anonymous ? 'true' : 'false' ); ?>, |
| 95 |
otherReasonID = <?php echo Freemius::REASON_OTHER; ?>, |
| 96 |
dontShareDataReasonID = <?php echo Freemius::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION; ?>; |
| 97 |
|
| 98 |
$modal.appendTo($('body')); |
| 99 |
|
| 100 |
registerEventHandlers(); |
| 101 |
|
| 102 |
function registerEventHandlers() { |
| 103 |
<?php |
| 104 |
if ( $fs->is_plugin() ) { ?> |
| 105 |
$deactivateLink.click(function (evt) { |
| 106 |
evt.preventDefault(); |
| 107 |
|
| 108 |
redirectLink = $(this).attr('href'); |
| 109 |
|
| 110 |
showModal(); |
| 111 |
}); |
| 112 |
<?php |
| 113 |
/** |
| 114 |
* For "theme" module type, the modal is shown when the current user clicks on |
| 115 |
* the "Activate" button of any other theme. The "Activate" button is actually |
| 116 |
* a link to the "Themes" page (/wp-admin/themes.php) containing query params |
| 117 |
* that tell WordPress to deactivate the current theme and activate a different theme. |
| 118 |
* |
| 119 |
* @author Leo Fajardo (@leorw) |
| 120 |
* @since 1.2.2 |
| 121 |
* |
| 122 |
* @since 1.2.2.7 Don't trigger the deactivation feedback form if activating the premium version of the theme. |
| 123 |
*/ |
| 124 |
} else { ?> |
| 125 |
$('body').on('click', '.theme-browser .theme:not([data-slug=<?php echo $slug ?>-premium]) .theme-actions .button.activate', function (evt) { |
| 126 |
evt.preventDefault(); |
| 127 |
|
| 128 |
redirectLink = $(this).attr('href'); |
| 129 |
|
| 130 |
showModal(); |
| 131 |
}); |
| 132 |
<?php |
| 133 |
} ?> |
| 134 |
|
| 135 |
$modal.on('input propertychange', '.reason-input input', function () { |
| 136 |
if (!isOtherReasonSelected()) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
var reason = $(this).val().trim(); |
| 141 |
|
| 142 |
/** |
| 143 |
* If reason is not empty, remove the error-message class of the message container |
| 144 |
* to change the message color back to default. |
| 145 |
*/ |
| 146 |
if (reason.length > 0) { |
| 147 |
$('.message').removeClass('error-message'); |
| 148 |
enableDeactivateButton(); |
| 149 |
} |
| 150 |
}); |
| 151 |
|
| 152 |
$modal.on('blur', '.reason-input input', function () { |
| 153 |
var $userReason = $(this); |
| 154 |
|
| 155 |
setTimeout(function () { |
| 156 |
if (!isOtherReasonSelected()) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* If reason is empty, add the error-message class to the message container |
| 162 |
* to change the message color to red. |
| 163 |
*/ |
| 164 |
if (0 === $userReason.val().trim().length) { |
| 165 |
$('.message').addClass('error-message'); |
| 166 |
disableDeactivateButton(); |
| 167 |
} |
| 168 |
}, 150); |
| 169 |
}); |
| 170 |
|
| 171 |
$modal.on('click', '.fs-modal-footer .button', function (evt) { |
| 172 |
evt.preventDefault(); |
| 173 |
|
| 174 |
if ($(this).hasClass('disabled')) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
var _parent = $(this).parents('.fs-modal:first'); |
| 179 |
var _this = $(this); |
| 180 |
|
| 181 |
if (_this.hasClass('allow-deactivate')) { |
| 182 |
var $radio = $('input[type="radio"]:checked'); |
| 183 |
|
| 184 |
if (0 === $radio.length) { |
| 185 |
// If no selected reason, just deactivate the plugin. |
| 186 |
window.location.href = redirectLink; |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
var $selected_reason = $radio.parents('li:first'), |
| 191 |
$input = $selected_reason.find('textarea, input[type="text"]'), |
| 192 |
userReason = ( 0 !== $input.length ) ? $input.val().trim() : ''; |
| 193 |
|
| 194 |
if (isOtherReasonSelected() && ( '' === userReason )) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
$.ajax({ |
| 199 |
url : ajaxurl, |
| 200 |
method : 'POST', |
| 201 |
data : { |
| 202 |
action : '<?php echo $fs->get_ajax_action( 'submit_uninstall_reason' ) ?>', |
| 203 |
security : '<?php echo $fs->get_ajax_security( 'submit_uninstall_reason' ) ?>', |
| 204 |
module_id : '<?php echo $fs->get_id() ?>', |
| 205 |
reason_id : $radio.val(), |
| 206 |
reason_info : userReason, |
| 207 |
is_anonymous: isAnonymousFeedback() |
| 208 |
}, |
| 209 |
beforeSend: function () { |
| 210 |
_parent.find('.fs-modal-footer .button').addClass('disabled'); |
| 211 |
_parent.find('.fs-modal-footer .button-secondary').text('Processing...'); |
| 212 |
}, |
| 213 |
complete : function () { |
| 214 |
// Do not show the dialog box, deactivate the plugin. |
| 215 |
window.location.href = redirectLink; |
| 216 |
} |
| 217 |
}); |
| 218 |
} else if (_this.hasClass('button-deactivate')) { |
| 219 |
// Change the Deactivate button's text and show the reasons panel. |
| 220 |
_parent.find('.button-deactivate').addClass('allow-deactivate'); |
| 221 |
|
| 222 |
showPanel('reasons'); |
| 223 |
} |
| 224 |
}); |
| 225 |
|
| 226 |
$modal.on('click', 'input[type="radio"]', function () { |
| 227 |
var $selectedReasonOption = $(this); |
| 228 |
|
| 229 |
// If the selection has not changed, do not proceed. |
| 230 |
if (selectedReasonID === $selectedReasonOption.val()) |
| 231 |
return; |
| 232 |
|
| 233 |
selectedReasonID = $selectedReasonOption.val(); |
| 234 |
|
| 235 |
if ( isAnonymous ) { |
| 236 |
if ( isReasonSelected( dontShareDataReasonID ) ) { |
| 237 |
$anonymousFeedback.hide(); |
| 238 |
} else { |
| 239 |
$anonymousFeedback.show(); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
var _parent = $(this).parents('li:first'); |
| 244 |
|
| 245 |
$modal.find('.reason-input').remove(); |
| 246 |
$modal.find( '.internal-message' ).hide(); |
| 247 |
$modal.find('.button-deactivate').html('<?php echo esc_js( sprintf( |
| 248 |
fs_text_inline( 'Submit & %s', 'deactivation-modal-button-submit' , $slug ), |
| 249 |
$fs->is_plugin() ? |
| 250 |
$deactivate_text : |
| 251 |
sprintf( $activate_x_text, $theme_text ) |
| 252 |
) ) ?>'); |
| 253 |
|
| 254 |
enableDeactivateButton(); |
| 255 |
|
| 256 |
if ( _parent.hasClass( 'has-internal-message' ) ) { |
| 257 |
_parent.find( '.internal-message' ).show(); |
| 258 |
} |
| 259 |
|
| 260 |
if (_parent.hasClass('has-input')) { |
| 261 |
var inputType = _parent.data('input-type'), |
| 262 |
inputPlaceholder = _parent.data('input-placeholder'), |
| 263 |
reasonInputHtml = '<div class="reason-input"><span class="message"></span>' + ( ( 'textfield' === inputType ) ? '<input type="text" />' : '<textarea rows="5"></textarea>' ) + '</div>'; |
| 264 |
|
| 265 |
_parent.append($(reasonInputHtml)); |
| 266 |
_parent.find('input, textarea').attr('placeholder', inputPlaceholder).focus(); |
| 267 |
|
| 268 |
if (isOtherReasonSelected()) { |
| 269 |
showMessage('<?php echo esc_js( fs_text_inline( 'Kindly tell us the reason so we can improve.', 'ask-for-reason-message' , $slug ) ); ?>'); |
| 270 |
disableDeactivateButton(); |
| 271 |
} |
| 272 |
} |
| 273 |
}); |
| 274 |
|
| 275 |
// If the user has clicked outside the window, cancel it. |
| 276 |
$modal.on('click', function (evt) { |
| 277 |
var $target = $(evt.target); |
| 278 |
|
| 279 |
// If the user has clicked anywhere in the modal dialog, just return. |
| 280 |
if ($target.hasClass('fs-modal-body') || $target.hasClass('fs-modal-footer')) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
// If the user has not clicked the close button and the clicked element is inside the modal dialog, just return. |
| 285 |
if (!$target.hasClass('button-close') && ( $target.parents('.fs-modal-body').length > 0 || $target.parents('.fs-modal-footer').length > 0 )) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
closeModal(); |
| 290 |
return false; |
| 291 |
}); |
| 292 |
} |
| 293 |
|
| 294 |
function isAnonymousFeedback() { |
| 295 |
if ( ! isAnonymous ) { |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
return ( isReasonSelected( dontShareDataReasonID ) || $anonymousFeedback.find( 'input' ).prop( 'checked' ) ); |
| 300 |
} |
| 301 |
|
| 302 |
function isReasonSelected( reasonID ) { |
| 303 |
// Get the selected radio input element. |
| 304 |
var $selectedReasonOption = $modal.find('input[type="radio"]:checked'); |
| 305 |
|
| 306 |
return ( reasonID == $selectedReasonOption.val() ); |
| 307 |
} |
| 308 |
|
| 309 |
function isOtherReasonSelected() { |
| 310 |
return isReasonSelected( otherReasonID ); |
| 311 |
} |
| 312 |
|
| 313 |
function showModal() { |
| 314 |
resetModal(); |
| 315 |
|
| 316 |
// Display the dialog box. |
| 317 |
$modal.addClass('active'); |
| 318 |
|
| 319 |
$('body').addClass('has-fs-modal'); |
| 320 |
} |
| 321 |
|
| 322 |
function closeModal() { |
| 323 |
$modal.removeClass('active'); |
| 324 |
|
| 325 |
$('body').removeClass('has-fs-modal'); |
| 326 |
} |
| 327 |
|
| 328 |
function resetModal() { |
| 329 |
selectedReasonID = false; |
| 330 |
|
| 331 |
enableDeactivateButton(); |
| 332 |
|
| 333 |
// Uncheck all radio buttons. |
| 334 |
$modal.find('input[type="radio"]').prop('checked', false); |
| 335 |
|
| 336 |
// Remove all input fields ( textfield, textarea ). |
| 337 |
$modal.find('.reason-input').remove(); |
| 338 |
|
| 339 |
$modal.find('.message').hide(); |
| 340 |
|
| 341 |
if ( isAnonymous ) { |
| 342 |
$anonymousFeedback.find( 'input' ).prop( 'checked', false ); |
| 343 |
|
| 344 |
// Hide, since by default there is no selected reason. |
| 345 |
$anonymousFeedback.hide(); |
| 346 |
} |
| 347 |
|
| 348 |
var $deactivateButton = $modal.find('.button-deactivate'); |
| 349 |
|
| 350 |
/* |
| 351 |
* If the modal dialog has no confirmation message, that is, it has only one panel, then ensure |
| 352 |
* that clicking the deactivate button will actually deactivate the plugin. |
| 353 |
*/ |
| 354 |
if ($modal.hasClass('no-confirmation-message')) { |
| 355 |
$deactivateButton.addClass('allow-deactivate'); |
| 356 |
|
| 357 |
showPanel('reasons'); |
| 358 |
} else { |
| 359 |
$deactivateButton.removeClass('allow-deactivate'); |
| 360 |
|
| 361 |
showPanel('confirm'); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
function showMessage(message) { |
| 366 |
$modal.find('.message').text(message).show(); |
| 367 |
} |
| 368 |
|
| 369 |
function enableDeactivateButton() { |
| 370 |
$modal.find('.button-deactivate').removeClass('disabled'); |
| 371 |
} |
| 372 |
|
| 373 |
function disableDeactivateButton() { |
| 374 |
$modal.find('.button-deactivate').addClass('disabled'); |
| 375 |
} |
| 376 |
|
| 377 |
function showPanel(panelType) { |
| 378 |
$modal.find('.fs-modal-panel').removeClass('active '); |
| 379 |
$modal.find('[data-panel-id="' + panelType + '"]').addClass('active'); |
| 380 |
|
| 381 |
updateButtonLabels(); |
| 382 |
} |
| 383 |
|
| 384 |
function updateButtonLabels() { |
| 385 |
var $deactivateButton = $modal.find('.button-deactivate'); |
| 386 |
|
| 387 |
// Reset the deactivate button's text. |
| 388 |
if ('confirm' === getCurrentPanel()) { |
| 389 |
$deactivateButton.text('<?php echo esc_js( sprintf( |
| 390 |
fs_text_inline( 'Yes - %s', 'deactivation-modal-button-confirm' , $slug ), |
| 391 |
$fs->is_plugin() ? |
| 392 |
$deactivate_text : |
| 393 |
sprintf( $activate_x_text, $theme_text ) |
| 394 |
) ) ?>'); |
| 395 |
} else { |
| 396 |
$deactivateButton.html('<?php echo esc_js( sprintf( |
| 397 |
fs_text_inline('Skip & %s', 'skip-and-x', $slug ), |
| 398 |
$fs->is_plugin() ? |
| 399 |
$deactivate_text : |
| 400 |
sprintf( $activate_x_text, $theme_text ) |
| 401 |
) ) ?>'); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
function getCurrentPanel() { |
| 406 |
return $modal.find('.fs-modal-panel.active').attr('data-panel-id'); |
| 407 |
} |
| 408 |
})(jQuery); |
| 409 |
</script> |
| 410 |
|