| 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\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; |
| 13 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception; |
| 14 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception; |
| 15 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\WP_Request_Exception; |
| 16 |
use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 17 |
use Yoast\WP\SEO\Conditionals\New_Premium_Or_Free_AI_Conditional; |
| 18 |
use Yoast\WP\SEO\Main; |
| 19 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 20 |
|
| 21 |
/** |
| 22 |
* Registers a route to get suggestions from the AI API |
| 23 |
* |
| 24 |
* @makePublic |
| 25 |
* |
| 26 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 27 |
*/ |
| 28 |
class Get_Usage_Route implements Route_Interface { |
| 29 |
|
| 30 |
use Route_Permission_Trait; |
| 31 |
|
| 32 |
/** |
| 33 |
* The namespace for this route. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 38 |
|
| 39 |
/** |
| 40 |
* The prefix for this route. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public const ROUTE_PREFIX = '/ai_generator/get_usage'; |
| 45 |
|
| 46 |
/** |
| 47 |
* The auth strategy factory. |
| 48 |
* |
| 49 |
* @var AI_Request_Sender_Factory |
| 50 |
*/ |
| 51 |
private $ai_request_sender_factory; |
| 52 |
|
| 53 |
/** |
| 54 |
* The consent handler instance. |
| 55 |
* |
| 56 |
* @var Consent_Handler |
| 57 |
*/ |
| 58 |
private $consent_handler; |
| 59 |
|
| 60 |
/** |
| 61 |
* Represents the add-on manager. |
| 62 |
* |
| 63 |
* @var WPSEO_Addon_Manager |
| 64 |
*/ |
| 65 |
private $addon_manager; |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the conditionals based in which this loadable should be active. |
| 69 |
* |
| 70 |
* @return array<string> The conditionals. |
| 71 |
*/ |
| 72 |
public static function get_conditionals() { |
| 73 |
return [ AI_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Class constructor. |
| 78 |
* |
| 79 |
* @param AI_Request_Sender_Factory $ai_request_sender_factory The auth strategy factory. |
| 80 |
* @param Consent_Handler $consent_handler The consent handler instance. |
| 81 |
* @param WPSEO_Addon_Manager $addon_manager The add-on manager instance. |
| 82 |
*/ |
| 83 |
public function __construct( AI_Request_Sender_Factory $ai_request_sender_factory, Consent_Handler $consent_handler, WPSEO_Addon_Manager $addon_manager ) { |
| 84 |
$this->addon_manager = $addon_manager; |
| 85 |
$this->ai_request_sender_factory = $ai_request_sender_factory; |
| 86 |
$this->consent_handler = $consent_handler; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Registers routes with WordPress. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function register_routes() { |
| 95 |
\register_rest_route( |
| 96 |
self::ROUTE_NAMESPACE, |
| 97 |
self::ROUTE_PREFIX, |
| 98 |
[ |
| 99 |
'methods' => 'POST', |
| 100 |
'args' => [ |
| 101 |
'is_woo_product_entity' => [ |
| 102 |
'type' => 'boolean', |
| 103 |
'default' => false, |
| 104 |
], |
| 105 |
], |
| 106 |
'callback' => [ $this, 'get_usage' ], |
| 107 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 108 |
], |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Runs the callback that gets the monthly usage of the user. |
| 114 |
* |
| 115 |
* @param WP_REST_Request $request The request object. |
| 116 |
* |
| 117 |
* @return WP_REST_Response The response of the callback action. |
| 118 |
*/ |
| 119 |
public function get_usage( $request ): WP_REST_Response { |
| 120 |
$is_woo_product_entity = $request->get_param( 'is_woo_product_entity' ); |
| 121 |
$user = \wp_get_current_user(); |
| 122 |
try { |
| 123 |
$parameters = new Usage_Parameters( $user, $this->is_free( $is_woo_product_entity ), \gmdate( 'Y-m' ) ); |
| 124 |
$sender = $this->ai_request_sender_factory->create( $user ); |
| 125 |
$response = $sender->get_usage( $parameters ); |
| 126 |
$data = \json_decode( $response->get_body() ); |
| 127 |
} catch ( Remote_Request_Exception |WP_Request_Exception $e ) { |
| 128 |
if ( $e instanceof Forbidden_Exception && ! $e instanceof Insufficient_Scope_Exception ) { |
| 129 |
$this->consent_handler->revoke_consent( $user->ID ); |
| 130 |
} |
| 131 |
$message = [ |
| 132 |
'errorMessage' => $e->getMessage(), |
| 133 |
'errorIdentifier' => $e->get_error_identifier(), |
| 134 |
'errorCode' => $e->getCode(), |
| 135 |
]; |
| 136 |
if ( $e instanceof Too_Many_Requests_Exception ) { |
| 137 |
$message['missingLicenses'] = $e->get_missing_licenses(); |
| 138 |
} |
| 139 |
return new WP_REST_Response( |
| 140 |
$message, |
| 141 |
$e->getCode(), |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
return new WP_REST_Response( $data ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Whether the current request should read the time-unbound free-usage bucket. |
| 150 |
* |
| 151 |
* A user without the relevant paid subscription is on the free tier, whose usage is tracked in |
| 152 |
* the free-usage bucket rather than per period. |
| 153 |
* |
| 154 |
* @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity. |
| 155 |
* |
| 156 |
* @return bool True when the user is on the free tier for this request type. |
| 157 |
*/ |
| 158 |
public function is_free( $is_woo_product_entity = false ): bool { |
| 159 |
if ( $is_woo_product_entity ) { |
| 160 |
return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ); |
| 161 |
} |
| 162 |
return ! $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ); |
| 163 |
} |
| 164 |
} |
| 165 |
|