summaryrefslogtreecommitdiff
path: root/Postman/PostmanOAuthToken.php
blob: 81efbfef7dcae0e2fddb413af17238f6646d40b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
if (! class_exists ( 'PostmanOAuthToken.php' )) {
	
	class PostmanOAuthToken {
		const OPTIONS_NAME = 'postman_auth_token';
		//
		const REFRESH_TOKEN = 'refresh_token';
		const EXPIRY_TIME = 'auth_token_expires';
		const ACCESS_TOKEN = 'access_token';
		const VENDOR_NAME = 'vendor_name';
		//
		private $vendorName;
		private $accessToken;
		private $refreshToken;
		private $expiryTime;
		
		// singleton instance
		public static function getInstance() {
			static $inst = null;
			if ($inst === null) {
				$inst = new PostmanOAuthToken ();
			}
			return $inst;
		}
		
		// private constructor
		private function __construct() {
			$this->load ();
		}
		
		/**
		 * Is there a valid access token and refresh token
		 */
		public function isValid() {
			$accessToken = $this->getAccessToken ();
			$refreshToken = $this->getRefreshToken ();
			return ! (empty ( $accessToken ) || empty ( $refreshToken ));
		}
		
		/**
		 * Load the Postman OAuth token properties to the database
		 */
		private function load() {
			$a = get_option ( PostmanOAuthToken::OPTIONS_NAME );

			if ( ! is_array( $a ) ) {
			    return;
            }

			if ( isset( $a [PostmanOAuthToken::ACCESS_TOKEN] ) ) {
                $this->setAccessToken ( $a [PostmanOAuthToken::ACCESS_TOKEN] );
            }

            if ( isset( $a [PostmanOAuthToken::REFRESH_TOKEN] ) ) {
                $this->setRefreshToken($a [PostmanOAuthToken::REFRESH_TOKEN]);
            }

            if ( isset( $a [PostmanOAuthToken::EXPIRY_TIME] ) ) {
                $this->setExpiryTime($a [PostmanOAuthToken::EXPIRY_TIME]);
            }

            if ( isset( $a [PostmanOAuthToken::VENDOR_NAME] ) ) {
                $this->setVendorName($a [PostmanOAuthToken::VENDOR_NAME]);
            }
		}
		
		/**
		 * Save the Postman OAuth token properties to the database
		 */
		public function save() {
			$a [PostmanOAuthToken::ACCESS_TOKEN] = $this->getAccessToken ();
			$a [PostmanOAuthToken::REFRESH_TOKEN] = $this->getRefreshToken ();
			$a [PostmanOAuthToken::EXPIRY_TIME] = $this->getExpiryTime ();
			$a [PostmanOAuthToken::VENDOR_NAME] = $this->getVendorName ();
			update_option ( PostmanOAuthToken::OPTIONS_NAME, $a );
		}
		public function getVendorName() {
			return $this->vendorName;
		}
		public function getExpiryTime() {
			return $this->expiryTime;
		}
		public function getAccessToken() {
			return $this->accessToken;
		}
		public function getRefreshToken() {
			return $this->refreshToken;
		}
		public function setVendorName($name) {
			$this->vendorName = sanitize_text_field ( $name );
		}
		public function setExpiryTime($time) {
			$this->expiryTime = sanitize_text_field ( $time );
		}
		public function setAccessToken($token) {
			$this->accessToken = sanitize_text_field ( $token );
		}
		public function setRefreshToken($token) {
			$this->refreshToken = sanitize_text_field ( $token );
		}
	}
}