PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
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 / dashboard / user-interface / tracking / setup-steps-tracking-route.php

setup-steps-tracking-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/dashboard/user-interface/tracking/setup-steps-tracking-route.php

181 lines 4.9 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\Dashboard\User_Interface\Tracking;
5
6 use Exception;
7 use WP_Error;
8 use WP_REST_Request;
9 use WP_REST_Response;
10 use Yoast\WP\SEO\Conditionals\No_Conditionals;
11 use Yoast\WP\SEO\Dashboard\Infrastructure\Tracking\Setup_Steps_Tracking_Repository_Interface;
12 use Yoast\WP\SEO\Helpers\Capability_Helper;
13 use Yoast\WP\SEO\Main;
14 use Yoast\WP\SEO\Routes\Route_Interface;
15
16 /**
17 * Registers a route to keep track of the Site Kit usage.
18 *
19 * @makePublic
20 *
21 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
22 */
23 class Setup_Steps_Tracking_Route implements Route_Interface {
24
25 use No_Conditionals;
26
27 /**
28 * The namespace for this route.
29 *
30 * @var string
31 */
32 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
33
34 /**
35 * The prefix for this route.
36 *
37 * @var string
38 */
39 public const ROUTE_PREFIX = '/setup_steps_tracking';
40
41 /**
42 * Holds the repository instance.
43 *
44 * @var Setup_Steps_Tracking_Repository_Interface
45 */
46 private $setup_steps_tracking_repository;
47
48 /**
49 * Holds the capability helper instance.
50 *
51 * @var Capability_Helper
52 */
53 private $capability_helper;
54
55 /**
56 * Constructs the class.
57 *
58 * @param Setup_Steps_Tracking_Repository_Interface $setup_steps_tracking_repository The repository.
59 * @param Capability_Helper $capability_helper The capability helper.
60 */
61 public function __construct(
62 Setup_Steps_Tracking_Repository_Interface $setup_steps_tracking_repository,
63 Capability_Helper $capability_helper
64 ) {
65 $this->setup_steps_tracking_repository = $setup_steps_tracking_repository;
66 $this->capability_helper = $capability_helper;
67 }
68
69 /**
70 * Registers routes with WordPress.
71 *
72 * @return void
73 */
74 public function register_routes() {
75 \register_rest_route(
76 self::ROUTE_NAMESPACE,
77 self::ROUTE_PREFIX,
78 [
79 [
80 'methods' => 'POST',
81 'callback' => [ $this, 'track_setup_steps' ],
82 'permission_callback' => [ $this, 'check_capabilities' ],
83 'args' => [
84 'setup_widget_loaded' => [
85 'required' => false,
86 'type' => 'string',
87 'enum' => [ 'yes', 'no' ],
88 ],
89 'first_interaction_stage' => [
90 'required' => false,
91 'type' => 'string',
92 'enum' => [ 'install', 'activate', 'setup', 'grantConsent', 'successfullyConnected' ],
93 ],
94 'last_interaction_stage' => [
95 'required' => false,
96 'type' => 'string',
97 'enum' => [ 'install', 'activate', 'setup', 'grantConsent', 'successfullyConnected' ],
98 ],
99 'setup_widget_temporarily_dismissed' => [
100 'required' => false,
101 'type' => 'string',
102 'enum' => [ 'yes', 'no' ],
103 ],
104 'setup_widget_permanently_dismissed' => [
105 'required' => false,
106 'type' => 'string',
107 'enum' => [ 'yes', 'no' ],
108 ],
109 ],
110 ],
111 ],
112 );
113 }
114
115 /**
116 * Stores tracking information.
117 *
118 * @param WP_REST_Request $request The request object.
119 *
120 * @return WP_REST_Response|WP_Error The success or failure response.
121 */
122 public function track_setup_steps( WP_REST_Request $request ) {
123 $data = [
124 'setup_widget_loaded' => $request->get_param( 'setupWidgetLoaded' ),
125 'first_interaction_stage' => $request->get_param( 'firstInteractionStage' ),
126 'last_interaction_stage' => $request->get_param( 'lastInteractionStage' ),
127 'setup_widget_temporarily_dismissed' => $request->get_param( 'setupWidgetTemporarilyDismissed' ),
128 'setup_widget_permanently_dismissed' => $request->get_param( 'setupWidgetPermanentlyDismissed' ),
129 ];
130
131 // Filter out null values from the data array.
132 $data = \array_filter(
133 $data,
134 static function ( $value ) {
135 return $value !== null;
136 },
137 );
138
139 // Check if all values are null then return an error that no valid params were passed.
140 if ( empty( $data ) ) {
141 return new WP_Error(
142 'wpseo_set_site_kit_usage_tracking',
143 \__( 'No valid parameters were passed.', 'wordpress-seo' ),
144 [ 'status' => 400 ],
145 );
146 }
147
148 $result = true;
149 foreach ( $data as $key => $value ) {
150 try {
151 $result = $this->setup_steps_tracking_repository->set_setup_steps_tracking_element( $key, $value );
152 } catch ( Exception $exception ) {
153 return new WP_Error(
154 'wpseo_set_site_kit_usage_tracking',
155 $exception->getMessage(),
156 (object) [],
157 );
158 }
159 if ( ! $result ) {
160 break;
161 }
162 }
163
164 return new WP_REST_Response(
165 [
166 'success' => $result,
167 ],
168 ( $result ) ? 200 : 400,
169 );
170 }
171
172 /**
173 * Checks if the current user has the required capabilities.
174 *
175 * @return bool
176 */
177 public function check_capabilities() {
178 return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
179 }
180 }
181