=> __('Enable/Disable', 'siwk'), 'type' => 'checkbox', 'label' => __('Enable Sign in with Klarna', 'siwk'), 'default' => $this->default()['siwk_enabled']), 'siwk_callback_url' => array('name' => 'siwk_callback_url', 'title' => __('Redirect URL', 'siwk'), 'type' => 'text', 'description' => __('Add this URL to your list of allowed redirect URLs in the "Sign in with Klarna" settings on the Klarna merchant portal.', 'siwk'), 'default' => Redirect::get_callback_url(), 'css' => 'min-width: 100%', 'custom_attributes' => array('readonly' => 'readonly')), 'siwk_button_theme' => array('name' => 'siwk_button_theme', 'title' => __('Button theme'), 'type' => 'select', 'description' => __('The button\'s color theme.', 'siwk'), 'default' => $this->default()['siwk_button_theme'], 'options' => array('default' => __('Dark', 'siwk'), 'light' => __('Light', 'siwk'), 'outlined' => __('Outlined', 'siwk')), 'desc_tip' => \true), 'siwk_button_shape' => array('name' => 'siwk_button_shape', 'title' => __('Button shape'), 'type' => 'select', 'description' => __('The button\'s shape.', 'siwk'), 'default' => $this->default()['siwk_button_shape'], 'options' => array('default' => __('Rounded', 'siwk'), 'rect' => __('Rectangular', 'siwk'), 'pill' => __('Pill', 'siwk')), 'desc_tip' => \true), 'siwk_logo_alignment' => array('name' => 'siwk_logo_alignment', 'title' => __('Badge alignment'), 'type' => 'select', 'description' => __('Change alignment of the Klarna logo on the call to action button.', 'siwk'), 'default' => $this->default()['siwk_logo_alignment'], 'options' => array('default' => __('Badge', 'siwk'), 'left' => __('Left', 'siwk'), 'center' => __('Centered', 'siwk')), 'desc_tip' => \true), 'siwk_cart_placement' => array('name' => 'siwk_cart_placement', 'title' => __('Cart page placement', 'siwk'), 'type' => 'select', 'description' => __('Change the placement of the "Sign in with Klarna" button on the cart page.', 'siwk'), 'default' => $this->default()['siwk_cart_placement'], 'options' => array('10' => __('Before "Proceed to checkout" button', 'siwk'), '100' => __('After "Proceed to checkout" button', 'siwk')), 'desc_tip' => \true), 'siwk_required_scopes_email' => array('name' => 'siwk_required_scopes', 'title' => __('Required Customer Data', 'siwk'), 'type' => 'checkbox', 'label' => __('Email Address', 'siwk'), 'default' => 'yes', 'disabled' => \true, 'custom_attributes' => array('checked' => 'true')), 'siwk_required_scopes_name' => array('name' => 'siwk_required_scopes_name', 'type' => 'checkbox', 'label' => __('Full name', 'siwk'), 'default' => 'yes', 'disabled' => \true, 'class' => 'siwk', 'custom_attributes' => array('checked' => 'true')), 'siwk_required_scopes_phone' => array('name' => 'siwk_required_scopes_phone', 'type' => 'checkbox', 'label' => __('Phone number', 'siwk'), 'default' => 'yes', 'disabled' => \true, 'class' => 'siwk', 'custom_attributes' => array('checked' => 'true')), 'siwk_required_scopes_billing_address' => array('name' => 'siwk_required_scopes_billing_address', 'type' => 'checkbox', 'label' => __('Billing address', 'siwk'), 'description' => __('These scopes are included by default, as necessary for creating a WooCommerce customer account in your shop. More about available scopes with Sign in with Klarna here. Additional scopes can be customized if applicable, more info here.', 'siwk'), 'disabled' => \true, 'class' => 'siwk', 'custom_attributes' => array('checked' => 'true')), 'siwk_optional_scopes_date_of_birth' => array('name' => 'siwk_optional_scopes_date_of_birth', 'title' => 'Request Additional Customer Data', 'type' => 'checkbox', 'label' => __('Date of birth', 'siwk'), 'default' => 'no'), 'siwk_optional_scopes_country' => array('name' => 'siwk_optional_scopes_country', 'type' => 'checkbox', 'label' => __('Country', 'siwk'), 'default' => 'no', 'class' => 'siwk'), 'siwk_optional_scopes_language' => array('name' => 'siwk_optional_scopes_language', 'type' => 'checkbox', 'label' => __('Language Preference', 'siwk'), 'default' => 'no', 'class' => 'siwk'), 'siwk_previews' => array('type' => 'kp_section_end', 'previews' => array(array('title' => __('Preview', 'siwk'), 'image' => $this->get_preview_image())))));
}
/**
* Get the preview image URL for the "Sign in with Klarna" button.
*
* @return string The preview image url.
*/
private function get_preview_image()
{
$theme = $this->button_theme;
$shape = $this->button_shape;
$alignment = $this->logo_alignment;
return plugin_dir_url(__FILE__) . "assets/img/preview-{$shape}_shape-{$theme}_theme-{$alignment}_alignment.png";
}
/**
* Update the internal settings state.
*
* @param array $settings The settings to extract from.
* @return void
*/
private function update($settings)
{
$default = $this->default();
$this->enabled = $settings['siwk_enabled'] ?? $default['siwk_enabled'];
$this->test_mode = $settings['testmode'] ?? $default['siwk_test_mode'];
$this->button_theme = $settings['siwk_button_theme'] ?? $default['siwk_button_theme'];
$this->button_shape = $settings['siwk_button_shape'] ?? $default['siwk_button_shape'];
$this->logo_alignment = $settings['siwk_logo_alignment'] ?? $default['siwk_logo_alignment'];
$this->cart_placement = $settings['siwk_cart_placement'] ?? $default['siwk_cart_placement'];
$this->market = \kp_get_klarna_country();
$this->client_id = \kp_get_client_id($this->market);
$this->locale = apply_filters('siwk_locale', \str_replace('_', '-', get_locale()));
// The array keys match the name of the scopes they define.
$this->scope = array('profile:name' => wc_string_to_bool($settings['siwk_required_scopes_name'] ?? 'yes'), 'profile:email' => wc_string_to_bool($settings['siwk_required_scopes_email'] ?? 'yes'), 'profile:phone' => wc_string_to_bool($settings['siwk_required_scopes_phone'] ?? 'yes'), 'profile:locale' => wc_string_to_bool($settings['siwk_optional_scopes_language'] ?? 'no'), 'profile:country' => wc_string_to_bool($settings['siwk_optional_scopes_country'] ?? 'no'), 'profile:date_of_birth' => wc_string_to_bool($settings['siwk_optional_scopes_date_of_birth'] ?? 'no'));
}
/**
* Retrieve the default settings values.
*
* @return array
*/
private function default()
{
return array('siwk_client_id' => '', 'siwk_enabled' => 'no', 'siwk_test_mode' => 'no', 'siwk_title_theme' => __('Theme, button shape & placements', 'siwk'), 'siwk_button_theme' => 'default', 'siwk_button_shape' => 'default', 'siwk_logo_alignment' => 'default', 'siwk_cart_placement' => 10, 'siwk_callback_url' => Redirect::get_callback_url());
}
}