1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base API client class. |
4
|
|
|
* @package Writing_On_GitHub |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Writing_On_GitHub_Base_Client |
9
|
|
|
*/ |
10
|
|
|
class Writing_On_GitHub_Base_Client { |
11
|
|
|
|
12
|
|
|
const HOST_OPTION_KEY = 'wogh_host'; |
13
|
|
|
const TOKEN_OPTION_KEY = 'wogh_oauth_token'; |
14
|
|
|
const REPO_OPTION_KEY = 'wogh_repository'; |
15
|
|
|
const BRANCH_OPTION_KEY = 'wogh_branch'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Application container. |
19
|
|
|
* |
20
|
|
|
* @var Writing_On_GitHub |
21
|
|
|
*/ |
22
|
|
|
protected $app; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Instantiates a new Api object. |
26
|
|
|
* |
27
|
|
|
* @param Writing_On_GitHub $app Application container. |
28
|
|
|
*/ |
29
|
8 |
|
public function __construct( Writing_On_GitHub $app ) { |
30
|
8 |
|
$this->app = $app; |
31
|
8 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generic GitHub API interface and response handler |
35
|
|
|
* |
36
|
|
|
* @param string $method HTTP method. |
37
|
|
|
* @param string $endpoint API endpoint. |
38
|
|
|
* @param array $body Request body. |
39
|
|
|
* |
40
|
|
|
* @return stdClass|WP_Error |
41
|
|
|
*/ |
42
|
8 |
|
protected function call( $method, $endpoint, $body = array() ) { |
43
|
8 |
|
if ( is_wp_error( $error = $this->can_call() ) ) { |
44
|
|
|
/* @var WP_Error $error */ |
45
|
3 |
|
return $error; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$args = array( |
49
|
5 |
|
'method' => $method, |
50
|
|
|
'headers' => array( |
51
|
5 |
|
'Authorization' => 'token ' . $this->oauth_token(), |
52
|
5 |
|
), |
53
|
5 |
|
); |
54
|
|
|
|
55
|
5 |
|
if ( 'GET' !== $method ) { |
56
|
1 |
|
$args['body'] = json_encode( $body ); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
// $tmpbody = isset( $args['body'] ) ? $args['body'] : ''; |
60
|
|
|
// error_log( "writing-on-github-call $method $endpoint $tmpbody" ); |
61
|
|
|
|
62
|
5 |
|
$response = wp_remote_request( $endpoint, $args ); |
63
|
5 |
|
$status = wp_remote_retrieve_header( $response, 'status' ); |
64
|
5 |
|
$body = json_decode( wp_remote_retrieve_body( $response ) ); |
65
|
|
|
|
66
|
5 |
|
if ( '2' !== substr( $status, 0, 1 ) && '3' !== substr( $status, 0, 1 ) ) { |
67
|
2 |
|
return new WP_Error( |
68
|
2 |
|
strtolower( str_replace( ' ', '_', $status ) ), |
69
|
2 |
|
sprintf( |
70
|
2 |
|
__( 'Method %s to endpoint %s failed with error: %s', 'writing-on-github' ), |
71
|
2 |
|
$method, |
72
|
2 |
|
$endpoint, |
73
|
2 |
|
( $body && $body->message ) ? $body->message : 'Unknown error' |
74
|
2 |
|
) |
75
|
2 |
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return $body; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Validates whether the Api object can make a call. |
83
|
|
|
* |
84
|
|
|
* @return true|WP_Error |
85
|
|
|
*/ |
86
|
8 |
|
protected function can_call() { |
87
|
8 |
|
if ( ! $this->oauth_token() ) { |
88
|
1 |
|
return new WP_Error( |
89
|
1 |
|
'missing_token', |
90
|
1 |
|
__( 'Writing On GitHub needs an auth token. Please update your settings.', 'writing-on-github' ) |
91
|
1 |
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
$repo = $this->repository(); |
95
|
|
|
|
96
|
7 |
|
if ( ! $repo ) { |
97
|
1 |
|
return new WP_Error( |
98
|
1 |
|
'missing_repository', |
99
|
1 |
|
__( 'Writing On GitHub needs a repository. Please update your settings.', 'writing-on-github' ) |
100
|
1 |
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
$parts = explode( '/', $repo ); |
104
|
|
|
|
105
|
6 |
|
if ( 2 !== count( $parts ) ) { |
106
|
1 |
|
return new WP_Error( |
107
|
1 |
|
'malformed_repository', |
108
|
1 |
|
__( 'Writing On GitHub needs a properly formed repository. Please update your settings.', 'writing-on-github' ) |
109
|
1 |
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the repository to sync with |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
8 |
|
public function repository() { |
121
|
8 |
|
return (string) get_option( self::REPO_OPTION_KEY ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the user's oauth token |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
8 |
|
public function oauth_token() { |
130
|
8 |
|
return (string) get_option( self::TOKEN_OPTION_KEY ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the GitHub host to sync with (for GitHub Enterprise support) |
135
|
|
|
*/ |
136
|
8 |
|
public function api_base() { |
137
|
8 |
|
return get_option( self::HOST_OPTION_KEY ); |
138
|
|
|
} |
139
|
|
|
|
140
|
8 |
|
public function branch() { |
141
|
8 |
|
$branch = get_option( self::BRANCH_OPTION_KEY ); |
142
|
8 |
|
return $branch ? $branch : 'master'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* API endpoint for the master branch reference |
147
|
|
|
*/ |
148
|
|
|
public function reference_endpoint() { |
149
|
|
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
150
|
|
|
$url = $url . $this->repository() . '/git/refs/heads/' . $this->branch(); |
151
|
|
|
|
152
|
|
|
return $url; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Api to get and create commits |
157
|
|
|
*/ |
158
|
|
|
public function commit_endpoint() { |
159
|
|
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
160
|
|
|
$url = $url . $this->repository() . '/git/commits'; |
161
|
|
|
|
162
|
|
|
return $url; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Api to compare commits |
167
|
|
|
*/ |
168
|
1 |
|
public function compare_endpoint() { |
169
|
1 |
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
170
|
1 |
|
$url = $url . $this->repository() . '/compare'; |
171
|
|
|
|
172
|
1 |
|
return $url; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Api to get and create trees |
177
|
|
|
*/ |
178
|
6 |
|
public function tree_endpoint() { |
179
|
6 |
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
180
|
6 |
|
$url = $url . $this->repository() . '/git/trees'; |
181
|
|
|
|
182
|
6 |
|
return $url; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Builds the proper blob API endpoint for a given post |
187
|
|
|
* |
188
|
|
|
* Returns String the relative API call path |
189
|
|
|
*/ |
190
|
1 |
|
public function blob_endpoint() { |
191
|
1 |
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
192
|
1 |
|
$url = $url . $this->repository() . '/git/blobs'; |
193
|
|
|
|
194
|
1 |
|
return $url; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Builds the proper content API endpoint for a given post |
199
|
|
|
* |
200
|
|
|
* Returns String the relative API call path |
201
|
|
|
*/ |
202
|
1 |
|
public function content_endpoint( $path = false ) { |
203
|
1 |
|
$url = $this->api_base() . '/repos/'; |
|
|
|
|
204
|
1 |
|
$url = $url . $this->repository() . '/contents'; |
205
|
|
|
|
206
|
1 |
|
if ( ! empty($path) ) { |
207
|
1 |
|
$url .= '/' . $path; |
208
|
1 |
|
} |
209
|
|
|
|
210
|
1 |
|
return $url; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|