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 +34 -33 27.5 → trunk View file →
@@ -4,14 +4,16 @@
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 17 use Yoast\WP\SEO\Main;
16 18 use Yoast\WP\SEO\Routes\Route_Interface;
17 19
@@ -40,20 +42,20 @@
40 42 */
41 43 public const ROUTE_PREFIX = '/ai_generator/get_usage';
42 44
43 45 /**
44 - * The token manager instance.
46 + * The auth strategy factory.
45 47 *
46 - * @var Token_Manager
48 + * @var AI_Request_Sender_Factory
47 49 */
48 - private $token_manager;
50 + private $ai_request_sender_factory;
49 51
50 52 /**
51 - * The request handler instance.
53 + * The consent handler instance.
52 54 *
53 - * @var Request_Handler
55 + * @var Consent_Handler
54 56 */
55 - private $request_handler;
57 + private $consent_handler;
56 58
57 59 /**
58 60 * Represents the add-on manager.
59 61 *
@@ -72,16 +74,16 @@
72 74
73 75 /**
74 76 * Class constructor.
75 77 *
76 - * @param Token_Manager $token_manager The token manager instance.
77 - * @param Request_Handler $request_handler The request handler instance.
78 - * @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.
79 81 */
80 - public function __construct( Token_Manager $token_manager, Request_Handler $request_handler, WPSEO_Addon_Manager $addon_manager ) {
81 - $this->addon_manager = $addon_manager;
82 - $this->token_manager = $token_manager;
83 - $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;
84 86 }
85 87
86 88 /**
87 89 * Registers routes with WordPress.
@@ -116,16 +118,16 @@
116 118 public function get_usage( $request ): WP_REST_Response {
117 119 $is_woo_product_entity = $request->get_param( 'is_woo_product_entity' );
118 120 $user = \wp_get_current_user();
119 121 try {
120 - $token = $this->token_manager->get_or_request_access_token( $user );
121 - $request_headers = [
122 - 'Authorization' => "Bearer $token",
123 - ];
124 - $action_path = $this->get_action_path( $is_woo_product_entity );
125 - $response = $this->request_handler->handle( new Request( $action_path, [], $request_headers, false ) );
126 - $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() );
127 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 + }
128 130 $message = [
129 131 'errorMessage' => $e->getMessage(),
130 132 'errorIdentifier' => $e->get_error_identifier(),
131 133 'errorCode' => $e->getCode(),
@@ -142,21 +144,20 @@
142 144 return new WP_REST_Response( $data );
143 145 }
144 146
145 147 /**
146 - * Get action path for the request.
148 + * Whether the current request should read the time-unbound free-usage bucket.
147 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 + *
148 153 * @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity.
149 154 *
150 - * @return string The action path.
155 + * @return bool True when the user is on the free tier for this request type.
151 156 */
152 - public function get_action_path( $is_woo_product_entity = false ): string {
153 - $unlimited = '/usage/' . \gmdate( 'Y-m' );
154 - if ( $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) ) {
155 - 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 );
156 160 }
157 - if ( ! $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
158 - return $unlimited;
159 - }
160 - return '/usage/free-usages';
161 + return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG );
161 162 }
162 163 }