| 1 |
<?php |
| 2 |
|
| 3 |
class FSActivate extends FS_LITE{ |
| 4 |
|
| 5 |
protected $url = 'https://api.bplugins.com/wp-json/data/v1/accept-data'; |
| 6 |
protected $freemius_install_form_action = 'https://wp.freemius.com/action/service/user/install/'; |
| 7 |
protected $status = false; |
| 8 |
protected $post_type = ''; |
| 9 |
protected $nonce = null; |
| 10 |
protected $last_check = null; |
| 11 |
protected $marketing_allowed = false; |
| 12 |
protected static $_instance = null; |
| 13 |
protected $email = null; |
| 14 |
|
| 15 |
function __construct($config, $__FILE__){ |
| 16 |
parent::__construct($config, $__FILE__); |
| 17 |
$this->config->version = $this->version; |
| 18 |
$this->register(); |
| 19 |
} |
| 20 |
|
| 21 |
private function register(){ |
| 22 |
date_default_timezone_set('Asia/Dhaka'); |
| 23 |
$this->status = get_option("$this->prefix-opt_in", false); |
| 24 |
$this->last_check = get_option("$this->prefix-info-check", time()-1); |
| 25 |
$this->marketing_allowed = get_option("$this->prefix-marketing-allowed", false); |
| 26 |
|
| 27 |
add_filter("plugin_action_links_$this->base_name", [$this, 'opt_in_button'] ); |
| 28 |
add_action("wp_ajax_$this->prefix-opt-in-update", [$this, 'opt_in_update']); |
| 29 |
add_action('admin_head', [$this, 'admin_head']); |
| 30 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); |
| 31 |
|
| 32 |
if(!$this->status){ |
| 33 |
add_action('admin_menu', [$this, 'add_opt_in_menu']); |
| 34 |
} |
| 35 |
|
| 36 |
register_activation_hook($this->__FILE__, [&$this, '_activate_plugin_hook']); |
| 37 |
|
| 38 |
add_action('admin_footer', [$this, 'opt_in_modal']); |
| 39 |
add_action('admin_footer', [$this, 'initialize_opt_in']); |
| 40 |
|
| 41 |
add_action('wp_ajax_bsdk_fetch_info_'.$this->config->id, [$this, 'fetch_info']); |
| 42 |
add_action('wp_ajax_fs_init', [$this, 'fs_init']); |
| 43 |
add_action('wp_ajax_fs_api_request', [$this, 'fs_api_request']); |
| 44 |
add_action('wp_ajax_fs_notice_dismiss_'.$this->config->slug, [$this, 'fs_notice_dismiss']); |
| 45 |
add_action( 'admin_notices', [$this, 'fs_admin_notice'] ); |
| 46 |
|
| 47 |
register_deactivation_hook($this->__FILE__, [&$this, '_deactivate_plugin_hook']); |
| 48 |
} |
| 49 |
|
| 50 |
function fs_admin_notice(){ |
| 51 |
$fs_accounts = $this->get_fs_accounts(); |
| 52 |
$notice = $fs_accounts['admin_notices'][$this->config->slug]['activation_pending'] ?? []; |
| 53 |
echo "<div class='fs_notice_board' data-nonce='". esc_attr(wp_create_nonce( 'wp_ajax' ))."' data-slug='". esc_attr($this->config->slug)."' data data-notice='". esc_attr(wp_json_encode($notice))."'></div>"; |
| 54 |
} |
| 55 |
|
| 56 |
function fs_notice_dismiss(){ |
| 57 |
if(!wp_verify_nonce( $_POST['nonce'], "wp_ajax")){ |
| 58 |
wp_send_json_error(); |
| 59 |
} |
| 60 |
$fs_accounts = $this->get_fs_accounts(); |
| 61 |
unset($fs_accounts['admin_notices'][$this->config->slug]['activation_pending']); |
| 62 |
update_option('fs_accounts', $fs_accounts); |
| 63 |
wp_send_json_success('notice dismissed ' . $this->config->slug); |
| 64 |
} |
| 65 |
|
| 66 |
function fs_init(){ |
| 67 |
if(!wp_verify_nonce( $_POST['nonce'], "wp_ajax")){ |
| 68 |
wp_send_json_error(); |
| 69 |
} |
| 70 |
try { |
| 71 |
$info = $this->sanitize_array($_POST['info']); |
| 72 |
$notice = $this->sanitize_array($_POST['notice'] ?? null); |
| 73 |
$fs_accounts = $this->get_fs_accounts(); |
| 74 |
if(isset($info['is_skip_activation']) && $info['is_skip_activation']){ |
| 75 |
$site = $this->get_data(); |
| 76 |
if($site->secret_key && $site->public_key){ |
| 77 |
if(isset($fs_accounts['plugin_data'][$this->config->slug]['is_anonymous'])){ |
| 78 |
unset($fs_accounts['plugin_data'][$this->config->slug]['is_anonymous']); |
| 79 |
} |
| 80 |
}else { |
| 81 |
$fs_accounts['plugin_data'][$this->config->slug]['is_anonymous'] = ['is' => true]; |
| 82 |
} |
| 83 |
}else { |
| 84 |
if(isset($info['user_id']) && $info['user_id']) { |
| 85 |
$user_api = new Freemius_Lite('user', $info['user_id'], $info['user_public_key'], $info['user_secret_key']); |
| 86 |
$site = (object) $this->sanitize_array($_POST['site']); |
| 87 |
$user_data = $user_api->FS_Api(''); |
| 88 |
$fs_accounts = $this->get_fs_accounts($info['user_id'], $user_data, $site); |
| 89 |
if(isset($fs_accounts['admin_notices'][$this->config->slug]['activation_pending'])){ |
| 90 |
unset($fs_accounts['admin_notices'][$this->config->slug]['activation_pending']); |
| 91 |
} |
| 92 |
}else if($notice && isset($info['pending_activation'])){ |
| 93 |
$notice['activation_pending']['message'] = str_replace(['{name}', '{email}'], [$this->plugin_name, wp_get_current_user()->user_email], $notice['activation_pending']['message']); |
| 94 |
$fs_accounts['admin_notices'][$this->config->slug] = $notice; |
| 95 |
} |
| 96 |
if(isset( $fs_accounts['plugin_data'][$this->config->slug]['is_anonymous'])){ |
| 97 |
unset( $fs_accounts['plugin_data'][$this->config->slug]['is_anonymous']); |
| 98 |
} |
| 99 |
} |
| 100 |
update_option('fs_accounts', $fs_accounts); |
| 101 |
wp_send_json_success($fs_accounts); |
| 102 |
|
| 103 |
|
| 104 |
} catch (\Throwable $th) { |
| 105 |
wp_send_json_error($th->getMessage()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function get_request($key = null){ |
| 110 |
return isset($_GET[$key]) ? sanitize_text_field($_GET[$key]): false; |
| 111 |
} |
| 112 |
|
| 113 |
function post_request($key = null){ |
| 114 |
return isset($_POST[$key]) ? sanitize_text_field($_POST[$key]): false; |
| 115 |
} |
| 116 |
|
| 117 |
function _deactivate_plugin_hook(){ |
| 118 |
$api = $this->event_hook(); |
| 119 |
if($api){ |
| 120 |
register_uninstall_hook($this->__FILE__, ['FSActivate', '_uninstall_plugin']); |
| 121 |
$api->plugin_deactivated('?sdk_ver='.$this->fs_version.'&url='.site_url(), $this->get_anonymous_id()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
function _activate_plugin_hook(){ |
| 126 |
$api = $this->event_hook(true); |
| 127 |
if($api){ |
| 128 |
$api->plugin_activated('', $this->get_anonymous_id(), $this->version); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function _uninstall_plugin(){ |
| 133 |
$api = $this->event_hook(); |
| 134 |
if($api){ |
| 135 |
$api->plugin_uninstall('?sdk_ver='.$this->fs_version.'&url='.site_url(), $this->get_anonymous_id()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// function sanitize_array |
| 140 |
|
| 141 |
function event_hook($is_active = false){ |
| 142 |
$site = $this->get_data(); |
| 143 |
$plugin_data = $this->get_data('plugin_data'); |
| 144 |
if(!$site){ |
| 145 |
return false; |
| 146 |
} |
| 147 |
$site->is_active = $is_active; |
| 148 |
$this->update_store('sites', $site); |
| 149 |
|
| 150 |
if(!isset($plugin_data->is_site_tracking_allowed) || !$plugin_data->is_site_tracking_allowed){ |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
$api = new Freemius_Lite('install', $site->id, $site->public_key, $site->secret_key); |
| 155 |
return $api; |
| 156 |
} |
| 157 |
|
| 158 |
function fs_api_request(){ |
| 159 |
if(!wp_verify_nonce( $_POST['nonce'], "wp_ajax")){ |
| 160 |
wp_send_json_error(); |
| 161 |
} |
| 162 |
|
| 163 |
$method = $this->post_request('method'); |
| 164 |
$url= $this->post_request('url'); |
| 165 |
$headers = $this->sanitize_array( $_POST['headers']); |
| 166 |
$body = $this->sanitize_array( $_POST['body']); |
| 167 |
|
| 168 |
try { |
| 169 |
$response = wp_remote_request($url, [ |
| 170 |
'method' => $method, |
| 171 |
'headers' => $headers, |
| 172 |
'body' => wp_json_encode($body) |
| 173 |
]); |
| 174 |
$body = json_decode(wp_remote_retrieve_body($response)); |
| 175 |
wp_send_json_success($body); |
| 176 |
} catch (\Throwable $th) { |
| 177 |
//throw $th; |
| 178 |
} |
| 179 |
|
| 180 |
wp_send_json_error('error'); |
| 181 |
} |
| 182 |
|
| 183 |
function fetch_info(){ |
| 184 |
$nonce = $_POST['nonce']; |
| 185 |
if(!wp_verify_nonce($nonce, "wp_ajax")){ |
| 186 |
wp_send_json_error(); |
| 187 |
} |
| 188 |
|
| 189 |
$plugin_data = []; |
| 190 |
unset($this->config->__FILE__); |
| 191 |
$result = []; |
| 192 |
$thread = sanitize_text_field($_POST['thread'] ?? null); |
| 193 |
if($thread === "permission_update"){ |
| 194 |
$api = new Freemius_Lite(); |
| 195 |
$permissions = sanitize_text_field($_POST['permissions']); |
| 196 |
$is_enabled = sanitize_text_field($_POST['is_enabled']) === 'true'; |
| 197 |
$fs_accounts = $this->get_fs_accounts(); |
| 198 |
$result = $api->permission_update($fs_accounts, $this->config, compact('permissions', 'is_enabled')); |
| 199 |
if(!$result['success']){ |
| 200 |
wp_send_json_error($result['message']); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
wp_send_json_success(wp_parse_args( wp_parse_args($this->extend_config(), ['result' => $result]), (array) $this->config )); |
| 205 |
} |
| 206 |
|
| 207 |
function extend_config(){ |
| 208 |
if(!$this->version){ |
| 209 |
return; |
| 210 |
} |
| 211 |
global $wp_version; |
| 212 |
$user = wp_get_current_user(); |
| 213 |
$type = sanitize_text_field($_POST['type'] ?? 'form'); |
| 214 |
$plugin_data = []; |
| 215 |
$site = []; |
| 216 |
|
| 217 |
if($type === 'modal'){ |
| 218 |
$fs_accounts = get_option('fs_accounts', []); |
| 219 |
if(isset($fs_accounts['plugin_data'][$this->config->slug])){ |
| 220 |
$plugin_data = $fs_accounts['plugin_data'][$this->config->slug]; |
| 221 |
} |
| 222 |
if(isset($fs_accounts['sites'][$this->config->slug])){ |
| 223 |
$site = [ |
| 224 |
'scope' => 'install', |
| 225 |
'id' => $fs_accounts['sites'][$this->config->slug]->install_id, |
| 226 |
'public_key' => $fs_accounts['sites'][$this->config->slug]->public_key, |
| 227 |
'secret_key' => $fs_accounts['sites'][$this->config->slug]->secret_key |
| 228 |
]; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return [ |
| 233 |
'freemius_form_action' => $this->freemius_install_form_action, |
| 234 |
'uid' => $this->get_anonymous_id(), |
| 235 |
'platform_version' => $wp_version, |
| 236 |
'programming_language_version' => phpversion(), |
| 237 |
'user_email' => $user->user_email, |
| 238 |
'plugin_version' => $this->version, |
| 239 |
'site_name' => get_bloginfo('name'), |
| 240 |
'admin_url' => admin_url(), |
| 241 |
'site_url' => site_url(), |
| 242 |
'nonce' => wp_create_nonce($this->config->slug.'_activate_new'), |
| 243 |
'is_marketing_allowed' => $this->marketing_allowed, |
| 244 |
'user_first_name'=> $user->user_firstname, |
| 245 |
'user_last_name'=> $user->user_lastname, |
| 246 |
'plugin_name' => $this->plugin_name, |
| 247 |
'plugin_data' => $plugin_data, |
| 248 |
'site' => $site |
| 249 |
]; |
| 250 |
} |
| 251 |
|
| 252 |
function get_request_decode($key = null){ |
| 253 |
$key = isset($_GET[$key]) ? $_GET[$key] : false; |
| 254 |
if($key && strlen($key) === 32){ |
| 255 |
$key = sanitize_text_field(str_replace('<', '<', $key) ); |
| 256 |
$key = str_replace(['<', '>', '&'], [ '<', '>', '&'], $key); |
| 257 |
return $key; |
| 258 |
} |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
function initialize_opt_in(){ |
| 263 |
?> |
| 264 |
<script> |
| 265 |
document.addEventListener('DOMContentLoaded', function(){ |
| 266 |
if(typeof bsdkOptInFormHandler === 'function'){ |
| 267 |
bsdkOptInFormHandler('<?php echo esc_html($this->prefix) ?>', '<?php echo esc_html($this->config->slug) ?>'); |
| 268 |
} |
| 269 |
}); |
| 270 |
</script> |
| 271 |
<?php |
| 272 |
} |
| 273 |
|
| 274 |
function end_process(){ |
| 275 |
$location = admin_url(isset($this->config->first_path) ? $this->config->first_path : 'plugins.php'); |
| 276 |
?><script>window.location.href = '<?php echo esc_html($location) ?>';</script><?php |
| 277 |
} |
| 278 |
|
| 279 |
function admin_head(){ |
| 280 |
$redirect = get_option("$this->prefix-redirect", false); |
| 281 |
if (!$redirect && !strpos($_SERVER['REQUEST_URI'], 'post.php') && !strpos($_SERVER['REQUEST_URI'], 'post-new.php')) { |
| 282 |
update_option("$this->prefix-redirect", true); ?><script>window.location.href = '<?php echo "admin.php?page=" . dirname($this->base_name) ?>'</script><?php |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
function opt_in_button($links){ |
| 287 |
$fs_accounts = get_option('fs_accounts'); |
| 288 |
$classes = "optInBtn "; |
| 289 |
if($this->config->platform === 'freemius'){ |
| 290 |
$this->marketing_allowed = $this->get_is_all_tracking_allowed(); |
| 291 |
if($this->marketing_allowed !== null){ |
| 292 |
$classes .= $this->config->slug; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
$opt_in_link = admin_url("admin.php?page=" . dirname($this->base_name)); |
| 297 |
|
| 298 |
$settings_link = '<a href="'.$opt_in_link.'" class="'.$classes.'" id="'.esc_attr($this->prefix).'OptInBtn" data-status="'.esc_attr($this->marketing_allowed ? 'agree': 'not-allowed').'">'.esc_html($this->marketing_allowed ? 'Opt Out' : 'Opt In').'</a>'; |
| 299 |
|
| 300 |
array_unshift($links, $settings_link); |
| 301 |
return $links; |
| 302 |
} |
| 303 |
|
| 304 |
// add a temp page to show opt-in form |
| 305 |
function add_opt_in_menu(){ |
| 306 |
add_submenu_page( '', $this->plugin_name, $this->plugin_name, 'manage_options', dirname($this->base_name), [$this, 'opt_in_form'] |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
// enqueue assets |
| 311 |
function enqueue_assets($hook){ |
| 312 |
wp_enqueue_script("bsdk-admin-notice", plugin_dir_url(plugin_dir_path( __DIR__ )).'dist/admin-notice.js', ['react', 'react-dom', 'wp-util'], $this->version); |
| 313 |
|
| 314 |
wp_register_script("bsdk-opt-in", plugin_dir_url(plugin_dir_path( __DIR__ )).'dist/opt-in-form.js', ['react', 'react-dom', 'wp-util'], $this->version); |
| 315 |
wp_register_style("bsdk-opt-in", plugin_dir_url(plugin_dir_path( __DIR__ )).'dist/opt-in-form.css', [], $this->version); |
| 316 |
|
| 317 |
if($hook === 'plugins.php' || $hook === "admin_page_".dirname($this->base_name)){ |
| 318 |
wp_enqueue_script("bsdk-opt-in"); |
| 319 |
wp_enqueue_style("bsdk-opt-in"); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
function initialize_fs_accounts(){ |
| 324 |
$fs_accounts = $this->get_fs_accounts(); |
| 325 |
update_option('fs_accounts', $fs_accounts); |
| 326 |
} |
| 327 |
|
| 328 |
// show the opt-in form |
| 329 |
function opt_in_form(){ |
| 330 |
update_option("$this->prefix-redirect", true); |
| 331 |
$this->initialize_fs_accounts(); |
| 332 |
?> |
| 333 |
<div |
| 334 |
data-nonce="<?php echo esc_attr(wp_create_nonce( 'wp_ajax' )) ?>" |
| 335 |
data-plugin-id = "<?php echo esc_attr($this->config->id) ?>" |
| 336 |
id="<?php echo esc_attr($this->prefix); ?>OptInForm"> |
| 337 |
</div> |
| 338 |
<?php |
| 339 |
} |
| 340 |
|
| 341 |
// opt-in modal on plugins.php page |
| 342 |
function opt_in_modal($hook){ |
| 343 |
global $wp_version; |
| 344 |
$screen = \get_current_screen(); |
| 345 |
if($screen->base === 'plugins'){ |
| 346 |
?> |
| 347 |
<div |
| 348 |
id="<?php echo esc_attr($this->prefix) ?>OptInModal" |
| 349 |
data-slug="<?php echo esc_attr($this->config->slug) ?>" |
| 350 |
data-nonce="<?php echo esc_attr(wp_create_nonce( 'wp_ajax' )) ?>" |
| 351 |
data-plugin-id = "<?php echo esc_attr($this->config->id) ?>"> |
| 352 |
</div> |
| 353 |
<?php |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
function get_is_all_tracking_allowed(){ |
| 358 |
$fs_accounts = $this->get_fs_accounts(); |
| 359 |
$slug = $this->config->slug; |
| 360 |
$plugin_data = isset($fs_accounts['plugin_data'][$slug])? (object) $fs_accounts['plugin_data'][$slug] : null; |
| 361 |
|
| 362 |
|
| 363 |
if(!$plugin_data || (isset($plugin_data->is_anonymous) && $plugin_data->is_anonymous['is']) || isset($fs_accounts['admin_notices'][$this->config->slug]['activation_pending'])){ |
| 364 |
return null; |
| 365 |
} |
| 366 |
|
| 367 |
if(!isset($plugin_data->is_user_tracking_allowed)){ |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
if($plugin_data->is_user_tracking_allowed && $plugin_data->is_site_tracking_allowed && $plugin_data->is_events_tracking_allowed && $plugin_data->is_extensions_tracking_allowed){ |
| 372 |
return true; |
| 373 |
} |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
function sanitize_array($array){ |
| 379 |
if(!is_array($array)){ |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
foreach($array as $key => $value){ |
| 384 |
if(strpos($key, 'secret_key') !== false && strlen($value) == 32){ |
| 385 |
$value = sanitize_text_field(str_replace('<', '<', $value) ); |
| 386 |
$value = sanitize_text_field($value); |
| 387 |
$array[$key] = str_replace(['<', '>', '&'], [ '<', '>', '&'], $value); |
| 388 |
}else { |
| 389 |
if(is_array($value)){ |
| 390 |
$array[$key] = $this->sanitize_array($value); |
| 391 |
}else { |
| 392 |
$array[$key] =$value == 'true' ? true : ($value == 'false' ? false : sanitize_text_field($value)); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
return $array; |
| 397 |
} |
| 398 |
|
| 399 |
} |