PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Dropbox2 / OAuth / Consumer / WordPress.php

WordPress.php in InfiniteWP Client trunk, at lib/Dropbox2/OAuth/Consumer/WordPress.php

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