| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\AI_Consent\User_Interface; |
| 5 |
|
| 6 |
use RuntimeException; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yoast\WP\SEO\AI_Authorization\Application\Token_Manager; |
| 10 |
use Yoast\WP\SEO\AI_Consent\Application\Consent_Handler; |
| 11 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Bad_Request_Exception; |
| 12 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Forbidden_Exception; |
| 13 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception; |
| 14 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Not_Found_Exception; |
| 15 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Payment_Required_Exception; |
| 16 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Request_Timeout_Exception; |
| 17 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception; |
| 18 |
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception; |
| 19 |
use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 20 |
use Yoast\WP\SEO\Conditionals\Old_Premium_AI_Conditional; |
| 21 |
use Yoast\WP\SEO\Main; |
| 22 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 23 |
|
| 24 |
/** |
| 25 |
* Registers a route toget suggestions from the AI API |
| 26 |
* |
| 27 |
* @makePublic |
| 28 |
* |
| 29 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 30 |
*/ |
| 31 |
class Consent_Route implements Route_Interface { |
| 32 |
|
| 33 |
/** |
| 34 |
* The namespace for this route. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 39 |
|
| 40 |
/** |
| 41 |
* The prefix for this route. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public const ROUTE_PREFIX = '/ai_generator/consent'; |
| 46 |
|
| 47 |
/** |
| 48 |
* The consent handler instance. |
| 49 |
* |
| 50 |
* @var Consent_Handler |
| 51 |
*/ |
| 52 |
private $consent_handler; |
| 53 |
|
| 54 |
/** |
| 55 |
* The token manager instance. |
| 56 |
* |
| 57 |
* @var Token_Manager |
| 58 |
*/ |
| 59 |
private $token_manager; |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the conditionals based in which this loadable should be active. |
| 63 |
* |
| 64 |
* @return array<string> The conditionals. |
| 65 |
*/ |
| 66 |
public static function get_conditionals() { |
| 67 |
return [ AI_Conditional::class, Old_Premium_AI_Conditional::class ]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Class constructor. |
| 72 |
* |
| 73 |
* @param Consent_Handler $consent_handler The consent handler. |
| 74 |
* @param Token_Manager $token_manager The token manager. |
| 75 |
*/ |
| 76 |
public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager ) { |
| 77 |
$this->consent_handler = $consent_handler; |
| 78 |
$this->token_manager = $token_manager; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Registers routes with WordPress. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function register_routes() { |
| 87 |
\register_rest_route( |
| 88 |
self::ROUTE_NAMESPACE, |
| 89 |
self::ROUTE_PREFIX, |
| 90 |
[ |
| 91 |
'methods' => 'POST', |
| 92 |
'args' => [ |
| 93 |
'consent' => [ |
| 94 |
'required' => true, |
| 95 |
'type' => 'boolean', |
| 96 |
'description' => 'Whether the consent to use AI-based services has been given by the user.', |
| 97 |
], |
| 98 |
], |
| 99 |
'callback' => [ $this, 'consent' ], |
| 100 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 101 |
], |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Runs the callback to store the consent given by the user to use AI-based services. |
| 107 |
* |
| 108 |
* @param WP_REST_Request $request The request object. |
| 109 |
* |
| 110 |
* @return WP_REST_Response The response of the callback action. |
| 111 |
*/ |
| 112 |
public function consent( WP_REST_Request $request ): WP_REST_Response { |
| 113 |
$user_id = \get_current_user_id(); |
| 114 |
$consent = (bool) $request->get_param( 'consent' ); |
| 115 |
|
| 116 |
try { |
| 117 |
if ( $consent ) { |
| 118 |
// Store the consent at user level. |
| 119 |
$this->consent_handler->grant_consent( $user_id ); |
| 120 |
} |
| 121 |
else { |
| 122 |
// Delete the consent at user level. |
| 123 |
$this->consent_handler->revoke_consent( $user_id ); |
| 124 |
// Invalidate the token if the user revoked the consent. |
| 125 |
$this->token_manager->token_invalidate( $user_id ); |
| 126 |
} |
| 127 |
} catch ( Bad_Request_Exception |Forbidden_Exception |Internal_Server_Error_Exception |Not_Found_Exception |Payment_Required_Exception |Request_Timeout_Exception |Service_Unavailable_Exception |Too_Many_Requests_Exception |RuntimeException $e ) { |
| 128 |
return new WP_REST_Response( ( $consent ) ? 'Failed to store consent.' : 'Failed to revoke consent.', 500 ); |
| 129 |
} |
| 130 |
|
| 131 |
return new WP_REST_Response( ( $consent ) ? 'Consent successfully stored.' : 'Consent successfully revoked.' ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Checks: |
| 136 |
* - if the user is logged |
| 137 |
* - if the user can edit posts |
| 138 |
* |
| 139 |
* @return bool Whether the user is logged in, can edit posts and the feature is active. |
| 140 |
*/ |
| 141 |
public function check_permissions(): bool { |
| 142 |
$user = \wp_get_current_user(); |
| 143 |
if ( $user === null || $user->ID < 1 ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
return \user_can( $user, 'edit_posts' ); |
| 148 |
} |
| 149 |
} |
| 150 |
|