mirror of
https://ghproxy.net/https://github.com/abhijitb/helix.git
synced 2025-08-28 06:26:00 +08:00
code refactor
This commit is contained in:
parent
9dbbd4a93c
commit
2e385d1f9d
3 changed files with 71 additions and 48 deletions
|
@ -146,54 +146,26 @@ function helix_update_settings( $request ) {
|
||||||
// Get the WordPress option name for this setting
|
// Get the WordPress option name for this setting
|
||||||
$option_name = helix_get_wp_option_name( $setting );
|
$option_name = helix_get_wp_option_name( $setting );
|
||||||
|
|
||||||
// Special handling for timezone setting
|
// Handle special settings that need custom update logic
|
||||||
if ( $setting === 'timezone' ) {
|
$result = helix_update_setting( $setting, $sanitized_value );
|
||||||
$result = helix_update_timezone_setting( $sanitized_value );
|
|
||||||
} else {
|
// If special handling didn't apply, update normally
|
||||||
// Update the WordPress option normally
|
if ( ! $result ) {
|
||||||
$result = update_option( $option_name, $sanitized_value );
|
$result = update_option( $option_name, $sanitized_value );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special handling for WPLANG option
|
|
||||||
if ( $option_name === 'WPLANG' && ! $result ) {
|
|
||||||
// Check if switch_to_locale function is available
|
|
||||||
if ( function_exists( 'switch_to_locale' ) ) {
|
|
||||||
// Check if the language file exists
|
|
||||||
$lang_dir = WP_CONTENT_DIR . '/languages/';
|
|
||||||
$lang_file = $lang_dir . $sanitized_value . '.po';
|
|
||||||
|
|
||||||
// If language file doesn't exist, try to install it
|
|
||||||
if ( ! file_exists( $lang_file ) ) {
|
|
||||||
// Try to install the language pack using WordPress core functions
|
|
||||||
$install_result = helix_install_language_pack( $sanitized_value );
|
|
||||||
|
|
||||||
if ( $install_result ) {
|
|
||||||
// Try updating the option again
|
|
||||||
$result = update_option( $option_name, $sanitized_value );
|
|
||||||
|
|
||||||
if ( $result ) {
|
if ( $result ) {
|
||||||
$updated_settings[ $setting ] = $sanitized_value;
|
$updated_settings[ $setting ] = $sanitized_value;
|
||||||
continue; // Skip to next setting
|
} else {
|
||||||
}
|
// Provide specific error messages for special settings
|
||||||
}
|
if ( $setting === 'language' ) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we still can't update, provide a helpful error message
|
|
||||||
if ( ! $result ) {
|
|
||||||
$error_msg = sprintf(
|
$error_msg = sprintf(
|
||||||
__( 'Language "%s" could not be installed automatically. Please install the language pack manually via WordPress Admin → Settings → General → Site Language.', 'helix' ),
|
__( 'Language "%s" could not be installed automatically. Please install the language pack manually via WordPress Admin → Settings → General → Site Language.', 'helix' ),
|
||||||
$sanitized_value
|
$sanitized_value
|
||||||
);
|
);
|
||||||
$errors[ $setting ] = $error_msg;
|
|
||||||
continue; // Skip to next setting
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $result ) {
|
|
||||||
$updated_settings[ $setting ] = $sanitized_value;
|
|
||||||
} else {
|
} else {
|
||||||
$error_msg = __( 'Failed to update setting.', 'helix' );
|
$error_msg = __( 'Failed to update setting.', 'helix' );
|
||||||
|
}
|
||||||
$errors[ $setting ] = $error_msg;
|
$errors[ $setting ] = $error_msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,6 +316,57 @@ function helix_sanitize_setting_value( $setting, $value ) {
|
||||||
return $sanitized;
|
return $sanitized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update special settings that require custom logic.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @param string $setting The setting key.
|
||||||
|
* @param mixed $value The sanitized value.
|
||||||
|
* @param string $option_name The WordPress option name.
|
||||||
|
* @return bool|null True if successful, false if failed, null if not a special setting.
|
||||||
|
*/
|
||||||
|
function helix_update_setting( $setting, $value ) {
|
||||||
|
// Handle timezone setting
|
||||||
|
if ( $setting === 'timezone' ) {
|
||||||
|
return helix_update_timezone_setting( $value );
|
||||||
|
} else if ( $setting === 'language' ) {
|
||||||
|
return helix_update_language_setting( $value );
|
||||||
|
}
|
||||||
|
// Not a special setting
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update language setting with automatic language pack installation.
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @param string $locale The language locale to set.
|
||||||
|
* @return bool True if successful, false otherwise.
|
||||||
|
*/
|
||||||
|
function helix_update_language_setting( $locale ) {
|
||||||
|
// Try to update the option first
|
||||||
|
$result = update_option( 'WPLANG', $locale );
|
||||||
|
|
||||||
|
// If it failed, try to install the language pack
|
||||||
|
if ( ! $result && function_exists( 'switch_to_locale' ) ) {
|
||||||
|
// Check if the language file exists
|
||||||
|
$lang_dir = WP_CONTENT_DIR . '/languages/';
|
||||||
|
$lang_file = $lang_dir . $locale . '.po';
|
||||||
|
|
||||||
|
// If language file doesn't exist, try to install it
|
||||||
|
if ( ! file_exists( $lang_file ) ) {
|
||||||
|
$install_result = helix_install_language_pack( $locale );
|
||||||
|
|
||||||
|
if ( $install_result ) {
|
||||||
|
// Try updating the option again
|
||||||
|
$result = update_option( 'WPLANG', $locale );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update timezone setting by handling both city-based timezones and GMT offsets.
|
* Update timezone setting by handling both city-based timezones and GMT offsets.
|
||||||
*
|
*
|
||||||
|
|
12
composer.lock
generated
12
composer.lock
generated
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "eb1f0708b5830b82ea720f047c348230",
|
"content-hash": "684994342b1eecfad8ef44a86ff89124",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
|
@ -187,16 +187,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpcsstandards/phpcsutils",
|
"name": "phpcsstandards/phpcsutils",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/PHPCSStandards/PHPCSUtils.git",
|
"url": "https://github.com/PHPCSStandards/PHPCSUtils.git",
|
||||||
"reference": "65355670ac17c34cd235cf9d3ceae1b9252c4dad"
|
"reference": "f7eb16f2fa4237d5db9e8fed8050239bee17a9bd"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/65355670ac17c34cd235cf9d3ceae1b9252c4dad",
|
"url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/f7eb16f2fa4237d5db9e8fed8050239bee17a9bd",
|
||||||
"reference": "65355670ac17c34cd235cf9d3ceae1b9252c4dad",
|
"reference": "f7eb16f2fa4237d5db9e8fed8050239bee17a9bd",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -276,7 +276,7 @@
|
||||||
"type": "thanks_dev"
|
"type": "thanks_dev"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-06-12T04:32:33+00:00"
|
"time": "2025-08-10T01:04:45+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "squizlabs/php_codesniffer",
|
"name": "squizlabs/php_codesniffer",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue