The conditionals. */ public static function get_conditionals() { return [ AI_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ]; } /** * Class constructor. * * @param Token_Manager $token_manager The token manager instance. * @param Request_Handler $request_handler The request handler instance. * @param Consent_Handler $consent_handler The consent handler instance. * @param WPSEO_Addon_Manager $addon_manager The add-on manager instance. */ public function __construct( Token_Manager $token_manager, Request_Handler $request_handler, Consent_Handler $consent_handler, WPSEO_Addon_Manager $addon_manager ) { $this->addon_manager = $addon_manager; $this->token_manager = $token_manager; $this->request_handler = $request_handler; $this->consent_handler = $consent_handler; } /** * Registers routes with WordPress. * * @return void */ public function register_routes() { \register_rest_route( self::ROUTE_NAMESPACE, self::ROUTE_PREFIX, [ 'methods' => 'POST', 'args' => [ 'is_woo_product_entity' => [ 'type' => 'boolean', 'default' => false, ], ], 'callback' => [ $this, 'get_usage' ], 'permission_callback' => [ $this, 'check_permissions' ], ], ); } /** * Runs the callback that gets the monthly usage of the user. * * @param WP_REST_Request $request The request object. * * @return WP_REST_Response The response of the callback action. */ public function get_usage( $request ): WP_REST_Response { $is_woo_product_entity = $request->get_param( 'is_woo_product_entity' ); $user = \wp_get_current_user(); try { $token = $this->token_manager->get_or_request_access_token( $user ); $request_headers = [ 'Authorization' => "Bearer $token", ]; $action_path = $this->get_action_path( $is_woo_product_entity ); $response = $this->request_handler->handle( new Request( $action_path, [], $request_headers, false ) ); $data = \json_decode( $response->get_body() ); } catch ( Remote_Request_Exception | WP_Request_Exception $e ) { if ( $e instanceof Forbidden_Exception ) { // The API signals that consent is revoked; sync local state. $this->consent_handler->revoke_consent( $user->ID ); } $message = [ 'errorMessage' => $e->getMessage(), 'errorIdentifier' => $e->get_error_identifier(), 'errorCode' => $e->getCode(), ]; if ( $e instanceof Too_Many_Requests_Exception ) { $message['missingLicenses'] = $e->get_missing_licenses(); } return new WP_REST_Response( $message, $e->getCode(), ); } return new WP_REST_Response( $data ); } /** * Get action path for the request. * * @param bool $is_woo_product_entity Whether the request is for a WooCommerce product entity. * * @return string The action path. */ public function get_action_path( $is_woo_product_entity = false ): string { $unlimited = '/usage/' . \gmdate( 'Y-m' ); if ( $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ) ) { return $unlimited; } if ( ! $is_woo_product_entity && $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) { return $unlimited; } return '/usage/free-usages'; } }