Add form location settings
This commit is contained in:
+94
-8
@@ -21,27 +21,66 @@ final class Settings {
|
||||
const CAPABILITY = 'manage_options';
|
||||
const DEFAULT_INSTANCE_URL = 'https://captcha.cleverogre.com';
|
||||
|
||||
public const LOCATION_LOGIN_PAGE = 'login_page';
|
||||
public const LOCATION_REGISTRATION_PAGE = 'registration_page';
|
||||
public const LOCATION_LOST_PASSWORD_PAGE = 'lost_password_page';
|
||||
public const LOCATION_LOGIN_FORM = 'login_form';
|
||||
public const LOCATION_COMMENT_FORM = 'comment_form';
|
||||
public const LOCATION_POST_PASSWORD_FORM = 'post_password_form';
|
||||
|
||||
protected function __construct() {
|
||||
add_action('admin_menu', [$this, 'menu']);
|
||||
add_action('admin_init', [$this, 'init']);
|
||||
}
|
||||
|
||||
private function get(string $key = '') {
|
||||
private static function get(string $key = '') {
|
||||
$data = (array) get_option(Plugin::get_id(), []);
|
||||
if (!empty($key)) return array_key_exists($key, $data) ? $data[$key] : null;
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function get_instance_url():string {
|
||||
return !empty($value = (string) $this->get('instance_url')) ? $value : self::DEFAULT_INSTANCE_URL;
|
||||
private static function is_checked(string $name, string $value):bool {
|
||||
return in_array($value, (array) self::get($name));
|
||||
}
|
||||
|
||||
public function get_site_key():string {
|
||||
return (string) $this->get('site_key');
|
||||
public static function get_instance_url():string {
|
||||
return !empty($value = (string) self::get('instance_url')) ? $value : self::DEFAULT_INSTANCE_URL;
|
||||
}
|
||||
|
||||
public function get_secret_key():string {
|
||||
return (string) $this->get('secret_key');
|
||||
public static function get_site_key():string {
|
||||
return (string) self::get('site_key');
|
||||
}
|
||||
|
||||
public static function get_secret_key():string {
|
||||
return (string) self::get('secret_key');
|
||||
}
|
||||
|
||||
public static function get_form_locations():array {
|
||||
return (array) self::get('form_locations');
|
||||
}
|
||||
|
||||
public static function is_login_page_enabled():bool {
|
||||
return in_array(self::LOCATION_LOGIN_PAGE, self::get_form_locations());
|
||||
}
|
||||
|
||||
public static function is_registration_page_enabled():bool {
|
||||
return in_array(self::LOCATION_REGISTRATION_PAGE, self::get_form_locations());
|
||||
}
|
||||
|
||||
public static function is_lost_password_page_enabled():bool {
|
||||
return in_array(self::LOCATION_LOST_PASSWORD_PAGE, self::get_form_locations());
|
||||
}
|
||||
|
||||
public static function is_login_form_enabled():bool {
|
||||
return in_array(self::LOCATION_LOGIN_FORM, self::get_form_locations());
|
||||
}
|
||||
|
||||
public static function is_comment_form_enabled():bool {
|
||||
return in_array(self::LOCATION_COMMENT_FORM, self::get_form_locations());
|
||||
}
|
||||
|
||||
public static function is_post_password_form_enabled():bool {
|
||||
return in_array(self::LOCATION_POST_PASSWORD_FORM, self::get_form_locations());
|
||||
}
|
||||
|
||||
public function menu() {
|
||||
@@ -95,6 +134,35 @@ final class Settings {
|
||||
'description' => Plugin::__('Provide your Cap secret key.'),
|
||||
]
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'forms',
|
||||
Plugin::__('CAPTCHA Forms'),
|
||||
[$this, 'section'],
|
||||
Plugin::get_id(),
|
||||
[
|
||||
'description' => Plugin::__('Define which forms you would like to secure with CAPTCHA.'),
|
||||
]
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_form_locations',
|
||||
Plugin::__('Form locations'),
|
||||
[$this, 'checkbox_field'],
|
||||
Plugin::get_id(),
|
||||
'forms',
|
||||
[
|
||||
'name' => 'form_locations',
|
||||
'options' => [
|
||||
self::LOCATION_LOGIN_PAGE => Plugin::__('Login page'),
|
||||
self::LOCATION_REGISTRATION_PAGE => Plugin::__('Registration page'),
|
||||
self::LOCATION_LOST_PASSWORD_PAGE => Plugin::__('Lost password page'),
|
||||
self::LOCATION_LOGIN_FORM => Plugin::__('Custom login form'),
|
||||
self::LOCATION_COMMENT_FORM => Plugin::__('Comment form'),
|
||||
self::LOCATION_POST_PASSWORD_FORM => Plugin::__('Password-protected posts and pages'),
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function page() {
|
||||
@@ -132,7 +200,7 @@ final class Settings {
|
||||
$args['name']
|
||||
),
|
||||
'id' => $args['name'],
|
||||
'value' => (string) $this->get($args['name']),
|
||||
'value' => (string) self::get($args['name']),
|
||||
];
|
||||
if ($args['readonly'] ?? false) $attributes['readonly'] = 'readonly';
|
||||
if ($args['required'] ?? false) $attributes['required'] = 'required';
|
||||
@@ -174,6 +242,24 @@ final class Settings {
|
||||
}
|
||||
}
|
||||
|
||||
public function checkbox_field(array $args):void {
|
||||
if (empty($args['name'] ?? '') || !isset($options) || empty($options)) return;
|
||||
|
||||
echo implode('<br>', array_map(
|
||||
fn (string $name, string $label):string => sprintf(
|
||||
'<label><input type="checkbox" name="%s[%s][]" value="%s" %s> %s (%s)</label>',
|
||||
esc_attr(Plugin::get_id()),
|
||||
esc_attr($args['name']),
|
||||
esc_attr($name),
|
||||
checked(self::is_checked($args['name'], $name), display: false),
|
||||
esc_html($label),
|
||||
esc_html($name)
|
||||
),
|
||||
array_keys($args['options']),
|
||||
array_values($args['options'])
|
||||
));
|
||||
}
|
||||
|
||||
public static function get_url():string {
|
||||
return admin_url('options-general.php?page=' . Plugin::get_id());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user