lint fixes

This commit is contained in:
Abhijit Bhatnagar 2025-08-17 02:25:54 +05:30
parent 2e385d1f9d
commit b2e6a0a21f
2 changed files with 120 additions and 121 deletions

View file

@ -21,8 +21,8 @@ add_action( 'rest_api_init', 'helix_register_rest_routes' );
* @since 1.0.0 * @since 1.0.0
*/ */
function helix_register_rest_routes() { function helix_register_rest_routes() {
// Get the schemas // Get the schemas.
$get_schema = helix_get_settings_schema(); $get_schema = helix_get_settings_schema();
$update_schema = helix_update_settings_schema(); $update_schema = helix_update_settings_schema();
// Settings endpoints. // Settings endpoints.
@ -117,14 +117,14 @@ function helix_get_settings() {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
function helix_update_settings( $request ) { function helix_update_settings( $request ) {
$params = $request->get_params(); $params = $request->get_params();
$allowed_settings = helix_get_allowed_settings(); $allowed_settings = helix_get_allowed_settings();
$updated_settings = array(); $updated_settings = array();
$errors = array(); $errors = array();
// Process each setting // Process each setting.
foreach ( $params as $setting => $value ) { foreach ( $params as $setting => $value ) {
// Check if setting is allowed // Check if setting is allowed.
if ( ! in_array( $setting, $allowed_settings, true ) ) { if ( ! in_array( $setting, $allowed_settings, true ) ) {
$error_msg = sprintf( $error_msg = sprintf(
/* translators: %s: Setting name */ /* translators: %s: Setting name */
@ -135,7 +135,7 @@ function helix_update_settings( $request ) {
continue; continue;
} }
// Validate and sanitize the value // Validate and sanitize the value.
$sanitized_value = helix_sanitize_setting_value( $setting, $value ); $sanitized_value = helix_sanitize_setting_value( $setting, $value );
if ( is_wp_error( $sanitized_value ) ) { if ( is_wp_error( $sanitized_value ) ) {
@ -143,13 +143,13 @@ function helix_update_settings( $request ) {
continue; continue;
} }
// 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 );
// Handle special settings that need custom update logic // Handle special settings that need custom update logic.
$result = helix_update_setting( $setting, $sanitized_value ); $result = helix_update_setting( $setting, $sanitized_value );
// If special handling didn't apply, update normally // If special handling didn't apply, update normally.
if ( ! $result ) { if ( ! $result ) {
$result = update_option( $option_name, $sanitized_value ); $result = update_option( $option_name, $sanitized_value );
} }
@ -157,9 +157,10 @@ function helix_update_settings( $request ) {
if ( $result ) { if ( $result ) {
$updated_settings[ $setting ] = $sanitized_value; $updated_settings[ $setting ] = $sanitized_value;
} else { } else {
// Provide specific error messages for special settings // Provide specific error messages for special settings.
if ( $setting === 'language' ) { if ( 'language' === $setting ) {
$error_msg = sprintf( $error_msg = sprintf(
/* translators: %s: Language name */
__( '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
); );

View file

@ -35,8 +35,8 @@ function helix_get_settings_schema() {
*/ */
function helix_update_settings_schema() { function helix_update_settings_schema() {
$settings_config = helix_get_settings_config(); $settings_config = helix_get_settings_config();
$schema = array( $schema = array(
'type' => 'object', 'type' => 'object',
'properties' => array(), 'properties' => array(),
); );
@ -46,12 +46,12 @@ function helix_update_settings_schema() {
'type' => get_rest_api_type( $setting_config['type'] ), 'type' => get_rest_api_type( $setting_config['type'] ),
); );
// Add enum validation if applicable // Add enum validation if applicable.
if ( isset( $setting_config['enum'] ) ) { if ( isset( $setting_config['enum'] ) ) {
$schema['properties'][ $setting_key ]['enum'] = $setting_config['enum']; $schema['properties'][ $setting_key ]['enum'] = $setting_config['enum'];
} }
// Add minimum/maximum validation for numbers // Add minimum/maximum validation for numbers.
if ( isset( $setting_config['min'] ) ) { if ( isset( $setting_config['min'] ) ) {
$schema['properties'][ $setting_key ]['minimum'] = $setting_config['min']; $schema['properties'][ $setting_key ]['minimum'] = $setting_config['min'];
} }
@ -287,9 +287,9 @@ function helix_sanitize_setting_value( $setting, $value ) {
break; break;
} }
// For any type with enum values, validate against allowed values // For any type with enum values, validate against allowed values.
if ( isset( $setting_config['enum'] ) ) { if ( isset( $setting_config['enum'] ) ) {
// Extract values from enum options if they are objects with 'value' property // Extract values from enum options if they are objects with 'value' property.
$enum_values = array(); $enum_values = array();
foreach ( $setting_config['enum'] as $option ) { foreach ( $setting_config['enum'] as $option ) {
if ( is_array( $option ) && isset( $option['value'] ) ) { if ( is_array( $option ) && isset( $option['value'] ) ) {
@ -322,17 +322,16 @@ function helix_sanitize_setting_value( $setting, $value ) {
* @since 1.0.0 * @since 1.0.0
* @param string $setting The setting key. * @param string $setting The setting key.
* @param mixed $value The sanitized value. * @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. * @return bool|null True if successful, false if failed, null if not a special setting.
*/ */
function helix_update_setting( $setting, $value ) { function helix_update_setting( $setting, $value ) {
// Handle timezone setting // Handle timezone setting.
if ( $setting === 'timezone' ) { if ( 'timezone' === $setting ) {
return helix_update_timezone_setting( $value ); return helix_update_timezone_setting( $value );
} else if ( $setting === 'language' ) { } elseif ( 'language' === $setting ) {
return helix_update_language_setting( $value ); return helix_update_language_setting( $value );
} }
// Not a special setting // Not a special setting.
return false; return false;
} }
@ -344,21 +343,21 @@ function helix_update_setting( $setting, $value ) {
* @return bool True if successful, false otherwise. * @return bool True if successful, false otherwise.
*/ */
function helix_update_language_setting( $locale ) { function helix_update_language_setting( $locale ) {
// Try to update the option first // Try to update the option first.
$result = update_option( 'WPLANG', $locale ); $result = update_option( 'WPLANG', $locale );
// If it failed, try to install the language pack // If it failed, try to install the language pack.
if ( ! $result && function_exists( 'switch_to_locale' ) ) { if ( ! $result && function_exists( 'switch_to_locale' ) ) {
// Check if the language file exists // Check if the language file exists.
$lang_dir = WP_CONTENT_DIR . '/languages/'; $lang_dir = WP_CONTENT_DIR . '/languages/';
$lang_file = $lang_dir . $locale . '.po'; $lang_file = $lang_dir . $locale . '.po';
// If language file doesn't exist, try to install it // If language file doesn't exist, try to install it.
if ( ! file_exists( $lang_file ) ) { if ( ! file_exists( $lang_file ) ) {
$install_result = helix_install_language_pack( $locale ); $install_result = helix_install_language_pack( $locale );
if ( $install_result ) { if ( $install_result ) {
// Try updating the option again // Try updating the option again.
$result = update_option( 'WPLANG', $locale ); $result = update_option( 'WPLANG', $locale );
} }
} }
@ -375,43 +374,43 @@ function helix_update_language_setting( $locale ) {
* @return bool True if update succeeded, false otherwise. * @return bool True if update succeeded, false otherwise.
*/ */
function helix_update_timezone_setting( $timezone_value ) { function helix_update_timezone_setting( $timezone_value ) {
// Check if it's a GMT offset (starts with UTC+ or UTC- or is numeric) // Check if it's a GMT offset (starts with UTC+ or UTC- or is numeric).
if ( preg_match( '/^UTC[+-](\d+(?:\.\d+)?)$/', $timezone_value, $matches ) ) { if ( preg_match( '/^UTC[+-](\d+(?:\.\d+)?)$/', $timezone_value, $matches ) ) {
// Extract the numeric offset // Extract the numeric offset.
$offset = $matches[1]; $offset = $matches[1];
$is_negative = strpos( $timezone_value, 'UTC-' ) === 0; $is_negative = strpos( $timezone_value, 'UTC-' ) === 0;
// Convert to numeric value (negative if UTC-) // Convert to numeric value (negative if UTC-).
$numeric_offset = $is_negative ? -1 * floatval( $offset ) : floatval( $offset ); $numeric_offset = $is_negative ? -1 * floatval( $offset ) : floatval( $offset );
// Save to gmt_offset option // Save to gmt_offset option.
$result = update_option( 'gmt_offset', $numeric_offset ); $result = update_option( 'gmt_offset', $numeric_offset );
// Clear the timezone_string option since we're using GMT offset // Clear the timezone_string option since we're using GMT offset.
if ( $result ) { if ( $result ) {
update_option( 'timezone_string', '' ); update_option( 'timezone_string', '' );
} }
return $result; return $result;
} elseif ( is_numeric( $timezone_value ) ) { } elseif ( is_numeric( $timezone_value ) ) {
// Direct numeric offset (like "5.5") // Direct numeric offset (like "5.5").
$numeric_offset = floatval( $timezone_value ); $numeric_offset = floatval( $timezone_value );
// Save to gmt_offset option // Save to gmt_offset option.
$result = update_option( 'gmt_offset', $numeric_offset ); $result = update_option( 'gmt_offset', $numeric_offset );
// Clear the timezone_string option since we're using GMT offset // Clear the timezone_string option since we're using GMT offset.
if ( $result ) { if ( $result ) {
update_option( 'timezone_string', '' ); update_option( 'timezone_string', '' );
} }
return $result; return $result;
} else { } else {
// City-based timezone (like "Asia/Kolkata") // City-based timezone (like "Asia/Kolkata").
// Save to timezone_string option // Save to timezone_string option.
$result = update_option( 'timezone_string', $timezone_value ); $result = update_option( 'timezone_string', $timezone_value );
// Clear the gmt_offset option since we're using city-based timezone // Clear the gmt_offset option since we're using city-based timezone.
if ( $result ) { if ( $result ) {
update_option( 'gmt_offset', '' ); update_option( 'gmt_offset', '' );
} }
@ -429,44 +428,44 @@ function helix_update_timezone_setting( $timezone_value ) {
function helix_get_available_languages() { function helix_get_available_languages() {
$language_options = array(); $language_options = array();
// Add English (United States) as default // Add English (United States) as default.
$language_options[] = array( $language_options[] = array(
'value' => '', 'value' => '',
'label' => 'English (United States)' 'label' => 'English (United States)',
); );
// First, try to get installed languages // First, try to get installed languages.
if ( function_exists( 'get_available_languages' ) ) { if ( function_exists( 'get_available_languages' ) ) {
$installed_languages = get_available_languages(); $installed_languages = get_available_languages();
} else { } else {
$installed_languages = array(); $installed_languages = array();
} }
// Always try to include the required file first // Always try to include the required file first.
if ( ! function_exists( 'wp_get_available_translations' ) && file_exists( ABSPATH . 'wp-admin/includes/translation-install.php' ) ) { if ( ! function_exists( 'wp_get_available_translations' ) && file_exists( ABSPATH . 'wp-admin/includes/translation-install.php' ) ) {
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); require_once ABSPATH . 'wp-admin/includes/translation-install.php';
} }
// Get all available translations (including uninstalled ones) // Get all available translations (including uninstalled ones).
if ( function_exists( 'wp_get_available_translations' ) ) { if ( function_exists( 'wp_get_available_translations' ) ) {
$available_translations = wp_get_available_translations(); $available_translations = wp_get_available_translations();
// Add all available languages // Add all available languages.
foreach ( $available_translations as $locale => $translation_data ) { foreach ( $available_translations as $locale => $translation_data ) {
$label = isset( $translation_data['native_name'] ) ? $translation_data['native_name'] : $locale; $label = isset( $translation_data['native_name'] ) ? $translation_data['native_name'] : $locale;
// Mark installed languages differently // Mark installed languages differently.
$is_installed = in_array( $locale, $installed_languages ); $is_installed = in_array( $locale, $installed_languages, true );
$display_label = $is_installed ? $label : $label . ' (Not Installed)'; $display_label = $is_installed ? $label : $label . ' (Not Installed)';
$language_options[] = array( $language_options[] = array(
'value' => $locale, 'value' => $locale,
'label' => $display_label, 'label' => $display_label,
'installed' => $is_installed 'installed' => $is_installed,
); );
} }
} else { } else {
// Fallback to common languages if wp_get_available_translations is still not available // Fallback to common languages if wp_get_available_translations is still not available.
$language_options = helix_get_fallback_languages( $installed_languages ); $language_options = helix_get_fallback_languages( $installed_languages );
} }
@ -481,7 +480,7 @@ function helix_get_available_languages() {
* @return bool True if installation succeeded, false otherwise. * @return bool True if installation succeeded, false otherwise.
*/ */
function helix_install_language_pack( $locale ) { function helix_install_language_pack( $locale ) {
// Make sure we have the required functions // Make sure we have the required functions.
if ( ! function_exists( 'wp_download_language_pack' ) ) { if ( ! function_exists( 'wp_download_language_pack' ) ) {
if ( file_exists( ABSPATH . 'wp-admin/includes/translation-install.php' ) ) { if ( file_exists( ABSPATH . 'wp-admin/includes/translation-install.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php'; require_once ABSPATH . 'wp-admin/includes/translation-install.php';
@ -490,7 +489,7 @@ function helix_install_language_pack( $locale ) {
} }
} }
// Make sure we have the filesystem API // Make sure we have the filesystem API.
if ( ! function_exists( 'request_filesystem_credentials' ) ) { if ( ! function_exists( 'request_filesystem_credentials' ) ) {
if ( file_exists( ABSPATH . 'wp-admin/includes/file.php' ) ) { if ( file_exists( ABSPATH . 'wp-admin/includes/file.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/file.php';
@ -499,17 +498,17 @@ function helix_install_language_pack( $locale ) {
} }
} }
// Check if the function is now available // Check if the function is now available.
if ( ! function_exists( 'wp_download_language_pack' ) ) { if ( ! function_exists( 'wp_download_language_pack' ) ) {
return false; return false;
} }
// Check if filesystem API is available // Check if filesystem API is available.
if ( ! function_exists( 'request_filesystem_credentials' ) ) { if ( ! function_exists( 'request_filesystem_credentials' ) ) {
return false; return false;
} }
// Get available translations to verify the language exists // Get available translations to verify the language exists.
if ( function_exists( 'wp_get_available_translations' ) ) { if ( function_exists( 'wp_get_available_translations' ) ) {
$available_translations = wp_get_available_translations(); $available_translations = wp_get_available_translations();
@ -518,7 +517,7 @@ function helix_install_language_pack( $locale ) {
} }
} }
// Try to download and install the language pack // Try to download and install the language pack.
try { try {
$download_result = wp_download_language_pack( $locale ); $download_result = wp_download_language_pack( $locale );
@ -527,8 +526,8 @@ function helix_install_language_pack( $locale ) {
return false; return false;
} }
// Verify the language file now exists // Verify the language file now exists.
$lang_dir = WP_CONTENT_DIR . '/languages/'; $lang_dir = WP_CONTENT_DIR . '/languages/';
$lang_file = $lang_dir . $locale . '.po'; $lang_file = $lang_dir . $locale . '.po';
if ( file_exists( $lang_file ) ) { if ( file_exists( $lang_file ) ) {
@ -536,7 +535,6 @@ function helix_install_language_pack( $locale ) {
} else { } else {
return false; return false;
} }
} catch ( Exception $e ) { } catch ( Exception $e ) {
return false; return false;
} }
@ -552,7 +550,7 @@ function helix_install_language_pack( $locale ) {
function helix_get_fallback_languages( $installed_languages = array() ) { function helix_get_fallback_languages( $installed_languages = array() ) {
$language_options = array(); $language_options = array();
// Common languages as fallback // Common languages as fallback.
$common_languages = array( $common_languages = array(
'en_GB' => 'English (United Kingdom)', 'en_GB' => 'English (United Kingdom)',
'es_ES' => 'Español', 'es_ES' => 'Español',
@ -561,28 +559,28 @@ function helix_get_fallback_languages( $installed_languages = array() ) {
'it_IT' => 'Italiano', 'it_IT' => 'Italiano',
'pt_BR' => 'Português do Brasil', 'pt_BR' => 'Português do Brasil',
'ru_RU' => 'Русский', 'ru_RU' => 'Русский',
'ja' => '日本語', 'ja' => '日本語',
'zh_CN' => '简体中文', 'zh_CN' => '简体中文',
'ar' => 'العربية', 'ar' => 'العربية',
'hi_IN' => 'हिन्दी', 'hi_IN' => 'हिन्दी',
'ko_KR' => '한국어', 'ko_KR' => '한국어',
'nl_NL' => 'Nederlands', 'nl_NL' => 'Nederlands',
'sv_SE' => 'Svenska', 'sv_SE' => 'Svenska',
'da_DK' => 'Dansk', 'da_DK' => 'Dansk',
'fi' => 'Suomi', 'fi' => 'Suomi',
'no' => 'Norsk', 'no' => 'Norsk',
'pl_PL' => 'Polski', 'pl_PL' => 'Polski',
'tr_TR' => 'Türkçe', 'tr_TR' => 'Türkçe',
); );
foreach ( $common_languages as $code => $name ) { foreach ( $common_languages as $code => $name ) {
$is_installed = in_array( $code, $installed_languages ); $is_installed = in_array( $code, $installed_languages, true );
$display_label = $is_installed ? $name : $name . ' (Not Installed)'; $display_label = $is_installed ? $name : $name . ' (Not Installed)';
$language_options[] = array( $language_options[] = array(
'value' => $code, 'value' => $code,
'label' => $display_label, 'label' => $display_label,
'installed' => $is_installed 'installed' => $is_installed,
); );
} }
@ -598,21 +596,21 @@ function helix_get_fallback_languages( $installed_languages = array() ) {
function helix_get_available_timezones() { function helix_get_available_timezones() {
$timezone_options = array(); $timezone_options = array();
// Use WordPress core function to get timezone choices // Use WordPress core function to get timezone choices.
if ( function_exists( 'wp_timezone_choice' ) ) { if ( function_exists( 'wp_timezone_choice' ) ) {
// Get the HTML output from wp_timezone_choice // Get the HTML output from wp_timezone_choice.
$timezone_html = wp_timezone_choice( get_option( 'timezone_string', 'UTC' ) ); $timezone_html = wp_timezone_choice( get_option( 'timezone_string', 'UTC' ) );
// Parse the HTML to extract option values and labels // Parse the HTML to extract option values and labels.
if ( preg_match_all( '/<option[^>]*value=["\']([^"\']*)["\'][^>]*>([^<]*)<\/option>/', $timezone_html, $matches, PREG_SET_ORDER ) ) { if ( preg_match_all( '/<option[^>]*value=["\']([^"\']*)["\'][^>]*>([^<]*)<\/option>/', $timezone_html, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) { foreach ( $matches as $match ) {
$value = $match[1]; $value = $match[1];
$label = trim( $match[2] ); $label = trim( $match[2] );
// Skip empty values // Skip empty values.
if ( ! empty( $value ) || $value === '0' ) { if ( ! empty( $value ) || '0' === $value ) {
$timezone_options[] = array( $timezone_options[] = array(
'value' => $value, 'value' => $value,
'label' => $label 'label' => $label,
); );
} }
} }