Skip to content

Commit

Permalink
Merge pull request #9441 from google/enhancement/9333-siwg-settings
Browse files Browse the repository at this point in the history
Enhancement/9333 siwg settings
  • Loading branch information
tofumatt authored Oct 9, 2024
2 parents 3a6fd17 + d12bdac commit 3233a32
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 10 deletions.
80 changes: 70 additions & 10 deletions includes/Modules/Sign_In_With_Google/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
namespace Google\Site_Kit\Modules\Sign_In_With_Google;

use Google\Site_Kit\Core\Modules\Module_Settings;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;

/**
* Class for Sign_In_With_Google settings.
Expand All @@ -21,10 +19,21 @@
* @access private
* @ignore
*/
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface {
use Setting_With_Owned_Keys_Trait;
class Settings extends Module_Settings {

const OPTION = 'googlesitekit_siwg_settings';
const OPTION = 'googlesitekit_sign-in-with-google_settings';

const TEXT_CONTINUE_WITH_GOOGLE = 'Continue with Google';
const TEXT_SIGN_IN = 'Sign in';
const TEXT_SIGN_IN_WITH_GOOGLE = 'Sign in with Google';
const TEXT_SIGN_UP_WITH_GOOGLE = 'Sign up with Google';

const THEME_LIGHT = 'light';
const THEME_NEUTRAL = 'neutral';
const THEME_DARK = 'dark';

const SHAPE_RECTANGULAR = 'rectangular';
const SHAPE_PILL = 'pill';

/**
* Registers the setting in WordPress.
Expand All @@ -44,17 +53,68 @@ public function register() {
* @return array An array of default settings values.
*/
protected function get_default() {
return array();
return array(
'clientID' => '',
'text' => self::TEXT_SIGN_IN_WITH_GOOGLE,
'theme' => self::THEME_LIGHT,
'shape' => self::SHAPE_RECTANGULAR,
);
}

/**
* Returns keys for owned settings.
* Gets the callback for sanitizing the setting's value before saving.
*
* @since 1.137.0
*
* @return array An array of keys for owned settings.
* @return callable|null
*/
public function get_owned_keys() {
return array();
protected function get_sanitize_callback() {
return function ( $option ) {
if ( ! is_array( $option ) ) {
return $option;
}

if ( isset( $option['clientID'] ) ) {
$option['clientID'] = (string) $option['clientID'];
}

if ( isset( $option['text'] ) ) {
$text_options = array(
self::TEXT_CONTINUE_WITH_GOOGLE,
self::TEXT_SIGN_IN,
self::TEXT_SIGN_IN_WITH_GOOGLE,
self::TEXT_SIGN_UP_WITH_GOOGLE,
);

if ( ! in_array( $option['text'], $text_options, true ) ) {
$option['text'] = self::TEXT_SIGN_IN_WITH_GOOGLE;
}
}

if ( isset( $option['theme'] ) ) {
$theme_options = array(
self::THEME_LIGHT,
self::THEME_NEUTRAL,
self::THEME_DARK,
);

if ( ! in_array( $option['theme'], $theme_options, true ) ) {
$option['theme'] = self::THEME_LIGHT;
}
}

if ( isset( $option['shape'] ) ) {
$shape_options = array(
self::SHAPE_RECTANGULAR,
self::SHAPE_PILL,
);

if ( ! in_array( $option['shape'], $shape_options, true ) ) {
$option['shape'] = self::SHAPE_RECTANGULAR;
}
}

return $option;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Class Google\Site_Kit\Tests\Modules\Sign_In_With_Google\SettingsTest
*
* @package Google\Site_Kit\Tests\Modules\Sign_In_With_Google
* @copyright 2024 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\Modules\Sign_In_With_Google;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Modules\Sign_In_With_Google\Settings;
use Google\Site_Kit\Tests\Modules\SettingsTestCase;

/**
* @group Modules
* @group Sign_In_With_Google
*/
class SettingsTest extends SettingsTestCase {

public function test_get_default() {
$settings = new Settings( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) );
$settings->register();
$default = get_option( Settings::OPTION );

$this->assertIsArray( $default );
$this->assertArrayHasKey( 'clientID', $default );
$this->assertArrayHasKey( 'text', $default );
$this->assertArrayHasKey( 'theme', $default );
$this->assertArrayHasKey( 'shape', $default );

$this->assertSame( '', $default['clientID'] );
$this->assertSame( Settings::TEXT_SIGN_IN_WITH_GOOGLE, $default['text'] );
$this->assertSame( Settings::THEME_LIGHT, $default['theme'] );
$this->assertSame( Settings::SHAPE_RECTANGULAR, $default['shape'] );
}

/**
* @inheritDoc
*/
protected function get_option_name() {
return Settings::OPTION;
}
}

0 comments on commit 3233a32

Please sign in to comment.