mirror of
https://ghproxy.net/https://github.com/abhijitb/helix.git
synced 2025-08-28 02:52:20 +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
|
||||
$option_name = helix_get_wp_option_name( $setting );
|
||||
|
||||
// Special handling for timezone setting
|
||||
if ( $setting === 'timezone' ) {
|
||||
$result = helix_update_timezone_setting( $sanitized_value );
|
||||
} else {
|
||||
// Update the WordPress option normally
|
||||
$result = update_option( $option_name, $sanitized_value );
|
||||
}
|
||||
// Handle special settings that need custom update logic
|
||||
$result = helix_update_setting( $setting, $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 ) {
|
||||
$updated_settings[ $setting ] = $sanitized_value;
|
||||
continue; // Skip to next setting
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we still can't update, provide a helpful error message
|
||||
if ( ! $result ) {
|
||||
$error_msg = sprintf(
|
||||
__( 'Language "%s" could not be installed automatically. Please install the language pack manually via WordPress Admin → Settings → General → Site Language.', 'helix' ),
|
||||
$sanitized_value
|
||||
);
|
||||
$errors[ $setting ] = $error_msg;
|
||||
continue; // Skip to next setting
|
||||
}
|
||||
// If special handling didn't apply, update normally
|
||||
if ( ! $result ) {
|
||||
$result = update_option( $option_name, $sanitized_value );
|
||||
}
|
||||
|
||||
if ( $result ) {
|
||||
$updated_settings[ $setting ] = $sanitized_value;
|
||||
} else {
|
||||
$error_msg = __( 'Failed to update setting.', 'helix' );
|
||||
// Provide specific error messages for special settings
|
||||
if ( $setting === 'language' ) {
|
||||
$error_msg = sprintf(
|
||||
__( 'Language "%s" could not be installed automatically. Please install the language pack manually via WordPress Admin → Settings → General → Site Language.', 'helix' ),
|
||||
$sanitized_value
|
||||
);
|
||||
} else {
|
||||
$error_msg = __( 'Failed to update setting.', 'helix' );
|
||||
}
|
||||
$errors[ $setting ] = $error_msg;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -316,6 +316,57 @@ function helix_sanitize_setting_value( $setting, $value ) {
|
|||
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.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue