| 1 |
<?php |
| 2 |
class FS_Lite{ |
| 3 |
|
| 4 |
protected $prefix = ''; |
| 5 |
protected $config = ''; |
| 6 |
protected $base_name = null; |
| 7 |
protected $plugin_name = ''; |
| 8 |
protected $product = ""; |
| 9 |
protected $key = null; |
| 10 |
protected $__FILE__ = null; |
| 11 |
protected $_upgraded = false; |
| 12 |
protected $version = false; |
| 13 |
protected $dir = __DIR__; |
| 14 |
protected $path = null; |
| 15 |
protected $blockHandler = null; |
| 16 |
protected $fs_version = '2.5.12'; |
| 17 |
protected $accounts_key = 'bs_accounts'; |
| 18 |
|
| 19 |
function __construct($config, $__FILE__){ |
| 20 |
$this->config = $config; |
| 21 |
$this->prefix = $this->config->prefix; |
| 22 |
$this->__FILE__ = $__FILE__; |
| 23 |
$this->base_name = plugin_basename( $this->__FILE__ ); |
| 24 |
$this->blockHandler = $this->config->blockHandler; |
| 25 |
$this->path = $this->config->slug .'/'. basename($this->__FILE__); |
| 26 |
|
| 27 |
if(!class_exists('Freemius_Lite') && file_exists(__DIR__.'/Freemius_Lite.php') ){ |
| 28 |
require_once(__DIR__.'/Freemius_Lite.php'); |
| 29 |
} |
| 30 |
|
| 31 |
if( ! function_exists('get_plugin_data') ){ |
| 32 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 33 |
} |
| 34 |
|
| 35 |
$plugin_data = \get_plugin_data( $this->__FILE__ ); |
| 36 |
$this->plugin_name = $plugin_data['Name']; |
| 37 |
|
| 38 |
$this->version = isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] === 'localhost' ? time() : $plugin_data['Version']; |
| 39 |
} |
| 40 |
|
| 41 |
function get_anonymous_id( $blog_id = null ) { |
| 42 |
$unique_id = get_option( 'unique_id', null, $blog_id ); |
| 43 |
|
| 44 |
|
| 45 |
if ( empty( $unique_id ) || ! is_string( $unique_id ) ) { |
| 46 |
$key = $this->fs_strip_url_protocol( site_url() ); |
| 47 |
|
| 48 |
$secure_auth = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : ''; |
| 49 |
|
| 50 |
if ( empty( $secure_auth ) || |
| 51 |
false !== strpos( $secure_auth, ' ' ) || |
| 52 |
'put your unique phrase here' === $secure_auth |
| 53 |
) { |
| 54 |
$secure_auth = md5( microtime() ); |
| 55 |
} |
| 56 |
|
| 57 |
$unique_id = md5( $key . $secure_auth ); |
| 58 |
|
| 59 |
update_option( 'unique_id', $unique_id); |
| 60 |
} |
| 61 |
|
| 62 |
return $unique_id; |
| 63 |
} |
| 64 |
|
| 65 |
function get_data($type = 'sites'){ |
| 66 |
$fs_accounts = $this->get_fs_accounts(); |
| 67 |
if(isset($fs_accounts[$type][$this->config->slug])){ |
| 68 |
return $fs_accounts[$type][$this->config->slug]; |
| 69 |
} |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
function update_store($key, $value){ |
| 74 |
$fs_accounts = $this->get_fs_accounts(); |
| 75 |
$fs_accounts[$key][$this->config->slug] = $value; |
| 76 |
update_option('fs_accounts', $fs_accounts); |
| 77 |
} |
| 78 |
|
| 79 |
function fs_starts_with( $haystack, $needle ) { |
| 80 |
$length = strlen( $needle ); |
| 81 |
return ( substr( $haystack, 0, $length ) === $needle ); |
| 82 |
} |
| 83 |
|
| 84 |
function fs_strip_url_protocol( $url ) { |
| 85 |
if ( ! $this->fs_starts_with( $url, 'http' ) ) { |
| 86 |
return $url; |
| 87 |
} |
| 88 |
|
| 89 |
$protocol_pos = strpos( $url, '://' ); |
| 90 |
|
| 91 |
if ( $protocol_pos > 5 ) { |
| 92 |
return $url; |
| 93 |
} |
| 94 |
return substr( $url, $protocol_pos + 3 ); |
| 95 |
} |
| 96 |
|
| 97 |
function get_fs_accounts($user_id = null, $user_data = [], $site = null){ |
| 98 |
$fs_accounts = get_option('fs_accounts', []); |
| 99 |
if(!array_key_exists('id_slug_type_path_map', $fs_accounts)){ |
| 100 |
$fs_accounts['id_slug_type_path_map'] = []; |
| 101 |
} |
| 102 |
if(!array_key_exists('plugin_data', $fs_accounts)){ |
| 103 |
$fs_accounts['plugin_data'] = []; |
| 104 |
} |
| 105 |
if(!array_key_exists('file_slug_map', $fs_accounts)){ |
| 106 |
$fs_accounts['file_slug_map'] = []; |
| 107 |
} |
| 108 |
if(!array_key_exists('users', $fs_accounts)){ |
| 109 |
$fs_accounts['users'] = []; |
| 110 |
} |
| 111 |
if(!array_key_exists('sites', $fs_accounts)){ |
| 112 |
$fs_accounts['sites'] = []; |
| 113 |
} |
| 114 |
|
| 115 |
if(!$this->path){ |
| 116 |
return $fs_accounts; |
| 117 |
} |
| 118 |
|
| 119 |
$fs_accounts['id_slug_type_path_map'][$this->config->id] = [ |
| 120 |
'slug' => $this->config->slug, |
| 121 |
'type' => 'plugin', |
| 122 |
'path' => $this->path |
| 123 |
]; |
| 124 |
|
| 125 |
if(!isset($fs_accounts['plugin_data'][$this->config->slug]) || $user_id){ |
| 126 |
$fs_accounts['plugin_data'][$this->config->slug] = $this->get_plugin_data($user_id); |
| 127 |
} |
| 128 |
|
| 129 |
$fs_accounts['file_slug_map'][$this->path] = $this->config->slug; |
| 130 |
|
| 131 |
if($user_id){ |
| 132 |
$fs_accounts['users'][$user_id] = $user_data; |
| 133 |
} |
| 134 |
if($site){ |
| 135 |
$fs_accounts['sites'][$this->config->slug] = $site; |
| 136 |
} |
| 137 |
|
| 138 |
return $fs_accounts; |
| 139 |
} |
| 140 |
|
| 141 |
function get_plugin_data($user_id = null){ |
| 142 |
$data = [ |
| 143 |
'plugin_main_file' => (object) [ |
| 144 |
'path' => $this->path |
| 145 |
], |
| 146 |
"is_network_activated" => false, |
| 147 |
"install_timestamp" => time(), |
| 148 |
"sdk_last_version" => $this->fs_version, |
| 149 |
'sdk_version' => $this->fs_version, |
| 150 |
"sdk_upgrade_mode" => "1", |
| 151 |
"sdk_downgrade_mode" => false, |
| 152 |
"plugin_last_version" => $this->version, |
| 153 |
"plugin_version" => $this->version, |
| 154 |
"plugin_upgrade_mode" => true, |
| 155 |
"plugin_downgrade_mode" => false, |
| 156 |
"was_plugin_loaded" => true, |
| 157 |
"is_plugin_new_install" => false, |
| 158 |
"connectivity_test" => [ |
| 159 |
"is_connected" => false, |
| 160 |
"host" => isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost', |
| 161 |
"server_ip" => '', |
| 162 |
"is_active" => true, |
| 163 |
"timestamp" => time(), |
| 164 |
"version" => $this->version, |
| 165 |
], |
| 166 |
"prev_is_premium" => false, |
| 167 |
"sticky_optin_added" => true |
| 168 |
]; |
| 169 |
|
| 170 |
if($user_id){ |
| 171 |
return wp_parse_args([ |
| 172 |
'is_diagnostic_tracking_allowed' => true, |
| 173 |
'is_extensions_tracking_allowed' => true, |
| 174 |
'is_user_tracking_allowed' => true, |
| 175 |
'is_site_tracking_allowed' => true, |
| 176 |
'is_events_tracking_allowed' => true, |
| 177 |
], $data); |
| 178 |
}else { |
| 179 |
return $data; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|