form.php
363 lines
| 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.2 |
| 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 | $confirmation_message = $fs->apply_filters( 'uninstall_confirmation_message', '' ); |
| 20 | |
| 21 | $reasons = $VARS['reasons']; |
| 22 | |
| 23 | $reasons_list_items_html = ''; |
| 24 | |
| 25 | foreach ( $reasons as $reason ) { |
| 26 | $list_item_classes = 'reason' . ( ! empty( $reason['input_type'] ) ? ' has-input' : '' ); |
| 27 | |
| 28 | if ( isset( $reason['internal_message'] ) && ! empty( $reason['internal_message'] ) ) { |
| 29 | $list_item_classes .= ' has-internal-message'; |
| 30 | $reason_internal_message = $reason['internal_message']; |
| 31 | } else { |
| 32 | $reason_internal_message = ''; |
| 33 | } |
| 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 = |
| 55 | '<label class="anonymous-feedback-label"><input type="checkbox" class="anonymous-feedback-checkbox"> ' |
| 56 | . fs_text( 'anonymous-feedback', $slug ) |
| 57 | . '</label>'; |
| 58 | } else { |
| 59 | $anonymous_feedback_checkbox_html = ''; |
| 60 | } |
| 61 | |
| 62 | fs_enqueue_local_style( 'dialog-boxes', '/admin/dialog-boxes.css' ); |
| 63 | ?> |
| 64 | <script type="text/javascript"> |
| 65 | (function ($) { |
| 66 | var reasonsHtml = <?php echo json_encode( $reasons_list_items_html ); ?>, |
| 67 | modalHtml = |
| 68 | '<div class="fs-modal fs-modal-deactivation-feedback<?php echo empty( $confirmation_message ) ? ' no-confirmation-message' : ''; ?>">' |
| 69 | + ' <div class="fs-modal-dialog">' |
| 70 | + ' <div class="fs-modal-header">' |
| 71 | + ' <h4><?php fs_esc_attr_echo('quick-feedback' , $slug) ?></h4>' |
| 72 | + ' </div>' |
| 73 | + ' <div class="fs-modal-body">' |
| 74 | + ' <div class="fs-modal-panel" data-panel-id="confirm"><p><?php echo $confirmation_message; ?></p></div>' |
| 75 | + ' <div class="fs-modal-panel active" data-panel-id="reasons"><h3><strong><?php fs_esc_attr_echo( 'deactivation-share-reason' , $slug ) ?>:</strong></h3><ul id="reasons-list">' + reasonsHtml + '</ul></div>' |
| 76 | + ' </div>' |
| 77 | + ' <div class="fs-modal-footer">' |
| 78 | + ' <?php echo $anonymous_feedback_checkbox_html ?>' |
| 79 | + ' <a href="#" class="button button-secondary button-deactivate"></a>' |
| 80 | + ' <a href="#" class="button button-primary button-close"><?php fs_echo( 'cancel' , $slug ) ?></a>' |
| 81 | + ' </div>' |
| 82 | + ' </div>' |
| 83 | + '</div>', |
| 84 | $modal = $(modalHtml), |
| 85 | $deactivateLink = $('#the-list .deactivate > [data-slug=<?php echo $VARS['slug']; ?>].fs-slug').prev(), |
| 86 | $anonymousFeedback = $modal.find( '.anonymous-feedback-label' ), |
| 87 | isAnonymous = <?php echo ( $is_anonymous ? 'true' : 'false' ); ?>, |
| 88 | selectedReasonID = false, |
| 89 | otherReasonID = <?php echo Freemius::REASON_OTHER; ?>, |
| 90 | dontShareDataReasonID = <?php echo Freemius::REASON_DONT_LIKE_TO_SHARE_MY_INFORMATION; ?>; |
| 91 | |
| 92 | $modal.appendTo($('body')); |
| 93 | |
| 94 | registerEventHandlers(); |
| 95 | |
| 96 | function registerEventHandlers() { |
| 97 | $deactivateLink.click(function (evt) { |
| 98 | evt.preventDefault(); |
| 99 | |
| 100 | showModal(); |
| 101 | }); |
| 102 | |
| 103 | $modal.on('input propertychange', '.reason-input input', function () { |
| 104 | if (!isOtherReasonSelected()) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | var reason = $(this).val().trim(); |
| 109 | |
| 110 | /** |
| 111 | * If reason is not empty, remove the error-message class of the message container |
| 112 | * to change the message color back to default. |
| 113 | */ |
| 114 | if (reason.length > 0) { |
| 115 | $('.message').removeClass('error-message'); |
| 116 | enableDeactivateButton(); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | $modal.on('blur', '.reason-input input', function () { |
| 121 | var $userReason = $(this); |
| 122 | |
| 123 | setTimeout(function () { |
| 124 | if (!isOtherReasonSelected()) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * If reason is empty, add the error-message class to the message container |
| 130 | * to change the message color to red. |
| 131 | */ |
| 132 | if (0 === $userReason.val().trim().length) { |
| 133 | $('.message').addClass('error-message'); |
| 134 | disableDeactivateButton(); |
| 135 | } |
| 136 | }, 150); |
| 137 | }); |
| 138 | |
| 139 | $modal.on('click', '.fs-modal-footer .button', function (evt) { |
| 140 | evt.preventDefault(); |
| 141 | |
| 142 | if ($(this).hasClass('disabled')) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | var _parent = $(this).parents('.fs-modal:first'); |
| 147 | var _this = $(this); |
| 148 | |
| 149 | if (_this.hasClass('allow-deactivate')) { |
| 150 | var $radio = $('input[type="radio"]:checked'); |
| 151 | |
| 152 | if (0 === $radio.length) { |
| 153 | // If no selected reason, just deactivate the plugin. |
| 154 | window.location.href = $deactivateLink.attr('href'); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | var $selected_reason = $radio.parents('li:first'), |
| 159 | $input = $selected_reason.find('textarea, input[type="text"]'), |
| 160 | userReason = ( 0 !== $input.length ) ? $input.val().trim() : ''; |
| 161 | |
| 162 | if (isOtherReasonSelected() && ( '' === userReason )) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | $.ajax({ |
| 167 | url : ajaxurl, |
| 168 | method : 'POST', |
| 169 | data : { |
| 170 | action : '<?php echo $fs->get_ajax_action( 'submit_uninstall_reason' ) ?>', |
| 171 | security : '<?php echo $fs->get_ajax_security( 'submit_uninstall_reason' ) ?>', |
| 172 | slug : '<?php echo $slug ?>', |
| 173 | reason_id : $radio.val(), |
| 174 | reason_info : userReason, |
| 175 | is_anonymous: isAnonymousFeedback() |
| 176 | }, |
| 177 | beforeSend: function () { |
| 178 | _parent.find('.fs-modal-footer .button').addClass('disabled'); |
| 179 | _parent.find('.fs-modal-footer .button-secondary').text('Processing...'); |
| 180 | }, |
| 181 | complete : function () { |
| 182 | // Do not show the dialog box, deactivate the plugin. |
| 183 | window.location.href = $deactivateLink.attr('href'); |
| 184 | } |
| 185 | }); |
| 186 | } else if (_this.hasClass('button-deactivate')) { |
| 187 | // Change the Deactivate button's text and show the reasons panel. |
| 188 | _parent.find('.button-deactivate').addClass('allow-deactivate'); |
| 189 | |
| 190 | showPanel('reasons'); |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | $modal.on('click', 'input[type="radio"]', function () { |
| 195 | var $selectedReasonOption = $(this); |
| 196 | |
| 197 | // If the selection has not changed, do not proceed. |
| 198 | if (selectedReasonID === $selectedReasonOption.val()) |
| 199 | return; |
| 200 | |
| 201 | selectedReasonID = $selectedReasonOption.val(); |
| 202 | |
| 203 | if ( isAnonymous ) { |
| 204 | if ( isReasonSelected( dontShareDataReasonID ) ) { |
| 205 | $anonymousFeedback.hide(); |
| 206 | } else { |
| 207 | $anonymousFeedback.show(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | var _parent = $(this).parents('li:first'); |
| 212 | |
| 213 | $modal.find('.reason-input').remove(); |
| 214 | $modal.find( '.internal-message' ).hide(); |
| 215 | $modal.find('.button-deactivate').text('<?php printf( fs_text( 'deactivation-modal-button-submit' , $slug ) ); ?>'); |
| 216 | |
| 217 | enableDeactivateButton(); |
| 218 | |
| 219 | if ( _parent.hasClass( 'has-internal-message' ) ) { |
| 220 | _parent.find( '.internal-message' ).show(); |
| 221 | } |
| 222 | |
| 223 | if (_parent.hasClass('has-input')) { |
| 224 | var inputType = _parent.data('input-type'), |
| 225 | inputPlaceholder = _parent.data('input-placeholder'), |
| 226 | reasonInputHtml = '<div class="reason-input"><span class="message"></span>' + ( ( 'textfield' === inputType ) ? '<input type="text" />' : '<textarea rows="5"></textarea>' ) + '</div>'; |
| 227 | |
| 228 | _parent.append($(reasonInputHtml)); |
| 229 | _parent.find('input, textarea').attr('placeholder', inputPlaceholder).focus(); |
| 230 | |
| 231 | if (isOtherReasonSelected()) { |
| 232 | showMessage('<?php printf( fs_text( 'ask-for-reason-message' , $slug ) ); ?>'); |
| 233 | disableDeactivateButton(); |
| 234 | } |
| 235 | } |
| 236 | }); |
| 237 | |
| 238 | // If the user has clicked outside the window, cancel it. |
| 239 | $modal.on('click', function (evt) { |
| 240 | var $target = $(evt.target); |
| 241 | |
| 242 | // If the user has clicked anywhere in the modal dialog, just return. |
| 243 | if ($target.hasClass('fs-modal-body') || $target.hasClass('fs-modal-footer')) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | // If the user has not clicked the close button and the clicked element is inside the modal dialog, just return. |
| 248 | if (!$target.hasClass('button-close') && ( $target.parents('.fs-modal-body').length > 0 || $target.parents('.fs-modal-footer').length > 0 )) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | closeModal(); |
| 253 | return false; |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | function isAnonymousFeedback() { |
| 258 | if ( ! isAnonymous ) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | return ( isReasonSelected( dontShareDataReasonID ) || $anonymousFeedback.find( 'input' ).prop( 'checked' ) ); |
| 263 | } |
| 264 | |
| 265 | function isReasonSelected( reasonID ) { |
| 266 | // Get the selected radio input element. |
| 267 | var $selectedReasonOption = $modal.find('input[type="radio"]:checked'); |
| 268 | |
| 269 | return ( reasonID == $selectedReasonOption.val() ); |
| 270 | } |
| 271 | |
| 272 | function isOtherReasonSelected() { |
| 273 | return isReasonSelected( otherReasonID ); |
| 274 | } |
| 275 | |
| 276 | function showModal() { |
| 277 | resetModal(); |
| 278 | |
| 279 | // Display the dialog box. |
| 280 | $modal.addClass('active'); |
| 281 | |
| 282 | $('body').addClass('has-fs-modal'); |
| 283 | } |
| 284 | |
| 285 | function closeModal() { |
| 286 | $modal.removeClass('active'); |
| 287 | |
| 288 | $('body').removeClass('has-fs-modal'); |
| 289 | } |
| 290 | |
| 291 | function resetModal() { |
| 292 | selectedReasonID = false; |
| 293 | |
| 294 | enableDeactivateButton(); |
| 295 | |
| 296 | // Uncheck all radio buttons. |
| 297 | $modal.find('input[type="radio"]').prop('checked', false); |
| 298 | |
| 299 | // Remove all input fields ( textfield, textarea ). |
| 300 | $modal.find('.reason-input').remove(); |
| 301 | |
| 302 | $modal.find('.message').hide(); |
| 303 | |
| 304 | if ( isAnonymous ) { |
| 305 | $anonymousFeedback.find( 'input' ).prop( 'checked', false ); |
| 306 | |
| 307 | // Hide, since by default there is no selected reason. |
| 308 | $anonymousFeedback.hide(); |
| 309 | } |
| 310 | |
| 311 | var $deactivateButton = $modal.find('.button-deactivate'); |
| 312 | |
| 313 | /* |
| 314 | * If the modal dialog has no confirmation message, that is, it has only one panel, then ensure |
| 315 | * that clicking the deactivate button will actually deactivate the plugin. |
| 316 | */ |
| 317 | if ($modal.hasClass('no-confirmation-message')) { |
| 318 | $deactivateButton.addClass('allow-deactivate'); |
| 319 | |
| 320 | showPanel('reasons'); |
| 321 | } else { |
| 322 | $deactivateButton.removeClass('allow-deactivate'); |
| 323 | |
| 324 | showPanel('confirm'); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | function showMessage(message) { |
| 329 | $modal.find('.message').text(message).show(); |
| 330 | } |
| 331 | |
| 332 | function enableDeactivateButton() { |
| 333 | $modal.find('.button-deactivate').removeClass('disabled'); |
| 334 | } |
| 335 | |
| 336 | function disableDeactivateButton() { |
| 337 | $modal.find('.button-deactivate').addClass('disabled'); |
| 338 | } |
| 339 | |
| 340 | function showPanel(panelType) { |
| 341 | $modal.find('.fs-modal-panel').removeClass('active '); |
| 342 | $modal.find('[data-panel-id="' + panelType + '"]').addClass('active'); |
| 343 | |
| 344 | updateButtonLabels(); |
| 345 | } |
| 346 | |
| 347 | function updateButtonLabels() { |
| 348 | var $deactivateButton = $modal.find('.button-deactivate'); |
| 349 | |
| 350 | // Reset the deactivate button's text. |
| 351 | if ('confirm' === getCurrentPanel()) { |
| 352 | $deactivateButton.text('<?php printf( fs_text( 'deactivation-modal-button-confirm' , $slug ) ); ?>'); |
| 353 | } else { |
| 354 | $deactivateButton.text('<?php printf( fs_text( 'skip-deactivate' , $slug ) ); ?>'); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | function getCurrentPanel() { |
| 359 | return $modal.find('.fs-modal-panel.active').attr('data-panel-id'); |
| 360 | } |
| 361 | })(jQuery); |
| 362 | </script> |
| 363 |