| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Log_API |
| 4 |
{ |
| 5 |
public $logtivityStoreLogEndpoint = 'https://api.logtivity.io/logs/store'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Option class to access the plugin settings |
| 9 |
* |
| 10 |
* @var object |
| 11 |
*/ |
| 12 |
protected $options; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$this->options = new Logtivity_Options; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the endpoint we post to with the log data to store |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function getStoreUrl() |
| 25 |
{ |
| 26 |
if (defined('Logtivity_Store_Endpoint')) { |
| 27 |
return Logtivity_Store_Endpoint; |
| 28 |
} |
| 29 |
|
| 30 |
return $this->logtivityStoreLogEndpoint; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Make a request to the Logtivity API |
| 35 |
* |
| 36 |
* @param string $url |
| 37 |
* @param array $body |
| 38 |
* @param string $method |
| 39 |
* @return mixed $response |
| 40 |
*/ |
| 41 |
public function makeRequest($url, $body, $method = 'POST') |
| 42 |
{ |
| 43 |
$api_key = logtivity_get_api_key(); |
| 44 |
|
| 45 |
if (!$api_key) |
| 46 |
{ |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$shouldLogLatestResponse = $this->options->shouldLogLatestResponse(); |
| 51 |
|
| 52 |
$response = wp_remote_post( $url, [ |
| 53 |
'method' => $method, |
| 54 |
'timeout' => ( $shouldLogLatestResponse ? 45 : 0.01), |
| 55 |
'blocking' => ( $shouldLogLatestResponse ? true : false), |
| 56 |
'redirection' => 5, |
| 57 |
'httpversion' => '1.0', |
| 58 |
'headers' => array(), |
| 59 |
'body' => array_merge(['api_key' => $api_key], $body), |
| 60 |
'cookies' => array() |
| 61 |
]); |
| 62 |
|
| 63 |
$response = wp_remote_retrieve_body($response); |
| 64 |
|
| 65 |
if ($shouldLogLatestResponse) { |
| 66 |
|
| 67 |
$this->options->update([ |
| 68 |
'logtivity_latest_response' => [ |
| 69 |
'date' => date("Y-m-d H:i:s"), |
| 70 |
'response' => print_r($response, true) |
| 71 |
] |
| 72 |
]); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
return $response; |
| 77 |
} |
| 78 |
} |