| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Connect\Classes; |
| 4 |
|
| 5 |
use ElementorOne\Connect\Facade; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Data |
| 13 |
* |
| 14 |
* Instance-based data storage class with runtime configuration support. |
| 15 |
* Each instance has its own configuration for isolated context. |
| 16 |
*/ |
| 17 |
class Data { |
| 18 |
const CLIENT_ID = '_client_id'; |
| 19 |
const CLIENT_SECRET = '_client_secret'; |
| 20 |
const USER_ACCESS_TOKEN = '_user_access_token'; |
| 21 |
const ACCESS_TOKEN = '_access_token'; |
| 22 |
const REFRESH_TOKEN = '_refresh_token'; |
| 23 |
const TOKEN_ID = '_token_id'; |
| 24 |
const OPTION_OWNER_USER_ID = '_owner_user_id'; |
| 25 |
const HOME_URL = '_home_url'; |
| 26 |
const SHARE_USAGE_DATA = '_share_usage_data'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Facade instance |
| 30 |
* @var Facade |
| 31 |
*/ |
| 32 |
private Facade $facade; |
| 33 |
|
| 34 |
/** |
| 35 |
* Data constructor |
| 36 |
* @param Facade $facade |
| 37 |
*/ |
| 38 |
public function __construct( Facade $facade ) { |
| 39 |
$this->facade = $facade; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get app prefix |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
protected function get_app_prefix(): string { |
| 48 |
return $this->facade->get_config( 'app_prefix' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get connect mode |
| 53 |
* |
| 54 |
* @return string 'site' or 'user' |
| 55 |
*/ |
| 56 |
protected function get_connect_mode(): string { |
| 57 |
return $this->facade->get_config( 'connect_mode' ) ?? 'site'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get option |
| 62 |
* @param $option_name |
| 63 |
* @param $default_value |
| 64 |
* |
| 65 |
* @return false|mixed|null |
| 66 |
*/ |
| 67 |
public function get_option( $option_name, $default_value ) { |
| 68 |
return get_option( $this->get_app_prefix() . $option_name, $default_value ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Set option |
| 73 |
* @param $option_name |
| 74 |
* @param $option_value |
| 75 |
* @param $auto_load |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function set_option( $option_name, $option_value, $auto_load = false ): bool { |
| 80 |
return update_option( $this->get_app_prefix() . $option_name, $option_value, $auto_load ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Delete option |
| 85 |
* @param $option_name |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public function delete_option( $option_name ): bool { |
| 89 |
return delete_option( $this->get_app_prefix() . $option_name ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get user data |
| 94 |
* @param $user_id |
| 95 |
* @param $data_name |
| 96 |
* @param mixed|bool $default_value |
| 97 |
* |
| 98 |
* @return false|mixed |
| 99 |
*/ |
| 100 |
public function get_user_data( $user_id, $data_name, $default_value = false ) { |
| 101 |
$data = get_user_meta( $user_id, $this->get_app_prefix() . $data_name, true ); |
| 102 |
|
| 103 |
return empty( $data ) ? $default_value : $data; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set user data |
| 108 |
* @param $user_id |
| 109 |
* @param $data_name |
| 110 |
* @param $value |
| 111 |
* |
| 112 |
* @return bool|int |
| 113 |
*/ |
| 114 |
public function set_user_data( $user_id, $data_name, $value ) { |
| 115 |
return update_user_meta( $user_id, $this->get_app_prefix() . $data_name, $value ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Delete user data |
| 120 |
* @param $user_id |
| 121 |
* @param $data_name |
| 122 |
* @return bool |
| 123 |
*/ |
| 124 |
public function delete_user_data( $user_id, $data_name ): bool { |
| 125 |
return delete_user_meta( $user_id, $this->get_app_prefix() . $data_name ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get connect mode data |
| 130 |
* @param ...$data |
| 131 |
* @return false|mixed|null|string |
| 132 |
*/ |
| 133 |
public function get_connect_mode_data( ...$data ) { |
| 134 |
if ( $this->get_connect_mode() === 'site' ) { |
| 135 |
return $this->get_option( ...$data ); |
| 136 |
} |
| 137 |
$user_id = get_current_user_id(); |
| 138 |
return $this->get_user_data( ...( [ $user_id, ...$data ] ) ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Set connect mode data |
| 143 |
* @param ...$data |
| 144 |
* @return bool|int |
| 145 |
*/ |
| 146 |
public function set_connect_mode_data( ...$data ) { |
| 147 |
if ( $this->get_connect_mode() === 'site' ) { |
| 148 |
return $this->set_option( ...$data ); |
| 149 |
} |
| 150 |
$user_id = get_current_user_id(); |
| 151 |
return $this->set_user_data( ...( [ $user_id, ...$data ] ) ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get client ID |
| 156 |
* @return false|mixed|string|null |
| 157 |
*/ |
| 158 |
public function get_client_id() { |
| 159 |
return $this->get_connect_mode_data( self::CLIENT_ID, false ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get client secret |
| 164 |
* @return false|mixed|string|null |
| 165 |
*/ |
| 166 |
public function get_client_secret() { |
| 167 |
return $this->get_connect_mode_data( self::CLIENT_SECRET, false ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Set client ID |
| 172 |
* @param $value |
| 173 |
* @return bool |
| 174 |
*/ |
| 175 |
public function set_client_id( $value ): bool { |
| 176 |
return $this->set_connect_mode_data( self::CLIENT_ID, $value ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Set client secret |
| 181 |
* @param $value |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
public function set_client_secret( $value ): bool { |
| 185 |
return $this->set_connect_mode_data( self::CLIENT_SECRET, $value ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get access token |
| 190 |
* @return false|mixed|string|null |
| 191 |
*/ |
| 192 |
public function get_access_token( $grant_type = GrantTypes::CLIENT_CREDENTIALS ) { |
| 193 |
$key = GrantTypes::CLIENT_CREDENTIALS === $grant_type ? self::ACCESS_TOKEN : self::USER_ACCESS_TOKEN; |
| 194 |
return $this->get_connect_mode_data( $key, false ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get token ID |
| 199 |
* @return false|mixed|string|null |
| 200 |
*/ |
| 201 |
public function get_token_id() { |
| 202 |
return $this->get_connect_mode_data( self::TOKEN_ID, false ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get refresh token |
| 207 |
* @return false|mixed|string|null |
| 208 |
*/ |
| 209 |
public function get_refresh_token() { |
| 210 |
return $this->get_connect_mode_data( self::REFRESH_TOKEN, false ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get home URL |
| 215 |
* @return false|mixed|string|null |
| 216 |
*/ |
| 217 |
public function get_home_url() { |
| 218 |
$raw = $this->get_connect_mode_data( self::HOME_URL, false ); |
| 219 |
$is_base64 = base64_encode( base64_decode( $raw, true ) ) === $raw; |
| 220 |
return $is_base64 ? base64_decode( $raw ) : $raw; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Set home URL |
| 225 |
* @param string|null $home_url |
| 226 |
* @return bool |
| 227 |
*/ |
| 228 |
public function set_home_url( ?string $home_url = null ): bool { |
| 229 |
$home_url = $home_url ?? $this->facade->home_url()->get_current(); |
| 230 |
return $this->set_connect_mode_data( self::HOME_URL, base64_encode( $home_url ) ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Set owner user ID |
| 235 |
* @param int $user_id |
| 236 |
* @return bool |
| 237 |
*/ |
| 238 |
public function set_owner_user_id( int $user_id ): bool { |
| 239 |
return $this->set_connect_mode_data( self::OPTION_OWNER_USER_ID, $user_id ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Get user is owner option |
| 244 |
* @return int |
| 245 |
*/ |
| 246 |
public function get_owner_user_id(): int { |
| 247 |
return (int) $this->get_connect_mode_data( self::OPTION_OWNER_USER_ID, false ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Check if user is subscription owner |
| 252 |
* @return bool |
| 253 |
*/ |
| 254 |
public function user_is_subscription_owner(): bool { |
| 255 |
$owner_id = (int) $this->get_connect_mode_data( self::OPTION_OWNER_USER_ID, false ); |
| 256 |
|
| 257 |
return get_current_user_id() === $owner_id; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Clear session |
| 262 |
* @param bool $with_client |
| 263 |
* @return void |
| 264 |
*/ |
| 265 |
public function clear_session( $with_client = false ) { |
| 266 |
if ( $this->get_connect_mode() === 'site' ) { |
| 267 |
if ( $with_client ) { |
| 268 |
$this->delete_option( self::CLIENT_ID ); |
| 269 |
$this->delete_option( self::CLIENT_SECRET ); |
| 270 |
} |
| 271 |
$this->delete_option( self::ACCESS_TOKEN ); |
| 272 |
$this->delete_option( self::USER_ACCESS_TOKEN ); |
| 273 |
$this->delete_option( self::REFRESH_TOKEN ); |
| 274 |
$this->delete_option( self::TOKEN_ID ); |
| 275 |
$this->delete_option( self::OPTION_OWNER_USER_ID ); |
| 276 |
$this->delete_option( self::HOME_URL ); |
| 277 |
$this->delete_option( self::SHARE_USAGE_DATA ); |
| 278 |
} else { |
| 279 |
$user_id = get_current_user_id(); |
| 280 |
if ( $with_client ) { |
| 281 |
$this->delete_user_data( $user_id, self::CLIENT_ID ); |
| 282 |
$this->delete_user_data( $user_id, self::CLIENT_SECRET ); |
| 283 |
} |
| 284 |
$this->delete_user_data( $user_id, self::ACCESS_TOKEN ); |
| 285 |
$this->delete_user_data( $user_id, self::USER_ACCESS_TOKEN ); |
| 286 |
$this->delete_user_data( $user_id, self::REFRESH_TOKEN ); |
| 287 |
$this->delete_user_data( $user_id, self::TOKEN_ID ); |
| 288 |
$this->delete_user_data( $user_id, self::OPTION_OWNER_USER_ID ); |
| 289 |
$this->delete_user_data( $user_id, self::HOME_URL ); |
| 290 |
$this->delete_user_data( $user_id, self::SHARE_USAGE_DATA ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Get share usage data |
| 296 |
* @return false|mixed|string|null |
| 297 |
*/ |
| 298 |
public function get_share_usage_data() { |
| 299 |
return $this->get_connect_mode_data( self::SHARE_USAGE_DATA, false ); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Set share usage data |
| 304 |
* @param string $share_usage_data |
| 305 |
* @return bool |
| 306 |
*/ |
| 307 |
public function set_share_usage_data( string $share_usage_data ): bool { |
| 308 |
return $this->set_connect_mode_data( self::SHARE_USAGE_DATA, $share_usage_data ); |
| 309 |
} |
| 310 |
} |
| 311 |
|