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

Yoast / wordpress-seo / 9594812bf110d67afe78816058eb15ec830063d2

15 Jul 2025 10:16AM UTC coverage: 52.595% (-1.0%) from 53.638%
9594812bf110d67afe78816058eb15ec830063d2

Pull #22432

github

web-flow
Merge branch 'trunk' into feature/ai-generator-in-free
Pull Request #22432: Merge feature branch into trunk

8341 of 15027 branches covered (55.51%)

Branch coverage included in aggregate %.

382 of 1769 new or added lines in 122 files covered. (21.59%)

2 existing lines in 2 files now uncovered.

30934 of 59648 relevant lines covered (51.86%)

40029.26 hits per line

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

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

5
use RuntimeException;
6
use WP_REST_Request;
7
use WP_REST_Response;
8
use Yoast\WP\SEO\AI_Authorization\Infrastructure\Access_Token_User_Meta_Repository_Interface;
9
use Yoast\WP\SEO\AI_Authorization\Infrastructure\Code_Verifier_User_Meta_Repository_Interface;
10
use Yoast\WP\SEO\AI_Authorization\Infrastructure\Refresh_Token_User_Meta_Repository_Interface;
11
use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Unauthorized_Exception;
12
use Yoast\WP\SEO\Conditionals\AI_Conditional;
13
use Yoast\WP\SEO\Main;
14
use Yoast\WP\SEO\Routes\Route_Interface;
15

16
/**
17
 * The base class for the callback routes.
18
 */
19
abstract class Abstract_Callback_Route implements Route_Interface {
20

21
        /**
22
         *  The namespace for this route.
23
         *
24
         * @var string
25
         */
26
        public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
27

28
        /**
29
         * The access token repository instance.
30
         *
31
         * @var Access_Token_User_Meta_Repository_Interface
32
         */
33
        private $access_token_repository;
34

35
        /**
36
         * The refresh token repository instance.
37
         *
38
         * @var Refresh_Token_User_Meta_Repository_Interface
39
         */
40
        private $refresh_token_repository;
41

42
        /**
43
         * The code verifier instance.
44
         *
45
         * @var Code_Verifier_User_Meta_Repository_Interface
46
         */
47
        private $code_verifier_repository;
48

49
        /**
50
         * Returns the conditionals based in which this loadable should be active.
51
         *
52
         * @return array<string> The conditionals.
53
         */
NEW
54
        public static function get_conditionals() {
×
NEW
55
                return [ AI_Conditional::class ];
×
56
        }
57

58
        /**
59
         * Callback_Route constructor.
60
         *
61
         * @param Access_Token_User_Meta_Repository_Interface  $access_token_repository  The access token repository instance.
62
         * @param Refresh_Token_User_Meta_Repository_Interface $refresh_token_repository The refresh token repository instance.
63
         * @param Code_Verifier_User_Meta_Repository_Interface $code_verifier_repository The code verifier instance.
64
         */
NEW
65
        public function __construct( Access_Token_User_Meta_Repository_Interface $access_token_repository, Refresh_Token_User_Meta_Repository_Interface $refresh_token_repository, Code_Verifier_User_Meta_Repository_Interface $code_verifier_repository ) {
×
NEW
66
                $this->access_token_repository  = $access_token_repository;
×
NEW
67
                $this->refresh_token_repository = $refresh_token_repository;
×
NEW
68
                $this->code_verifier_repository = $code_verifier_repository;
×
69
        }
70

71
        // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
72

73
        /**
74
         * Runs the callback to store connection credentials and the tokens locally.
75
         *
76
         * @param WP_REST_Request $request The request object.
77
         *
78
         * @return WP_REST_Response The response of the callback action.
79
         *
80
         * @throws Unauthorized_Exception If the code challenge  is not valid.
81
         * @throws RuntimeException If the verification code is not found.
82
         */
NEW
83
        public function callback( WP_REST_Request $request ): WP_REST_Response {
×
NEW
84
                $user_id = $request['user_id'];
×
85
                try {
NEW
86
                        $code_verifier = $this->code_verifier_repository->get_code_verifier( $user_id );
×
87

NEW
88
                        if ( $request['code_challenge'] !== \hash( 'sha256', $code_verifier->get_code() ) ) {
×
NEW
89
                                throw new Unauthorized_Exception( 'Unauthorized' );
×
90
                        }
91

NEW
92
                        $this->access_token_repository->store_token( $user_id, $request['access_jwt'] );
×
NEW
93
                        $this->refresh_token_repository->store_token( $user_id, $request['refresh_jwt'] );
×
NEW
94
                        $this->code_verifier_repository->delete_code_verifier( $user_id );
×
NEW
95
                } catch ( Unauthorized_Exception | RuntimeException $e ) {
×
NEW
96
                        return new WP_REST_Response( 'Unauthorized.', 401 );
×
97
                }
98

NEW
99
                return new WP_REST_Response(
×
NEW
100
                        [
×
NEW
101
                                'message'       => 'Tokens successfully stored.',
×
NEW
102
                                'code_verifier' => $code_verifier->get_code(),
×
NEW
103
                        ]
×
NEW
104
                );
×
105
        }
106

107
        // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
108
}
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

© 2026 Coveralls, Inc