Plugin Directory

source: ultimakit-for-wp/tags/1.2.1/src/freemius/templates/forms/user-change.php @ 3110582

Last change on this file since 3110582 was 3110582, checked in by ankitmaru, 4 weeks ago

Minor Fixes & Improvements.

File size: 11.7 KB
Line 
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.3.2
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
21    /**
22     * @var object[] $license_owners
23     */
24    $license_owners = $VARS['license_owners'];
25
26    $change_user_message                  = fs_text_inline( 'By changing the user, you agree to transfer the account ownership to:', 'change-user--message', $slug );
27    $header_title                         = fs_text_inline( 'Change User', 'change-user', $slug );
28    $user_change_button_text              = fs_text_inline( 'I Agree - Change User', 'agree-change-user', $slug );
29    $other_text                           = fs_text_inline( 'Other', 'other', $slug );
30    $enter_email_address_placeholder_text = fs_text_inline( 'Enter email address', 'enter-email-address', $slug );
31
32    $user_change_options_html = <<< HTML
33    <div class="fs-user-change-options-container">
34        <table>
35            <tbody>
36HTML;
37
38        foreach ( $license_owners as $license_owner ) {
39            $user_change_options_html .= <<< HTML
40                <tr class="fs-email-address-container">
41                    <td><input id="fs_email_address_{$license_owner->id}" type="radio" name="fs_email_address" value="{$license_owner->id}"></td>
42                    <td><label for="fs_email_address_{$license_owner->id}">{$license_owner->email}</label></td>
43                </tr>
44HTML;
45        }
46
47        $user_change_options_html .= <<< HTML
48                <tr>
49                    <td><input id="fs_other_email_address_radio" type="radio" name="fs_email_address" value="other"></td>
50                    <td class="fs-other-email-address-container">
51                        <div>
52                            <label for="fs_email_address">{$other_text}: </label>
53                            <div>
54                                <input id="fs_other_email_address_text_field" class="fs-email-address" type="text" placeholder="{$enter_email_address_placeholder_text}" tabindex="1">
55                            </div>
56                        </div>
57                    </td>
58                </tr>
59            </tbody>
60        </table>
61    </div>
62HTML;
63
64    $modal_content_html = <<< HTML
65    <div class="notice notice-error inline fs-change-user-result-message"><p></p></div>
66    <p>{$change_user_message}</p>
67    {$user_change_options_html}
68HTML;
69
70    fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' );
71?>
72<script type="text/javascript">
73(function( $ ) {
74    $( document ).ready(function() {
75        var modalContentHtml            = <?php echo json_encode( $modal_content_html ) ?>,
76            modalHtml                   =
77                '<div class="fs-modal fs-modal-change-user fs-modal-change-user-<?php echo $fs->get_unique_affix() ?>">'
78                + '     <div class="fs-modal-dialog">'
79                + '             <div class="fs-modal-header">'
80                + '                 <h4><?php echo esc_js( $header_title ) ?></h4>'
81                + '         <a href="!#" class="fs-close"><i class="dashicons dashicons-no" title="<?php echo esc_js( fs_text_x_inline( 'Dismiss', 'close window', 'dismiss', $slug ) ) ?>"></i></a>'
82                + '             </div>'
83                + '             <div class="fs-modal-body">'
84                + '                     <div class="fs-modal-panel active">' + modalContentHtml + '</div>'
85                + '             </div>'
86                + '             <div class="fs-modal-footer">'
87                + '                     <button class="button button-secondary button-close" tabindex="4"><?php fs_esc_js_echo_inline( 'Cancel', 'cancel', $slug ) ?></button>'
88                + '                     <button class="button button-primary fs-user-change-button" tabindex="3"><?php echo esc_js( $user_change_button_text ) ?></button>'
89                + '             </div>'
90                + '     </div>'
91                + '</div>',
92            $modal                      = $( modalHtml ),
93            $userChangeButton           = $modal.find( '.fs-user-change-button' ),
94            $otherEmailAddressRadio     = $modal.find( '#fs_other_email_address_radio' ),
95            $changeUserResultMessage    = $modal.find( '.fs-change-user-result-message' ),
96            $otherEmailAddressContainer = $modal.find( '.fs-other-email-address-container' ),
97            $otherEmailAddressTextField = $modal.find( '#fs_other_email_address_text_field' ),
98            $licenseOwners              = $modal.find( 'input[type="radio"][name="fs_email_address"]' );
99
100        $modal.appendTo( $( 'body' ) );
101
102        var previousEmailAddress = null;
103
104        function registerEventHandlers() {
105            $licenseOwners.change( function() {
106                var otherEmailAddress           = $otherEmailAddressTextField.val().trim(),
107                    otherEmailAddressIsSelected = isOtherEmailAddressSelected();
108
109                if ( otherEmailAddressIsSelected ) {
110                    $otherEmailAddressTextField.focus();
111                }
112
113                if ( otherEmailAddress.length > 0 || ! otherEmailAddressIsSelected ) {
114                    enableUserChangeButton();
115                } else {
116                    disableUserChangeButton();
117                }
118            } );
119
120            $otherEmailAddressContainer.click( function () {
121                $otherEmailAddressRadio.click();
122            } );
123
124            // Handle for the "Change User" button on the "Account" page.
125            $( '#fs_change_user' ).click( function ( evt ) {
126                evt.preventDefault();
127
128                showModal( evt );
129            } );
130
131            /**
132             * Disables the "Change User" button when the email address is empty.
133             */
134            $modal.on( 'keyup paste delete cut', 'input#fs_other_email_address_text_field', function () {
135                setTimeout( function () {
136                    var emailAddress = $otherEmailAddressRadio.val().trim();
137
138                    if ( emailAddress === previousEmailAddress ) {
139                        return;
140                    }
141
142                    if ( '' === emailAddress ) {
143                        disableUserChangeButton();
144                    } else {
145                        enableUserChangeButton();
146                    }
147
148                    previousEmailAddress = emailAddress;
149                }, 100 );
150            } ).focus();
151
152            $modal.on( 'input propertychange', 'input#fs_other_email_address_text_field', function () {
153                var emailAddress = $( this ).val().trim();
154
155                /**
156                 * If email address is not empty, enable the "Change User" button.
157                 */
158                if ( emailAddress.length > 0 ) {
159                    enableUserChangeButton();
160                }
161            } );
162
163            $modal.on( 'blur', 'input#fs_other_email_address_text_field', function( evt ) {
164                var emailAddress = $( this ).val().trim();
165
166                /**
167                 * If email address is empty, disable the "Change User" button.
168                 */
169                if ( 0 === emailAddress.length ) {
170                   disableUserChangeButton();
171                }
172            } );
173
174            $modal.on( 'click', '.fs-user-change-button', function ( evt ) {
175                evt.preventDefault();
176
177                if ( $( this ).hasClass( 'disabled' ) ) {
178                    return;
179                }
180
181                var emailAddress   = '',
182                    licenseOwnerID = null;
183
184                if ( ! isOtherEmailAddressSelected() ) {
185                    licenseOwnerID = $licenseOwners.filter( ':checked' ).val();
186                } else {
187                    emailAddress = $otherEmailAddressTextField.val().trim();
188
189                    if ( 0 === emailAddress.length ) {
190                        return;
191                    }
192                }
193
194                disableUserChangeButton();
195
196                $.ajax( {
197                    url       : <?php echo Freemius::ajax_url() ?>,
198                    method    : 'POST',
199                    data      : {
200                        action       : '<?php echo $fs->get_ajax_action( 'change_user' ) ?>',
201                        security     : '<?php echo $fs->get_ajax_security( 'change_user' ) ?>',
202                        email_address: emailAddress,
203                        user_id      : licenseOwnerID,
204                        module_id    : '<?php echo $fs->get_id() ?>'
205                    },
206                    beforeSend: function () {
207                        $userChangeButton
208                            .text( '<?php fs_esc_js_echo_inline( 'Changing user, please wait', 'changing-user-please-wait', $slug ) ?>...' )
209                            .prepend('<i class="fs-ajax-spinner"></i>');
210
211                        $(document.body).css({'cursor': 'wait'});
212                    },
213                    success   : function( result ) {
214                        if ( result.success ) {
215                            // Redirect to the "Account" page.
216                            window.location.reload();
217                        } else {
218                            $(document.body).css({'cursor': 'auto'});
219
220                            showError( result.error.message ? result.error.message : result.error );
221                            resetUserChangeButton();
222                        }
223                    },
224                    error     : function () {
225                        $(document.body).css({'cursor': 'auto'});
226
227                        showError( '<?php fs_esc_js_echo_inline( 'Unexpected error, try again in 5 minutes. If the error persists, please contact support.', 'unexpected-error', $slug ) ?>' );
228
229                        resetUserChangeButton();
230                    }
231                } );
232            } );
233
234            // If the user has clicked outside the window, close the modal.
235            $modal.on( 'click', '.fs-close, .button-secondary', function () {
236                closeModal();
237                return false;
238            } );
239        }
240
241        registerEventHandlers();
242
243        /**
244         * @returns {Boolean}
245         */
246        function isOtherEmailAddressSelected() {
247            return ( 'other' === $licenseOwners.filter( ':checked' ).val() );
248        }
249
250        function showModal() {
251            resetModal();
252
253            // Display the dialog box.
254            $modal.addClass( 'active' );
255            $( 'body' ).addClass( 'has-fs-modal' );
256
257            // Select the first radio button.
258            $licenseOwners.get( 0 ).click();
259
260            $otherEmailAddressTextField.val( '' );
261        }
262
263        function closeModal() {
264            $modal.removeClass( 'active' );
265            $( 'body' ).removeClass( 'has-fs-modal' );
266        }
267
268        function resetUserChangeButton() {
269            enableUserChangeButton();
270            $userChangeButton.text( <?php echo json_encode( $user_change_button_text ) ?> );
271        }
272
273        function resetModal() {
274            hideError();
275            resetUserChangeButton();
276        }
277
278        function enableUserChangeButton() {
279            $userChangeButton.removeClass( 'disabled' );
280        }
281
282        function disableUserChangeButton() {
283            $userChangeButton.addClass( 'disabled' );
284        }
285
286        function hideError() {
287            $changeUserResultMessage.hide();
288        }
289
290        function showError( msg ) {
291            $changeUserResultMessage.find( ' > p' ).html( msg );
292            $changeUserResultMessage.show();
293        }
294    });
295})( jQuery );
296</script>
Note: See TracBrowser for help on using the repository browser.