| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Api |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Option class to access the plugin settings |
| 7 |
* |
| 8 |
* @var object |
| 9 |
*/ |
| 10 |
protected $options; |
| 11 |
|
| 12 |
/** |
| 13 |
* Should we wait to return the response from the API? |
| 14 |
* |
| 15 |
* @var boolean |
| 16 |
*/ |
| 17 |
public $waitForResponse = true; |
| 18 |
|
| 19 |
/** |
| 20 |
* The API key for either the site or team |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $api_key; |
| 25 |
|
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
$this->options = new Logtivity_Options; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the API URL for the Logtivity endpoint |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function getEndpoint($endpoint) |
| 37 |
{ |
| 38 |
return logtivity_get_api_url() . $endpoint; |
| 39 |
} |
| 40 |
|
| 41 |
public function post($url, $body) |
| 42 |
{ |
| 43 |
return $this->makeRequest($url, $body, 'POST'); |
| 44 |
} |
| 45 |
|
| 46 |
public function get($url, $body) |
| 47 |
{ |
| 48 |
return $this->makeRequest($url, $body, 'GET'); |
| 49 |
} |
| 50 |
|
| 51 |
public function setApiKey($api_key) |
| 52 |
{ |
| 53 |
$this->api_key = $api_key; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Make a request to the Logtivity API |
| 60 |
* |
| 61 |
* @param string $url |
| 62 |
* @param array $body |
| 63 |
* @param string $method |
| 64 |
* @return mixed $response |
| 65 |
*/ |
| 66 |
public function makeRequest($url, $body, $method = 'POST') |
| 67 |
{ |
| 68 |
if (!$this->api_key) { |
| 69 |
$this->api_key = logtivity_get_api_key(); |
| 70 |
} |
| 71 |
|
| 72 |
if (!$this->api_key) |
| 73 |
{ |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$shouldLogLatestResponse = $this->waitForResponse || $this->options->shouldLogLatestResponse(); |
| 78 |
|
| 79 |
$response = wp_remote_post($this->getEndpoint($url), [ |
| 80 |
'method' => $method, |
| 81 |
'timeout' => ( $shouldLogLatestResponse ? 45 : 0.01), |
| 82 |
'blocking' => ( $shouldLogLatestResponse ? true : false), |
| 83 |
'redirection' => 5, |
| 84 |
'httpversion' => '1.0', |
| 85 |
'headers' => [ |
| 86 |
'Authorization' => 'Bearer '.$this->api_key |
| 87 |
], |
| 88 |
'body' => $body, |
| 89 |
'cookies' => array() |
| 90 |
]); |
| 91 |
|
| 92 |
$response = wp_remote_retrieve_body($response); |
| 93 |
|
| 94 |
if ($shouldLogLatestResponse && $this->notUpdatingWidgetInCustomizer() && $method === 'POST') { |
| 95 |
|
| 96 |
$this->options->update([ |
| 97 |
'logtivity_latest_response' => [ |
| 98 |
'date' => date("Y-m-d H:i:s"), |
| 99 |
'response' => print_r($response, true) |
| 100 |
] |
| 101 |
], |
| 102 |
false |
| 103 |
); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
return $response; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* You cannot call an extra update_option during a widget update so we make |
| 112 |
* sure not to log the most recent log response in this case. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
private function notUpdatingWidgetInCustomizer() |
| 117 |
{ |
| 118 |
if (!isset($_POST['wp_customize'])) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
if (!isset($_POST['action'])) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
return ! ($_POST['action'] === 'update-widget' && $_POST['wp_customize'] === 'on'); |
| 127 |
} |
| 128 |
} |