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

149 lines 4.4 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\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\Main;
21 use Yoast\WP\SEO\Routes\Route_Interface;
22
23 /**
24 * Registers a route toget suggestions from the AI API
25 *
26 * @makePublic
27 *
28 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
29 */
30 class Consent_Route implements Route_Interface {
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/consent';
45
46 /**
47 * The consent handler instance.
48 *
49 * @var Consent_Handler
50 */
51 private $consent_handler;
52
53 /**
54 * The token manager instance.
55 *
56 * @var Token_Manager
57 */
58 private $token_manager;
59
60 /**
61 * Returns the conditionals based in which this loadable should be active.
62 *
63 * @return array<string> The conditionals.
64 */
65 public static function get_conditionals() {
66 return [ AI_Conditional::class ];
67 }
68
69 /**
70 * Class constructor.
71 *
72 * @param Consent_Handler $consent_handler The consent handler.
73 * @param Token_Manager $token_manager The token manager.
74 */
75 public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager ) {
76 $this->consent_handler = $consent_handler;
77 $this->token_manager = $token_manager;
78 }
79
80 /**
81 * Registers routes with WordPress.
82 *
83 * @return void
84 */
85 public function register_routes() {
86 \register_rest_route(
87 self::ROUTE_NAMESPACE,
88 self::ROUTE_PREFIX,
89 [
90 'methods' => 'POST',
91 'args' => [
92 'consent' => [
93 'required' => true,
94 'type' => 'boolean',
95 'description' => 'Whether the consent to use AI-based services has been given by the user.',
96 ],
97 ],
98 'callback' => [ $this, 'consent' ],
99 'permission_callback' => [ $this, 'check_permissions' ],
100 ],
101 );
102 }
103
104 /**
105 * Runs the callback to store the consent given by the user to use AI-based services.
106 *
107 * @param WP_REST_Request $request The request object.
108 *
109 * @return WP_REST_Response The response of the callback action.
110 */
111 public function consent( WP_REST_Request $request ): WP_REST_Response {
112 $user_id = \get_current_user_id();
113 $consent = (bool) $request->get_param( 'consent' );
114
115 try {
116 if ( $consent ) {
117 // Store the consent at user level.
118 $this->consent_handler->grant_consent( $user_id );
119 }
120 else {
121 // Delete the consent at user level.
122 $this->consent_handler->revoke_consent( $user_id );
123 // Invalidate the token if the user revoked the consent.
124 $this->token_manager->token_invalidate( $user_id );
125 }
126 } 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 ) {
127 return new WP_REST_Response( ( $consent ) ? 'Failed to store consent.' : 'Failed to revoke consent.', 500 );
128 }
129
130 return new WP_REST_Response( ( $consent ) ? 'Consent successfully stored.' : 'Consent successfully revoked.' );
131 }
132
133 /**
134 * Checks:
135 * - if the user is logged
136 * - if the user can edit posts
137 *
138 * @return bool Whether the user is logged in, can edit posts and the feature is active.
139 */
140 public function check_permissions(): bool {
141 $user = \wp_get_current_user();
142 if ( $user === null || $user->ID < 1 ) {
143 return false;
144 }
145
146 return \user_can( $user, 'edit_posts' );
147 }
148 }
149