Add floating mode support
This commit is contained in:
+58
-5
@@ -33,6 +33,13 @@ final class Settings {
|
||||
public const LOCATION_WC_REGISTER = 'wc_register';
|
||||
public const LOCATION_WC_CHECKOUT = 'wc_checkout';
|
||||
|
||||
public const WIDGET_TYPE_STANDARD = 'standard';
|
||||
public const WIDGET_TYPE_FLOATING = 'floating';
|
||||
public const WIDGET_TYPES = [
|
||||
self::WIDGET_TYPE_STANDARD,
|
||||
self::WIDGET_TYPE_FLOATING,
|
||||
];
|
||||
|
||||
protected function __construct() {
|
||||
add_action('admin_menu', [$this, 'menu']);
|
||||
add_action('admin_init', [$this, 'init']);
|
||||
@@ -44,6 +51,10 @@ final class Settings {
|
||||
return $data;
|
||||
}
|
||||
|
||||
private static function has(string $key):bool {
|
||||
return array_key_exists($key, self::get());
|
||||
}
|
||||
|
||||
private static function set(string $key, $value = null):void {
|
||||
$data = self::get();
|
||||
if (is_null($value) && isset($data[$key])) {
|
||||
@@ -54,7 +65,8 @@ final class Settings {
|
||||
update_option(Plugin::get_id(), $data);
|
||||
}
|
||||
|
||||
private static function is_checked(string $name, string $value):bool {
|
||||
private static function is_checked(string $name, string $value, string|null $default = null):bool {
|
||||
if (!is_null($default) && !self::has($name)) return $value == $default;
|
||||
return in_array($value, (array) self::get($name));
|
||||
}
|
||||
|
||||
@@ -139,6 +151,16 @@ final class Settings {
|
||||
return self::is_form_location_enabled(self::LOCATION_WC_CHECKOUT);
|
||||
}
|
||||
|
||||
public static function get_widget_type():string {
|
||||
$value = (array) self::get('widget_type');
|
||||
$value = !empty($value) ? array_values($value)[0] : '';
|
||||
return in_array($value, self::WIDGET_TYPES) ? $value : self::WIDGET_TYPE_STANDARD;
|
||||
}
|
||||
|
||||
public static function is_widget_floating():bool {
|
||||
return self::get_widget_type() === self::WIDGET_TYPE_FLOATING;
|
||||
}
|
||||
|
||||
public function menu() {
|
||||
if (!current_user_can(self::CAPABILITY)) return;
|
||||
add_options_page(
|
||||
@@ -204,11 +226,11 @@ final class Settings {
|
||||
|
||||
add_settings_section(
|
||||
'forms',
|
||||
Plugin::__('CAPTCHA Forms'),
|
||||
Plugin::__('Forms'),
|
||||
[$this, 'section'],
|
||||
Plugin::get_id(),
|
||||
[
|
||||
'description' => Plugin::__('Define which forms you would like to secure with CAPTCHA.'),
|
||||
'description' => Plugin::__('Define which forms you would like to secure with Cap.'),
|
||||
]
|
||||
);
|
||||
|
||||
@@ -250,6 +272,36 @@ final class Settings {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
add_settings_section(
|
||||
'widget',
|
||||
Plugin::__('Widget'),
|
||||
[$this, 'section'],
|
||||
Plugin::get_id(),
|
||||
[
|
||||
'description' => Plugin::__('Change how the Cap widget is displayed on the frontend.'),
|
||||
]
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_widget_type',
|
||||
Plugin::__('Widget type'),
|
||||
[$this, 'checkbox_field'],
|
||||
Plugin::get_id(),
|
||||
'widget',
|
||||
[
|
||||
'type' => 'radio',
|
||||
'name' => 'widget_type',
|
||||
'options' => [
|
||||
self::WIDGET_TYPE_STANDARD => Plugin::__('Standard (default)'),
|
||||
self::WIDGET_TYPE_FLOATING => Plugin::__('Floating'),
|
||||
],
|
||||
'default' => self::WIDGET_TYPE_STANDARD,
|
||||
'description' => Plugin::__('Not sure which type you want to use? <a href="https://trycap.dev/guide/demo.html" target="_blank">View the demo</a> to try them out.'),
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function page() {
|
||||
@@ -334,11 +386,12 @@ final class Settings {
|
||||
|
||||
echo implode('<br>', array_map(
|
||||
fn (string $name, string $label):string => sprintf(
|
||||
'<label><input type="checkbox" name="%s[%s][]" value="%s" %s> %s</label>',
|
||||
'<label><input type="%s" name="%s[%s][]" value="%s" %s> %s</label>',
|
||||
esc_attr($args['type'] ?? 'checkbox'),
|
||||
esc_attr(Plugin::get_id()),
|
||||
esc_attr($args['name']),
|
||||
esc_attr($name),
|
||||
checked(self::is_checked($args['name'], $name), display: false),
|
||||
checked(self::is_checked($args['name'], $name, $args['default'] ?? null), display: false),
|
||||
esc_html($label)
|
||||
),
|
||||
array_keys($args['options']),
|
||||
|
||||
Reference in New Issue
Block a user