| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* OAuth consumer using the WordPress API |
| 5 |
* @author David Anderson <david@wordshell.net> |
| 6 |
* @link https://github.com/DavidAnderson684/Dropbox |
| 7 |
* @package Dropbox\OAuth |
| 8 |
* @subpackage Consumer |
| 9 |
*/ |
| 10 |
|
| 11 |
class Dropbox_ConsumerWordPress extends Dropbox_ConsumerAbstract |
| 12 |
{ |
| 13 |
|
| 14 |
/** |
| 15 |
* Set properties and begin authentication |
| 16 |
* @param string $key |
| 17 |
* @param string $secret |
| 18 |
* @param \Dropbox\OAuth\Consumer\StorageInterface $storage |
| 19 |
* @param string $callback |
| 20 |
*/ |
| 21 |
public function __construct($key, $secret, Dropbox_StorageInterface $storage, $callback = null) |
| 22 |
{ |
| 23 |
// Check we are in a WordPress environment |
| 24 |
if (!defined('ABSPATH')) { |
| 25 |
throw new Dropbox_Exception('The WordPress OAuth consumer requires a WordPress environment'); |
| 26 |
} |
| 27 |
|
| 28 |
$this->consumerKey = $key; |
| 29 |
$this->consumerSecret = $secret; |
| 30 |
$this->storage = $storage; |
| 31 |
$this->callback = $callback; |
| 32 |
$this->authenticate(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Execute an API call |
| 37 |
* @param string $method The HTTP method |
| 38 |
* @param string $url The API endpoint |
| 39 |
* @param string $call The API method to call |
| 40 |
* @param array $additional Additional parameters |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function fetch($method, $url, $call, array $additional = array()) |
| 44 |
{ |
| 45 |
// Get the signed request URL |
| 46 |
$request = $this->getSignedRequest($method, $url, $call, $additional); |
| 47 |
if ($method == 'GET') { |
| 48 |
$args = array ( ); |
| 49 |
$response = wp_remote_get($request['url'], $args); |
| 50 |
$this->outFile = null; |
| 51 |
} elseif ($method == 'POST') { |
| 52 |
$args = array( 'body' => $request['postfields'] ); |
| 53 |
$response = wp_remote_post($request['url'], $args ); |
| 54 |
} elseif ($method == 'PUT' && $this->inFile) { |
| 55 |
return new WP_Error('unsupported', __("WordPress does not have a native HTTP PUT function")); |
| 56 |
} |
| 57 |
|
| 58 |
// If the response body is not a JSON encoded string |
| 59 |
// we'll return the entire response body |
| 60 |
// Important to do this first, as the next section relies on the decoding having taken place |
| 61 |
if (!$body = json_decode($response['body'])) { |
| 62 |
$body = $response['body']; |
| 63 |
} |
| 64 |
|
| 65 |
// Check if an error occurred and throw an Exception. This is part of the authentication process - don't modify. |
| 66 |
if (!empty($body->error)) { |
| 67 |
$message = $body->error . ' (Status Code: ' . $response['code'] . ')'; |
| 68 |
throw new Dropbox_Exception($message); |
| 69 |
} |
| 70 |
|
| 71 |
if (is_wp_error($response)) { |
| 72 |
$message = $response->get_error_message(); |
| 73 |
throw new Dropbox_Exception($message); |
| 74 |
} |
| 75 |
|
| 76 |
$results = array ( 'body' => $body, 'code' => $response['response']['code'], 'headers' => $response['headers'] ); |
| 77 |
return $results; |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|