-
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
View only settings. #7633
View only settings. #7633
Conversation
Build files for 0b87b4c are ready:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @zutigrm – this is almost there, a few minor things to address, mostly to simplify the implementation a bit.
if ( $settings instanceof Setting_With_ViewOnly_Keys_Interface && ! $can_manage_options() ) { | ||
return new WP_REST_Response( $settings->get_view_only_settings() ); | ||
} | ||
|
||
if ( $can_manage_options() ) { | ||
return new WP_REST_Response( $module->get_settings()->get() ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check can_manage_options
first, that way we only need to check it once. If the user can't, we'd then only check if it supports view-only keys.
If it doesn't support view-only keys, we should probably return an error rather than an empty array. Let's go with a default WP_Error
with a code of no_view_only_settings
. The HTTP status is 500 by default which is good for this case too.
tests/phpunit/includes/Core/Modules/FakeModule_WithViewOnlySettings.php
Outdated
Show resolved
Hide resolved
private function share_fake_module_with_user_role( $shared_with_role ) { | ||
$user = self::factory()->user->create_and_get( array( 'role' => $shared_with_role ) ); | ||
wp_set_current_user( $user->ID ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method does quite a bit more than one might expect by the name which also makes it less reusable in other tests. Please keep helper methods to be focused in their purpose and limited in their side-effects. This method should only be concerned with adding a module to the shared roles (it could be more useful if it wasn't specific to fake-module
). Instantiating a few classes where needed is okay, but it's better to use those that are shared throughout as you are.
tests/phpunit/integration/Core/Modules/REST_Modules_ControllerTest.php
Outdated
Show resolved
Hide resolved
tests/phpunit/integration/Modules/Analytics_4/Settings_ViewOnly_TraitTest.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @zutigrm ! This is a nice improvement over the first iteration and almost there, just a few small points left to address!
Edit: these are quite minor so I'll push these now to unblock this important issue.
return new WP_REST_Response( $view_only_settings ); | ||
} | ||
|
||
return new WP_Error( 'no_view_only_settings', __( 'No view-only settings available.', 'google-site-kit' ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The translatable message shouldn't be necessary here, this is mostly an internal error that we don't expect to encounter, except maybe in tests. Adding this adds a new string to be translated for the plugin, but it shouldn't be possible to trigger, but also, this message probably wouldn't be very useful to the user to see either.
} | ||
|
||
$settings = $module->get_settings(); | ||
if ( $settings instanceof Setting_With_ViewOnly_Keys_Interface ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're doing a bit more within this block than before, let's invert the condition to return the error here instead and then this can all be un-indented below since we'd know that settings implemented the interface. Alternatively, the body of this condition could be extracted to a helper method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On closer inspection, this isn't necessary since the block here can be further simplified a fair bit. See below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few more thoughts about the changes I'm making which are just about simplifying what we have. Thanks @zutigrm !
$settings = $module->get_settings(); | ||
if ( $settings instanceof Setting_With_ViewOnly_Keys_Interface ) { | ||
$view_only_keys = $settings->get_view_only_keys(); | ||
$settings = $settings->get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we should avoid redeclaring $settings
as a different type.
$view_only_keys = $settings->get_view_only_keys(); | ||
$settings = $settings->get(); | ||
|
||
if ( empty( $view_only_keys ) || empty( $settings ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition is unnecessary because array_intersect_key
below will return an empty array in either case anyways, and array_flip
is also safe to use with an empty array 👍 $settings
will always be an array and never be empty since the full value will always be an array with one or more key-value pairs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
Addresses issue:
Relevant technical choices
Implemented per IB. With addition of
VIEW_SPLASH
permission inreadable
settings route, as it is needed for proper access for the roles.PR Author Checklist
Do not alter or remove anything below. The following sections will be managed by moderators only.
Code Reviewer Checklist
Merge Reviewer Checklist