← All changes
|
src/ai/generator/user-interface/get-usage-route.php
+35
-35
27.7
→
trunk
View file →
| @@ -4,16 +4,17 @@ | ||
| 4 | 4 | namespace Yoast\WP\SEO\AI\Generator\User_Interface; |
| 5 | 5 | |
| 6 | 6 | use WP_REST_Response; |
| 7 | 7 | use WPSEO_Addon_Manager; |
| 8 | -use Yoast\WP\SEO\AI\Authorization\Application\Token_Manager; | |
| 9 | -use Yoast\WP\SEO\AI\HTTP_Request\Application\Request_Handler; | |
| 8 | +use Yoast\WP\SEO\AI\Authentication\Application\AI_Request_Sender_Factory; | |
| 9 | +use Yoast\WP\SEO\AI\Consent\Application\Consent_Handler; | |
| 10 | +use Yoast\WP\SEO\AI\Generator\Domain\Usage_Parameters; | |
| 11 | +use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Forbidden_Exception; | |
| 12 | +use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Insufficient_Scope_Exception; | |
| 10 | 13 | use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception; |
| 11 | 14 | use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception; |
| 12 | 15 | use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\WP_Request_Exception; |
| 13 | -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Request; | |
| 14 | 16 | use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 15 | -use Yoast\WP\SEO\Conditionals\New_Premium_Or_Free_AI_Conditional; | |
| 16 | 17 | use Yoast\WP\SEO\Main; |
| 17 | 18 | use Yoast\WP\SEO\Routes\Route_Interface; |
| 18 | 19 | |
| 19 | 20 | /** |
| @@ -41,20 +42,20 @@ | ||
| 41 | 42 | */ |
| 42 | 43 | public const ROUTE_PREFIX = '/ai_generator/get_usage'; |
| 43 | 44 | |
| 44 | 45 | /** |
| 45 | - * The token manager instance. | |
| 46 | + * The auth strategy factory. | |
| 46 | 47 | * |
| 47 | - * @var Token_Manager | |
| 48 | + * @var AI_Request_Sender_Factory | |
| 48 | 49 | */ |
| 49 | - private $token_manager; | |
| 50 | + private $ai_request_sender_factory; | |
| 50 | 51 | |
| 51 | 52 | /** |
| 52 | - * The request handler instance. | |
| 53 | + * The consent handler instance. | |
| 53 | 54 | * |
| 54 | - * @var Request_Handler | |
| 55 | + * @var Consent_Handler | |
| 55 | 56 | */ |
| 56 | - private $request_handler; | |
| 57 | + private $consent_handler; | |
| 57 | 58 | |
| 58 | 59 | /** |
| 59 | 60 | * Represents the add-on manager. |
| 60 | 61 | * |
| @@ -67,22 +68,22 @@ | ||
| 67 | 68 | * |
| 68 | 69 | * @return array<string> The conditionals. |
| 69 | 70 | */ |
| 70 | 71 | public static function get_conditionals() { |
| 71 | - return [ AI_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ]; | |
| 72 | + return [ AI_Conditional::class ]; | |
| 72 | 73 | } |
| 73 | 74 | |
| 74 | 75 | /** |
| 75 | 76 | * Class constructor. |
| 76 | 77 | * |
| 77 | - * @param Token_Manager $token_manager The token manager instance. | |
| 78 | - * @param Request_Handler $request_handler The request handler instance. | |
| 79 | - * @param WPSEO_Addon_Manager $addon_manager The add-on manager instance. | |
| 78 | + * @param AI_Request_Sender_Factory $ai_request_sender_factory The auth strategy factory. | |
| 79 | + * @param Consent_Handler $consent_handler The consent handler instance. | |
| 80 | + * @param WPSEO_Addon_Manager $addon_manager The add-on manager instance. | |
| 80 | 81 | */ |
| 81 | - public function __construct( Token_Manager $token_manager, Request_Handler $request_handler, WPSEO_Addon_Manager $addon_manager ) { | |
| 82 | - $this->addon_manager = $addon_manager; | |
| 83 | - $this->token_manager = $token_manager; | |
| 84 | - $this->request_handler = $request_handler; | |
| 82 | + public function __construct( AI_Request_Sender_Factory $ai_request_sender_factory, Consent_Handler $consent_handler, WPSEO_Addon_Manager $addon_manager ) { | |
| 83 | + $this->addon_manager = $addon_manager; | |
| 84 | + $this->ai_request_sender_factory = $ai_request_sender_factory; | |
| 85 | + $this->consent_handler = $consent_handler; | |
| 85 | 86 | } |
| 86 | 87 | |
| 87 | 88 | /** |
| 88 | 89 | * Registers routes with WordPress. |
| @@ -117,16 +118,16 @@ | ||
| 117 | 118 | public function get_usage( $request ): WP_REST_Response { |
| 118 | 119 | $is_woo_product_entity = $request->get_param( 'is_woo_product_entity' ); |
| 119 | 120 | $user = \wp_get_current_user(); |
| 120 | 121 | try { |
| 121 | - $token = $this->token_manager->get_or_request_access_token( $user ); | |
| 122 | - $request_headers = [ | |
| 123 | - 'Authorization' => "Bearer $token", | |
| 124 | - ]; | |
| 125 | - $action_path = $this->get_action_path( $is_woo_product_entity ); | |
| 126 | - $response = $this->request_handler->handle( new Request( $action_path, [], $request_headers, false ) ); | |
| 127 | - $data = \json_decode( $response->get_body() ); | |
| 122 | + $parameters = new Usage_Parameters( $user, $this->is_free( $is_woo_product_entity ), \gmdate( 'Y-m' ) ); | |
| 123 | + $sender = $this->ai_request_sender_factory->create( $user ); | |
| 124 | + $response = $sender->get_usage( $parameters ); | |
| 125 | + $data = \json_decode( $response->get_body() ); | |
| 128 | 126 | } catch ( Remote_Request_Exception | WP_Request_Exception $e ) { |
| 127 | + if ( $e instanceof Forbidden_Exception && ! $e instanceof Insufficient_Scope_Exception ) { | |
| 128 | + $this->consent_handler->revoke_consent( $user->ID ); | |
| 129 | + } | |
| 129 | 130 | $message = [ |
| 130 | 131 | 'errorMessage' => $e->getMessage(), |
| 131 | 132 | 'errorIdentifier' => $e->get_error_identifier(), |
| 132 | 133 | 'errorCode' => $e->getCode(), |
| @@ -143,21 +144,20 @@ | ||
| 143 | 144 | return new WP_REST_Response( $data ); |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | /** |
| 147 | - * Get action path for the request. | |
| 148 | + * Whether the current request should read the time-unbound free-usage bucket. | |
| 148 | 149 | * |
| 150 | + * A user without the relevant paid subscription is on the free tier, whose usage is tracked in | |
| 151 | + * the free-usage bucket rather than per period. | |
| 152 | + * | |
| 149 | 153 | * @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity. |
| 150 | 154 | * |
| 151 | - * @return string The action path. | |
| 155 | + * @return bool True when the user is on the free tier for this request type. | |
| 152 | 156 | */ |
| 153 | - public function get_action_path( $is_woo_product_entity = false ): string { | |
| 154 | - $unlimited = '/usage/' . \gmdate( 'Y-m' ); | |
| 155 | - if ( $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) ) { | |
| 156 | - return $unlimited; | |
| 157 | + public function is_free( $is_woo_product_entity = false ): bool { | |
| 158 | + if ( $is_woo_product_entity ) { | |
| 159 | + return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ); | |
| 157 | 160 | } |
| 158 | - if ( ! $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) { | |
| 159 | - return $unlimited; | |
| 160 | - } | |
| 161 | - return '/usage/free-usages'; | |
| 161 | + return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ); | |
| 162 | 162 | } |
| 163 | 163 | } |