Add cli commands for settings

This commit is contained in:
Cooper Dalrymple
2026-06-09 16:29:06 -05:00
parent c37712d2b5
commit 97ec73035e
4 changed files with 280 additions and 4 deletions
+74 -3
View File
@@ -27,11 +27,38 @@ final class Settings {
public const LOCATION_LOGIN_FORM = 'login_form';
public const LOCATION_COMMENT_FORM = 'comment_form';
public const LOCATION_POST_PASSWORD_FORM = 'post_password_form';
public const WP_LOCATIONS = [
self::LOCATION_LOGIN_PAGE,
self::LOCATION_REGISTRATION_PAGE,
self::LOCATION_LOST_PASSWORD_FORM,
self::LOCATION_LOGIN_FORM,
self::LOCATION_COMMENT_FORM,
self::LOCATION_POST_PASSWORD_FORM,
];
public const LOCATION_WC_LOGIN = 'wc_login';
public const LOCATION_WC_LOST_PASSWORD = 'wc_lost_password';
public const LOCATION_WC_REGISTER = 'wc_register';
public const LOCATION_WC_CHECKOUT = 'wc_checkout';
public const WC_LOCATIONS = [
self::LOCATION_WC_LOGIN,
self::LOCATION_WC_LOST_PASSWORD,
self::LOCATION_WC_REGISTER,
self::LOCATION_WC_CHECKOUT,
];
public const ALL_LOCATIONS = [
self::LOCATION_LOGIN_PAGE,
self::LOCATION_REGISTRATION_PAGE,
self::LOCATION_LOST_PASSWORD_FORM,
self::LOCATION_LOGIN_FORM,
self::LOCATION_COMMENT_FORM,
self::LOCATION_POST_PASSWORD_FORM,
self::LOCATION_WC_LOGIN,
self::LOCATION_WC_LOST_PASSWORD,
self::LOCATION_WC_REGISTER,
self::LOCATION_WC_CHECKOUT,
];
public const WIDGET_TYPE_STANDARD = 'standard';
public const WIDGET_TYPE_FLOATING = 'floating';
@@ -84,14 +111,30 @@ final class Settings {
return $url;
}
public static function set_instance_url(string $value):bool {
if (!wp_http_validate_url($value)) return false;
self::set('instance_url', $value);
return true;
}
public static function get_site_key():string {
return (string) self::get('site_key');
}
public static function set_site_key(string $value):bool {
self::get('site_key');
return true;
}
public static function get_secret_key():string {
return (string) self::get('secret_key');
}
public static function set_secret_key(string $value):bool {
self::get('secret_key');
return true;
}
public static function is_connected():bool {
return !empty(self::get_site_key()) && !empty(self::get_secret_key());
}
@@ -111,6 +154,26 @@ final class Settings {
return in_array($name, self::get_form_locations());
}
public static function set_form_location(string $name, bool $enabled):bool {
if (in_array($name, self::WP_LOCATIONS)) {
$key = 'form_locations';
} else if (in_array($name, self::WC_LOCATIONS)) {
$key = 'wc_form_locations';
} else {
return false;
}
$value = (array) self::get($key);
if (!!$enabled && !in_array($name, $value)) {
$value[] = $name;
} else if (!$enabled && in_array($name, $value)) {
unset($value[array_search($name, $value)]);
}
self::set($key, $value);
return true;
}
public static function is_login_page_enabled():bool {
return self::is_form_location_enabled(self::LOCATION_LOGIN_PAGE);
}
@@ -152,8 +215,9 @@ final class Settings {
}
public static function get_widget_type():string {
$value = (array) self::get('widget_type');
$value = !empty($value) ? array_values($value)[0] : '';
$value = self::get('widget_type');
if (is_array($value) && !empty($value)) $value = array_values($value)[0];
if (!is_string($value)) $value = '';
return in_array($value, self::WIDGET_TYPES) ? $value : self::WIDGET_TYPE_STANDARD;
}
@@ -161,6 +225,12 @@ final class Settings {
return self::get_widget_type() === self::WIDGET_TYPE_FLOATING;
}
public static function set_widget_type(string $value):bool {
if (!in_array($value, self::WIDGET_TYPES)) return false;
self::set('widget_type', $value);
return true;
}
public function menu() {
if (!current_user_can(self::CAPABILITY)) return;
add_options_page(
@@ -397,10 +467,11 @@ final class Settings {
echo implode('<br>', array_map(
fn (string $name, string $label):string => sprintf(
'<label><input type="%s" name="%s[%s][]" value="%s" %s>&nbsp;%s</label>',
'<label><input type="%s" name="%s[%s]%s" value="%s" %s>&nbsp;%s</label>',
esc_attr($args['type'] ?? 'checkbox'),
esc_attr(Plugin::get_id()),
esc_attr($args['name']),
(($args['type'] ?? 'checkbox') ? '[]' : ''),
esc_attr($name),
checked(self::is_checked($args['name'], $name, $args['default'] ?? null), display: false),
esc_html($label)