| 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.0.3 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
wp_enqueue_script( 'jquery' ); |
| 14 |
wp_enqueue_script( 'json2' ); |
| 15 |
fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.min.js' ); |
| 16 |
fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' ); |
| 17 |
fs_enqueue_local_style( 'fs_common', '/admin/common.css' ); |
| 18 |
|
| 19 |
/** |
| 20 |
* @var array $VARS |
| 21 |
*/ |
| 22 |
$slug = $VARS['slug']; |
| 23 |
$fs = freemius( $slug ); |
| 24 |
|
| 25 |
$timestamp = time(); |
| 26 |
|
| 27 |
$context_params = array( |
| 28 |
'plugin_id' => $fs->get_id(), |
| 29 |
'plugin_public_key' => $fs->get_public_key(), |
| 30 |
'plugin_version' => $fs->get_plugin_version(), |
| 31 |
); |
| 32 |
|
| 33 |
// Get site context secure params. |
| 34 |
if ( $fs->is_registered() ) { |
| 35 |
$site = $fs->get_site(); |
| 36 |
$plugin_id = fs_request_get( 'plugin_id', $fs->get_id() ); |
| 37 |
|
| 38 |
if ( $plugin_id != $fs->get_id() ) { |
| 39 |
if ( $fs->is_addon_activated( $plugin_id ) ) { |
| 40 |
$fs_addon = Freemius::get_instance_by_id( $plugin_id ); |
| 41 |
$site = $fs_addon->get_site(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$context_params = array_merge( $context_params, FS_Security::instance()->get_context_params( |
| 46 |
$site, |
| 47 |
$timestamp, |
| 48 |
'checkout' |
| 49 |
) ); |
| 50 |
} else { |
| 51 |
$current_user = Freemius::_get_current_wp_user(); |
| 52 |
|
| 53 |
// Add site and user info to the request, this information |
| 54 |
// is NOT being stored unless the user complete the purchase |
| 55 |
// and agrees to the TOS. |
| 56 |
$context_params = array_merge( $context_params, array( |
| 57 |
'user_firstname' => $current_user->user_firstname, |
| 58 |
'user_lastname' => $current_user->user_lastname, |
| 59 |
'user_email' => $current_user->user_email, |
| 60 |
'home_url' => home_url(), |
| 61 |
) ); |
| 62 |
|
| 63 |
$fs_user = Freemius::_get_user_by_email( $current_user->user_email ); |
| 64 |
|
| 65 |
if ( is_object( $fs_user ) ) { |
| 66 |
$context_params = array_merge( $context_params, FS_Security::instance()->get_context_params( |
| 67 |
$fs_user, |
| 68 |
$timestamp, |
| 69 |
'checkout' |
| 70 |
) ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ( $fs->is_payments_sandbox() ) { |
| 75 |
// Append plugin secure token for sandbox mode authentication. |
| 76 |
$context_params['sandbox'] = FS_Security::instance()->get_secure_token( |
| 77 |
$fs->get_plugin(), |
| 78 |
$timestamp, |
| 79 |
'checkout' |
| 80 |
); |
| 81 |
|
| 82 |
/** |
| 83 |
* @since 1.1.7.3 Add security timestamp for sandbox even for anonymous user. |
| 84 |
*/ |
| 85 |
if ( empty( $context_params['s_ctx_ts'] ) ) { |
| 86 |
$context_params['s_ctx_ts'] = $timestamp; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$return_url = $fs->_get_sync_license_url( isset( $_GET['plugin_id'] ) ? $_GET['plugin_id'] : $fs->get_id() ); |
| 91 |
|
| 92 |
$query_params = array_merge( $context_params, $_GET, array( |
| 93 |
// Current plugin version. |
| 94 |
'plugin_version' => $fs->get_plugin_version(), |
| 95 |
'sdk_version' => WP_FS__SDK_VERSION, |
| 96 |
'return_url' => $return_url, |
| 97 |
// Admin CSS URL for style/design competability. |
| 98 |
// 'wp_admin_css' => get_bloginfo('wpurl') . "/wp-Admin/load-styles.php?c=1&load=buttons,wp-Admin,dashicons", |
| 99 |
) ); |
| 100 |
?> |
| 101 |
<div id="fs_checkout" class="wrap" style="margin: 0 0 -65px -20px;"> |
| 102 |
<div id="iframe"></div> |
| 103 |
<script type="text/javascript"> |
| 104 |
// http://stackoverflow.com/questions/4583703/jquery-post-request-not-ajax |
| 105 |
jQuery(function ($) { |
| 106 |
$.extend({ |
| 107 |
form: function (url, data, method) { |
| 108 |
if (method == null) method = 'POST'; |
| 109 |
if (data == null) data = {}; |
| 110 |
|
| 111 |
var form = $('<form>').attr({ |
| 112 |
method: method, |
| 113 |
action: url |
| 114 |
}).css({ |
| 115 |
display: 'none' |
| 116 |
}); |
| 117 |
|
| 118 |
var addData = function (name, data) { |
| 119 |
if ($.isArray(data)) { |
| 120 |
for (var i = 0; i < data.length; i++) { |
| 121 |
var value = data[i]; |
| 122 |
addData(name + '[]', value); |
| 123 |
} |
| 124 |
} else if (typeof data === 'object') { |
| 125 |
for (var key in data) { |
| 126 |
if (data.hasOwnProperty(key)) { |
| 127 |
addData(name + '[' + key + ']', data[key]); |
| 128 |
} |
| 129 |
} |
| 130 |
} else if (data != null) { |
| 131 |
form.append($('<input>').attr({ |
| 132 |
type : 'hidden', |
| 133 |
name : String(name), |
| 134 |
value: String(data) |
| 135 |
})); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
for (var key in data) { |
| 140 |
if (data.hasOwnProperty(key)) { |
| 141 |
addData(key, data[key]); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return form.appendTo('body'); |
| 146 |
} |
| 147 |
}); |
| 148 |
}); |
| 149 |
|
| 150 |
(function ($) { |
| 151 |
$(function () { |
| 152 |
|
| 153 |
var |
| 154 |
// Keep track of the iframe height. |
| 155 |
iframe_height = 800, |
| 156 |
base_url = '<?php echo WP_FS__ADDRESS ?>', |
| 157 |
// Pass the parent page URL into the Iframe in a meaningful way (this URL could be |
| 158 |
// passed via query string or hard coded into the child page, it depends on your needs). |
| 159 |
src = base_url + '/checkout/?<?php echo ( isset( $_REQUEST['XDEBUG_SESSION'] ) ? 'XDEBUG_SESSION=' . $_REQUEST['XDEBUG_SESSION'] . '&' : '' ) . http_build_query( $query_params ) ?>#' + encodeURIComponent(document.location.href), |
| 160 |
|
| 161 |
// Append the Iframe into the DOM. |
| 162 |
iframe = $('<iframe " src="' + src + '" width="100%" height="' + iframe_height + 'px" scrolling="no" frameborder="0" style="background: transparent;"><\/iframe>') |
| 163 |
.appendTo('#iframe'); |
| 164 |
|
| 165 |
FS.PostMessage.init(base_url, [iframe[0]]); |
| 166 |
FS.PostMessage.receiveOnce('height', function (data) { |
| 167 |
var h = data.height; |
| 168 |
if (!isNaN(h) && h > 0 && h != iframe_height) { |
| 169 |
iframe_height = h; |
| 170 |
iframe.height(iframe_height + 'px'); |
| 171 |
|
| 172 |
FS.PostMessage.postScroll(iframe[0]); |
| 173 |
} |
| 174 |
}); |
| 175 |
|
| 176 |
FS.PostMessage.receiveOnce('install', function (data) { |
| 177 |
// Post data to activation URL. |
| 178 |
$.form('<?php echo fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 179 |
'fs_action' => $slug . '_activate_new', |
| 180 |
'plugin_id' => isset( $_GET['plugin_id'] ) ? $_GET['plugin_id'] : $fs->get_id() |
| 181 |
) ), $slug . '_activate_new' ) ?>', { |
| 182 |
user_id : data.user.id, |
| 183 |
user_secret_key : data.user.secret_key, |
| 184 |
user_public_key : data.user.public_key, |
| 185 |
install_id : data.install.id, |
| 186 |
install_secret_key: data.install.secret_key, |
| 187 |
install_public_key: data.install.public_key |
| 188 |
}).submit(); |
| 189 |
}); |
| 190 |
|
| 191 |
FS.PostMessage.receiveOnce('pending_activation', function (data) { |
| 192 |
$.form('<?php echo fs_nonce_url( $fs->_get_admin_page_url( 'account', array( |
| 193 |
'fs_action' => $slug . '_activate_new', |
| 194 |
'plugin_id' => fs_request_get( 'plugin_id', $fs->get_id() ), |
| 195 |
'pending_activation' => true, |
| 196 |
) ), $slug . '_activate_new' ) ?>', { |
| 197 |
user_email: data.user_email |
| 198 |
}).submit(); |
| 199 |
}); |
| 200 |
|
| 201 |
FS.PostMessage.receiveOnce('get_context', function () { |
| 202 |
console.debug('receiveOnce', 'get_context'); |
| 203 |
|
| 204 |
// If the user didn't connect his account with Freemius, |
| 205 |
// once he accepts the Terms of Service and Privacy Policy, |
| 206 |
// and then click the purchase button, the context information |
| 207 |
// of the user will be shared with Freemius in order to complete the |
| 208 |
// purchase workflow and activate the license for the right user. |
| 209 |
<?php $install_data = array_merge( $fs->get_opt_in_params(), |
| 210 |
array( |
| 211 |
'activation_url' => fs_nonce_url( $fs->_get_admin_page_url( '', |
| 212 |
array( |
| 213 |
'fs_action' => $slug . '_activate_new', |
| 214 |
'plugin_id' => fs_request_get( 'plugin_id', $fs->get_id() ), |
| 215 |
|
| 216 |
) ), |
| 217 |
$slug . '_activate_new' ) |
| 218 |
) ) ?> |
| 219 |
FS.PostMessage.post('context', <?php echo json_encode( $install_data ) ?>, iframe[0]); |
| 220 |
}); |
| 221 |
|
| 222 |
FS.PostMessage.receiveOnce('get_dimensions', function (data) { |
| 223 |
console.debug('receiveOnce', 'get_dimensions'); |
| 224 |
|
| 225 |
FS.PostMessage.post('dimensions', { |
| 226 |
height : $(document.body).height(), |
| 227 |
scrollTop: $(document).scrollTop() |
| 228 |
}, iframe[0]); |
| 229 |
}); |
| 230 |
}); |
| 231 |
})(jQuery); |
| 232 |
</script> |
| 233 |
</div> |
| 234 |
<?php |
| 235 |
$params = array( |
| 236 |
'page' => 'checkout', |
| 237 |
'module_id' => $fs->get_id(), |
| 238 |
'module_slug' => $slug, |
| 239 |
'module_version' => $fs->get_plugin_version(), |
| 240 |
); |
| 241 |
fs_require_template( 'powered-by.php', $params ); |
| 242 |
?> |
| 243 |
|