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.2.0 |
---|
7 | */ |
---|
8 | |
---|
9 | if ( ! defined( 'ABSPATH' ) ) { |
---|
10 | exit; |
---|
11 | } |
---|
12 | |
---|
13 | /** |
---|
14 | * @var array $VARS |
---|
15 | * @var Freemius $fs |
---|
16 | */ |
---|
17 | $fs = freemius( $VARS['id'] ); |
---|
18 | $slug = $fs->get_slug(); |
---|
19 | |
---|
20 | $message_header = sprintf( |
---|
21 | /* translators: %1$s: Number of trial days; %2$s: Plan name; */ |
---|
22 | fs_text_inline( 'You are 1-click away from starting your %1$s-day free trial of the %2$s plan.', 'start-trial-prompt-header', $slug ), |
---|
23 | '<span class="var-trial_period"></span>', |
---|
24 | '<span class="var-plan_title"></span>' |
---|
25 | ); |
---|
26 | $message_content = sprintf( |
---|
27 | /* translators: %s: Link to freemius.com */ |
---|
28 | fs_text_inline( 'For compliance with the WordPress.org guidelines, before we start the trial we ask that you opt in with your user and non-sensitive site information, allowing the %s to periodically send data to %s to check for version updates and to validate your trial.', 'start-trial-prompt-message', $slug ), |
---|
29 | $fs->get_module_type(), |
---|
30 | sprintf( |
---|
31 | '<a href="%s" target="_blank" rel="noopener">%s</a>', |
---|
32 | 'https://freemius.com', |
---|
33 | 'freemius.com' |
---|
34 | ) |
---|
35 | ); |
---|
36 | |
---|
37 | $modal_content_html = <<< HTML |
---|
38 | <div class="notice notice-error inline"><p></p></div> |
---|
39 | <h3>{$message_header}</h3> |
---|
40 | <p>{$message_content}</p> |
---|
41 | HTML; |
---|
42 | |
---|
43 | fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' ); |
---|
44 | ?> |
---|
45 | <script type="text/javascript"> |
---|
46 | (function ($) { |
---|
47 | $(document).ready(function () { |
---|
48 | var modalContentHtml = <?php echo json_encode( $modal_content_html ); ?>, |
---|
49 | modalHtml = |
---|
50 | '<div class="fs-modal fs-modal-license-key-resend">' |
---|
51 | + ' <div class="fs-modal-dialog">' |
---|
52 | + ' <div class="fs-modal-header">' |
---|
53 | + ' <h4><?php echo esc_js( fs_text_x_inline( 'Start free trial', 'call to action', 'start-free-trial', $slug ) ) ?></h4>' |
---|
54 | + ' </div>' |
---|
55 | + ' <div class="fs-modal-body">' + modalContentHtml + '</div>' |
---|
56 | + ' <div class="fs-modal-footer">' |
---|
57 | + ' <button class="button button-secondary button-close">' + <?php fs_json_encode_echo_inline( 'Cancel', 'cancel', $slug ) ?> +'</button>' |
---|
58 | + ' <button class="button button-primary button-connect">' + <?php fs_json_encode_echo_inline( 'Approve & Start Trial', 'approve-start-trial', $slug ) ?> +'</button>' |
---|
59 | + ' </div>' |
---|
60 | + ' </div>' |
---|
61 | + '</div>', |
---|
62 | $modal = $( modalHtml ), |
---|
63 | trialData; |
---|
64 | |
---|
65 | $modal.appendTo($('body')); |
---|
66 | |
---|
67 | var $errorNotice = $modal.find('.notice-error'); |
---|
68 | |
---|
69 | registerEventHandlers(); |
---|
70 | |
---|
71 | function registerEventHandlers() { |
---|
72 | $modal.on('click', '.button-close', function () { |
---|
73 | closeModal(); |
---|
74 | return false; |
---|
75 | }); |
---|
76 | |
---|
77 | $modal.on('click', '.button-connect', function (evt) { |
---|
78 | evt.preventDefault(); |
---|
79 | |
---|
80 | var $button = $(this); |
---|
81 | |
---|
82 | $.ajax({ |
---|
83 | url : <?php echo Freemius::ajax_url() ?>, |
---|
84 | method : 'POST', |
---|
85 | data : { |
---|
86 | action : '<?php echo $fs->get_ajax_action( 'start_trial' ) ?>', |
---|
87 | security : '<?php echo $fs->get_ajax_security( 'start_trial' ) ?>', |
---|
88 | module_id: '<?php echo $fs->get_id() ?>', |
---|
89 | trial : trialData |
---|
90 | }, |
---|
91 | beforeSend: function () { |
---|
92 | // Disable all buttons during trial activation. |
---|
93 | $modal.find('.button').prop('disabled', true); |
---|
94 | |
---|
95 | $button.text(<?php fs_json_encode_echo_inline( 'Starting trial', 'starting-trial', $slug ) ?> + '...'); |
---|
96 | |
---|
97 | setLoadingMode(); |
---|
98 | }, |
---|
99 | success : function (resultObj) { |
---|
100 | if (resultObj.success) { |
---|
101 | $button.text(<?php fs_json_encode_echo_inline( 'Please wait', 'please-wait', $slug ) ?> + '...'); |
---|
102 | |
---|
103 | // Redirect to the "Account" page and sync the license. |
---|
104 | window.location.href = resultObj.data.next_page; |
---|
105 | } else { |
---|
106 | $button.text(<?php fs_json_encode_echo( 'approve-start-trial', $slug ) ?>); |
---|
107 | |
---|
108 | resetLoadingMode(); |
---|
109 | showError(resultObj.error); |
---|
110 | } |
---|
111 | } |
---|
112 | }); |
---|
113 | }); |
---|
114 | } |
---|
115 | |
---|
116 | window.openTrialConfirmationModal = function showModal(data) { |
---|
117 | resetModal(); |
---|
118 | |
---|
119 | // Display the dialog box. |
---|
120 | $modal.addClass('active'); |
---|
121 | |
---|
122 | trialData = data; |
---|
123 | |
---|
124 | var $modalBody = $modal.find('.fs-modal-body'), |
---|
125 | $var; |
---|
126 | |
---|
127 | for (var key in data) { |
---|
128 | if (data.hasOwnProperty(key)) { |
---|
129 | $var = $modalBody.find('.var-' + key); |
---|
130 | |
---|
131 | if ($var.length > 0) |
---|
132 | $var.html(data[key]); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | $('body').addClass('has-fs-modal'); |
---|
137 | }; |
---|
138 | |
---|
139 | function closeModal() { |
---|
140 | $modal.removeClass('active'); |
---|
141 | |
---|
142 | $('body').removeClass('has-fs-modal'); |
---|
143 | } |
---|
144 | |
---|
145 | function resetModal() { |
---|
146 | hideError(); |
---|
147 | } |
---|
148 | |
---|
149 | function hideError() { |
---|
150 | $errorNotice.hide(); |
---|
151 | } |
---|
152 | |
---|
153 | function setLoadingMode() { |
---|
154 | $modal.find('.button') |
---|
155 | // Re-enable all buttons. |
---|
156 | .prop('disabled', trialData) |
---|
157 | // Stop loading cursor. |
---|
158 | .css({'cursor': 'wait'}); |
---|
159 | |
---|
160 | // Stop loading cursor. |
---|
161 | $(document.body).css({'cursor': 'wait'}); |
---|
162 | } |
---|
163 | |
---|
164 | function resetLoadingMode() { |
---|
165 | $modal.find('.button') |
---|
166 | // Re-enable all buttons. |
---|
167 | .prop('disabled', false) |
---|
168 | // Stop loading cursor. |
---|
169 | .css({'cursor': 'initial'}); |
---|
170 | |
---|
171 | // Stop loading cursor. |
---|
172 | $(document.body).css({'cursor': 'initial'}); |
---|
173 | } |
---|
174 | |
---|
175 | function showError(msg) { |
---|
176 | $errorNotice.find(' > p').html(msg); |
---|
177 | $errorNotice.show(); |
---|
178 | } |
---|
179 | }); |
---|
180 | })(jQuery); |
---|
181 | </script> |
---|