| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Admin\Services; |
| 4 |
|
| 5 |
use ElementorOne\Admin\Helpers\Utils; |
| 6 |
use ElementorOne\Logger; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Feedback |
| 14 |
*/ |
| 15 |
class Feedback { |
| 16 |
|
| 17 |
/** |
| 18 |
* Logger instance |
| 19 |
* @var Logger |
| 20 |
*/ |
| 21 |
private Logger $logger; |
| 22 |
|
| 23 |
/** |
| 24 |
* Instance |
| 25 |
* @var Feedback|null |
| 26 |
*/ |
| 27 |
private static ?Feedback $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get instance |
| 31 |
* @return Feedback|null |
| 32 |
*/ |
| 33 |
public static function instance(): ?Feedback { |
| 34 |
if ( ! self::$instance ) { |
| 35 |
self::$instance = new self(); |
| 36 |
} |
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
*/ |
| 43 |
private function __construct() { |
| 44 |
$this->logger = new Logger( self::class ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get product feedback URL |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_product_feedback_url(): string { |
| 52 |
return Client::get_client_base_url() . '/feedback/api/v1/product-feedback'; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Send product feedback |
| 57 |
* @param string $product |
| 58 |
* @param string $subject |
| 59 |
* @param string $title |
| 60 |
* @param string $description |
| 61 |
* @param string|null $country_code |
| 62 |
* @return void |
| 63 |
* @throws \ElementorOne\Admin\Exceptions\ClientException |
| 64 |
*/ |
| 65 |
public function send_product_feedback( string $product, string $subject, string $title, string $description, ?string $country_code = null ): void { |
| 66 |
try { |
| 67 |
$body = [ |
| 68 |
'product' => $product, |
| 69 |
'subject' => $subject, |
| 70 |
'title' => $title, |
| 71 |
'description' => $description, |
| 72 |
'referrerUrl' => wp_get_referer(), |
| 73 |
]; |
| 74 |
|
| 75 |
if ( ! empty( $country_code ) ) { |
| 76 |
$body['countryCode'] = $country_code; |
| 77 |
} |
| 78 |
|
| 79 |
Utils::get_api_client()->request( |
| 80 |
$this->get_product_feedback_url(), |
| 81 |
[ |
| 82 |
'method' => 'POST', |
| 83 |
'body' => wp_json_encode( $body ), |
| 84 |
] |
| 85 |
); |
| 86 |
} catch ( \Throwable $th ) { |
| 87 |
$this->logger->error( $th->getMessage() ); |
| 88 |
|
| 89 |
if ( $th instanceof \ElementorOne\Admin\Exceptions\ClientException ) { |
| 90 |
throw $th; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|