| @@ -7,17 +7,11 @@ | ||
| 7 | 7 | use WP_REST_Request; |
| 8 | 8 | use WP_REST_Response; |
| 9 | 9 | use Yoast\WP\SEO\AI\Authorization\Application\Token_Manager; |
| 10 | 10 | use Yoast\WP\SEO\AI\Consent\Application\Consent_Handler; |
| 11 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Bad_Request_Exception; | |
| 12 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Forbidden_Exception; | |
| 13 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception; | |
| 14 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Not_Found_Exception; | |
| 15 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Payment_Required_Exception; | |
| 16 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Request_Timeout_Exception; | |
| 17 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception; | |
| 18 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception; | |
| 11 | +use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception; | |
| 19 | 12 | use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 13 | +use Yoast\WP\SEO\Loggers\Logger; | |
| 20 | 14 | use Yoast\WP\SEO\Main; |
| 21 | 15 | use Yoast\WP\SEO\Routes\Route_Interface; |
| 22 | 16 | |
| 23 | 17 | /** |
| @@ -57,8 +51,15 @@ | ||
| 57 | 51 | */ |
| 58 | 52 | private $token_manager; |
| 59 | 53 | |
| 60 | 54 | /** |
| 55 | + * The logger instance. | |
| 56 | + * | |
| 57 | + * @var Logger | |
| 58 | + */ | |
| 59 | + private $logger; | |
| 60 | + | |
| 61 | + /** | |
| 61 | 62 | * Returns the conditionals based in which this loadable should be active. |
| 62 | 63 | * |
| 63 | 64 | * @return array<string> The conditionals. |
| 64 | 65 | */ |
| @@ -70,12 +71,15 @@ | ||
| 70 | 71 | * Class constructor. |
| 71 | 72 | * |
| 72 | 73 | * @param Consent_Handler $consent_handler The consent handler. |
| 73 | 74 | * @param Token_Manager $token_manager The token manager. |
| 75 | + * @param Logger $logger The logger. | |
| 74 | 76 | */ |
| 75 | - public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager ) { | |
| 77 | + public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager, Logger $logger ) { | |
| 76 | 78 | $this->consent_handler = $consent_handler; |
| 77 | - $this->token_manager = $token_manager; | |
| 79 | + // @TODO: Remove the token manager as soon as we don't care about BC, because it's no longer used. | |
| 80 | + $this->token_manager = $token_manager; | |
| 81 | + $this->logger = $logger; | |
| 78 | 82 | } |
| 79 | 83 | |
| 80 | 84 | /** |
| 81 | 85 | * Registers routes with WordPress. |
| @@ -117,18 +121,18 @@ | ||
| 117 | 121 | // Store the consent at user level. |
| 118 | 122 | $this->consent_handler->grant_consent( $user_id ); |
| 119 | 123 | } |
| 120 | 124 | else { |
| 121 | - // Delete the consent at user level. | |
| 125 | + // Revoke the consent locally and remotely (this also invalidates the JWT tokens). | |
| 122 | 126 | $this->consent_handler->revoke_consent( $user_id ); |
| 123 | - // Invalidate the token if the user revoked the consent. | |
| 124 | - $this->token_manager->token_invalidate( $user_id ); | |
| 125 | 127 | } |
| 126 | - } catch ( Bad_Request_Exception | Forbidden_Exception | Internal_Server_Error_Exception | Not_Found_Exception | Payment_Required_Exception | Request_Timeout_Exception | Service_Unavailable_Exception | Too_Many_Requests_Exception | RuntimeException $e ) { | |
| 127 | - return new WP_REST_Response( ( $consent ) ? 'Failed to store consent.' : 'Failed to revoke consent.', 500 ); | |
| 128 | + } catch ( Remote_Request_Exception | RuntimeException $e ) { | |
| 129 | + $status_code = ( $e instanceof Remote_Request_Exception ) ? $e->getCode() : 500; | |
| 130 | + $this->logger->error( $e->getMessage(), [ 'exception' => $e ] ); | |
| 131 | + return new WP_REST_Response( ( $consent ) ? 'Failed to give consent.' : 'Failed to revoke consent.', $status_code ); | |
| 128 | 132 | } |
| 129 | 133 | |
| 130 | - return new WP_REST_Response( ( $consent ) ? 'Consent successfully stored.' : 'Consent successfully revoked.' ); | |
| 134 | + return new WP_REST_Response( ( $consent ) ? 'Consent successfully given.' : 'Consent successfully revoked.' ); | |
| 131 | 135 | } |
| 132 | 136 | |
| 133 | 137 | /** |
| 134 | 138 | * Checks: |