PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 / consent / user-interface / consent-route.php

consent-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/ai/consent/user-interface/consent-route.php

153 lines 4.0 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\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\Remote_Request_Exception;
12 use Yoast\WP\SEO\Conditionals\AI_Conditional;
13 use Yoast\WP\SEO\Loggers\Logger;
14 use Yoast\WP\SEO\Main;
15 use Yoast\WP\SEO\Routes\Route_Interface;
16
17 /**
18 * Registers a route toget suggestions from the AI API
19 *
20 * @makePublic
21 *
22 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
23 */
24 class Consent_Route implements Route_Interface {
25
26 /**
27 * The namespace for this route.
28 *
29 * @var string
30 */
31 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
32
33 /**
34 * The prefix for this route.
35 *
36 * @var string
37 */
38 public const ROUTE_PREFIX = '/ai_generator/consent';
39
40 /**
41 * The consent handler instance.
42 *
43 * @var Consent_Handler
44 */
45 private $consent_handler;
46
47 /**
48 * The token manager instance.
49 *
50 * @var Token_Manager
51 */
52 private $token_manager;
53
54 /**
55 * The logger instance.
56 *
57 * @var Logger
58 */
59 private $logger;
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 ];
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 * @param Logger $logger The logger.
76 */
77 public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager, Logger $logger ) {
78 $this->consent_handler = $consent_handler;
79 // @TODO: Remove the token manager as soon as we don't care about BC, because it's no longer used.
80 $this->token_manager = $token_manager;
81 $this->logger = $logger;
82 }
83
84 /**
85 * Registers routes with WordPress.
86 *
87 * @return void
88 */
89 public function register_routes() {
90 \register_rest_route(
91 self::ROUTE_NAMESPACE,
92 self::ROUTE_PREFIX,
93 [
94 'methods' => 'POST',
95 'args' => [
96 'consent' => [
97 'required' => true,
98 'type' => 'boolean',
99 'description' => 'Whether the consent to use AI-based services has been given by the user.',
100 ],
101 ],
102 'callback' => [ $this, 'consent' ],
103 'permission_callback' => [ $this, 'check_permissions' ],
104 ],
105 );
106 }
107
108 /**
109 * Runs the callback to store the consent given by the user to use AI-based services.
110 *
111 * @param WP_REST_Request $request The request object.
112 *
113 * @return WP_REST_Response The response of the callback action.
114 */
115 public function consent( WP_REST_Request $request ): WP_REST_Response {
116 $user_id = \get_current_user_id();
117 $consent = (bool) $request->get_param( 'consent' );
118
119 try {
120 if ( $consent ) {
121 // Store the consent at user level.
122 $this->consent_handler->grant_consent( $user_id );
123 }
124 else {
125 // Revoke the consent locally and remotely (this also invalidates the JWT tokens).
126 $this->consent_handler->revoke_consent( $user_id );
127 }
128 } catch ( Remote_Request_Exception |RuntimeException $e ) {
129 $status_code = ( $e instanceof Remote_Request_Exception ) ? $e->getCode() : 500;
130 $this->logger->error( $e->getMessage(), [ 'exception' => $e ] );
131 return new WP_REST_Response( ( $consent ) ? 'Failed to give consent.' : 'Failed to revoke consent.', $status_code );
132 }
133
134 return new WP_REST_Response( ( $consent ) ? 'Consent successfully given.' : 'Consent successfully revoked.' );
135 }
136
137 /**
138 * Checks:
139 * - if the user is logged
140 * - if the user can edit posts
141 *
142 * @return bool Whether the user is logged in, can edit posts and the feature is active.
143 */
144 public function check_permissions(): bool {
145 $user = \wp_get_current_user();
146 if ( $user === null || $user->ID < 1 ) {
147 return false;
148 }
149
150 return \user_can( $user, 'edit_posts' );
151 }
152 }
153