optout.php
12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 1.2.1.5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @var array $VARS
* @var Freemius $fs
*/
$fs = freemius( $VARS['id'] );
$slug = $fs->get_slug();
$action = $fs->is_tracking_allowed() ?
'stop_tracking' :
'allow_tracking';
$reconnect_url = $fs->get_activation_url( array(
'nonce' => wp_create_nonce( $fs->get_unique_affix() . '_reconnect' ),
'fs_action' => ( $fs->get_unique_affix() . '_reconnect' ),
) );
$plugin_title = "<strong>{$fs->get_plugin()->title}</strong>";
$opt_out_text = fs_text_x_inline( 'Opt Out', 'verb', 'opt-out', $slug );
$opt_in_text = fs_text_x_inline( 'Opt In', 'verb', 'opt-in', $slug );
if ( $fs->is_premium() ) {
$opt_in_message_appreciation = fs_text_inline( 'Connectivity to the licensing engine was successfully re-established. Automatic security & feature updates are now available through the WP Admin Dashboard.', 'premium-opt-in-message-appreciation', $slug );
$opt_out_message_subtitle = sprintf( fs_text_inline( 'Warning: Opting out will block automatic updates', 'premium-opt-out-message-appreciation', $slug ), $fs->get_module_type() );
$opt_out_message_usage_tracking = sprintf( fs_text_inline( 'Ongoing connectivity with the licensing engine is essential for receiving automatic security & feature updates of the paid product. To receive these updates, data like your license key, %1$s version, and WordPress version, is periodically sent to the server to check for updates. By opting out, you understand that your site won\'t receive automatic updates for %2$s from within the WP Admin Dashboard. This can put your site at risk, and we highly recommend to keep this connection active. If you do choose to opt-out, you\'ll need to check for %1$s updates and install them manually.', 'premium-opt-out-message-usage-tracking', $slug ), $fs->get_module_type(), $plugin_title );
$primary_cta_label = fs_text_inline( 'I\'d like to keep automatic updates', 'premium-opt-out-cancel', $slug );
} else {
$opt_in_message_appreciation = sprintf( fs_text_inline( 'We appreciate your help in making the %s better by letting us track some usage data.', 'opt-in-message-appreciation', $slug ), $fs->get_module_type() );
$opt_out_message_subtitle = $opt_in_message_appreciation;
$opt_out_message_usage_tracking = sprintf( fs_text_inline( "Usage tracking is done in the name of making %s better. Making a better user experience, prioritizing new features, and more good things. We'd really appreciate if you'll reconsider letting us continue with the tracking.", 'opt-out-message-usage-tracking', $slug ), $plugin_title );
$primary_cta_label = fs_text_inline( 'On second thought - I want to continue helping', 'opt-out-cancel', $slug );
}
$opt_out_message_clicking_opt_out = sprintf(
fs_text_inline( 'By clicking "Opt Out", we will no longer be sending any data from %s to %s.', 'opt-out-message-clicking-opt-out', $slug ),
$plugin_title,
sprintf(
'<a href="%s" target="_blank" rel="noopener">%s</a>',
'https://freemius.com',
'freemius.com'
)
);
$admin_notice_params = array(
'id' => '',
'slug' => $fs->get_id(),
'type' => 'success',
'sticky' => false,
'plugin' => $fs->get_plugin()->title,
'message' => $opt_in_message_appreciation
);
$admin_notice_html = fs_get_template( 'admin-notice.php', $admin_notice_params );
$modal_content_html = "
<h2" . ( $fs->is_premium() ? ' style="color: red"' : '' ) . ">{$opt_out_message_subtitle}</h2>
<div class=\"notice notice-error inline opt-out-error-message\"><p></p></div>
<p>{$opt_out_message_usage_tracking}</p>
<p>{$opt_out_message_clicking_opt_out}</p>
<label class=\"fs-permission-extensions\"><div class=\"fs-switch fs-small fs-round fs-" . ( $fs->is_extensions_tracking_allowed() ? 'on' : 'off' ) . "\"><div class=\"fs-toggle\"></div></div> " . fs_text_inline( 'Plugins & themes tracking' ) . " <span class=\"fs-switch-feedback success\"></span></label>";
fs_enqueue_local_style( 'fs_dialog_boxes', '/admin/dialog-boxes.css' );
fs_enqueue_local_style( 'fs_common', '/admin/common.css' );
?>
<script type="text/javascript">
(function( $ ) {
$( document ).ready(function() {
var modalContentHtml = <?php echo json_encode( $modal_content_html ) ?>,
modalHtml =
'<div class="fs-modal fs-modal-opt-out">'
+ ' <div class="fs-modal-dialog">'
+ ' <div class="fs-modal-header">'
+ ' <h4><?php echo esc_js( $opt_out_text ) ?></h4>'
+ ' </div>'
+ ' <div class="fs-modal-body">'
+ ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>'
+ ' </div>'
+ ' <div class="fs-modal-footer">'
+ ' <button class="button <?php echo $fs->is_premium() ? 'button-primary warn' : 'button-secondary' ?> button-opt-out" tabindex="1"><?php echo esc_js( $opt_out_text ) ?></button>'
+ ' <button class="button <?php echo $fs->is_premium() ? 'button-secondary' : 'button-primary' ?> button-close" tabindex="2"><?php echo esc_js( $primary_cta_label ) ?></button>'
+ ' </div>'
+ ' </div>'
+ '</div>',
$modal = $(modalHtml),
$adminNotice = $( <?php echo json_encode( $admin_notice_html ) ?> ),
action = '<?php echo $action ?>',
actionLinkSelector = 'span.opt-in-or-opt-out.<?php echo $slug ?> a',
$optOutButton = $modal.find( '.button-opt-out' ),
$optOutErrorMessage = $modal.find( '.opt-out-error-message' ),
$extensionsTracking = $modal.find( '.fs-permission-extensions' ),
$body = $( 'body' ),
moduleID = '<?php echo $fs->get_id() ?>';
$modal.data( 'action', action );
$modal.appendTo( $body );
function registerActionLinkClick() {
$body.on( 'click', actionLinkSelector, function( evt ) {
evt.preventDefault();
if ( 'stop_tracking' == $modal.data( 'action' ) ) {
showModal();
} else {
optIn();
}
return false;
});
}
function registerEventHandlers() {
registerActionLinkClick();
$modal.on( 'click', '.button-opt-out', function( evt ) {
evt.preventDefault();
if ( $( this ).hasClass( 'disabled' ) ) {
return;
}
disableOptOutButton();
optOut();
});
// If the user has clicked outside the window, close the modal.
$modal.on( 'click', '.fs-close, .button-close', function() {
closeModal();
return false;
});
}
<?php if ( $fs->is_registered() ) : ?>
registerEventHandlers();
<?php endif ?>
function showModal() {
resetModal();
// Display the dialog box.
$modal.addClass( 'active' );
$body.addClass( 'has-fs-modal' );
}
function closeModal() {
$modal.removeClass( 'active' );
$body.removeClass( 'has-fs-modal' );
}
function resetOptOutButton() {
enableOptOutButton();
$optOutButton.text( <?php echo json_encode( $opt_out_text ) ?> );
}
function resetModal() {
hideError();
resetOptOutButton();
}
function optIn() {
sendRequest();
}
function optOut() {
sendRequest();
}
function sendRequest() {
var $actionLink = $( actionLinkSelector );
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action : ( 'stop_tracking' == action ?
'<?php echo $fs->get_ajax_action( 'stop_tracking' ) ?>' :
'<?php echo $fs->get_ajax_action( 'allow_tracking' ) ?>'
),
security : ( 'stop_tracking' == action ?
'<?php echo $fs->get_ajax_security( 'stop_tracking' ) ?>' :
'<?php echo $fs->get_ajax_security( 'allow_tracking' ) ?>'
),
module_id: moduleID,
_wp_http_referer: '<?php echo $fs->current_page_url() ?>'
},
beforeSend: function() {
if ( 'allow_tracking' == action ) {
$actionLink.text( '<?php fs_esc_js_echo_inline( 'Opting in', 'opting-in', $slug ) ?>...' );
} else {
$optOutButton.text( '<?php fs_esc_js_echo_inline( 'Opting out', 'opting-out', $slug ) ?>...' );
}
},
success: function( resultObj ) {
if ( resultObj.success ) {
if ( 'allow_tracking' == action ) {
action = 'stop_tracking';
$actionLink.text( '<?php echo esc_js( $opt_out_text ) ?>' );
showOptInAppreciationMessageAndScrollToTop();
} else {
action = 'allow_tracking';
$actionLink.text( '<?php echo esc_js( $opt_in_text ) ?>' );
closeModal();
if ( $adminNotice.length > 0 ) {
$adminNotice.remove();
}
}
$modal.data( 'action', action );
} else {
showError( resultObj.error );
resetOptOutButton();
}
}
});
}
var isUpdatingPermission = false;
$extensionsTracking.on('click', function() {
if (isUpdatingPermission) {
return false;
}
isUpdatingPermission = true;
var $switch = $extensionsTracking.find( '.fs-switch' ),
$switchFeedback = $extensionsTracking.find( '.fs-switch-feedback' );
$switch
.toggleClass( 'fs-on' )
.toggleClass( 'fs-off' );
$switchFeedback.html( '<i class="fs-ajax-spinner"></i>' );
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action : '<?php echo $fs->get_ajax_action( 'update_tracking_permission' ) ?>',
security : '<?php echo $fs->get_ajax_security( 'update_tracking_permission' ) ?>',
module_id : moduleID,
_wp_http_referer: '<?php echo $fs->current_page_url() ?>',
permission: 'extensions',
is_enabled: $switch.hasClass('fs-on')
},
success: function( resultObj ) {
if ( resultObj.success ) {
$switchFeedback.html( '<i class="dashicons dashicons-yes"></i> <?php echo esc_js( fs_text_inline( 'Saved', 'saved', $slug ) ) ?>' )
} else {
$switch
.toggleClass( 'fs-on' )
.toggleClass( 'fs-off' );
}
isUpdatingPermission = false;
}
});
});
function enableOptOutButton() {
$optOutButton.removeClass( 'disabled' );
}
function disableOptOutButton() {
$optOutButton.addClass( 'disabled' );
}
function hideError() {
$optOutErrorMessage.hide();
}
function showOptInAppreciationMessageAndScrollToTop() {
$adminNotice.insertAfter( $( '#wpbody-content' ).find( ' > .wrap > h1' ) );
window.scrollTo(0, 0);
}
function showError( msg ) {
$optOutErrorMessage.find( ' > p' ).html( msg );
$optOutErrorMessage.show();
}
<?php if ( $fs->is_theme() ) : ?>
/**
* Add opt-in/out button to the active theme's buttons collection
* in the theme's extended details overlay.
*
* @author Vova Feldman (@svovaf)
* @since 1.2.2.7
*/
$('.theme-overlay').contentChange(function () {
if (0 === $('.theme-overlay.active').length) {
// Add opt-in/out button only to the currently active theme.
return;
}
if ($('#fs_theme_opt_in_out').length > 0){
// Button already there.
return;
}
var label = (('stop_tracking' == action) ?
'<?php echo esc_js( $opt_out_text ) ?>' :
'<?php echo esc_js( $opt_in_text ) ?>'),
href = (('stop_tracking' != action) ?
'<?php echo ( $fs->is_registered() ? '' : esc_js( $reconnect_url ) ) ?>' :
'');
var $actionLink = $('<a id="fs_theme_opt_in_out" href="' + encodeURI(href) + '" class="button">' + label + '</a>');
actionLinkSelector = '#fs_theme_opt_in_out';
$modal.data( 'action', action );
$('.theme-wrap .theme-actions .active-theme').append($actionLink);
if ('' === href) {
registerActionLinkClick();
}
});
<?php endif ?>
});
})( jQuery );
</script>