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

Yoast / wordpress-seo / 6e83e13262ffbbdcc660400ccc30a0c62264848d

03 Apr 2025 10:24AM UTC coverage: 54.494% (+0.06%) from 54.437%
6e83e13262ffbbdcc660400ccc30a0c62264848d

Pull #22125

github

web-flow
Merge b6213fb7e into bcea5d97e
Pull Request #22125: Add tracking of user progress in the Site Kit setup widged

7799 of 13850 branches covered (56.31%)

Branch coverage included in aggregate %.

118 of 150 new or added lines in 10 files covered. (78.67%)

1 existing line in 1 file now uncovered.

29682 of 54930 relevant lines covered (54.04%)

43392.3 hits per line

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

86.67
/src/dashboard/user-interface/tracking/setup-steps-tracking-route.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
namespace Yoast\WP\SEO\Dashboard\User_Interface\Tracking;
4

5
use Exception;
6
use WP_Error;
7
use WP_REST_Request;
8
use WP_REST_Response;
9
use Yoast\WP\SEO\Conditionals\No_Conditionals;
10
use Yoast\WP\SEO\Dashboard\Infrastructure\Tracking\Setup_Steps_Tracking_Repository_Interface;
11
use Yoast\WP\SEO\Helpers\Capability_Helper;
12
use Yoast\WP\SEO\Main;
13
use Yoast\WP\SEO\Routes\Route_Interface;
14

15
/**
16
 * Registers a route to keep track of the Site Kit usage.
17
 *
18
 * @makePublic
19
 *
20
 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
21
 */
22
class Setup_Steps_Tracking_Route implements Route_Interface {
23

24
        use No_Conditionals;
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 = '/setup_steps_tracking';
39

40
        /**
41
         * Holds the repository instance.
42
         *
43
         * @var Setup_Steps_Tracking_Repository_Interface
44
         */
45
        private $setup_steps_tracking_repository;
46

47
        /**
48
         * Holds the capability helper instance.
49
         *
50
         * @var Capability_Helper
51
         */
52
        private $capability_helper;
53

54
        /**
55
         * Constructs the class.
56
         *
57
         * @param Setup_Steps_Tracking_Repository_Interface $setup_steps_tracking_repository The repository.
58
         * @param Capability_Helper                         $capability_helper               The capability helper.
59
         */
60
        public function __construct(
6✔
61
                Setup_Steps_Tracking_Repository_Interface $setup_steps_tracking_repository,
62
                Capability_Helper $capability_helper
63
        ) {
1✔
64
                $this->setup_steps_tracking_repository = $setup_steps_tracking_repository;
6✔
65
                $this->capability_helper               = $capability_helper;
6✔
66
        }
1✔
67

68
        /**
69
         * Registers routes with WordPress.
70
         *
71
         * @return void
72
         */
73
        public function register_routes() {
2✔
74
                \register_rest_route(
2✔
75
                        self::ROUTE_NAMESPACE,
2✔
76
                        self::ROUTE_PREFIX,
2✔
77
                        [
1✔
78
                                [
1✔
79
                                        'methods'             => 'POST',
2✔
80
                                        'callback'            => [ $this, 'track_setup_steps' ],
2✔
81
                                        'permission_callback' => [ $this, 'check_capabilities' ],
2✔
82
                                        'args'                => [
1✔
83
                                                'setup_widget_loaded' => [
1✔
84
                                                        'required'          => false,
1✔
85
                                                        'type'              => 'string',
1✔
86
                                                        'enum'              => [ 'yes', 'no' ],
1✔
87
                                                ],
1✔
88
                                                'first_interaction_stage' => [
1✔
89
                                                        'required'          => false,
1✔
90
                                                        'type'              => 'string',
1✔
91
                                                        'enum'              => [ 'install', 'activate', 'setup', 'grantConsent', 'successfullyConnected' ],
1✔
92
                                                ],
1✔
93
                                                'last_interaction_stage' => [
1✔
94
                                                        'required'          => false,
1✔
95
                                                        'type'              => 'string',
1✔
96
                                                        'enum'              => [ 'install', 'activate', 'setup', 'grantConsent', 'successfullyConnected' ],
1✔
97
                                                ],
1✔
98
                                                'setup_widget_temporarily_dismissed' => [
1✔
99
                                                        'required'          => false,
1✔
100
                                                        'type'              => 'string',
1✔
101
                                                        'enum'              => [ 'yes', 'no' ],
1✔
102
                                                ],
1✔
103
                                                'setup_widget_permanently_dismissed' => [
1✔
104
                                                        'required'          => false,
1✔
105
                                                        'type'              => 'string',
1✔
106
                                                        'enum'              => [ 'yes', 'no' ],
1✔
107
                                                ],
1✔
108
                                        ],
1✔
109
                                ],
1✔
110
                        ]
1✔
111
                );
1✔
112
        }
1✔
113

114
        /**
115
         * Stores tracking information.
116
         *
117
         * @param WP_REST_Request $request The request object.
118
         *
119
         * @return WP_REST_Response|WP_Error The success or failure response.
120
         */
121
        public function track_setup_steps( WP_REST_Request $request ) {
2✔
122
                $data = \array_filter(
2✔
123
                        [
2✔
124
                                'setup_widget_loaded'                => $request->get_param( 'setupWidgetLoaded' ),
2✔
125
                                'first_interaction_stage'            => $request->get_param( 'firstInteractionStage' ),
2✔
126
                                'last_interaction_stage'             => $request->get_param( 'lastInteractionStage' ),
2✔
127
                                'setup_widget_temporarily_dismissed' => $request->get_param( 'setupWidgetTemporarilyDismissed' ),
2✔
128
                                'setup_widget_permanently_dismissed' => $request->get_param( 'setupWidgetPermanentlyDismissed' ),
2✔
129
                        ],
2✔
130
                        static function ( $element_value ) {
2✔
131
                                        return ! \is_null( $element_value );
2✔
132
                        }
2✔
133
                );
2✔
134

NEW
135
                if ( empty( $data ) ) {
×
NEW
136
                        return new WP_Error(
×
NEW
137
                                'wpseo_set_site_kit_usage_tracking',
×
NEW
138
                                \__( 'No valid parameters were provided.', 'wordpress-seo' ),
×
139
                                (object) []
140
                        );
141
                }
142

143
                $result = true;
2✔
144
                foreach ( $data as $element_name => $element_value ) {
145
                        try {
NEW
146
                                $result = $this->setup_steps_tracking_repository->set_setup_steps_tracking_element( $element_name, $element_value );
×
NEW
147
                        } catch ( Exception $exception ) {
×
NEW
148
                                return new WP_Error(
×
NEW
149
                                        'wpseo_set_site_kit_usage_tracking',
×
NEW
150
                                        $exception->getMessage(),
×
151
                                        (object) []
152
                                );
153
                        }
NEW
154
                        if ( ! $result ) {
×
155
                                break;
156
                        }
157
                }
158

159
                return new WP_REST_Response(
2✔
160
                        [
2✔
161
                                'success' => $result,
2✔
162
                        ],
2✔
163
                        ( $result ) ? 200 : 400
2✔
164
                );
2✔
165
        }
166

167
        /**
168
         * Checks if the current user has the required capabilities.
169
         *
170
         * @return bool
171
         */
172
        public function check_capabilities() {
173
                return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
174
        }
175
}
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