• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / e57e01309ec9fc3afc714e6ce0fd243e7b6d1f90

27 May 2025 12:40PM UTC coverage: 45.733%. First build
e57e01309ec9fc3afc714e6ce0fd243e7b6d1f90

Pull #22275

github

web-flow
Merge f0a8d33c9 into 5f64dc203
Pull Request #22275: Move and refactor ai generator rest endpoints

0 of 392 new or added lines in 26 files covered. (0.0%)

15548 of 33997 relevant lines covered (45.73%)

3.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ai-consent/user-interface/consent-route.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
namespace Yoast\WP\SEO\AI_Consent\User_Interface;
4

5
use RuntimeException;
6
use WP_REST_Request;
7
use WP_REST_Response;
8
use Yoast\WP\SEO\AI_Authorization\Application\Token_Manager;
9
use Yoast\WP\SEO\AI_Consent\Application\Consent_Handler;
10
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Bad_Request_Exception;
11
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Forbidden_Exception;
12
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception;
13
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Not_Found_Exception;
14
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Payment_Required_Exception;
15
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Request_Timeout_Exception;
16
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception;
17
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
18
use Yoast\WP\SEO\Conditionals\AI_Conditional;
19
use Yoast\WP\SEO\Main;
20
use Yoast\WP\SEO\Routes\Route_Interface;
21

22
/**
23
 * Registers a route toget suggestions from the AI API
24
 *
25
 * @makePublic
26
 *
27
 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
28
 */
29
class Consent_Route implements Route_Interface {
30
        /**
31
         *  The namespace for this route.
32
         *
33
         * @var string
34
         */
35
        public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
36

37
        /**
38
         *  The prefix for this route.
39
         *
40
         * @var string
41
         */
42
        public const ROUTE_PREFIX = '/ai_generator/consent';
43

44
        /**
45
         * The consent handler instance.
46
         *
47
         * @var Consent_Handler
48
         */
49
        private $consent_handler;
50

51
        /**
52
         * The token manager instance.
53
         *
54
         * @var Token_Manager
55
         */
56
        private $token_manager;
57

58
        /**
59
         * Returns the conditionals based in which this loadable should be active.
60
         *
61
         * @return array<string> The conditionals.
62
         */
NEW
63
        public static function get_conditionals() {
×
NEW
64
                return [ AI_Conditional::class ];
×
65
        }
66

67
        /**
68
         * Class constructor.
69
         *
70
         * @param Consent_Handler $consent_handler The consent handler.
71
         * @param Token_Manager   $token_manager   The token manager.
72
         */
NEW
73
        public function __construct( Consent_Handler $consent_handler, Token_Manager $token_manager ) {
×
NEW
74
                $this->consent_handler = $consent_handler;
×
NEW
75
                $this->token_manager   = $token_manager;
×
76
        }
77

78
        /**
79
         * Registers routes with WordPress.
80
         *
81
         * @return void
82
         */
NEW
83
        public function register_routes() {
×
NEW
84
                \register_rest_route(
×
NEW
85
                        self::ROUTE_NAMESPACE,
×
NEW
86
                        self::ROUTE_PREFIX,
×
NEW
87
                        [
×
NEW
88
                                'methods'             => 'POST',
×
NEW
89
                                'args'                => [
×
NEW
90
                                        'consent' => [
×
NEW
91
                                                'required'    => true,
×
NEW
92
                                                'type'        => 'boolean',
×
NEW
93
                                                'description' => 'Whether the consent to use AI-based services has been given by the user.',
×
NEW
94
                                        ],
×
NEW
95
                                ],
×
NEW
96
                                'callback'            => [ $this, 'consent' ],
×
NEW
97
                                'permission_callback' => [ $this, 'check_permissions' ],
×
NEW
98
                        ]
×
NEW
99
                );
×
100
        }
101

102
        /**
103
         * Runs the callback to store the consent given by the user to use AI-based services.
104
         *
105
         * @param WP_REST_Request $request The request object.
106
         *
107
         * @return WP_REST_Response The response of the callback action.
108
         */
NEW
109
        public function consent( WP_REST_Request $request ): WP_REST_Response {
×
NEW
110
                $user_id = \get_current_user_id();
×
NEW
111
                $consent = \boolval( $request['consent'] );
×
112

113
                try {
NEW
114
                        if ( $consent ) {
×
115
                                // Store the consent at user level.
NEW
116
                                $this->consent_handler->grant_consent( $user_id );
×
117
                        }
118
                        else {
119
                                // Delete the consent at user level.
NEW
120
                                $this->consent_handler->revoke_consent( $user_id );
×
121
                                // Invalidate the token if the user revoked the consent.
NEW
122
                                $this->token_manager->token_invalidate( $user_id );
×
123
                        }
NEW
124
                } 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 ) {
×
NEW
125
                        return new WP_REST_Response( ( $consent ) ? 'Failed to store consent.' : 'Failed to revoke consent.', 500 );
×
126
                }
127

NEW
128
                        return new WP_REST_Response( ( $consent ) ? 'Consent successfully stored.' : 'Consent successfully revoked.' );
×
129
        }
130

131
        /**
132
         * Checks:
133
         * - if the user is logged
134
         * - if the user can edit posts
135
         *
136
         * @return bool Whether the user is logged in, can edit posts and the feature is active.
137
         */
NEW
138
        public function check_permissions(): bool {
×
NEW
139
                $user = \wp_get_current_user();
×
NEW
140
                if ( $user === null || $user->ID < 1 ) {
×
NEW
141
                        return false;
×
142
                }
143

NEW
144
                return \user_can( $user, 'edit_posts' );
×
145
        }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc