PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
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
wordpress-seo / src / ai-generator / user-interface / get-usage-route.php

get-usage-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/ai-generator/user-interface/get-usage-route.php

164 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\AI_Generator\User_Interface;
5
6 use WP_REST_Response;
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;
10 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Remote_Request_Exception;
11 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
12 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\WP_Request_Exception;
13 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Request;
14 use Yoast\WP\SEO\Conditionals\AI_Conditional;
15 use Yoast\WP\SEO\Conditionals\Old_Premium_AI_Conditional;
16 use Yoast\WP\SEO\Main;
17 use Yoast\WP\SEO\Routes\Route_Interface;
18
19 /**
20 * Registers a route to get suggestions from the AI API
21 *
22 * @makePublic
23 *
24 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
25 */
26 class Get_Usage_Route implements Route_Interface {
27
28 use Route_Permission_Trait;
29
30 /**
31 * The namespace for this route.
32 *
33 * @var string
34 */
35 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
36
37 /**
38 * The prefix for this route.
39 *
40 * @var string
41 */
42 public const ROUTE_PREFIX = '/ai_generator/get_usage';
43
44 /**
45 * The token manager instance.
46 *
47 * @var Token_Manager
48 */
49 private $token_manager;
50
51 /**
52 * The request handler instance.
53 *
54 * @var Request_Handler
55 */
56 private $request_handler;
57
58 /**
59 * Represents the add-on manager.
60 *
61 * @var WPSEO_Addon_Manager
62 */
63 private $addon_manager;
64
65 /**
66 * Returns the conditionals based in which this loadable should be active.
67 *
68 * @return array<string> The conditionals.
69 */
70 public static function get_conditionals() {
71 return [ AI_Conditional::class, Old_Premium_AI_Conditional::class ];
72 }
73
74 /**
75 * Class constructor.
76 *
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.
80 */
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;
85 }
86
87 /**
88 * Registers routes with WordPress.
89 *
90 * @return void
91 */
92 public function register_routes() {
93 \register_rest_route(
94 self::ROUTE_NAMESPACE,
95 self::ROUTE_PREFIX,
96 [
97 'methods' => 'POST',
98 'args' => [
99 'is_woo_product_entity' => [
100 'type' => 'boolean',
101 'default' => false,
102 ],
103 ],
104 'callback' => [ $this, 'get_usage' ],
105 'permission_callback' => [ $this, 'check_permissions' ],
106 ],
107 );
108 }
109
110 /**
111 * Runs the callback that gets the monthly usage of the user.
112 *
113 * @param WP_REST_Request $request The request object.
114 *
115 * @return WP_REST_Response The response of the callback action.
116 */
117 public function get_usage( $request ): WP_REST_Response {
118 $is_woo_product_entity = $request->get_param( 'is_woo_product_entity' );
119 $user = \wp_get_current_user();
120 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() );
128 } catch ( Remote_Request_Exception |WP_Request_Exception $e ) {
129 $message = [
130 'errorMessage' => $e->getMessage(),
131 'errorIdentifier' => $e->get_error_identifier(),
132 'errorCode' => $e->getCode(),
133 ];
134 if ( $e instanceof Too_Many_Requests_Exception ) {
135 $message['missingLicenses'] = $e->get_missing_licenses();
136 }
137 return new WP_REST_Response(
138 $message,
139 $e->getCode(),
140 );
141 }
142
143 return new WP_REST_Response( $data );
144 }
145
146 /**
147 * Get action path for the request.
148 *
149 * @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity.
150 *
151 * @return string The action path.
152 */
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 }
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';
162 }
163 }
164