Files
mahak-authenticate-bridge/src/app/Http/Controllers/SettingsPageFormController.php
T

134 lines
4.7 KiB
PHP

<?php
namespace Dabestaniha\AuthenticateBridge\App\Http\Controllers;
class SettingsPageFormController extends InvocableController
{
public function __invoke()
{
foreach (config('mahak.options') as $configured => $options_key) {
$args = [];
if ($configured === 'debug-mode') {
$args['sanitize_callback'] = function ($value): int {
return (int) filter_var($value, FILTER_VALIDATE_BOOLEAN);
};
$args['default'] = 0;
}
register_setting(config('mahak.settings-group'), $options_key, $args);
}
add_settings_section(
config('mahak.section-id'),
config('mahak.translations.section-id'),
null,
config('mahak.settings-page')
);
add_settings_field(
config('mahak.options.user-identifier'),
config('mahak.translations.user-identifier'),
fn () => $this->user_identifier(),
config('mahak.settings-page'),
config('mahak.section-id')
);
add_settings_field(
config('mahak.options.jwt-secret'),
config('mahak.translations.jwt-secret'),
fn () => $this->jwt_secret(),
config('mahak.settings-page'),
config('mahak.section-id')
);
add_settings_field(
config('mahak.options.login-route'),
config('mahak.translations.login-route'),
fn () => $this->login_route(),
config('mahak.settings-page'),
config('mahak.section-id')
);
add_settings_field(
config('mahak.options.after-login-route'),
config('mahak.translations.after-login-route'),
fn () => $this->after_login_route(),
config('mahak.settings-page'),
config('mahak.section-id')
);
add_settings_field(
config('mahak.options.debug-mode'),
config('mahak.translations.debug-mode'),
fn () => $this->debug_mode(),
config('mahak.settings-page'),
config('mahak.section-id')
);
$loginRouteOptionName = config('mahak.options.login-route');
add_filter("pre_update_option_{$loginRouteOptionName}", function ($value) {
return trim($value, '/');
});
$afterLoginRouteOptionName = config('mahak.options.after-login-route');
add_filter("pre_update_option_{$afterLoginRouteOptionName}", function ($value) {
return trim($value, '/');
});
}
public function user_identifier(): void
{
$name = config('mahak.options.user-identifier');
$value = get_option($name, 'mobile');
$html = "<select name='$name'>";
foreach (config('mahak.user-identifiers') as $identifier) {
$selected = $value === $identifier ? "selected='selected'" : null;
$translate = config("mahak.translations.$identifier");
$html .= "<option value='$identifier' $selected>$translate</option>";
}
$html .= "</select>";
echo $html;
}
public function jwt_secret(): void
{
$name = config('mahak.options.jwt-secret');
$value = esc_attr(get_option($name, ''));
echo "<input type='text' name='$name' value='$value' class='regular-text ltr' />";
}
public function login_route(): void
{
$name = config('mahak.options.login-route');
$value = esc_attr(get_option($name, config('mahak.login_route_default')));
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
echo '<span style="color: #666;">'.home_url('/').'</span>';
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
echo '</div>';
}
public function after_login_route(): void
{
$name = config('mahak.options.after-login-route');
$value = esc_attr(get_option($name, config('mahak.after_login_route_default')));
echo '<div class="ltr" style="float: right; display: flex; align-items: center; gap: 5px;">';
echo '<span style="color: #666;">'.home_url('/').'</span>';
echo "<input type='text' name='$name' value='$value' class='regular-text' />";
echo '</div>';
}
public function debug_mode(): void
{
$name = config('mahak.options.debug-mode');
$enabled = (bool) get_option($name, false);
echo "<input type='hidden' name='".esc_attr($name)."' value='0' />";
echo "<label><input type='checkbox' name='".esc_attr($name)."' value='1' ".checked($enabled, true, false).' /> ';
echo esc_html(config('mahak.translations.debug-mode-description')).'</label>';
}
}