| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | namespace bhr\Admin\Controller; |
|---|
| 4 | |
|---|
| 5 | use bhr\Admin\Model\AdminModel; |
|---|
| 6 | use bhr\Admin\Model\Helper as ModelHelper; |
|---|
| 7 | use bhr\Includes\Helper as IncludesHelper; |
|---|
| 8 | use Error; |
|---|
| 9 | use Exception; |
|---|
| 10 | use WP_REST_Request; |
|---|
| 11 | use WP_REST_Response; |
|---|
| 12 | |
|---|
| 13 | class CallbackController { |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * @var AdminModel |
|---|
| 17 | */ |
|---|
| 18 | private $AdminModel; |
|---|
| 19 | |
|---|
| 20 | public function __construct() { |
|---|
| 21 | $this->AdminModel = new AdminModel(); |
|---|
| 22 | $this->AdminModel->getConfigurationFromDb(); |
|---|
| 23 | $this->AdminModel->getPlatformSettingsFromDb(); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Log message from API v3 callback request |
|---|
| 28 | * @param WP_REST_Request $request |
|---|
| 29 | * |
|---|
| 30 | * @return WP_REST_Response response |
|---|
| 31 | */ |
|---|
| 32 | public function log_callback_message( $request ) |
|---|
| 33 | { |
|---|
| 34 | try { |
|---|
| 35 | $params = $request->get_params(); |
|---|
| 36 | if ( empty( $params['problems'] ) ) { |
|---|
| 37 | ModelHelper::salesmanago_log('There is no data in request body', __FILE__ ); |
|---|
| 38 | exit; |
|---|
| 39 | } |
|---|
| 40 | foreach ( $params['problems'] as $problem ) |
|---|
| 41 | { |
|---|
| 42 | ModelHelper::salesmanago_log( $problem, __CLASS__, true ); |
|---|
| 43 | } |
|---|
| 44 | $this->AdminModel->getConfiguration()->setIsNewApiError( true ); |
|---|
| 45 | $this->AdminModel->saveConfiguration(); |
|---|
| 46 | |
|---|
| 47 | return new WP_REST_Response( array ( |
|---|
| 48 | 'status' => 200, |
|---|
| 49 | 'response' => 'Message logged' |
|---|
| 50 | ) ); |
|---|
| 51 | } catch ( Error | Exception $e ) { |
|---|
| 52 | error_log( $e->getMessage() ); |
|---|
| 53 | exit; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * User acknowledges being notified about Product API Error from Callback |
|---|
| 59 | */ |
|---|
| 60 | public function acknowledge_callback_message() |
|---|
| 61 | { |
|---|
| 62 | $this->AdminModel->getConfiguration()->setIsNewApiError( false ); |
|---|
| 63 | $this->AdminModel->saveConfiguration(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Validate the request using its token |
|---|
| 68 | * @param string $token |
|---|
| 69 | * @return bool is_valid_callback |
|---|
| 70 | */ |
|---|
| 71 | public function validate_api_v3_callback( $token ) |
|---|
| 72 | { |
|---|
| 73 | return $token === IncludesHelper::generate_sm_token(); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|