PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
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 28.2, at src/ai/consent/user-interface/consent-route.php

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