oauthservice.php
3.37 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class NF_FU_External_Abstracts_Service
*/
abstract class NF_FU_External_Abstracts_Oauthservice extends NF_FU_External_Abstracts_Service {
protected $consumer_key;
protected $tokens = array();
protected $transient_expires = 18000; // 5 hours
/**
* Get provider slug
*
* @return string
*/
protected function slug() {
if ( isset( $this->provider_slug ) ) {
return $this->provider_slug;
}
return $this->slug;
}
protected function get_transient_key() {
return 'nf_fu_' . $this->slug() . '_authorised';
}
/**
* Get the plugins page URL to return to for oAuth.
*
* @return string
*/
protected function get_callback_url() {
return NF_File_Uploads()->page->get_url( 'external', array(), false );
}
/**
* @param array $args
*
* @return string
*/
protected function get_authorize_url( $args = array() ) {
delete_transient( $this->get_transient_key() );
return NF_File_Uploads()->externals->wpoauth()->get_authorize_url( $this->slug(), $this->consumer_key, $this->get_callback_url(), $args );
}
/**
* @return string
*/
protected function get_disconnect_url() {
return NF_File_Uploads()->externals->wpoauth()->get_disconnect_url( $this->slug(), $this->get_callback_url() );
}
protected function disconnect() {
NF_File_Uploads()->externals->wpoauth()->disconnect( $this->slug() );
}
/**
* Get the connect/disconnect URL in the settings template.
*/
public function connect_url() {
if ( $this->is_connected() ) {
$url = $this->get_disconnect_url();
?>
<a id="<?php echo $this->slug(); ?>-disconnect" href="<?php echo $url; ?>" class="button-secondary"><?php _e( 'Disconnect', 'ninja-forms-uploads' ); ?></a>
<?php $this->get_disconnection_description(); ?>
<?php } else {
$url = $this->get_authorize_url();
?>
<a id="<?php echo $this->slug(); ?>-connect" href="<?php echo $url; ?>" class="button-primary"><?php _e( 'Connect', 'ninja-forms-uploads' ); ?></a>
<?php
}
}
protected function get_disconnection_description() {}
/**
* Is the service connected?
*
* @param null|array $settings
*
* @return bool
*/
public function is_connected( $settings = null ) {
if ( ! $this->has_access_token() ) {
return false;
}
if ( ! $this->transient_expires || false === ( $authorised = get_transient( $this->get_transient_key() ) ) ) {
$authorised = $this->is_authorized();
if ( $this->transient_expires ) {
set_transient( $this->get_transient_key(), $authorised, $this->transient_expires );
}
}
return $authorised;
}
/**
* @return bool
*/
protected abstract function is_authorized();
/**
* @return bool
*/
public function has_access_token() {
return ( bool ) $this->get_access_token();
}
/**
* Ger the access token
*
* @return bool
*/
public function get_access_token() {
if ( isset( $this->tokens[ $this->slug() ] ) ) {
return $this->tokens[ $this->slug() ];
}
$oauth2_token = NF_File_Uploads()->externals->wpoauth()->token_manager->get_access_token( $this->slug() );
if ( $oauth2_token ) {
$this->tokens[ $this->slug() ] = $oauth2_token;
return $oauth2_token;
}
return false;
}
/**
* Get a new access token.
*
* @return string|bool
*/
public function refresh_access_token() {
return NF_File_Uploads()->externals->wpoauth()->refresh_access_token( $this->consumer_key, $this->slug() );
}
}