| 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.2.1.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @var array $VARS |
| 15 |
* @var Freemius $fs |
| 16 |
*/ |
| 17 |
$slug = $VARS['slug']; |
| 18 |
$fs = freemius( $slug ); |
| 19 |
|
| 20 |
$action = $fs->is_tracking_allowed() ? |
| 21 |
'stop_tracking' : |
| 22 |
'allow_tracking'; |
| 23 |
|
| 24 |
$plugin_title = "<strong>{$fs->get_plugin()->title}</strong>"; |
| 25 |
$opt_out_button_text = fs_text( 'opt-out', $slug ); |
| 26 |
// @todo Change 'plugin' with module type when migrating with 1.2.2 (themes version). |
| 27 |
$opt_out_message_appreciation = sprintf( fs_text( 'opt-out-message-appreciation', $slug ), 'plugin' ); |
| 28 |
$opt_out_message_usage_tracking = sprintf( fs_text( 'opt-out-message-usage-tracking', $slug ), $plugin_title ); |
| 29 |
$opt_out_message_clicking_opt_out = sprintf( |
| 30 |
fs_text( 'opt-out-message-clicking-opt-out', $slug ), |
| 31 |
$plugin_title, |
| 32 |
sprintf( |
| 33 |
'<a href="%s" target="_blank">%s</a>', |
| 34 |
'https://freemius.com', |
| 35 |
'freemius.com' |
| 36 |
) |
| 37 |
); |
| 38 |
|
| 39 |
$admin_notice_params = array( |
| 40 |
'id' => '', |
| 41 |
'slug' => $fs->get_id(), |
| 42 |
'type' => 'success', |
| 43 |
'sticky' => false, |
| 44 |
'plugin' => $fs->get_plugin()->title, |
| 45 |
'message' => $opt_out_message_appreciation |
| 46 |
); |
| 47 |
|
| 48 |
$admin_notice_html = fs_get_template( 'Admin-notice.php', $admin_notice_params ); |
| 49 |
|
| 50 |
$modal_content_html = <<< HTML |
| 51 |
<h2>{$opt_out_message_appreciation}</h2> |
| 52 |
<div class="notice notice-error inline opt-out-error-message"><p></p></div> |
| 53 |
<p>{$opt_out_message_usage_tracking}</p> |
| 54 |
<p>{$opt_out_message_clicking_opt_out}</p> |
| 55 |
HTML; |
| 56 |
|
| 57 |
fs_enqueue_local_style( 'dialog-boxes', '/admin/dialog-boxes.css' ); |
| 58 |
fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); |
| 59 |
?> |
| 60 |
<script type="text/javascript"> |
| 61 |
(function( $ ) { |
| 62 |
$( document ).ready(function() { |
| 63 |
var modalContentHtml = <?php echo json_encode( $modal_content_html ) ?>, |
| 64 |
modalHtml = |
| 65 |
'<div class="fs-modal fs-modal-opt-out">' |
| 66 |
+ ' <div class="fs-modal-dialog">' |
| 67 |
+ ' <div class="fs-modal-header">' |
| 68 |
+ ' <h4><?php echo esc_js($opt_out_button_text) ?></h4>' |
| 69 |
+ ' </div>' |
| 70 |
+ ' <div class="fs-modal-body">' |
| 71 |
+ ' <div class="fs-modal-panel active">' + modalContentHtml + '</div>' |
| 72 |
+ ' </div>' |
| 73 |
+ ' <div class="fs-modal-footer">' |
| 74 |
+ ' <button class="button button-secondary button-opt-out" tabindex="1"><?php echo esc_js($opt_out_button_text) ?></button>' |
| 75 |
+ ' <button class="button button-primary button-close" tabindex="2"><?php fs_esc_js_echo( 'opt-out-cancel', $slug ) ?></button>' |
| 76 |
+ ' </div>' |
| 77 |
+ ' </div>' |
| 78 |
+ '</div>', |
| 79 |
$modal = $( modalHtml ), |
| 80 |
$adminNotice = $( <?php echo json_encode( $admin_notice_html ) ?> ), |
| 81 |
action = '<?php echo $action ?>', |
| 82 |
$actionLink = $( 'span.opt-in-or-opt-out.<?php echo $slug ?> a' ), |
| 83 |
$optOutButton = $modal.find( '.button-opt-out' ), |
| 84 |
$optOutErrorMessage = $modal.find( '.opt-out-error-message' ), |
| 85 |
pluginSlug = '<?php echo $slug ?>'; |
| 86 |
|
| 87 |
$actionLink.attr( 'data-action', action ); |
| 88 |
$modal.appendTo( $( 'body' ) ); |
| 89 |
|
| 90 |
function registerEventHandlers() { |
| 91 |
$actionLink.click(function( evt ) { |
| 92 |
evt.preventDefault(); |
| 93 |
|
| 94 |
if ( 'stop_tracking' == $actionLink.attr( 'data-action' ) ) { |
| 95 |
showModal(); |
| 96 |
} else { |
| 97 |
optIn(); |
| 98 |
} |
| 99 |
}); |
| 100 |
|
| 101 |
$modal.on( 'click', '.button-opt-out', function( evt ) { |
| 102 |
evt.preventDefault(); |
| 103 |
|
| 104 |
if ( $( this ).hasClass( 'disabled' ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
disableOptOutButton(); |
| 109 |
optOut(); |
| 110 |
}); |
| 111 |
|
| 112 |
// If the user has clicked outside the window, close the modal. |
| 113 |
$modal.on( 'click', '.fs-close, .button-close', function() { |
| 114 |
closeModal(); |
| 115 |
return false; |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
registerEventHandlers(); |
| 120 |
|
| 121 |
function showModal() { |
| 122 |
resetModal(); |
| 123 |
|
| 124 |
// Display the dialog box. |
| 125 |
$modal.addClass( 'active' ); |
| 126 |
$( 'body' ).addClass( 'has-fs-modal' ); |
| 127 |
} |
| 128 |
|
| 129 |
function closeModal() { |
| 130 |
$modal.removeClass( 'active' ); |
| 131 |
$( 'body' ).removeClass( 'has-fs-modal' ); |
| 132 |
} |
| 133 |
|
| 134 |
function resetOptOutButton() { |
| 135 |
enableOptOutButton(); |
| 136 |
$optOutButton.text( <?php echo json_encode( $opt_out_button_text ) ?> ); |
| 137 |
} |
| 138 |
|
| 139 |
function resetModal() { |
| 140 |
hideError(); |
| 141 |
resetOptOutButton(); |
| 142 |
} |
| 143 |
|
| 144 |
function optIn() { |
| 145 |
sendRequest(); |
| 146 |
} |
| 147 |
|
| 148 |
function optOut() { |
| 149 |
sendRequest(); |
| 150 |
} |
| 151 |
|
| 152 |
function sendRequest() { |
| 153 |
$.ajax({ |
| 154 |
url: ajaxurl, |
| 155 |
method: 'POST', |
| 156 |
data: { |
| 157 |
action : ( 'stop_tracking' == action ? |
| 158 |
'<?php echo $fs->get_ajax_action( 'stop_tracking' ) ?>' : |
| 159 |
'<?php echo $fs->get_ajax_action( 'allow_tracking' ) ?>' |
| 160 |
), |
| 161 |
security : ( 'stop_tracking' == action ? |
| 162 |
'<?php echo $fs->get_ajax_security( 'stop_tracking' ) ?>' : |
| 163 |
'<?php echo $fs->get_ajax_security( 'allow_tracking' ) ?>' |
| 164 |
), |
| 165 |
slug : pluginSlug |
| 166 |
}, |
| 167 |
beforeSend: function() { |
| 168 |
if ( 'opt-in' == action ) { |
| 169 |
$actionLink.text( <?php fs_json_encode_echo( 'opting-in', $slug ) ?> ); |
| 170 |
} else { |
| 171 |
$optOutButton.text( <?php fs_json_encode_echo( 'opting-out', $slug ) ?> ); |
| 172 |
} |
| 173 |
}, |
| 174 |
success: function( resultObj ) { |
| 175 |
if ( resultObj.success ) { |
| 176 |
if ( 'allow_tracking' == action ) { |
| 177 |
action = 'stop_tracking'; |
| 178 |
$actionLink.text( <?php fs_json_encode_echo( 'opt-out', $slug ) ?> ); |
| 179 |
showOptInAppreciationMessageAndScrollToTop(); |
| 180 |
} else { |
| 181 |
action = 'allow_tracking'; |
| 182 |
$actionLink.text( <?php fs_json_encode_echo( 'opt-in', $slug ) ?> ); |
| 183 |
closeModal(); |
| 184 |
|
| 185 |
if ( $adminNotice.length > 0 ) { |
| 186 |
$adminNotice.remove(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$actionLink.attr( 'data-action', action ); |
| 191 |
} else { |
| 192 |
showError( resultObj.error ); |
| 193 |
resetOptOutButton(); |
| 194 |
} |
| 195 |
} |
| 196 |
}); |
| 197 |
} |
| 198 |
|
| 199 |
function enableOptOutButton() { |
| 200 |
$optOutButton.removeClass( 'disabled' ); |
| 201 |
} |
| 202 |
|
| 203 |
function disableOptOutButton() { |
| 204 |
$optOutButton.addClass( 'disabled' ); |
| 205 |
} |
| 206 |
|
| 207 |
function hideError() { |
| 208 |
$optOutErrorMessage.hide(); |
| 209 |
} |
| 210 |
|
| 211 |
function showOptInAppreciationMessageAndScrollToTop() { |
| 212 |
$adminNotice.insertAfter( $( '#wpbody-content' ).find( ' > .wrap > h1' ) ); |
| 213 |
window.scrollTo(0, 0); |
| 214 |
} |
| 215 |
|
| 216 |
function showError( msg ) { |
| 217 |
$optOutErrorMessage.find( ' > p' ).html( msg ); |
| 218 |
$optOutErrorMessage.show(); |
| 219 |
} |
| 220 |
}); |
| 221 |
})( jQuery ); |
| 222 |
</script> |
| 223 |
|