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

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