PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
← All changes | src/ai/generator/user-interface/get-usage-route.php +28 -43 27.9 → trunk View file →
@@ -4,18 +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;
8 +use Yoast\WP\SEO\AI\Authentication\Application\AI_Request_Sender_Factory;
9 9 use Yoast\WP\SEO\AI\Consent\Application\Consent_Handler;
10 -use Yoast\WP\SEO\AI\HTTP_Request\Application\Request_Handler;
10 +use Yoast\WP\SEO\AI\Generator\Domain\Usage_Parameters;
11 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;
12 13 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception;
13 14 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
14 15 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\WP_Request_Exception;
15 -use Yoast\WP\SEO\AI\HTTP_Request\Domain\Request;
16 16 use Yoast\WP\SEO\Conditionals\AI_Conditional;
17 -use Yoast\WP\SEO\Conditionals\New_Premium_Or_Free_AI_Conditional;
18 17 use Yoast\WP\SEO\Main;
19 18 use Yoast\WP\SEO\Routes\Route_Interface;
20 19
21 20 /**
@@ -43,22 +42,15 @@
43 42 */
44 43 public const ROUTE_PREFIX = '/ai_generator/get_usage';
45 44
46 45 /**
47 - * The token manager instance.
46 + * The auth strategy factory.
48 47 *
49 - * @var Token_Manager
48 + * @var AI_Request_Sender_Factory
50 49 */
51 - private $token_manager;
50 + private $ai_request_sender_factory;
52 51
53 52 /**
54 - * The request handler instance.
55 - *
56 - * @var Request_Handler
57 - */
58 - private $request_handler;
59 -
60 - /**
61 53 * The consent handler instance.
62 54 *
63 55 * @var Consent_Handler
64 56 */
@@ -76,24 +68,22 @@
76 68 *
77 69 * @return array<string> The conditionals.
78 70 */
79 71 public static function get_conditionals() {
80 - return [ AI_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ];
72 + return [ AI_Conditional::class ];
81 73 }
82 74
83 75 /**
84 76 * Class constructor.
85 77 *
86 - * @param Token_Manager $token_manager The token manager instance.
87 - * @param Request_Handler $request_handler The request handler instance.
88 - * @param Consent_Handler $consent_handler The consent handler instance.
89 - * @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.
90 81 */
91 - public function __construct( Token_Manager $token_manager, Request_Handler $request_handler, Consent_Handler $consent_handler, WPSEO_Addon_Manager $addon_manager ) {
92 - $this->addon_manager = $addon_manager;
93 - $this->token_manager = $token_manager;
94 - $this->request_handler = $request_handler;
95 - $this->consent_handler = $consent_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;
96 86 }
97 87
98 88 /**
99 89 * Registers routes with WordPress.
@@ -128,18 +118,14 @@
128 118 public function get_usage( $request ): WP_REST_Response {
129 119 $is_woo_product_entity = $request->get_param( 'is_woo_product_entity' );
130 120 $user = \wp_get_current_user();
131 121 try {
132 - $token = $this->token_manager->get_or_request_access_token( $user );
133 - $request_headers = [
134 - 'Authorization' => "Bearer $token",
135 - ];
136 - $action_path = $this->get_action_path( $is_woo_product_entity );
137 - $response = $this->request_handler->handle( new Request( $action_path, [], $request_headers, false ) );
138 - $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() );
139 126 } catch ( Remote_Request_Exception | WP_Request_Exception $e ) {
140 - if ( $e instanceof Forbidden_Exception ) {
141 - // The API signals that consent is revoked; sync local state.
127 + if ( $e instanceof Forbidden_Exception && ! $e instanceof Insufficient_Scope_Exception ) {
142 128 $this->consent_handler->revoke_consent( $user->ID );
143 129 }
144 130 $message = [
145 131 'errorMessage' => $e->getMessage(),
@@ -158,21 +144,20 @@
158 144 return new WP_REST_Response( $data );
159 145 }
160 146
161 147 /**
162 - * Get action path for the request.
148 + * Whether the current request should read the time-unbound free-usage bucket.
163 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 + *
164 153 * @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity.
165 154 *
166 - * @return string The action path.
155 + * @return bool True when the user is on the free tier for this request type.
167 156 */
168 - public function get_action_path( $is_woo_product_entity = false ): string {
169 - $unlimited = '/usage/' . \gmdate( 'Y-m' );
170 - if ( $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) ) {
171 - 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 );
172 160 }
173 - if ( ! $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
174 - return $unlimited;
175 - }
176 - return '/usage/free-usages';
161 + return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG );
177 162 }
178 163 }