| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\PluginFeedback; |
| 4 |
|
| 5 |
class Request |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Sends the feedback data to the remote server. |
| 9 |
* |
| 10 |
* @param array $data The collected data. |
| 11 |
* @return bool True if the data was sent successfully, False otherwise. |
| 12 |
*/ |
| 13 |
public function send(array $data): bool |
| 14 |
{ |
| 15 |
|
| 16 |
$body = json_encode($data); |
| 17 |
|
| 18 |
// Create unique hash to prevent duplicate submissions |
| 19 |
$hash = md5($body); |
| 20 |
|
| 21 |
if (get_transient('ql_plugin_feedback_' . $hash)) { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
// Create a transient with the hash as the key and set the expiration time to 7 days |
| 26 |
set_transient('ql_plugin_feedback_' . $hash, true, 30 * DAY_IN_SECONDS); |
| 27 |
|
| 28 |
$response = wp_remote_post( |
| 29 |
'https://feedback.quadlayers.com/', |
| 30 |
array( |
| 31 |
'body' => $body, |
| 32 |
'headers' => array( |
| 33 |
'Content-Type' => 'application/json', |
| 34 |
), |
| 35 |
'headers' => [ |
| 36 |
'Content-Type' => 'application/json', |
| 37 |
'Content-Length' => strlen($body), |
| 38 |
'User-Agent' => 'WordPress/QuadLayers', // Set a custom User-Agent |
| 39 |
], |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
if (is_wp_error($response)) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$statusCode = wp_remote_retrieve_response_code($response); |
| 48 |
|
| 49 |
// Return true if the status code is 200 (OK) |
| 50 |
return $statusCode === 200; |
| 51 |
} |
| 52 |
} |
| 53 |
|