| 1 |
<?php |
| 2 |
namespace Elementor\Core\Common\Modules\Connect\Apps; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
abstract class Common_App extends Base_User_App { |
| 9 |
const OPTION_CONNECT_COMMON_DATA_KEY = self::OPTION_NAME_PREFIX . 'common_data'; |
| 10 |
|
| 11 |
protected static $common_data = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* @since 2.3.0 |
| 15 |
* @access public |
| 16 |
*/ |
| 17 |
public function get_option_name() { |
| 18 |
return static::OPTION_NAME_PREFIX . 'common_data'; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @since 2.3.0 |
| 23 |
* @access protected |
| 24 |
*/ |
| 25 |
protected function init_data() { |
| 26 |
if ( is_null( self::$common_data ) ) { |
| 27 |
self::$common_data = get_user_option( static::get_option_name() ); |
| 28 |
|
| 29 |
if ( ! self::$common_data ) { |
| 30 |
self::$common_data = []; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$this->data = & self::$common_data; |
| 35 |
} |
| 36 |
|
| 37 |
public function action_reset() { |
| 38 |
delete_user_option( get_current_user_id(), static::OPTION_CONNECT_COMMON_DATA_KEY ); |
| 39 |
|
| 40 |
parent::action_reset(); |
| 41 |
} |
| 42 |
|
| 43 |
public static function get_connect_user_id_from_access_token( $token ) { |
| 44 |
if ( ! is_string( $token ) ) { |
| 45 |
return null; |
| 46 |
} |
| 47 |
|
| 48 |
$parts = explode( '.', $token ); |
| 49 |
|
| 50 |
if ( count( $parts ) !== 3 ) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
try { |
| 55 |
$payload_encoded = $parts[1]; |
| 56 |
|
| 57 |
$payload_encoded = str_pad( $payload_encoded, strlen( $payload_encoded ) + ( 4 - strlen( $payload_encoded ) % 4 ) % 4, '=' ); |
| 58 |
|
| 59 |
$payload_json = base64_decode( strtr( $payload_encoded, '-_', '+/' ), true ); |
| 60 |
|
| 61 |
$payload = json_decode( $payload_json, true ); |
| 62 |
|
| 63 |
if ( ! isset( $payload['sub'] ) ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
return $payload['sub']; |
| 68 |
} catch ( \Exception $e ) { |
| 69 |
error_log( 'JWT Decoding Error: ' . $e->getMessage() ); |
| 70 |
return null; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
protected function get_connect_user_id() { |
| 75 |
return self::get_connect_user_id_from_access_token( $this->get( 'access_token' ) ); |
| 76 |
} |
| 77 |
} |
| 78 |
|