diff --git a/admin/rest-routes.php b/admin/rest-routes.php index c768cc8..0dcf204 100644 --- a/admin/rest-routes.php +++ b/admin/rest-routes.php @@ -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; } } diff --git a/admin/settings-api.php b/admin/settings-api.php index e6a5d47..02d8d09 100644 --- a/admin/settings-api.php +++ b/admin/settings-api.php @@ -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. * diff --git a/composer.lock b/composer.lock index 2c180d5..17efa14 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "eb1f0708b5830b82ea720f047c348230", + "content-hash": "684994342b1eecfad8ef44a86ff89124", "packages": [], "packages-dev": [ { @@ -187,16 +187,16 @@ }, { "name": "phpcsstandards/phpcsutils", - "version": "1.1.0", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHPCSUtils.git", - "reference": "65355670ac17c34cd235cf9d3ceae1b9252c4dad" + "reference": "f7eb16f2fa4237d5db9e8fed8050239bee17a9bd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/65355670ac17c34cd235cf9d3ceae1b9252c4dad", - "reference": "65355670ac17c34cd235cf9d3ceae1b9252c4dad", + "url": "https://api.github.com/repos/PHPCSStandards/PHPCSUtils/zipball/f7eb16f2fa4237d5db9e8fed8050239bee17a9bd", + "reference": "f7eb16f2fa4237d5db9e8fed8050239bee17a9bd", "shasum": "" }, "require": { @@ -276,7 +276,7 @@ "type": "thanks_dev" } ], - "time": "2025-06-12T04:32:33+00:00" + "time": "2025-08-10T01:04:45+00:00" }, { "name": "squizlabs/php_codesniffer",