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

Yoast / wordpress-seo / 50d784b00f80d7c48bc3808ab12c22beed79079f

27 Mar 2025 02:52PM UTC coverage: 54.895% (+0.05%) from 54.85%
50d784b00f80d7c48bc3808ab12c22beed79079f

Pull #22125

github

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

8056 of 14133 branches covered (57.0%)

Branch coverage included in aggregate %.

103 of 133 new or added lines in 10 files covered. (77.44%)

1 existing line in 1 file now uncovered.

30081 of 55340 relevant lines covered (54.36%)

43089.6 hits per line

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

85.51
/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', 'SET UP', 'CONNECT', 'COMPLETED' ],
1✔
92
                                                ],
1✔
93
                                                'last_interaction_stage' => [
1✔
94
                                                        'required'          => false,
1✔
95
                                                        'type'              => 'string',
1✔
96
                                                        'enum'              => [ 'INSTALL', 'ACTIVATE', 'SET UP', 'CONNECT', 'COMPLETED' ],
1✔
97
                                                ],
1✔
98
                                                'setup_widget_dismissed' => [
1✔
99
                                                        'required'          => false,
1✔
100
                                                        'type'              => 'string',
1✔
101
                                                        'enum'              => [ 'no', 'pageload', 'permanently' ],
1✔
102
                                                ],
1✔
103
                                        ],
1✔
104
                                ],
1✔
105
                        ]
1✔
106
                );
1✔
107
        }
1✔
108

109
        /**
110
         * Stores tracking information.
111
         *
112
         * @param WP_REST_Request $request The request object.
113
         *
114
         * @return WP_REST_Response|WP_Error The success or failure response.
115
         */
116
        public function track_setup_steps( WP_REST_Request $request ) {
2✔
117
                $data = \array_filter(
2✔
118
                        [
2✔
119
                                'setup_widget_loaded'     => $request->get_param( 'setup_widget_loaded' ),
2✔
120
                                'first_interaction_stage' => $request->get_param( 'first_interaction_stage' ),
2✔
121
                                'last_interaction_stage'  => $request->get_param( 'last_interaction_stage' ),
2✔
122
                                'setup_widget_dismissed'  => $request->get_param( 'setup_widget_dismissed' ),
2✔
123
                        ],
2✔
124
                        static function ( $element_value ) {
2✔
125
                                        return ! \is_null( $element_value );
2✔
126
                        }
2✔
127
                );
2✔
128

NEW
129
                if ( empty( $data ) ) {
×
NEW
130
                        return new WP_Error(
×
NEW
131
                                'wpseo_set_site_kit_usage_tracking',
×
NEW
132
                                \__( 'No valid parameters were provided.', 'wordpress-seo' ),
×
133
                                (object) []
134
                        );
135
                }
136

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

153
                return new WP_REST_Response(
2✔
154
                        [
2✔
155
                                'success' => $result,
2✔
156
                        ],
2✔
157
                        ( $result ) ? 200 : 400
2✔
158
                );
2✔
159
        }
160

161
        /**
162
         * Checks if the current user has the required capabilities.
163
         *
164
         * @return bool
165
         */
166
        public function check_capabilities() {
167
                return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
168
        }
169
}
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