*/ public static function defaults(): array { return array( 'enabled' => true, 'show_on_load' => true, 'show_online' => true, 'show_offline' => true, 'dismissible' => true, 'exclude_administrators' => false, 'position' => 'bottom-left', 'animation' => 'slide-up', 'skin' => 'light', 'accent_color' => '#2563eb', 'success_color' => '#16a34a', 'error_color' => '#dc2626', 'surface_color' => '#ffffff', 'text_color' => '#111827', 'online_icon_svg' => '', 'offline_icon_svg' => '', 'checking_icon_svg' => '', 'border_radius' => 18, 'max_width' => 420, 'online_title' => __( 'You are back online', 'connectcue' ), 'online_message' => __( 'Your internet connection has been restored.', 'connectcue' ), 'offline_title' => __( 'You are offline', 'connectcue' ), 'offline_message' => __( 'Check your connection. We will keep monitoring it.', 'connectcue' ), 'checking_title' => __( 'Checking connection', 'connectcue' ), 'checking_message' => __( 'Confirming that the internet is available.', 'connectcue' ), /* translators: %s: Number of seconds remaining before the next connection check. */ 'retry_text' => __( 'Checking again in %s seconds', 'connectcue' ), 'online_auto_hide' => 4500, 'offline_auto_hide' => 0, 'enable_health_check' => false, 'health_check_interval' => 60, 'health_check_timeout' => 5, 'health_check_url' => '', 'debug' => false, 'remove_data_on_uninstall'=> false, ); } /** * Return saved settings merged with defaults. * * @return array */ public static function get_all(): array { $saved = get_option( self::OPTION_NAME, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } $settings = wp_parse_args( $saved, self::defaults() ); /** * Filters the effective plugin settings. * * @param array $settings Effective settings. */ $filtered = apply_filters( 'connectcue_settings', $settings ); // Also validate historical/migrated settings and filter results, not only form submissions. return self::normalize_for_runtime( is_array( $filtered ) ? $filtered : $settings ); } /** * Get one setting. * * @param string $key Setting key. * @param mixed $fallback Fallback value. * @return mixed */ public static function get( string $key, mixed $fallback = null ): mixed { $settings = self::get_all(); return $settings[ $key ] ?? $fallback; } /** * Add defaults without overwriting existing values. */ public static function ensure_defaults(): void { $current = get_option( self::OPTION_NAME, null ); if ( null === $current || false === $current || ! is_array( $current ) ) { add_option( self::OPTION_NAME, self::defaults(), '', false ); return; } $merged = wp_parse_args( $current, self::defaults() ); if ( $merged !== $current ) { update_option( self::OPTION_NAME, $merged, false ); } } /** * Keep CSS property values in the exact hexadecimal color grammar. * Escaping HTML attributes alone does not protect CSS syntax. * * @param mixed $value Untrusted saved value. * @param string $fallback Known-safe default. */ public static function safe_css_color( mixed $value, string $fallback ): string { $color = is_string( $value ) ? sanitize_hex_color( $value ) : null; return $color ?: $fallback; } /** * Enforce an integer range before emitting a CSS length. */ public static function safe_css_pixels( mixed $value, int $minimum, int $maximum ): int { if ( ! is_int( $value ) && ! is_float( $value ) && ! ( is_string( $value ) && preg_match( '/^\d+$/D', $value ) ) ) { return $minimum; } return self::clamp( (int) $value, $minimum, $maximum ); } /** * Validate saved values on read, including imported settings and filter results. * Keeps compatibility with pre-existing saved values without trusting their types. * * @param array $input Settings from storage or filters. * @return array */ private static function normalize_for_runtime( array $input ): array { $defaults = self::defaults(); $output = wp_parse_args( $input, $defaults ); foreach ( array( 'enabled', 'show_on_load', 'show_online', 'show_offline', 'dismissible', 'exclude_administrators', 'enable_health_check', 'debug', 'remove_data_on_uninstall' ) as $key ) { $output[ $key ] = is_scalar( $output[ $key ] ) ? (bool) $output[ $key ] : $defaults[ $key ]; } foreach ( array( 'accent_color', 'success_color', 'error_color', 'surface_color', 'text_color' ) as $key ) { $output[ $key ] = self::safe_css_color( $output[ $key ], $defaults[ $key ] ); } foreach ( array( 'position' => array( 'top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right' ), 'animation' => array( 'fade', 'slide-up', 'slide-down', 'slide-left', 'slide-right', 'scale' ), 'skin' => array( 'light', 'dark', 'glass', 'custom' ), ) as $key => $allowed ) { $output[ $key ] = is_string( $output[ $key ] ) && in_array( $output[ $key ], $allowed, true ) ? $output[ $key ] : $defaults[ $key ]; } foreach ( array( 'online_title', 'online_message', 'offline_title', 'offline_message', 'checking_title', 'checking_message', 'retry_text' ) as $key ) { $output[ $key ] = is_string( $output[ $key ] ) ? sanitize_text_field( $output[ $key ] ) : $defaults[ $key ]; } foreach ( array( 'online_icon_svg', 'offline_icon_svg', 'checking_icon_svg' ) as $key ) { $output[ $key ] = is_string( $output[ $key ] ) ? self::sanitize_svg( $output[ $key ] ) : ''; } foreach ( array( 'border_radius' => array( 0, 32 ), 'max_width' => array( 280, 640 ), 'online_auto_hide' => array( 0, 60000 ), 'offline_auto_hide' => array( 0, 60000 ), 'health_check_interval' => array( 15, 3600 ), 'health_check_timeout' => array( 2, 30 ), ) as $key => $limits ) { $value = $output[ $key ]; $output[ $key ] = ( is_int( $value ) || ( is_string( $value ) && preg_match( '/^\d+$/D', $value ) ) ) ? self::clamp( (int) $value, $limits[0], $limits[1] ) : $defaults[ $key ]; } $output['health_check_url'] = is_string( $output['health_check_url'] ) ? esc_url_raw( $output['health_check_url'], array( 'http', 'https' ) ) : ''; return $output; } /** * Sanitize settings submitted through the Settings API. * * @param mixed $input Raw input. * @return array */ public static function sanitize( mixed $input ): array { $input = is_array( $input ) ? $input : array(); $defaults = self::defaults(); $boolean_keys = array( 'enabled', 'show_on_load', 'show_online', 'show_offline', 'dismissible', 'exclude_administrators', 'enable_health_check', 'debug', 'remove_data_on_uninstall', ); $output = $defaults; foreach ( $boolean_keys as $key ) { $output[ $key ] = ! empty( $input[ $key ] ); } $output['position'] = self::allowed_value( ( is_string( $input['position'] ?? null ) ? $input['position'] : '' ), array( 'top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right' ), (string) $defaults['position'] ); $output['animation'] = self::allowed_value( ( is_string( $input['animation'] ?? null ) ? $input['animation'] : '' ), array( 'fade', 'slide-up', 'slide-down', 'slide-left', 'slide-right', 'scale' ), (string) $defaults['animation'] ); $output['skin'] = self::allowed_value( ( is_string( $input['skin'] ?? null ) ? $input['skin'] : '' ), array( 'light', 'dark', 'glass', 'custom' ), (string) $defaults['skin'] ); $color_keys = array( 'accent_color', 'success_color', 'error_color', 'surface_color', 'text_color' ); foreach ( $color_keys as $key ) { $color = sanitize_hex_color( ( is_string( $input[ $key ] ?? null ) ? $input[ $key ] : '' ) ); $output[ $key ] = $color ?: $defaults[ $key ]; } $text_keys = array( 'online_title', 'online_message', 'offline_title', 'offline_message', 'checking_title', 'checking_message', 'retry_text', ); foreach ( $text_keys as $key ) { $value = sanitize_text_field( ( is_string( $input[ $key ] ?? null ) ? $input[ $key ] : '' ) ); $output[ $key ] = '' !== $value ? $value : $defaults[ $key ]; } foreach ( array( 'online_icon_svg', 'offline_icon_svg', 'checking_icon_svg' ) as $key ) { $output[ $key ] = self::sanitize_svg( ( is_string( $input[ $key ] ?? null ) ? $input[ $key ] : '' ) ); } $output['border_radius'] = self::clamp( absint( $input['border_radius'] ?? $defaults['border_radius'] ), 0, 32 ); $output['max_width'] = self::clamp( absint( $input['max_width'] ?? $defaults['max_width'] ), 280, 640 ); $output['online_auto_hide'] = self::clamp( absint( $input['online_auto_hide'] ?? $defaults['online_auto_hide'] ), 0, 60000 ); $output['offline_auto_hide'] = self::clamp( absint( $input['offline_auto_hide'] ?? $defaults['offline_auto_hide'] ), 0, 60000 ); $output['health_check_interval'] = self::clamp( absint( $input['health_check_interval'] ?? $defaults['health_check_interval'] ), 15, 3600 ); $output['health_check_timeout'] = self::clamp( absint( $input['health_check_timeout'] ?? $defaults['health_check_timeout'] ), 2, 30 ); $output['health_check_url'] = esc_url_raw( ( is_string( $input['health_check_url'] ?? null ) ? $input['health_check_url'] : '' ), array( 'http', 'https' ) ); return $output; } /** * Return the effective SVG icon for a connection state. * * Custom SVG markup is preferred when configured; otherwise a premium * built-in icon is returned. * * @param string $status Connection state. * @param array $settings Effective settings. * @return string Sanitized SVG markup. */ public static function icon_svg( string $status, array $settings = array() ): string { $allowed_statuses = array( 'online', 'offline', 'checking' ); if ( ! in_array( $status, $allowed_statuses, true ) ) { $status = 'checking'; } if ( empty( $settings ) ) { $settings = self::get_all(); } $key = $status . '_icon_svg'; $custom = isset( $settings[ $key ] ) ? self::sanitize_svg( (string) $settings[ $key ] ) : ''; if ( '' !== $custom ) { return $custom; } $icons = self::default_icons(); return $icons[ $status ]; } /** * Premium dependency-free SVG icon set. * * @return array */ public static function default_icons(): array { return array( 'online' => '', 'offline' => '', 'checking' => '', ); } /** * Strictly limited SVG elements and attributes for sanitizing icon markup. * * @return array> */ public static function svg_allowed_tags(): array { $common = array( 'class' => true, 'fill' => true, 'opacity' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'stroke-dasharray' => true, 'stroke-dashoffset' => true, 'transform' => true, ); $allowed = array( 'svg' => $common + array( 'xmlns' => true, 'viewbox' => true, 'width' => true, 'height' => true, 'aria-hidden' => true, 'focusable' => true, ), 'g' => $common, 'path' => $common + array( 'd' => true, 'fill-rule' => true, 'clip-rule' => true ), 'circle' => $common + array( 'cx' => true, 'cy' => true, 'r' => true ), 'ellipse' => $common + array( 'cx' => true, 'cy' => true, 'rx' => true, 'ry' => true ), 'rect' => $common + array( 'x' => true, 'y' => true, 'rx' => true, 'ry' => true, 'width' => true, 'height' => true ), 'line' => $common + array( 'x1' => true, 'x2' => true, 'y1' => true, 'y2' => true ), 'polyline' => $common + array( 'points' => true ), 'polygon' => $common + array( 'points' => true ), ); return $allowed; } /** * Sanitize administrator-supplied inline SVG icon markup. * * Scriptable elements, external references and embedded styles are not * permitted. Invalid markup falls back to the built-in icon. */ public static function sanitize_svg( string $svg ): string { $svg = trim( $svg ); if ( '' === $svg ) { return ''; } if ( ! str_contains( strtolower( $svg ), ' 'dark', 'skin4' => 'glass', default => 'light', }; } private static function legacy_animation( string $animation ): string { return match ( $animation ) { 'slideTop' => 'slide-down', 'slideBottom' => 'slide-up', 'slideRight' => 'slide-right', 'slideLeft' => 'slide-left', default => 'fade', }; } }