-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create REST endpoints to store and fetch user selected Key_Metrics
settings
#6256
Comments
Key_Metrics
settings classKey_Metrics
settings
ACs here look good 👍🏻 |
Thanks, @nfmohit. IB mostly looks good, but we also need to instantiate the controller and register it to make sure that the new endpoints are available for use. |
Excellent catch. I've updated the IB, thank you @eugene-manuilov! |
Thanks, @nfmohit. IB ✔️ |
|
Approval
|
Good point, @felixarntz. I have update #6486 PR created by @techanvil to also wrap the |
Feature Description
Following up from #6308, a new class should be created to register the new REST endpoint to store and fetch user selected metrics as well as any other user data related to the Key Metrics widget, such as the flag to toggle the visibility.
The REST controller class should make use of
Key_Metrics_Settings
class created in #6308 which deals with persistently storing the user selected key metrics in the database.The architecture and purpose of the new classes created can be similar to the existing
Dismissals
(Key_Metrics
),Dismissed_Items
(Key_Metrics_Settings
) andREST_Dismissals_Controller
(REST_Key_Metrics_Controller
) classes.Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
REST_Key_Metrics_Controller
class should be created in theGoogle\Site_Kit\Core\Key_Metrics
namespace similar to otherREST_*_Controller
classes.core/user/data/key-metrics
in the Site Kit REST API namespacePOST
endpoint should be registered for creating/updating the widget slugs picked by the user.Permissions::VIEW_SPLASH
or thePermissions::VIEW_DASHBOARD
capabilities (similar to $can_list_data here).data
parameter which should be merged into the current settings similar tomodules/:slug/data/settings
.data
request should be of the following shape:GET
endpoint should be registered for fetching the widget slugs picked by the user.Permissions::VIEW_SPLASH
or thePermissions::VIEW_DASHBOARD
capabilities (similar to $can_list_data here).googlesitekit_apifetch_preload_paths
filter.Implementation Brief
Key_Metrics
andKey_Metrics_Settings
classes #6308 is merged.includes/Core/Key_Metrics/Key_Metrics_Settings.php
file:merge()
, that should update the key metrics settings partially, i.e. callset()
and update only the passed settings, leaving other existing settings intact. This can be similar to themerge()
method inincludes/Core/Modules/Module_Settings.php
.includes/Core/Key_Metrics
folder, create a new file namedREST_Key_Metrics_Controller.php
. In this file:includes/Core/User_Input/REST_User_Input_Controller.php
can largely be used as an example.REST_Key_Metrics_Controller.php
. Its namespace should beGoogle\Site_Kit\Core\Key_Metrics
.Key_Metrics_Settings
class as a parameter, and it should be assigned to a private/protected class property named$key_metrics_settings
.get_rest_routes()
. In this method:$can_select_key_metrics
. This variable should returntrue
if eitherPermissions::VIEW_SPLASH
or thePermissions::VIEW_DASHBOARD
is truthy (similar to $can_list_data here).REST_ROUTE
defined for the newcore/user/data/key-metrics
route. This route should accept requests for bothGET
andPOST
methods. Here are the specifications for each method:GET
:permission_callback
property should be the$can_select_key_metrics
variable.method
property should beWP_REST_Server::READABLE
.callback
property should be a function that returns the response of theget()
method of$key_metrics_settings
class property as an object, wrapped withrest_ensure_response()
.POST
:permission_callback
property should be the$can_select_key_metrics
variable.method
property should beWP_REST_Server::CREATABLE
.callback
property should be a function that accepts the$request
(of typeWP_REST_Request
) parameter. Extract thedata
parameter from$request
using itsget_param()
method, assinging it to a new$data
variable. Check if the$data
variable is not set, or if it is not anarray
. If that is the case, return a400
WP_Error
withrest_missing_callback_param
as the code and a translatableMissing settings data.
as the error message. Call the newmerge()
method of$key_metrics_settings
class property, passing$data['settings']
to it. Return the response of theget()
method of$key_metrics_settings
class property as an object, wrapped withrest_ensure_response()
.args
property which should be an array with thedata
property. Thedata
property should also be an array with the following properties:type
ofobject
.description
ofMetrics to set
(translatable).validate
callback function which should check if the value is an array.register()
. In this method:googlesitekit_rest_routes
, that merges theget_rest_routes()
method to the existing routes.googlesitekit_apifetch_preload_paths
, that merges the newcore/user/data/key-metrics
route to the existing list of pre-fetched routes.includes/Core/Key_Metrics/Key_Metrics.php
file:REST_Key_Metrics_Controller
class passing the existing instance of theKey_Metrics_Settings
class. Assign it to a protected class property named$rest_controller
.register()
method:register()
method of the$rest_controller
property.Test Coverage
tests/phpunit/integration/Core/Key_Metrics/REST_Key_Metrics_ControllerTest.php
. In this file:register()
method.tests/phpunit/integration/Core/User_Input/REST_User_Input_ControllerTest.php
.QA Brief
QA:Eng
Changelog entry
The text was updated successfully, but these errors were encountered: