-
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
Changes from 5 commits
3b74495
e92b5cb
68e7e74
2bcd3f0
3d9f195
0b87b4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Google\Site_Kit\Core\REST_API\REST_Routes; | ||
use Google\Site_Kit\Core\REST_API\REST_Route; | ||
use Google\Site_Kit\Core\REST_API\Exception\Invalid_Datapoint_Exception; | ||
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface; | ||
use Google\Site_Kit\Modules\Analytics; | ||
use Google\Site_Kit\Modules\Analytics_4; | ||
use WP_REST_Server; | ||
|
@@ -424,7 +425,7 @@ private function get_rest_routes() { | |
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => function( WP_REST_Request $request ) { | ||
'callback' => function( WP_REST_Request $request ) use ( $can_manage_options ) { | ||
$slug = $request['slug']; | ||
try { | ||
$module = $this->modules->get_module( $slug ); | ||
|
@@ -435,9 +436,28 @@ private function get_rest_routes() { | |
if ( ! $module instanceof Module_With_Settings ) { | ||
return new WP_Error( 'invalid_module_slug', __( 'Module does not support settings.', 'google-site-kit' ), array( 'status' => 400 ) ); | ||
} | ||
return new WP_REST_Response( $module->get_settings()->get() ); | ||
|
||
if ( $can_manage_options() ) { | ||
return new WP_REST_Response( $module->get_settings()->get() ); | ||
} | ||
|
||
$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 commentThe reason will be displayed to describe this comment to others. Learn more. Here we should avoid redeclaring |
||
|
||
if ( empty( $view_only_keys ) || empty( $settings ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition is unnecessary because |
||
return array(); | ||
} | ||
|
||
$view_only_settings = array_intersect_key( $settings, array_flip( $view_only_keys ) ); | ||
|
||
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 commentThe 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. |
||
}, | ||
'permission_callback' => $can_manage_options, | ||
'permission_callback' => $can_list_data, | ||
), | ||
array( | ||
'methods' => WP_REST_Server::EDITABLE, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Interface Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface | ||
* | ||
* @package Google\Site_Kit\Core\Storage | ||
* @copyright 2023 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Core\Storage; | ||
|
||
/** | ||
* Interface for a settings class that includes view-only settings. | ||
* | ||
* @since n.e.x.t | ||
* @access private | ||
* @ignore | ||
*/ | ||
interface Setting_With_ViewOnly_Keys_Interface { | ||
|
||
/** | ||
* Returns keys for view-only settings. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array An array of keys for view-only settings. | ||
*/ | ||
public function get_view_only_keys(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Class Google\Site_Kit\Tests\Core\Modules\FakeModuleSettings_WithViewOnlyKeys | ||
* | ||
* @package Google\Site_Kit\Tests\Core\Modules | ||
* @copyright 2023 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Tests\Core\Modules; | ||
|
||
use Google\Site_Kit\Core\Modules\Module_Settings; | ||
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface; | ||
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Trait; | ||
|
||
class FakeModuleSettings_WithViewOnlyKeys extends Module_Settings implements Setting_With_ViewOnly_Keys_Interface { | ||
|
||
const OPTION = 'fake_module_settings_with_view_only'; | ||
|
||
public function get_view_only_keys() { | ||
return array( | ||
'viewOnlyKey', | ||
); | ||
} | ||
|
||
protected function get_default() { | ||
return array( | ||
'defaultKey' => 'default-value', | ||
'viewOnlyKey' => 'default-value', | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* FakeModule | ||
* | ||
* @package Google\Site_Kit | ||
* @copyright 2023 Google LLC | ||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 | ||
* @link https://sitekit.withgoogle.com | ||
*/ | ||
|
||
namespace Google\Site_Kit\Tests\Core\Modules; | ||
|
||
class FakeModule_WithViewOnlySettings extends FakeModule { | ||
|
||
/** | ||
* Sets up the module's settings instance which implements view-only keys. | ||
* | ||
* @return FakeModuleSettings_WithViewOnlyKeys | ||
*/ | ||
protected function setup_settings() { | ||
return new FakeModuleSettings_WithViewOnlyKeys( $this->options ); | ||
} | ||
|
||
} |
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