diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 9a0990f50d..ec3e8db085 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -27,25 +27,800 @@ function neve_is_css_var( $value ) { return false; } - $hex = '#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})'; - $color_fn = '(?:rgb|rgba|hsl|hsla)\(\s*[0-9a-z.,%\/\s-]+\)'; - $keyword = '[a-z]+(?:-[a-z]+)*'; - $fallback = '(?:(?P>nv_var)|' . $hex . '|' . $color_fn . '|' . $keyword . ')'; - $pattern = '/^(?Pvar\(\s*--[a-z0-9_-]+\s*(?:,\s*' . $fallback . '\s*)?\))$/i'; + if ( ! preg_match( '/^var\((.*)\)$/iD', $value, $matches ) ) { + return false; + } + + $body = $matches[1]; + $depth = 0; + $comma_at = null; + $length = strlen( $body ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $body[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } elseif ( ',' === $character && 0 === $depth && null === $comma_at ) { + $comma_at = $index; + } + } + + if ( 0 !== $depth ) { + return false; + } + + $name = trim( null === $comma_at ? $body : substr( $body, 0, $comma_at ) ); + if ( ! preg_match( '/^--[a-z0-9_-]+$/i', $name ) ) { + return false; + } + + if ( null === $comma_at ) { + return true; + } + + $fallback = trim( substr( $body, $comma_at + 1 ) ); + if ( '' === $fallback ) { + return false; + } + + if ( neve_closes_css_declaration( $fallback ) ) { + return false; + } + + if ( neve_is_css_var( $fallback ) || (bool) preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $fallback ) ) { + return true; + } + + if ( neve_is_css_function_color( $fallback ) ) { + return true; + } + + return (bool) preg_match( '/^[a-z]+(?:-[a-z]+)*$/i', $fallback ) + && '' !== neve_sanitize_row_text_color( $fallback ); +} + +/** + * Check whether a value is a single CSS gradient function. + * + * @param string $value Value to check. + * + * @return bool + */ +function neve_is_css_gradient( string $value ): bool { + $value = trim( $value ); + if ( ! preg_match( '/^(?:repeating-)?(linear|radial|conic)-gradient\((.*)\)$/iD', $value, $matches ) ) { + return false; + } + + $body = $matches[2]; + if ( '' === trim( $body ) || ! preg_match( '/^[a-z0-9#%.,\s\/_()+\-]+$/i', $body ) ) { + return false; + } + + $arguments = array(); + $current = ''; + $depth = 0; + $length = strlen( $body ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $body[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( ',' === $character && 0 === $depth ) { + $arguments[] = trim( $current ); + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + $arguments[] = trim( $current ); + + if ( ! neve_is_css_gradient_stop( $arguments[0], $matches[1] ) ) { + if ( ! neve_is_css_gradient_prelude( $matches[1], $arguments[0] ) ) { + return false; + } + array_shift( $arguments ); + } + + if ( count( $arguments ) < 2 ) { + return false; + } + + $previous_was_stop = false; + $last_index = count( $arguments ) - 1; + foreach ( $arguments as $index => $argument ) { + if ( neve_is_css_gradient_stop( $argument, $matches[1] ) ) { + $previous_was_stop = true; + continue; + } + + if ( ! $previous_was_stop || $index === $last_index || ! neve_is_css_gradient_hint( $argument, $matches[1] ) ) { + return false; + } + + $previous_was_stop = false; + } + + return true; +} + +/** + * Get shared numeric patterns used by the CSS gradient validators. + * + * @return array + */ +function neve_css_gradient_patterns(): array { + $number = '[+-]?(?:\d+(?:\.\d+)?|\.\d+)'; + $unsigned_number = '(?:\d+(?:\.\d+)?|\.\d+)'; + $zero = '[+-]?(?:0+(?:\.0+)?|\.0+)'; + $length_units = 'px|em|rem|vw|vh|vi|vb|vmin|vmax|svw|svh|svi|svb|svmin|svmax|lvw|lvh|lvi|lvb|lvmin|lvmax|dvw|dvh|dvi|dvb|dvmin|dvmax|cqw|cqh|cqi|cqb|cqmin|cqmax|ch|ex|cap|ic|lh|rlh|cm|mm|in|pt|pc|q'; + $length_unit = '(?:' . $length_units . ')'; + $absolute_length = '(?:' . $zero . '|' . $number . $length_unit . ')'; + $length = '(?:' . $absolute_length . '|' . $number . '%)'; + $nonnegative_length = '(?:\+?(?:0+(?:\.0+)?|\.0+)|\+?' . $unsigned_number . '(?:%|' . $length_units . '))'; + $calc_length = 'calc\(\s*' . $length . '(?:\s+[+-]\s+' . $length . ')+\s*\)'; + + return array( + 'number' => $number, + 'unsigned_number' => $unsigned_number, + 'zero' => $zero, + 'length_unit' => $length_unit, + 'absolute_length' => $absolute_length, + 'simple_length' => $length, + 'nonnegative_length' => $nonnegative_length, + 'length' => '(?:' . $length . '|' . $calc_length . ')', + ); +} + +/** + * Split a CSS value on a delimiter that is outside parentheses. + * + * @param string $value Value to split. + * @param string $delimiter Single-character delimiter. + * + * @return array|false + */ +function neve_css_split_top_level( string $value, string $delimiter = ',' ) { + $parts = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( $delimiter === $character && 0 === $depth ) { + $parts[] = trim( $current ); + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + + $parts[] = trim( $current ); + + return $parts; +} + +/** + * Read a leading CSS function and the suffix after its balanced closing parenthesis. + * + * @param string $value Value beginning with a CSS function. + * + * @return array{name: string, value: string, body: string, suffix: string}|false + */ +function neve_css_leading_function( string $value ) { + if ( ! preg_match( '/^([a-z][a-z0-9-]*)\(/i', $value, $matches ) ) { + return false; + } + + $depth = 0; + $length = strlen( $value ); + $start = strlen( $matches[1] ); + for ( $index = $start; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( 0 === $depth ) { + return array( + 'name' => strtolower( $matches[1] ), + 'value' => substr( $value, 0, $index + 1 ), + 'body' => substr( $value, $start + 1, $index - $start - 1 ), + 'suffix' => substr( $value, $index + 1 ), + ); + } + } + } + + return false; +} + +/** + * Get the supported CSS color interpolation method pattern. + * + * @return string + */ +function neve_css_gradient_interpolation_pattern(): string { + $rectangular = '(?:srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|lab|oklab|xyz|xyz-d50|xyz-d65)'; + $polar = '(?:hsl|hwb|lch|oklch)(?:\s+(?:shorter|longer|increasing|decreasing)\s+hue)?'; + $custom = '--[a-z0-9_-]+'; + + return 'in\s+(?:' . $rectangular . '|' . $polar . '|' . $custom . ')'; +} + +/** + * Match a gradient prelude with an optional color interpolation method. + * + * @param string $value Prelude value. + * @param string $base_pattern Direction, shape or position pattern. + * @param string $interpolation Color interpolation pattern. + * + * @return bool + */ +function neve_css_gradient_matches_prelude( string $value, string $base_pattern, string $interpolation ): bool { + if ( preg_match( '/^' . $base_pattern . '$/i', $value ) || preg_match( '/^' . $interpolation . '$/i', $value ) ) { + return true; + } + + if ( preg_match( '/^(' . $interpolation . ')\s+(.+)$/i', $value, $matches ) ) { + return (bool) preg_match( '/^' . $base_pattern . '$/i', $matches[2] ); + } + + if ( preg_match( '/^(.+)\s+(' . $interpolation . ')$/i', $value, $matches ) ) { + return (bool) preg_match( '/^' . $base_pattern . '$/i', $matches[1] ); + } + + return false; +} + +/** + * Check whether a gradient's optional first argument is a supported prelude. + * + * @param string $type Gradient type. + * @param string $value Prelude value. + * + * @return bool + */ +function neve_is_css_gradient_prelude( string $type, string $value ): bool { + $patterns = neve_css_gradient_patterns(); + $value = trim( $value ); + $number = $patterns['number']; + $zero = $patterns['zero']; + $angle = '(?:' . $zero . '|' . $number . '(?:deg|grad|rad|turn))'; + $dimension = $patterns['simple_length']; + $horizontal = '(?:left|center|right)'; + $vertical = '(?:top|center|bottom)'; + $position = '(?:(?:left|center|right|top|bottom|' . $dimension . ')|(?:' . $horizontal . '\s+' . $vertical . '|' . $vertical . '\s+' . $horizontal . ')|(?:' . $horizontal . '|' . $dimension . ')\s+(?:' . $vertical . '|' . $dimension . ')|(?:left|right)\s+' . $dimension . '\s+' . $vertical . '|' . $vertical . '\s+(?:left|right)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+' . $horizontal . '|' . $horizontal . '\s+(?:top|bottom)\s+' . $dimension . '|(?:left|right)\s+' . $dimension . '\s+(?:top|bottom)\s+' . $dimension . '|(?:top|bottom)\s+' . $dimension . '\s+(?:left|right)\s+' . $dimension . ')'; + $interpolation = neve_css_gradient_interpolation_pattern(); + + if ( 'linear' === strtolower( $type ) ) { + $direction = '(?:(?:left|right)(?:\s+(?:top|bottom))?|(?:top|bottom)(?:\s+(?:left|right))?)'; + $base = '(?:' . $angle . '|to\s+' . $direction . ')'; + + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); + } + + if ( 'radial' === strtolower( $type ) ) { + $unsigned_number = $patterns['unsigned_number']; + $extent = '(?:closest-side|closest-corner|farthest-side|farthest-corner)'; + $circle_radius = '(?:\+?(?:0+(?:\.0+)?|\.0+)|\+?' . $unsigned_number . $patterns['length_unit'] . ')'; + $ellipse_radius = $patterns['nonnegative_length']; + $size = '(?:' . $extent . '|' . $circle_radius . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')'; + $circle = '(?:circle(?:\s+(?:' . $extent . '|' . $circle_radius . '))?|(?:' . $extent . '|' . $circle_radius . ')\s+circle)'; + $ellipse = '(?:ellipse(?:\s+(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . '))?|(?:' . $extent . '|' . $ellipse_radius . '\s+' . $ellipse_radius . ')\s+ellipse)'; + $shape_size = '(?:' . $circle . '|' . $ellipse . ')'; + $base = '(?:(?:' . $shape_size . '|' . $size . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')'; + + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); + } + + $base = '(?:(?:from\s+' . $angle . ')(?:\s+at\s+' . $position . ')?|at\s+' . $position . ')'; + + return neve_css_gradient_matches_prelude( $value, $base, $interpolation ); +} + +/** + * Check whether a gradient stop contains a supported color and positions. + * + * @param string $value Gradient stop. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_stop( string $value, string $type ): bool { + $value = trim( $value ); + if ( preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?=\s|$)/i', $value, $matches ) ) { + return neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[0] ) ), $type ); + } + + $function = neve_css_leading_function( $value ); + if ( false !== $function && ( '' === $function['suffix'] || preg_match( '/^\s/', $function['suffix'] ) ) ) { + $is_color = 'var' === $function['name'] + ? neve_is_css_var( $function['value'] ) + : neve_is_css_gradient_function_color( $function['value'] ); + + return $is_color && neve_gradient_stop_has_valid_positions( $function['suffix'], $type ); + } + + if ( ! preg_match( '/^([a-z]+(?:-[a-z]+)*)(?=\s|$)/i', $value, $matches ) ) { + return false; + } + + return '' !== neve_sanitize_row_text_color( $matches[1] ) + && neve_gradient_stop_has_valid_positions( substr( $value, strlen( $matches[1] ) ), $type ); +} + +/** + * Check a function color used by a gradient stop. + * + * @param string $value Function color. + * + * @return bool + */ +function neve_is_css_gradient_function_color( string $value ): bool { + $function = neve_css_leading_function( trim( $value ) ); + if ( false === $function || '' !== trim( $function['suffix'] ) ) { + return false; + } + + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $component = $number . '%?'; + $alpha = $number . '%?'; + $hue = $number . '(?:deg|grad|rad|turn)?'; + $rgb_legacy = '(?:' . $number . '(?:\s*,\s*' . $number . '){2}|' . $number . '%(?:\s*,\s*' . $number . '%){2})'; + $rgb = '(?:rgb|rgba)\(\s*' . $rgb_legacy . '(?:\s*,\s*' . $alpha . ')?\s*\)'; + $rgb_modern = '(?:rgb|rgba)\(\s*' . $component . '\s+' . $component . '\s+' . $component . '(?:\s*\/\s*' . $alpha . ')?\s*\)'; + $hsl = '(?:hsl|hsla)\(\s*' . $hue . '\s*,\s*' . $number . '%\s*,\s*' . $number . '%(?:\s*,\s*' . $alpha . ')?\s*\)'; + $hsl_modern = '(?:hsl|hsla)\(\s*' . $hue . '\s+' . $number . '%\s+' . $number . '%(?:\s*\/\s*' . $alpha . ')?\s*\)'; + + if ( (bool) preg_match( '/^(?:' . $rgb . '|' . $rgb_modern . '|' . $hsl . '|' . $hsl_modern . ')$/i', $value ) ) { + return true; + } + + $none_percentage = '(?:none|' . $number . '%)'; + $none_component = '(?:none|' . $component . ')'; + $none_hue = '(?:none|' . $hue . ')'; + $optional_alpha = '(?:\s*\/\s*' . $none_component . ')?'; + $modern_components = '\s+' . $none_component . '\s+' . $none_component . '\s+' . $none_component . $optional_alpha . '\s*'; + + switch ( $function['name'] ) { + case 'hwb': + return (bool) preg_match( '/^\s*' . $none_hue . '\s+' . $none_percentage . '\s+' . $none_percentage . $optional_alpha . '\s*$/i', $function['body'] ); + case 'lab': + case 'oklab': + return (bool) preg_match( '/^\s*' . $none_component . '\s+' . $none_component . '\s+' . $none_component . $optional_alpha . '\s*$/i', $function['body'] ); + case 'lch': + case 'oklch': + return (bool) preg_match( '/^\s*' . $none_component . '\s+' . $none_component . '\s+' . $none_hue . $optional_alpha . '\s*$/i', $function['body'] ); + case 'color': + $space = '(?:srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65|--[a-z0-9_-]+)'; + + return (bool) preg_match( '/^\s*' . $space . $modern_components . '$/i', $function['body'] ); + case 'device-cmyk': + return (bool) preg_match( '/^\s*' . $none_component . '(?:\s+' . $none_component . '){3}' . $optional_alpha . '\s*$/i', $function['body'] ); + case 'color-mix': + return neve_is_css_color_mix( $function['body'] ); + case 'light-dark': + $colors = neve_css_split_top_level( $function['body'] ); + + return false !== $colors && 2 === count( $colors ) + && neve_is_css_gradient_color( $colors[0] ) + && neve_is_css_gradient_color( $colors[1] ); + default: + return false; + } +} + +/** + * Check a complete color value used by another color function. + * + * @param string $value Color value. + * + * @return bool + */ +function neve_is_css_gradient_color( string $value ): bool { + $value = trim( $value ); + if ( (bool) preg_match( '/^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $value ) ) { + return true; + } + + if ( neve_is_css_var( $value ) || neve_is_css_gradient_function_color( $value ) ) { + return true; + } + + return (bool) preg_match( '/^[a-z]+(?:-[a-z]+)*$/i', $value ) + && '' !== neve_sanitize_row_text_color( $value ); +} + +/** + * Check a color-mix() body. + * + * @param string $value Function body. + * + * @return bool + */ +function neve_is_css_color_mix( string $value ): bool { + $arguments = neve_css_split_top_level( $value ); + if ( false === $arguments || 3 !== count( $arguments ) ) { + return false; + } + + if ( ! preg_match( '/^' . neve_css_gradient_interpolation_pattern() . '$/i', $arguments[0] ) ) { + return false; + } + + $weights = array(); + foreach ( array_slice( $arguments, 1 ) as $color_stop ) { + if ( neve_is_css_gradient_color( $color_stop ) ) { + $weights[] = null; + continue; + } + + if ( ! preg_match( '/^(.*\S)\s+(' . neve_css_gradient_patterns()['number'] . '%)$/', $color_stop, $matches ) + || ! neve_is_css_gradient_color( $matches[1] ) ) { + return false; + } + + $weight = (float) substr( $matches[2], 0, -1 ); + if ( $weight < 0 || $weight > 100 ) { + return false; + } + $weights[] = $weight; + } + + return null === $weights[0] || null === $weights[1] || 0.0 !== $weights[0] + $weights[1]; +} + +/** + * Check the optional one or two positions after a gradient stop color. + * + * @param string $value Position suffix. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_gradient_stop_has_valid_positions( string $value, string $type ): bool { + $value = trim( $value ); + if ( '' === $value ) { + return true; + } + + $positions = neve_css_split_top_level_whitespace( $value ); + if ( false === $positions || count( $positions ) > 2 ) { + return false; + } + + foreach ( $positions as $position ) { + if ( ! neve_is_css_gradient_dimension( $position, $type ) ) { + return false; + } + } + + return true; +} + +/** + * Check a color transition hint between two gradient stops. + * + * @param string $value Transition hint. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_hint( string $value, string $type ): bool { + return neve_is_css_gradient_dimension( trim( $value ), $type ); +} + +/** + * Split a CSS value on whitespace that is outside parentheses. + * + * @param string $value Value to split. + * + * @return array|false + */ +function neve_css_split_top_level_whitespace( string $value ) { + $parts = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( 0 === $depth && ctype_space( $character ) ) { + if ( '' !== $current ) { + $parts[] = $current; + $current = ''; + } + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth ) { + return false; + } + + if ( '' !== $current ) { + $parts[] = $current; + } + + return $parts; +} + +/** + * Check a gradient stop dimension, including supported CSS math functions. + * + * @param string $value Dimension value. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_dimension( string $value, string $type ): bool { + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $zero = $patterns['zero']; + $direct = 'conic' === strtolower( $type ) + ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' + : $patterns['simple_length']; + + if ( (bool) preg_match( '/^' . $direct . '$/i', trim( $value ) ) ) { + return true; + } + + return neve_is_css_gradient_math_function( trim( $value ), $type ); +} + +/** + * Check a calc(), min(), max(), clamp() or var() dimension. + * + * @param string $value CSS function. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_math_function( string $value, string $type ): bool { + $value_type = neve_css_gradient_math_function_type( $value, $type ); + + return false !== $value_type && 'number' !== $value_type; +} + +/** + * Get the result type of a CSS math function. + * + * @param string $value CSS function. + * @param string $type Gradient type. + * + * @return string|false + */ +function neve_css_gradient_math_function_type( string $value, string $type ) { + $function = neve_css_leading_function( $value ); + if ( false === $function || '' !== trim( $function['suffix'] ) ) { + return false; + } + + if ( 'var' === $function['name'] ) { + $arguments = neve_css_split_top_level( $function['body'] ); + if ( false === $arguments || count( $arguments ) > 2 || ! preg_match( '/^\s*--[a-z0-9_-]+\s*$/i', $arguments[0] ) ) { + return false; + } + + if ( 2 === count( $arguments ) ) { + $fallback_type = neve_css_gradient_math_expression_type( $arguments[1], $type ); + if ( false === $fallback_type || 'number' === $fallback_type ) { + return false; + } + } + + return 'unknown'; + } + + if ( ! in_array( $function['name'], array( 'calc', 'min', 'max', 'clamp' ), true ) ) { + return false; + } + + $arguments = neve_css_split_top_level( $function['body'] ); + if ( false === $arguments || in_array( '', $arguments, true ) ) { + return false; + } + + if ( ( 'calc' === $function['name'] && 1 !== count( $arguments ) ) + || ( 'clamp' === $function['name'] && 3 !== count( $arguments ) ) ) { + return false; + } + + $result_type = false; + foreach ( $arguments as $argument ) { + $argument_type = neve_css_gradient_math_expression_type( $argument, $type ); + if ( false === $argument_type ) { + return false; + } + + if ( false === $result_type || 'unknown' === $result_type ) { + $result_type = $argument_type; + continue; + } + + if ( 'unknown' !== $argument_type && $argument_type !== $result_type ) { + return false; + } + } + + return $result_type; +} + +/** + * Check an arithmetic expression inside a CSS math function. + * + * @param string $value Expression. + * @param string $type Gradient type. + * + * @return bool + */ +function neve_is_css_gradient_math_expression( string $value, string $type ): bool { + return false !== neve_css_gradient_math_expression_type( $value, $type ); +} + +/** + * Get the result type of an arithmetic expression inside a CSS math function. + * + * @param string $value Expression. + * @param string $type Gradient type. + * + * @return string|false + */ +function neve_css_gradient_math_expression_type( string $value, string $type ) { + $value = trim( $value ); + if ( '' === $value ) { + return false; + } + + $operands = array(); + $operators = array(); + $current = ''; + $depth = 0; + $length = strlen( $value ); + for ( $index = 0; $index < $length; $index++ ) { + $character = $value[ $index ]; + if ( '(' === $character ) { + $depth++; + } elseif ( ')' === $character ) { + $depth--; + if ( $depth < 0 ) { + return false; + } + } + + if ( 0 === $depth && in_array( $character, array( '+', '-', '/' ), true ) ) { + $trimmed = trim( $current ); + if ( '' === $trimmed ) { + $current .= $character; + continue; + } + if ( in_array( $character, array( '+', '-' ), true ) + && ( ! ctype_space( $value[ $index - 1 ] ) || $index + 1 >= $length || ! ctype_space( $value[ $index + 1 ] ) ) ) { + return false; + } + $operands[] = $trimmed; + $operators[] = $character; + $current = ''; + continue; + } + + $current .= $character; + } + + if ( 0 !== $depth || '' === trim( $current ) ) { + return false; + } + $operands[] = trim( $current ); + + $result_type = neve_css_gradient_math_operand_type( $operands[0], $type ); + if ( false === $result_type ) { + return false; + } + + foreach ( $operators as $index => $operator ) { + $operand_type = neve_css_gradient_math_operand_type( $operands[ $index + 1 ], $type ); + if ( false === $operand_type ) { + return false; + } + + if ( '/' === $operator ) { + if ( 'dimension' === $operand_type ) { + if ( 'dimension' !== $result_type && 'unknown' !== $result_type ) { + return false; + } + $result_type = 'number'; + } elseif ( 'number' !== $operand_type && 'unknown' !== $operand_type ) { + return false; + } + continue; + } + + if ( 'unknown' === $result_type ) { + $result_type = $operand_type; + continue; + } + + if ( 'unknown' !== $operand_type && $operand_type !== $result_type ) { + return false; + } + } - return (bool) preg_match( $pattern, $value ); + return $result_type; } /** - * Check whether a value is a single CSS gradient function. + * Get the result type of one operand inside a CSS math expression. * - * @param string $value Value to check. + * @param string $value Operand. + * @param string $type Gradient type. * - * @return bool + * @return string|false */ -function neve_is_css_gradient( string $value ): bool { - // Recursion keeps parentheses balanced, so nothing can follow the closing one. - return (bool) preg_match( '/^(?:repeating-)?(?:linear|radial|conic)-gradient(\((?:[a-z0-9#%.,\s\/_-]|(?1))*\))$/i', trim( $value ) ); +function neve_css_gradient_math_operand_type( string $value, string $type ) { + $value = trim( $value ); + $patterns = neve_css_gradient_patterns(); + $number = $patterns['number']; + $zero = $patterns['zero']; + $direct = 'conic' === strtolower( $type ) + ? '(?:' . $zero . '|' . $number . '(?:%|deg|grad|rad|turn))' + : $patterns['simple_length']; + + if ( (bool) preg_match( '/^' . $number . '$/i', $value ) ) { + return 'number'; + } + + if ( (bool) preg_match( '/^' . $direct . '$/i', $value ) ) { + return 'dimension'; + } + + if ( strlen( $value ) > 2 && '(' === $value[0] && ')' === $value[ strlen( $value ) - 1 ] ) { + return neve_css_gradient_math_expression_type( substr( $value, 1, -1 ), $type ); + } + + return neve_css_gradient_math_function_type( $value, $type ); } /** @@ -56,7 +831,36 @@ function neve_is_css_gradient( string $value ): bool { * @return bool */ function neve_is_css_function_color( string $value ): bool { - return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*[-0-9.% ,]+\)$/', trim( $value ) ); + return neve_is_css_gradient_function_color( trim( $value ) ); +} + +/** + * Whether a value can close the CSS declaration it is printed in. + * + * JSON strings and arrays are checked value by value, so their own braces do not count. + * + * @param mixed $value Stored or incoming value. + * + * @return bool + */ +function neve_closes_css_declaration( $value ): bool { + if ( is_string( $value ) ) { + $decoded = json_decode( $value, true ); + + return is_array( $decoded ) ? neve_closes_css_declaration( $decoded ) : (bool) preg_match( '/[;{}<>]/', $value ); + } + + if ( ! is_array( $value ) ) { + return false; + } + + foreach ( $value as $key => $item ) { + if ( ( is_string( $key ) && (bool) preg_match( '/[;{}<>]/', $key ) ) || neve_closes_css_declaration( $item ) ) { + return true; + } + } + + return false; } /** @@ -86,10 +890,19 @@ function neve_sanitize_colors( $value ) { return trim( $value ); } + if ( preg_match( '/^#(?:[0-9a-f]{4}|[0-9a-f]{8})$/i', trim( $value ), $match ) ) { + return $match[0]; + } + // Is this an rgba color or a hex? $mode = ( false === strpos( $value, 'rgba' ) ) ? 'hex' : 'rgba'; if ( 'rgba' === $mode ) { - return neve_sanitize_rgba( $value ); + $component = '[+-]?(?:\d+(?:\.\d*)?|\.\d+)%?'; + if ( ! preg_match( '/^rgba\(\s*' . $component . '(?:\s*,\s*' . $component . '){2,3}\s*\)/', trim( $value ), $match ) ) { + return ''; + } + + return neve_sanitize_rgba( $match[0] ); } $hex_color = sanitize_hex_color( $value ); @@ -97,6 +910,60 @@ function neve_sanitize_colors( $value ) { return null === $hex_color ? '' : $hex_color; } +/** + * Sanitize row text colors, which historically allow CSS named colors. + * + * @param mixed $value Row text color. + * + * @return string + */ +function neve_sanitize_row_text_color( $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + if ( '' === $value ) { + return ''; + } + + $color = neve_sanitize_colors( $value ); + if ( '' !== $color ) { + return $color; + } + + if ( neve_closes_css_declaration( $value ) ) { + return ''; + } + + $named_colors = explode( + ' ', + 'aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen currentcolor transparent' + ); + $sanitized = strtolower( sanitize_text_field( $value ) ); + + return in_array( $sanitized, $named_colors, true ) ? $sanitized : ''; +} + +/** + * Sanitize a font family before it is stored or printed in CSS. + * + * @param mixed $value Font family value. + * + * @return string + */ +function neve_sanitize_font_family( $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + if ( (bool) preg_match( '/[;{}<>]/', $value ) ) { + return ''; + } + + $value = sanitize_text_field( $value ); + + return neve_closes_css_declaration( $value ) ? '' : $value; +} + /** * Sanitize rgba color. * @@ -159,9 +1026,38 @@ function neve_sanitize_range_value( $input ) { $range_value['desktop'] = isset( $range_value['desktop'] ) && is_numeric( $range_value['desktop'] ) ? floatval( $range_value['desktop'] ) : ''; $range_value['tablet'] = isset( $range_value['tablet'] ) && is_numeric( $range_value['tablet'] ) ? floatval( $range_value['tablet'] ) : ''; $range_value['mobile'] = isset( $range_value['mobile'] ) && is_numeric( $range_value['mobile'] ) ? floatval( $range_value['mobile'] ) : ''; + if ( isset( $range_value['suffix'] ) ) { + $range_value['suffix'] = neve_sanitize_css_suffix( $range_value['suffix'] ); + } return wp_json_encode( $range_value ); } +/** + * Sanitize a CSS unit stored by a responsive range control. + * + * @param mixed $unit CSS unit. + * + * @return string + */ +function neve_sanitize_css_unit( $unit ) { + if ( ! is_string( $unit ) || ! in_array( $unit, array( 'px', 'em', 'rem', 'vh', '%', '—' ), true ) ) { + return 'px'; + } + + return $unit; +} + +/** + * Sanitize a scalar or responsive CSS suffix. + * + * @param mixed $suffix CSS suffix value. + * + * @return string|array + */ +function neve_sanitize_css_suffix( $suffix ) { + return is_array( $suffix ) ? array_map( 'neve_sanitize_css_unit', $suffix ) : neve_sanitize_css_unit( $suffix ); +} + /** * Sanitize font weight values. * @@ -212,6 +1108,20 @@ function neve_sanitize_background( $value ) { return new WP_Error(); } + foreach ( array( 'imageUrl', 'colorValue', 'overlayColorValue' ) as $string_field ) { + if ( array_key_exists( $string_field, $value ) && ! is_string( $value[ $string_field ] ) ) { + return new WP_Error(); + } + } + + if ( isset( $value['focusPoint'] ) && ! is_array( $value['focusPoint'] ) ) { + return new WP_Error(); + } + + if ( isset( $value['overlayOpacity'] ) && ! is_numeric( $value['overlayOpacity'] ) ) { + return new WP_Error(); + } + if ( ! isset( $value['focusPoint'] ) ) { $value['focusPoint'] = [ 'x' => 0.5, @@ -248,22 +1158,263 @@ function neve_sanitize_background( $value ) { /** * Sanitize the button appearance control. * - * @param array $value the control value. + * @param mixed $value The control value. * - * @return array + * @return array */ function neve_sanitize_button_appearance( $value ) { + if ( ! is_array( $value ) ) { + return array(); + } + + $color_fields = array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ); + foreach ( $color_fields as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = 'transparent' === $value[ $field ] ? 'transparent' : neve_sanitize_colors( $value[ $field ] ); + } + } + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = neve_sanitize_button_dimension( $value[ $field ] ); + } + } + foreach ( array( 'shadowProperties', 'shadowPropertiesHover' ) as $field ) { + if ( array_key_exists( $field, $value ) ) { + $value[ $field ] = neve_sanitize_button_shadow_properties( $value[ $field ] ); + } + } + return $value; } +/** + * Sanitize button shadow offsets and blur. + * + * @param mixed $value Stored shadow properties. + * + * @return array{blur: int, width: int, height: int} + */ +function neve_sanitize_button_shadow_properties( $value ) { + $sanitized = array_fill_keys( array( 'blur', 'width', 'height' ), 0 ); + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $property ) { + if ( isset( $value[ $property ] ) && is_numeric( $value[ $property ] ) ) { + $sanitized[ $property ] = (int) $value[ $property ]; + } + } + $sanitized['blur'] = abs( $sanitized['blur'] ); + + return $sanitized; +} + +/** + * Sanitize the directional dimensions stored by a button appearance control. + * + * @param mixed $value Stored directional dimensions. + * + * @return array{top: int, right: int, bottom: int, left: int} + */ +function neve_sanitize_button_dimension( $value ) { + $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ); + if ( is_numeric( $value ) ) { + return array_fill_keys( array_keys( $sanitized ), absint( $value ) ); + } + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $direction ) { + if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { + $sanitized[ $direction ] = absint( $value[ $direction ] ); + } + } + + return $sanitized; +} + +/** + * Format a sanitized color as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored color value. + * + * @return string + */ +function neve_sanitize_color_css_rule( $css_prop, $value ) { + $color = 'transparent' === $value ? $value : neve_sanitize_colors( $value ); + + return '' === $color ? '' : sprintf( ' %s: %s;', $css_prop, $color ); +} + +/** + * Format a sanitized font family as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored font family. + * + * @return string + */ +function neve_sanitize_font_family_css_rule( $css_prop, $value ) { + $font_family = neve_sanitize_font_family( $value ); + if ( '' === $font_family ) { + return ''; + } + + \Neve\Views\Font_Manager::add_google_font( $font_family ); + + return sprintf( ' %s: %s;', $css_prop, \Neve\Core\Styles\Css_Prop::format_font_family_value( $font_family ) ); +} + +/** + * Format sanitized directional dimensions as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored directional dimensions. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{directional-prop: string, key: string, suffix?: string, is_responsive?: bool} $meta + * + * @return string + */ +function neve_sanitize_directional_css_rule( $css_prop, $value, $meta, $device ) { + $sanitized = neve_sanitize_directional_value( $value ); + if ( empty( array_filter( $sanitized, 'is_numeric' ) ) ) { + return ''; + } + + return \Neve\Core\Styles\Css_Prop::transform_directional_prop( $meta, $device, $sanitized, $css_prop, $meta['directional-prop'] ); +} + +/** + * Sanitize directional values before formatting them as CSS. + * + * @param mixed $value Stored directional dimensions. + * + * @return array + */ +function neve_sanitize_directional_value( $value ) { + $sanitized = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '' ); + if ( is_numeric( $value ) ) { + return array_fill_keys( array_keys( $sanitized ), (float) $value ); + } + if ( ! is_array( $value ) ) { + return $sanitized; + } + + foreach ( array_keys( $sanitized ) as $direction ) { + if ( isset( $value[ $direction ] ) && is_numeric( $value[ $direction ] ) ) { + $sanitized[ $direction ] = (float) $value[ $direction ]; + } + } + if ( isset( $value['unit'] ) ) { + $sanitized['unit'] = neve_sanitize_css_unit( $value['unit'] ); + } + + return $sanitized; +} + +/** + * Format a sanitized numeric value as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored numeric value. + * @param array $meta Dynamic selector metadata. + * @phpstan-param array{suffix?: string} $meta + * + * @return string + */ +function neve_sanitize_numeric_css_rule( $css_prop, $value, $meta ) { + $suffix = isset( $meta['suffix'] ) ? $meta['suffix'] : ''; + + return sprintf( ' %s: %d%s;', $css_prop, absint( $value ), $suffix ); +} + +/** + * Format a sanitized responsive range as a CSS rule. + * + * @param string $css_prop CSS property. + * @param mixed $value Stored range value for the current device. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{key: string, default?: mixed, as_json?: mixed} $meta + * + * @return string + */ +function neve_sanitize_responsive_range_css_rule( $css_prop, $value, $meta, $device ) { + $suffix = \Neve\Core\Styles\Css_Prop::get_suffix_responsive( $meta, $device ); + + return sprintf( '%s: %s%s;', $css_prop, (float) $value, $suffix ); +} + +/** + * Format sanitized button dimensions as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored directional dimensions. + * @param array $meta Dynamic selector metadata. + * @param string $device Current responsive device. + * @phpstan-param array{directional-prop: string, key: string, override?: int|string} $meta + * + * @return string + */ +function neve_sanitize_button_dimension_css_rule( $css_prop, $value, $meta, $device ) { + if ( isset( $meta['override'] ) ) { + return sprintf( '%s:%d;', $css_prop, absint( $meta['override'] ) ); + } + + return neve_sanitize_directional_css_rule( $css_prop, neve_sanitize_button_dimension( $value ), $meta, $device ); +} + +/** + * Format a sanitized button shadow as a CSS custom-property rule. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored shadow color. + * @param int $width Horizontal offset. + * @param int $height Vertical offset. + * @param int $blur Blur radius. + * + * @return string + */ +function neve_sanitize_shadow_css_rule( $css_prop, $value, $width, $height, $blur ) { + $color = neve_sanitize_colors( $value ); + if ( '' === $color ) { + $color = 'none'; + } + + return sprintf( '%1$s:%2$dpx %3$dpx %4$dpx %5$s;', $css_prop, $width, $height, $blur, $color ); +} + +/** + * Format a button shadow after normalizing its stored properties. + * + * @param string $css_prop CSS custom property. + * @param mixed $value Stored shadow color. + * @param mixed $properties Stored shadow properties. + * + * @return string + */ +function neve_sanitize_button_shadow_css_rule( $css_prop, $value, $properties ) { + $properties = neve_sanitize_button_shadow_properties( $properties ); + + return neve_sanitize_shadow_css_rule( $css_prop, $value, $properties['width'], $properties['height'], $properties['blur'] ); +} + /** * Sanitize the typography control. * - * @param array $value the control value. + * @param mixed $value The control value. * - * @return array + * @return array{}|array{fontWeight?: string|null, textTransform: string, fontSize: array, lineHeight: array, letterSpacing: array} */ function neve_sanitize_typography_control( $value ) { + if ( ! is_array( $value ) ) { + return array(); + } + $keys = [ 'lineHeight', 'letterSpacing', @@ -280,18 +1431,29 @@ function neve_sanitize_typography_control( $value ) { } // Font Weight. - if ( ! in_array( $value['fontWeight'], [ '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { + if ( isset( $value['fontWeight'] ) && ! in_array( $value['fontWeight'], [ 'none', 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900' ], true ) ) { $value['fontWeight'] = '300'; } // Text Transform. - if ( ! in_array( $value['textTransform'], [ 'none', 'uppercase', 'lowercase', 'capitalize' ], true ) ) { + if ( ! isset( $value['textTransform'] ) || ! in_array( $value['textTransform'], [ 'none', 'uppercase', 'lowercase', 'capitalize' ], true ) ) { $value['textTransform'] = 'none'; } - // Make sure we deal with arrays. + // Keep only numeric responsive values and allowlisted units. foreach ( [ 'letterSpacing', 'lineHeight', 'fontSize' ] as $value_type ) { - if ( ! is_array( $value[ $value_type ] ) ) { + if ( ! isset( $value[ $value_type ] ) || ! is_array( $value[ $value_type ] ) ) { $value[ $value_type ] = []; + continue; + } + + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + if ( isset( $value[ $value_type ][ $device ] ) && ! is_numeric( $value[ $value_type ][ $device ] ) ) { + unset( $value[ $value_type ][ $device ] ); + } + } + + if ( isset( $value[ $value_type ]['suffix'] ) ) { + $value[ $value_type ]['suffix'] = neve_sanitize_css_suffix( $value[ $value_type ]['suffix'] ); } } diff --git a/header-footer-grid/Core/Builder/Abstract_Builder.php b/header-footer-grid/Core/Builder/Abstract_Builder.php index 24173fec99..ccf84ec533 100644 --- a/header-footer-grid/Core/Builder/Abstract_Builder.php +++ b/header-footer-grid/Core/Builder/Abstract_Builder.php @@ -982,6 +982,25 @@ protected function get_row_featured_image( $image_url, $use_featured, $meta ) { return apply_filters( 'nv_builder_row_image_url', $image, $use_featured, $meta ); } + /** + * Print a row color variable after validating the stored value. + * + * @param string $css_prop CSS variable. + * @param mixed $value Stored color. + * + * @return string + */ + public function row_color_rule( $css_prop, $value ) { + if ( ! is_string( $value ) ) { + return ''; + } + + // The printed style is stripped of HTML as a whole; strip it here first so only CSS is checked. + $value = neve_sanitize_row_text_color( wp_strip_all_tags( $value ) ); + + return '' !== $value ? sprintf( ' %s: %s;', $css_prop, $value ) : ''; + } + /** * Method to generate css array for each row. * @@ -1033,12 +1052,14 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules['--rowbcolor'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::BORDER_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-light-bg)', + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ]; } $rules['--color'] = [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_' . self::TEXT_COLOR, Dynamic_Selector::META_DEFAULT => $default_colors['text'], + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ]; // Exclude the following rules for the header when the global header background is active. @@ -1061,6 +1082,7 @@ private function add_row_style( $row_index, $css_array = array() ) { '--bgcolor' => [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.colorValue', Dynamic_Selector::META_DEFAULT => $default_color, + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ], ] ); @@ -1071,7 +1093,8 @@ private function add_row_style( $row_index, $css_array = array() ) { $rules, [ '--overlaycolor' => [ - Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.overlayColorValue', + Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background.overlayColorValue', + Dynamic_Selector::META_FILTER => [ $this, 'row_color_rule' ], ], '--bgimage' => [ Dynamic_Selector::META_KEY => $this->control_id . '_' . $row_index . '_background', @@ -2118,6 +2141,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; @@ -2131,6 +2155,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; @@ -2144,6 +2169,7 @@ private function add_sidebar_styles( $css_array ) { Dynamic_Selector::META_DEFAULT => $default_sidebar_width, Dynamic_Selector::META_SUFFIX => 'responsive_suffix', Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_FILTER => 'neve_sanitize_responsive_range_css_rule', ], ], ]; diff --git a/header-footer-grid/Core/Components/Abstract_Component.php b/header-footer-grid/Core/Components/Abstract_Component.php index 7777a28f13..5a3f3eaa2a 100644 --- a/header-footer-grid/Core/Components/Abstract_Component.php +++ b/header-footer-grid/Core/Components/Abstract_Component.php @@ -669,6 +669,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::PADDING_ID ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_PADDING, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], '--margin' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::MARGIN_ID, @@ -676,6 +677,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::MARGIN_ID ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_MARGIN, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ]; @@ -729,6 +731,7 @@ public function add_style( array $css_array = array() ) { '--inheritedff' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FONT_FAMILY_ID, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FONT_FAMILY_ID ), + Dynamic_Selector::META_FILTER => 'neve_sanitize_font_family_css_rule', ], '--inheritedfw' => [ Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::TYPEFACE_ID . '.fontWeight', @@ -829,7 +832,7 @@ private function add_typography_controls() { 'transport' => 'postMessage', 'priority' => $priority + 1, 'type' => '\Neve\Customizer\Controls\React\Font_Family', - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'live_refresh_selector' => $this->default_typography_selector, 'live_refresh_css_prop' => array( 'cssVar' => [ diff --git a/header-footer-grid/Core/Components/Button.php b/header-footer-grid/Core/Components/Button.php index 6122547cf0..6fd967fc34 100644 --- a/header-footer-grid/Core/Components/Button.php +++ b/header-footer-grid/Core/Components/Button.php @@ -230,29 +230,35 @@ public function add_style( array $css_array = array() ) { '--primarybtnbg' => [ Dynamic_Selector::META_KEY => $id . '.background', Dynamic_Selector::META_DEFAULT => 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtncolor' => [ Dynamic_Selector::META_KEY => $id . '.text', Dynamic_Selector::META_DEFAULT => '#fff', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhoverbg' => [ Dynamic_Selector::META_KEY => $id . '.backgroundHover', Dynamic_Selector::META_DEFAULT => 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhovercolor' => [ Dynamic_Selector::META_KEY => $id . '.textHover', Dynamic_Selector::META_DEFAULT => '#fff', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $id . '.borderRadius', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $id . '.borderRadius', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], ]; if ( isset( $value['type'] ) && $value['type'] === 'outline' ) { $rules['--primarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $id . '.borderWidth', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $id . '.borderWidth', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } @@ -264,11 +270,8 @@ public function add_style( array $css_array = array() ) { if ( ! isset( $button_values['useShadow'] ) || empty( $button_values['useShadow'] ) ) { return sprintf( '%s:%s;', $css_prop, 'none' ); } - $blur = intval( $button_values['shadowProperties']['blur'] ); - $width = intval( $button_values['shadowProperties']['width'] ); - $height = intval( $button_values['shadowProperties']['height'] ); - return sprintf( '%s:%s;', $css_prop, sprintf( '%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $button_values['shadowProperties'] ?? array() ); }, ]; $rules['--primarybtnhovershadow'] = [ @@ -278,11 +281,8 @@ public function add_style( array $css_array = array() ) { if ( ! isset( $button_values['useShadowHover'] ) || empty( $button_values['useShadowHover'] ) ) { return sprintf( '%s:%s;', $css_prop, 'none' ); } - $blur = intval( $button_values['shadowPropertiesHover']['blur'] ); - $width = intval( $button_values['shadowPropertiesHover']['width'] ); - $height = intval( $button_values['shadowPropertiesHover']['height'] ); - return sprintf( '%s:%s;', $css_prop, sprintf( '%spx %spx %spx %s;', $width, $height, $blur, $value ) ); + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $button_values['shadowPropertiesHover'] ?? array() ); }, ]; diff --git a/header-footer-grid/Core/Components/MenuIcon.php b/header-footer-grid/Core/Components/MenuIcon.php index 26ce6369ee..c601f2df50 100644 --- a/header-footer-grid/Core/Components/MenuIcon.php +++ b/header-footer-grid/Core/Components/MenuIcon.php @@ -680,15 +680,23 @@ public function add_style( array $css_array = array() ) { $id = $this->get_id() . '_' . self::BUTTON_APPEARANCE; $rules = [ - '--bgcolor' => $id . '.background', - '--color' => $id . '.text', + '--bgcolor' => [ + Dynamic_Selector::META_KEY => $id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--color' => [ + Dynamic_Selector::META_KEY => $id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], '--borderradius' => [ - Dynamic_Selector::META_KEY => $id . '.borderRadius', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $id . '.borderRadius', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--borderwidth' => [ - Dynamic_Selector::META_KEY => $id . '.borderWidth', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $id . '.borderWidth', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], ]; @@ -713,6 +721,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => $this->default_label_margin_value, Dynamic_Selector::META_SUFFIX => 'responsive_unit', 'directional-prop' => Config::CSS_PROP_MARGIN, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ], ]; diff --git a/header-footer-grid/Core/Components/Search.php b/header-footer-grid/Core/Components/Search.php index 8be8acea09..899ccf064d 100644 --- a/header-footer-grid/Core/Components/Search.php +++ b/header-footer-grid/Core/Components/Search.php @@ -390,6 +390,7 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_SUFFIX => 'px', ], '--formfieldborderwidth' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_WIDTH, Dynamic_Selector::META_SUFFIX => 'px', @@ -397,6 +398,7 @@ public function add_style( array $css_array = array() ) { 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_RADIUS, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_RADIUS ), diff --git a/header-footer-grid/Core/Components/SearchResponsive.php b/header-footer-grid/Core/Components/SearchResponsive.php index 2e6dd1094d..be3ac03a39 100644 --- a/header-footer-grid/Core/Components/SearchResponsive.php +++ b/header-footer-grid/Core/Components/SearchResponsive.php @@ -571,12 +571,14 @@ public function add_style( array $css_array = array() ) { Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_FONT_SIZE ), ], '--formfieldborderwidth' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_WIDTH, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_WIDTH ), 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_KEY => $this->get_id() . '_' . self::FIELD_BORDER_RADIUS, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => SettingsManager::get_instance()->get_default( $this->get_id() . '_' . self::FIELD_BORDER_RADIUS ), diff --git a/header-footer-grid/Core/Components/Utility/SearchIconButton.php b/header-footer-grid/Core/Components/Utility/SearchIconButton.php index 34bf7dbb46..b056ec21fc 100644 --- a/header-footer-grid/Core/Components/Utility/SearchIconButton.php +++ b/header-footer-grid/Core/Components/Utility/SearchIconButton.php @@ -329,10 +329,22 @@ public function get_style( array $css_array = array() ) { $button_style_id = $this->component_id . '_' . self::BUTTON_STYLE; $rules = [ - '--primarybtnbg' => $button_style_id . '.background', - '--primarybtnhoverbg' => $button_style_id . '.background', - '--primarybtncolor' => $button_style_id . '.text', - '--primarybtnhovercolor' => $button_style_id . '.text', + '--primarybtnbg' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtnhoverbg' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtncolor' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], + '--primarybtnhovercolor' => [ + Dynamic_Selector::META_KEY => $button_style_id . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', + ], ]; $css_array[] = [ diff --git a/header-footer-grid/Traits/Core.php b/header-footer-grid/Traits/Core.php index 6542892595..1f47f805a9 100644 --- a/header-footer-grid/Traits/Core.php +++ b/header-footer-grid/Traits/Core.php @@ -128,11 +128,31 @@ public function sanitize_responsive_int_json( $input ) { * @return array */ public function sanitize_spacing_array( $input ) { - if ( is_array( $input ) ) { - return $input; + if ( ! is_array( $input ) ) { + return array(); + } + + foreach ( array( 'top', 'right', 'bottom', 'left' ) as $direction ) { + if ( isset( $input[ $direction ] ) && ! is_numeric( $input[ $direction ] ) ) { + unset( $input[ $direction ] ); + } + } + + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + if ( isset( $input[ $device ] ) ) { + $input[ $device ] = $this->sanitize_spacing_array( $input[ $device ] ); + } + $unit_key = $device . '-unit'; + if ( isset( $input[ $unit_key ] ) ) { + $input[ $unit_key ] = neve_sanitize_css_unit( $input[ $unit_key ] ); + } + } + + if ( isset( $input['unit'] ) ) { + $input['unit'] = neve_sanitize_css_unit( $input['unit'] ); } - return array(); + return $input; } /** diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index ff6c2b0598..b49e4dc196 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -236,6 +236,87 @@ protected function validation_issue( $code, $message, $field = '' ) { ); } + /** + * Run a background or color theme mod value through Neve's sanitizer for that kind of setting. + * + * @param string $key Theme mod key. + * @param mixed $value Incoming value. + * @return mixed Sanitized value; null when it is not a valid background or color, or can close its CSS declaration. An empty color stays empty. + */ + protected function sanitize_style_mod( string $key, $value ) { + if ( preg_match( '/_background$/', $key ) ) { + return $this->sanitize_background_mod( $value ); + } + + if ( preg_match( '/_button_appearance$/', $key ) ) { + if ( is_array( $value ) ) { + return $this->sanitize_button_appearance_mod( $value ); + } + if ( 'header_search_button_appearance' !== $key ) { + return null; + } + } + + if ( preg_match( '/_new_text_color$/', $key ) ) { + $color = neve_sanitize_row_text_color( $value ); + + return '' === $color && '' !== $value ? null : $color; + } + + if ( preg_match( '/_color$/', $key ) ) { + $color = neve_sanitize_colors( $value ); + + return '' === $color && '' !== $value ? null : $color; + } + + return neve_closes_css_declaration( $value ) ? null : $value; + } + + /** + * Sanitize a button appearance theme mod value. + * + * @param mixed $value Incoming button appearance. + * @return array|null Sanitized appearance; null when a non-empty color is invalid. + */ + private function sanitize_button_appearance_mod( $value ) { + if ( ! is_array( $value ) ) { + return null; + } + + $appearance = neve_sanitize_button_appearance( $value ); + foreach ( array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ) as $field ) { + if ( array_key_exists( $field, $value ) && '' !== $value[ $field ] && '' === $appearance[ $field ] ) { + return null; + } + } + + return neve_closes_css_declaration( $appearance ) ? null : $appearance; + } + + /** + * Sanitize a background theme mod value. + * + * @param mixed $value Incoming background. + * @return array|null Sanitized background; null when it, or a non-empty color in it, is invalid. + */ + private function sanitize_background_mod( $value ) { + $background = neve_sanitize_background( $value ); + if ( is_wp_error( $background ) ) { + return null; + } + + foreach ( array( 'colorValue', 'overlayColorValue' ) as $field ) { + if ( ! array_key_exists( $field, $value ) ) { + continue; + } + if ( ! is_string( $value[ $field ] ) || ( '' !== $value[ $field ] && '' === $background[ $field ] ) ) { + return null; + } + } + + return $background; + } + /** * Build a structured non-validation error payload for clients that collapse * WP_Error details into a generic transport error. diff --git a/inc/abilities/appearance_update.php b/inc/abilities/appearance_update.php index 7c0461323c..4cd36ebdc1 100644 --- a/inc/abilities/appearance_update.php +++ b/inc/abilities/appearance_update.php @@ -416,20 +416,27 @@ private function apply_colors( array $colors, array &$updated, array &$warnings * @return void */ private function apply_typography( array $typography, array &$updated ) { - if ( isset( $typography['body_font'] ) ) { - set_theme_mod( 'neve_body_font_family', sanitize_text_field( $typography['body_font'] ) ); - $updated[] = 'neve_body_font_family'; - } - if ( isset( $typography['headings_font'] ) ) { - set_theme_mod( 'neve_headings_font_family', sanitize_text_field( $typography['headings_font'] ) ); - $updated[] = 'neve_headings_font_family'; + $font_mods = array( + 'body_font' => 'neve_body_font_family', + 'headings_font' => 'neve_headings_font_family', + ); + foreach ( $font_mods as $input_key => $mod ) { + if ( ! isset( $typography[ $input_key ] ) ) { + continue; + } + $font = neve_sanitize_font_family( $typography[ $input_key ] ); + if ( '' !== $font || '' === $typography[ $input_key ] ) { + set_theme_mod( $mod, $font ); + $updated[] = $mod; + } } if ( isset( $typography['element'] ) && is_array( $typography['element'] ) && isset( $typography['element']['target'] ) ) { $target = $typography['element']['target']; $mod = 'body' === $target ? 'neve_typeface_general' : 'neve_' . $target . '_typeface_general'; - $current = get_theme_mod( $mod, array() ); + $default = \Neve\Core\Settings\Mods::get_alternative_mod_default( $mod ); + $current = get_theme_mod( $mod, is_array( $default ) ? $default : array() ); $current = is_array( $current ) ? $current : array(); $props = $typography['element']; @@ -437,7 +444,7 @@ private function apply_typography( array $typography, array &$updated ) { // Deep-merge so a partial update (e.g. only fontSize.desktop) keeps the // other breakpoints/sub-values instead of replacing the whole sub-object. - $merged = $this->rec_wp_parse_args( $props, $current ); + $merged = neve_sanitize_typography_control( $this->rec_wp_parse_args( $props, $current ) ); set_theme_mod( $mod, $merged ); $updated[] = $mod; } @@ -581,7 +588,7 @@ private function is_supported_color_value( $value ) { return false; } - if ( preg_match( '/^#[0-9a-fA-F]{3,8}$/', $value ) ) { + if ( preg_match( '/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/', $value ) ) { return true; } diff --git a/inc/abilities/builder_configure.php b/inc/abilities/builder_configure.php index 894f753ce4..a5297a0d3d 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -179,19 +179,28 @@ function ( $item ) use ( $component ) { */ protected function apply_row_style( $builder_id, $row, array $style ) { $prefix = 'hfg_' . $builder_id . '_layout_' . $row; + $builder = $this->get_builder( $builder_id ); + $mods = array(); $affected = array(); - if ( isset( $style['height'] ) ) { - set_theme_mod( $prefix . '_height', wp_json_encode( map_deep( $style['height'], 'sanitize_text_field' ) ) ); - $affected[] = $prefix . '_height'; + if ( isset( $style['height'] ) && null !== $builder ) { + $mods[ $prefix . '_height' ] = $builder->sanitize_responsive_int_json( map_deep( $style['height'], 'sanitize_text_field' ) ); } if ( isset( $style['background'] ) ) { - set_theme_mod( $prefix . '_background', map_deep( $style['background'], 'sanitize_text_field' ) ); - $affected[] = $prefix . '_background'; + $mods[ $prefix . '_background' ] = map_deep( $style['background'], 'sanitize_text_field' ); } if ( isset( $style['text_color'] ) ) { - set_theme_mod( $prefix . '_new_text_color', sanitize_text_field( $style['text_color'] ) ); - $affected[] = $prefix . '_new_text_color'; + $mods[ $prefix . '_new_text_color' ] = sanitize_text_field( $style['text_color'] ); + } + + foreach ( $mods as $key => $value ) { + $value = $this->sanitize_style_mod( $key, $value ); + if ( null === $value ) { + continue; + } + + set_theme_mod( $key, $value ); + $affected[] = $key; } return $affected; diff --git a/inc/abilities/site_set_theme_mod.php b/inc/abilities/site_set_theme_mod.php index dc7b1c3a6a..f010cf3de5 100644 --- a/inc/abilities/site_set_theme_mod.php +++ b/inc/abilities/site_set_theme_mod.php @@ -51,6 +51,21 @@ class Site_Set_Theme_Mod extends Abstract_Ability { 'hfg_header_layout_sidebar_new_text_color', ); + /** + * Sanitizers for allowlisted keys that are numbers or plain text rather than CSS values. Other keys use sanitize_style_mod(). + * + * @var array + */ + const SETTING_SANITIZERS = array( + 'neve_scroll_to_top_border_radius' => 'absint', + 'hfg_header_layout_sidebar_width' => 'neve_sanitize_range_value', + 'neve_scroll_to_top' => 'sanitize_text_field', + 'nav-icon_menu_icon' => 'sanitize_text_field', + 'nav-icon_svg_menu_icon' => 'sanitize_text_field', + 'nav-icon_menu_label' => 'sanitize_text_field', + 'hfg_header_layout_sidebar_layout' => 'sanitize_text_field', + ); + /** * {@inheritDoc} */ @@ -172,7 +187,20 @@ protected function run( array $input ) { ); } - set_theme_mod( $key, $this->sanitize_value( $input['value'] ) ); + $value = $this->sanitize_value( $input['value'] ); + $value = isset( self::SETTING_SANITIZERS[ $key ] ) + ? call_user_func( self::SETTING_SANITIZERS[ $key ], is_array( $value ) ? wp_json_encode( $value ) : $value ) + : $this->sanitize_style_mod( $key, $value ); + if ( null === $value ) { + return $this->validation_issue( + 'invalid_value', + /* translators: %s: input field name */ + sprintf( __( 'Invalid value for "%s".', 'neve' ), 'value' ), + 'value' + ); + } + + set_theme_mod( $key, $value ); } return array( diff --git a/inc/core/dynamic_css.php b/inc/core/dynamic_css.php index 60b186a3b3..590b3e6450 100644 --- a/inc/core/dynamic_css.php +++ b/inc/core/dynamic_css.php @@ -166,7 +166,9 @@ public static function get_root_css() { * @return string */ public static function get_fallback_font() { - $fallback = get_theme_mod( 'neve_fallback_font_family', 'Arial, Helvetica, sans-serif' ); + $default = 'Arial, Helvetica, sans-serif'; + $fallback = neve_sanitize_font_family( get_theme_mod( 'neve_fallback_font_family', $default ) ); + $fallback = '' === $fallback ? $default : $fallback; return '--nv-fallback-ff:' . $fallback . ';'; } diff --git a/inc/core/styles/css_prop.php b/inc/core/styles/css_prop.php index 5c3e96659e..6421cfe1b1 100644 --- a/inc/core/styles/css_prop.php +++ b/inc/core/styles/css_prop.php @@ -35,7 +35,9 @@ public static function get_unit_responsive( $meta, $device ) { $suffix = $all_value['unit']; } - return $suffix; + $suffix = neve_sanitize_css_unit( $suffix ); + + return '—' === $suffix ? '' : $suffix; } /** @@ -57,7 +59,11 @@ public static function get_suffix_responsive( $meta, $device ) { } } - return isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' );; + $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) && is_string( $all_value['suffix'] ) ? $all_value['suffix'] : 'px' ); + + $suffix = neve_sanitize_css_unit( $suffix ); + + return '—' === $suffix ? '' : $suffix; } /** @@ -71,6 +77,19 @@ public static function get_suffix_responsive( $meta, $device ) { * @return string */ public static function transform( $css_prop, $value, $meta, $device ) { + if ( isset( $meta[ Dynamic_Selector::META_KEY ] ) && preg_match( '/\.(fontSize|lineHeight|letterSpacing|fontWeight|textTransform)$/', $meta[ Dynamic_Selector::META_KEY ], $matches ) ) { + if ( in_array( $matches[1], array( 'fontSize', 'lineHeight', 'letterSpacing' ), true ) ) { + if ( ! is_numeric( $value ) ) { + return ''; + } + $value = (float) $value; + } elseif ( 'fontWeight' === $matches[1] && ! in_array( $value, array( 'none', 'normal', 'bold', 'bolder', 'lighter', '100', '200', '300', '400', '500', '600', '700', '800', '900' ), true ) ) { + return ''; + } elseif ( 'textTransform' === $matches[1] && ! in_array( $value, array( 'none', 'uppercase', 'lowercase', 'capitalize' ), true ) ) { + return ''; + } + } + //If we have a custom filter, let's call it. if ( isset( $meta['filter'] ) ) { if ( is_callable( $meta['filter'] ) ) { @@ -186,6 +205,9 @@ public static function transform( $css_prop, $value, $meta, $device ) { //Line height uses an awkward format saved, and we can't define it as responsive because we would need to use the suffix part. case Config::CSS_PROP_LINE_HEIGHT: case Config::CSS_PROP_FONT_SIZE: + if ( ! is_numeric( $value ) ) { + return ''; + } $suffix = isset( $meta[ Dynamic_Selector::META_SUFFIX ] ) ? $meta[ Dynamic_Selector::META_SUFFIX ] : 'em'; // We consider the provided suffix as default, in case that we have a responsive setting with responsive suffix. @@ -193,11 +215,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { $all_value = Mods::get( $meta['key'] ); $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) ? $all_value['suffix'] : $suffix ); } + $suffix = neve_sanitize_css_unit( $suffix ); + $suffix = '—' === $suffix ? '' : $suffix; - return sprintf( ' %s: %s%s;', $css_prop, $value, $suffix ); + return sprintf( ' %s: %s%s;', $css_prop, (float) $value, $suffix ); //Letter spacing has a legacy value of non-responsive which we need to take into consideration. case Config::CSS_PROP_LETTER_SPACING: - return sprintf( ' %s: %spx;', $css_prop, $value ); + return is_numeric( $value ) ? sprintf( ' %s: %spx;', $css_prop, (float) $value ) : ''; case Config::CSS_PROP_CUSTOM_BTN_TYPE: if ( $value !== 'outline' ) { return 'border:none;'; @@ -212,9 +236,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { return sprintf( ' %s: %s;', $css_prop, intval( $value ) ); case Config::CSS_PROP_FONT_FAMILY: + $value = neve_sanitize_font_family( $value ); if ( $value === 'default' ) { return ''; } + if ( '' === $value ) { + return ''; + } Font_Manager::add_google_font( $value ); return sprintf( ' %s: %s, var(--nv-fallback-ff);', $css_prop, $value ); @@ -225,9 +253,13 @@ public static function transform( $css_prop, $value, $meta, $device ) { case Config::CSS_PROP_GRID_TEMPLATE_COLS: return sprintf( ' %s: %s;', $css_prop, $value ); default: - $is_font_family_var = strpos( strtolower( $css_prop ), 'fontfamily' ) > - 1; + $is_font_family_var = false !== strpos( strtolower( $css_prop ), 'fontfamily' ); if ( $is_font_family_var ) { + $value = neve_sanitize_font_family( $value ); + if ( '' === $value ) { + return ''; + } Font_Manager::add_google_font( $value ); $value = self::format_font_family_value( $value ); @@ -259,13 +291,17 @@ public static function get_suffix( $meta, $device, $value, $css_prop ) { // If not responsive, most controls use 'unit' key inside value. if ( ! isset( $meta['is_responsive'] ) || $meta['is_responsive'] === false ) { - $suffix = isset( $value['unit'] ) ? $value['unit'] : $suffix; + $suffix = isset( $value['unit'] ) ? neve_sanitize_css_unit( $value['unit'] ) : $suffix; } // If responsive, try to find the suffix. if ( isset( $meta[ Dynamic_Selector::META_IS_RESPONSIVE ] ) && $meta[ Dynamic_Selector::META_IS_RESPONSIVE ] ) { $all_value = Mods::get( $meta['key'] ); - $suffix = isset( $all_value['suffix'][ $device ] ) ? $all_value['suffix'][ $device ] : ( isset( $all_value['suffix'] ) ? $all_value['suffix'] : $suffix ); + if ( isset( $all_value['suffix'][ $device ] ) ) { + $suffix = neve_sanitize_css_unit( $all_value['suffix'][ $device ] ); + } elseif ( isset( $all_value['suffix'] ) ) { + $suffix = neve_sanitize_css_unit( $all_value['suffix'] ); + } } if ( $suffix === 'responsive_unit' ) { diff --git a/inc/core/styles/css_vars.php b/inc/core/styles/css_vars.php index 07c8947118..36009259ae 100644 --- a/inc/core/styles/css_vars.php +++ b/inc/core/styles/css_vars.php @@ -46,38 +46,48 @@ public function get_button_rules() { $rules = [ '--primarybtnbg' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.background', + Dynamic_Selector::META_KEY => $mod_key_primary . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnbg' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.background', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.background', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhoverbg' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.backgroundHover', + Dynamic_Selector::META_KEY => $mod_key_primary . '.backgroundHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnhoverbg' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.backgroundHover', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.backgroundHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtncolor' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.text', + Dynamic_Selector::META_KEY => $mod_key_primary . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtncolor' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.text', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.text', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnhovercolor' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.textHover', + Dynamic_Selector::META_KEY => $mod_key_primary . '.textHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--secondarybtnhovercolor' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.textHover', + Dynamic_Selector::META_KEY => $mod_key_secondary . '.textHover', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--primarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.borderRadius', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $mod_key_primary . '.borderRadius', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--secondarybtnborderradius' => [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderRadius', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, + Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderRadius', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], ]; @@ -88,88 +98,79 @@ public function get_button_rules() { // Button Shadow Primary if ( isset( $primary_values['useShadow'] ) && ! empty( $primary_values['useShadow'] ) ) { $rules['--primarybtnshadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColor', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($primary_values) { - $blur = intval($primary_values['shadowProperties']['blur']); - $width = intval($primary_values['shadowProperties']['width']); - $height = intval($primary_values['shadowProperties']['height']); - - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); - } + Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColor', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $primary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $primary_values['shadowProperties'] ?? array() ); + }, ]; } // Button Shadow Primary Hover if ( isset( $primary_values['useShadowHover'] ) && ! empty( $primary_values['useShadowHover'] ) ) { $rules['--primarybtnhovershadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColorHover', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($primary_values) { - $blur = intval($primary_values['shadowPropertiesHover']['blur']); - $width = intval($primary_values['shadowPropertiesHover']['width']); - $height = intval($primary_values['shadowPropertiesHover']['height']); - - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); - } + Dynamic_Selector::META_KEY => $mod_key_primary . '.shadowColorHover', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $primary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $primary_values['shadowPropertiesHover'] ?? array() ); + }, ]; } // Button Shadow Secondary if ( isset( $secondary_values['useShadow'] ) && ! empty( $secondary_values['useShadow'] ) ) { $rules['--secondarybtnshadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColor', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($secondary_values) { - $blur = intval($secondary_values['shadowProperties']['blur']); - $width = intval($secondary_values['shadowProperties']['width']); - $height = intval($secondary_values['shadowProperties']['height']); - - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); - } + Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColor', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $secondary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $secondary_values['shadowProperties'] ?? array() ); + }, ]; } // Button Shadow Secondary Hover if ( isset( $secondary_values['useShadowHover'] ) && ! empty( $secondary_values['useShadowHover'] ) ) { $rules['--secondarybtnhovershadow'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColorHover', - Dynamic_Selector::META_DEFAULT => 'none', - Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ($secondary_values) { - $blur = intval($secondary_values['shadowPropertiesHover']['blur']); - $width = intval($secondary_values['shadowPropertiesHover']['width']); - $height = intval($secondary_values['shadowPropertiesHover']['height']); - - return sprintf( '%s:%s;', $css_prop, sprintf('%spx %spx %spx %s;', $width, $height, $blur, $value ) ); - } + Dynamic_Selector::META_KEY => $mod_key_secondary . '.shadowColorHover', + Dynamic_Selector::META_DEFAULT => 'none', + Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) use ( $secondary_values ) { + return neve_sanitize_button_shadow_css_rule( $css_prop, $value, $secondary_values['shadowPropertiesHover'] ?? array() ); + }, ]; } // Border Width if ( isset( $primary_values['type'] ) && $primary_values['type'] === 'outline' ) { $rules['--primarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $mod_key_primary . '.borderWidth', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $mod_key_primary . '.borderWidth', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } if ( isset( $secondary_values['type'] ) && $secondary_values['type'] === 'outline' ) { $rules['--secondarybtnborderwidth'] = [ - Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderWidth', - Dynamic_Selector::META_SUFFIX => 'px', - 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, + Dynamic_Selector::META_KEY => $mod_key_secondary . '.borderWidth', + Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_button_dimension_css_rule', + 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ]; } - $mod_key_primary = Config::MODS_BUTTON_PRIMARY_PADDING; - $default_primary = Mods::get_alternative_mod_default( Config::MODS_BUTTON_PRIMARY_PADDING ); + $mod_key_primary = Config::MODS_BUTTON_PRIMARY_PADDING; + $default_primary = Mods::get_alternative_mod_default( Config::MODS_BUTTON_PRIMARY_PADDING ); $rules['--btnpadding'] = [ Dynamic_Selector::META_KEY => $mod_key_primary, Dynamic_Selector::META_DEFAULT => $default_primary, Dynamic_Selector::META_IS_RESPONSIVE => true, - Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_FILTER => function ( $css_prop, $value, $meta, $device ) { + $value = neve_sanitize_directional_value( $value ); + if ( empty( array_filter( $value, 'is_numeric' ) ) ) { + return ''; + } + $mod_key_primary = Config::MODS_BUTTON_PRIMARY_STYLE; $default_primary = neve_get_button_appearance_default(); @@ -189,18 +190,18 @@ public function get_button_rules() { if ( ! isset( $appearance_values['type'] ) || $appearance_values['type'] !== 'outline' ) { continue; } - $border_width = $appearance_values['borderWidth']; + $border_width = neve_sanitize_button_dimension( $appearance_values['borderWidth'] ?? array() ); foreach ( $paddings[ $btn_type ] as $direction => $padding_value ) { if ( ! isset( $border_width[ $direction ] ) || absint( $border_width[ $direction ] ) === 0 ) { continue; } - if( ! is_numeric( $padding_value ) ){ + if ( ! is_numeric( $padding_value ) ) { continue; } $suffix_css_prop = $btn_type === 'primary' ? '--primarybtnpadding' : '--secondarybtnpadding'; - $suffix = Css_Prop::get_suffix( $meta, $device, $value, $suffix_css_prop ); + $suffix = Css_Prop::get_suffix( $meta, $device, $value, $suffix_css_prop ); $paddings[ $btn_type ][ 'is_outline_button_padding' ] = true; $paddings[ $btn_type ][ $direction ] = 'calc(' . $padding_value . $suffix . ' - ' . $border_width[ $direction ] . 'px)'; } diff --git a/inc/core/styles/frontend.php b/inc/core/styles/frontend.php index caae4504c0..acb0a3c83f 100644 --- a/inc/core/styles/frontend.php +++ b/inc/core/styles/frontend.php @@ -179,7 +179,9 @@ public function setup_blog_typography() { Dynamic_Selector::META_KEY => $args['mod'] . '.fontWeight', 'font' => 'mods_' . $args['font'], ], - '--texttransform' => $args['mod'] . '.textTransform', + '--texttransform' => [ + Dynamic_Selector::META_KEY => $args['mod'] . '.textTransform', + ], ]; } } @@ -251,11 +253,11 @@ public function setup_blog_card_style() { $output = ''; $unit = Css_Prop::get_unit_responsive( $meta, $device ); - if ( isset( $value['right'] ) && ! empty( $value['right'] ) ) { - $output .= sprintf( 'margin-right:-%1$s%2$s;', $value['right'], $unit ); + if ( isset( $value['right'] ) && is_numeric( $value['right'] ) && ! empty( $value['right'] ) ) { + $output .= sprintf( 'margin-right:-%1$s%2$s;', (float) $value['right'], $unit ); } - if ( isset( $value['left'] ) && ! empty( $value['left'] ) ) { - $output .= sprintf( 'margin-left:-%1$s%2$s;', $value['left'], $unit ); + if ( isset( $value['left'] ) && is_numeric( $value['left'] ) && ! empty( $value['left'] ) ) { + $output .= sprintf( 'margin-left:-%1$s%2$s;', (float) $value['left'], $unit ); } return $output; @@ -275,28 +277,12 @@ public function setup_blog_card_style() { }, ], '--padding' => [ - 'key' => 'neve_blog_content_padding', - 'default' => $content_padding_default, - 'is_responsive' => true, - 'directional-prop' => 'padding', - 'suffix' => 'responsive_unit', - 'filter' => function ( $css_prop, $value, $meta, $device ) { - $output = ''; - $unit = Css_Prop::get_unit_responsive( $meta, $device ); - - $check = array_filter( - $value, - function ( $item ) { - return ! empty( $item ) || $item === '0'; - } - ); - - if ( empty( $check ) ) { - return ''; - } - - return sprintf( $css_prop . ':%1$s%5$s %2$s%5$s %3$s%5$s %4$s%5$s;', $value['top'], $value['right'], $value['bottom'], $value['left'], $unit ); - }, + Dynamic_Selector::META_KEY => 'neve_blog_content_padding', + Dynamic_Selector::META_DEFAULT => $content_padding_default, + Dynamic_Selector::META_IS_RESPONSIVE => true, + Dynamic_Selector::META_DIRECTIONAL_PROP => 'padding', + Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', ], ]; @@ -581,31 +567,37 @@ private function setup_form_fields_style() { Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_WIDTH, Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_DEFAULT => $border_width_default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_BORDER_WIDTH, ], '--formfieldborderradius' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_RADIUS, Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_DEFAULT => $border_radius_default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_BORDER_RADIUS, ], '--formfieldbgcolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BACKGROUND_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-site-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldbordercolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_BORDER_COLOR, Dynamic_Selector::META_DEFAULT => '#8c8c8c', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldcolor' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_COLOR, Dynamic_Selector::META_DEFAULT => 'var(--nv-text-color)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--formfieldpadding' => [ Dynamic_Selector::META_KEY => Config::MODS_FORM_FIELDS_PADDING, Dynamic_Selector::META_DEFAULT => Mods::get_alternative_mod_default( Config::MODS_FORM_FIELDS_PADDING ), Dynamic_Selector::META_SUFFIX => 'px', Dynamic_Selector::META_IS_RESPONSIVE => false, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--formfieldtexttransform' => [ @@ -782,6 +774,7 @@ private function setup_single_post_style() { Dynamic_Selector::META_KEY => Config::MODS_POST_COMMENTS_PADDING, Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -803,6 +796,7 @@ private function setup_single_post_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->padding_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -907,6 +901,7 @@ private function setup_header_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default( 'cover' ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--justify' => [ @@ -972,6 +967,7 @@ private function setup_header_style() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_DEFAULT => $this->padding_default( 'cover' ), Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, ], '--bgcolor' => [ @@ -1021,6 +1017,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->content_vspacing_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_DIRECTIONAL_PROP => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; @@ -1039,6 +1036,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $default, + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', Dynamic_Selector::META_DIRECTIONAL_PROP => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; @@ -1058,6 +1056,7 @@ private function setup_content_vspacing() { Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', Dynamic_Selector::META_DEFAULT => $this->content_vspacing_default(), + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_DIRECTIONAL_ONE_AXIS, ], ]; @@ -1099,11 +1098,13 @@ private function get_scroll_to_top_rules() { '--color' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_icon_color', 'var(--nv-text-dark-bg)' ) ) ? 'transparent' : 'var(--nv-text-dark-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--padding' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_padding', Dynamic_Selector::META_IS_RESPONSIVE => true, Dynamic_Selector::META_SUFFIX => 'responsive_unit', + Dynamic_Selector::META_FILTER => 'neve_sanitize_directional_css_rule', 'directional-prop' => Config::CSS_PROP_PADDING, Dynamic_Selector::META_DEFAULT => array( 'desktop' => array( @@ -1133,18 +1134,22 @@ private function get_scroll_to_top_rules() { Dynamic_Selector::META_KEY => 'neve_scroll_to_top_border_radius', Dynamic_Selector::META_DEFAULT => 3, Dynamic_Selector::META_SUFFIX => 'px', + Dynamic_Selector::META_FILTER => 'neve_sanitize_numeric_css_rule', ], '--bgcolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_background_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_background_color', 'var(--nv-primary-accent)' ) ) ? 'transparent' : 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--hovercolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_hover_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_icon_hover_color', 'var(--nv-text-dark-bg)' ) ) ? 'transparent' : 'var(--nv-text-dark-bg)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--hoverbgcolor' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_background_hover_color', Dynamic_Selector::META_DEFAULT => empty( Mods::get( 'neve_scroll_to_top_background_hover_color', 'var(--nv-primary-accent)' ) ) ? 'transparent' : 'var(--nv-primary-accent)', + Dynamic_Selector::META_FILTER => 'neve_sanitize_color_css_rule', ], '--size' => [ Dynamic_Selector::META_KEY => 'neve_scroll_to_top_icon_size', diff --git a/inc/customizer/options/buttons.php b/inc/customizer/options/buttons.php index fbc40aaf09..5e1f8e3b75 100644 --- a/inc/customizer/options/buttons.php +++ b/inc/customizer/options/buttons.php @@ -71,17 +71,17 @@ public function add_controls() { new Control( $mod_key, array( - 'default' => $defaults, + 'default' => $defaults, + 'sanitize_callback' => array( $this, 'sanitize_spacing_array' ), ), array( - 'label' => __( 'Padding', 'neve' ), - 'sanitize_callback' => array( $this, 'sanitize_spacing_array' ), - 'section' => $this->section_id, - 'input_attrs' => [ + 'label' => __( 'Padding', 'neve' ), + 'section' => $this->section_id, + 'input_attrs' => [ 'units' => [ 'px', 'em', 'rem' ], 'min' => 0, ], - 'default' => $defaults, + 'default' => $defaults, ), '\Neve\Customizer\Controls\React\Spacing' ) @@ -91,7 +91,8 @@ public function add_controls() { new Control( Config::MODS_BUTTON_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'label' => esc_html__( 'Button Text', 'neve' ), diff --git a/inc/customizer/options/form_fields.php b/inc/customizer/options/form_fields.php index 3cbad367d2..4f55d22376 100644 --- a/inc/customizer/options/form_fields.php +++ b/inc/customizer/options/form_fields.php @@ -389,7 +389,8 @@ private function add_input_text_controls() { new Control( Config::MODS_FORM_FIELDS_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 32, @@ -491,7 +492,8 @@ private function add_form_labels_controls() { new Control( Config::MODS_FORM_FIELDS_LABELS_TYPEFACE, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 52, diff --git a/inc/customizer/options/layout_container.php b/inc/customizer/options/layout_container.php index bbf9360684..519d4f90ba 100644 --- a/inc/customizer/options/layout_container.php +++ b/inc/customizer/options/layout_container.php @@ -107,12 +107,12 @@ private function control_vertical_spacing() { new Control( Config::MODS_CONTENT_VSPACING, [ - 'default' => $this->content_vspacing_default(), - 'transport' => $this->selective_refresh, + 'default' => $this->content_vspacing_default(), + 'transport' => $this->selective_refresh, + 'sanitize_callback' => [ $this, 'sanitize_spacing_array' ], ], [ 'label' => __( 'Content Vertical Spacing', 'neve' ), - 'sanitize_callback' => [ $this, 'sanitize_spacing_array' ], 'section' => 'neve_container', 'input_attrs' => [ 'units' => [ 'px', 'vh' ], diff --git a/inc/customizer/options/typography.php b/inc/customizer/options/typography.php index 7dea9cd9c6..0c548bfad8 100644 --- a/inc/customizer/options/typography.php +++ b/inc/customizer/options/typography.php @@ -138,7 +138,7 @@ private function controls_typography_general() { Config::MODS_FONT_GENERAL, [ 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'default' => Mods::get_alternative_mod_default( Config::MODS_FONT_GENERAL ), ], array( @@ -181,8 +181,9 @@ private function controls_typography_general() { new Control( Config::MODS_TYPEFACE_GENERAL, [ - 'transport' => $this->selective_refresh, - 'default' => $defaults, + 'transport' => $this->selective_refresh, + 'default' => $defaults, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => 11, @@ -232,7 +233,7 @@ private function controls_typography_general() { 'neve_fallback_font_family', [ 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', 'default' => 'Arial, Helvetica, sans-serif', ], [ @@ -274,7 +275,7 @@ private function controls_typography_headings() { 'neve_headings_font_family', array( 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', ), array( 'section' => 'neve_typography_headings', @@ -355,7 +356,7 @@ private function controls_typography_headings() { $mod_key_font_family, array( 'transport' => $this->selective_refresh, - 'sanitize_callback' => 'sanitize_text_field', + 'sanitize_callback' => 'neve_sanitize_font_family', ), array( 'section' => 'neve_typography_headings', @@ -384,8 +385,9 @@ private function controls_typography_headings() { new Control( $mod_key, [ - 'transport' => $this->selective_refresh, - 'default' => $default_values, + 'transport' => $this->selective_refresh, + 'default' => $default_values, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => $priority += 2, @@ -510,7 +512,8 @@ private function controls_typography_blog() { new Control( $control_id, [ - 'transport' => $this->selective_refresh, + 'transport' => $this->selective_refresh, + 'sanitize_callback' => 'neve_sanitize_typography_control', ], [ 'priority' => $control_settings['priority'] += 1, diff --git a/tests/test-neve-ability-style-mods.php b/tests/test-neve-ability-style-mods.php new file mode 100644 index 0000000000..341629a49f --- /dev/null +++ b/tests/test-neve-ability-style-mods.php @@ -0,0 +1,914 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods the abilities rewrite. + */ + public function setUp(): void { + parent::setUp(); + + wp_set_current_user( 1 ); + $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; + + parent::tearDown(); + } + + /** + * Header row colors are sanitized and an invalid background is not saved. + */ + public function test_header_configure_sanitizes_row_style(): void { + set_theme_mod( self::HEADER_BG, 'previous' ); + set_theme_mod( self::HEADER_TEXT, '#123456' ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::INJECTED_CSS, + 'background' => 'not-a-background', + ), + ), + ) + ); + + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::HEADER_TEXT ) ); + $this->assertSame( 'previous', get_theme_mod( self::HEADER_BG ) ); + } + + /** + * Header row background colors are sanitized. + */ + public function test_header_configure_sanitizes_row_background(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => array( + 'type' => 'color', + 'colorValue' => self::INJECTED_CSS, + ), + ), + ), + ) + ); + + $background = get_theme_mod( self::HEADER_BG ); + $this->assertIsArray( $background ); + $this->assertSame( 'rgba(1,2,3,1)', $background['colorValue'] ); + } + + /** + * Footer row colors are sanitized. + */ + public function test_footer_configure_sanitizes_row_style(): void { + set_theme_mod( self::FOOTER_TEXT, '#123456' ); + + ( new Footer_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::INJECTED_CSS, + ), + ), + ) + ); + + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::FOOTER_TEXT ) ); + } + + /** + * A background that is not a background object is rejected and the stored value kept. + */ + public function test_site_set_theme_mod_rejects_invalid_background(): void { + set_theme_mod( self::SIDEBAR_BG, 'previous' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_BG, + 'value' => 'not-a-background', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'value', $result['error']['field'] ); + $this->assertSame( 'previous', get_theme_mod( self::SIDEBAR_BG ) ); + } + + /** + * A row height suffix that would close its CSS declaration is replaced by an allowed unit. + */ + public function test_header_configure_sanitizes_row_height(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'height' => array( + 'desktop' => 80, + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + ), + ), + ) + ); + + $height = json_decode( get_theme_mod( self::HEADER_HEIGHT ), true ); + $this->assertSame( 80, $height['desktop'] ); + $this->assertSame( 'px', $height['suffix']['desktop'] ); + } + + /** + * A background with an invalid nested color keeps the stored background. + */ + public function test_header_configure_keeps_background_on_invalid_nested_color(): void { + $background = array( + 'type' => 'color', + 'colorValue' => '#123456', + ); + set_theme_mod( self::HEADER_BG, $background ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => array( + 'type' => 'color', + 'colorValue' => 'notacolor', + ), + ), + ), + ) + ); + + $this->assertSame( $background, get_theme_mod( self::HEADER_BG ) ); + } + + /** + * Invalid nested background color types keep the stored background. + */ + public function test_header_configure_keeps_background_on_invalid_nested_color_types(): void { + $background = array( + 'type' => 'image', + 'colorValue' => '#123456', + 'overlayColorValue' => '#654321', + 'imageUrl' => '', + 'focusPoint' => array( 'x' => 0.5, 'y' => 0.5 ), + 'useFeatured' => false, + ); + + $invalid_values = array( + 'colorValue' => array( 'notacolor', array() ), + 'overlayColorValue' => array( 'notacolor', array() ), + 'imageUrl' => array( array() ), + 'focusPoint' => array( 'not-an-array' ), + 'overlayOpacity' => array( array() ), + ); + + foreach ( $invalid_values as $field => $invalid_values_for_field ) { + foreach ( $invalid_values_for_field as $invalid ) { + set_theme_mod( self::HEADER_BG, $background ); + $candidate = $background; + $candidate[ $field ] = $invalid; + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'background' => $candidate, + ), + ), + ) + ); + + $this->assertSame( $background, get_theme_mod( self::HEADER_BG ) ); + } + } + } + + /** + * The sidebar width goes through its range sanitizer. + */ + public function test_site_set_theme_mod_sanitizes_sidebar_width(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_WIDTH, + 'value' => '360;}body{background:lime}/*', + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 360.0, get_theme_mod( self::SIDEBAR_WIDTH ) ); + } + + /** + * Responsive sidebar width units are restricted to safe CSS unit strings. + */ + public function test_site_set_theme_mod_sanitizes_sidebar_width_units(): void { + $width = 360; + foreach ( array( self::INJECTED_CSS, array( 'mobile' => self::INJECTED_CSS ) ) as $suffix ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_WIDTH, + 'value' => array( + 'mobile' => $width, + 'tablet' => $width, + 'desktop' => $width, + 'suffix' => $suffix, + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $stored = json_decode( get_theme_mod( self::SIDEBAR_WIDTH ), true ); + $this->assertSame( 'px', is_array( $stored['suffix'] ) ? $stored['suffix']['mobile'] : $stored['suffix'] ); + } + } + + /** + * Previously stored sidebar width values and units are sanitized when CSS is generated. + */ + public function test_stored_sidebar_width_is_sanitized_when_css_is_generated(): void { + $devices = array( 'mobile', 'tablet', 'desktop' ); + $width = '360;}body{background:lime}/*'; + foreach ( array( self::INJECTED_CSS, array_fill_keys( $devices, self::INJECTED_CSS ) ) as $suffix ) { + set_theme_mod( + self::SIDEBAR_WIDTH, + wp_json_encode( + array( + 'mobile' => $width, + 'tablet' => $width, + 'desktop' => $width, + 'suffix' => $suffix, + ) + ) + ); + + foreach ( array( 'slide_left', 'pull_left', 'pull_right' ) as $layout ) { + set_theme_mod( 'hfg_header_layout_sidebar_layout', $layout ); + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $selector = new Dynamic_Selector( $builder->add_style( array() ) ); + $css_by_device = array( + 'mobile' => (string) $selector->for_mobile(), + 'tablet' => (string) $selector->for_tablet(), + 'desktop' => (string) $selector->for_desktop(), + ); + + foreach ( $css_by_device as $css ) { + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( 'width: 360px;', $css ); + if ( 'pull_left' === $layout ) { + $this->assertStringContainsString( 'left: 360px;', $css ); + } + if ( 'pull_right' === $layout ) { + $this->assertStringContainsString( 'right: 360px;', $css ); + } + } + } + } + } + + /** + * Responsive line heights remain unitless when configured without a unit. + */ + public function test_unitless_line_height_is_preserved(): void { + $devices = array( 'mobile', 'tablet', 'desktop' ); + set_theme_mod( + 'neve_typeface_general', + array( + 'lineHeight' => array( + 'mobile' => self::LINE_HEIGHT, + 'tablet' => self::LINE_HEIGHT, + 'desktop' => self::LINE_HEIGHT, + 'suffix' => array_fill_keys( $devices, '—' ), + ), + ) + ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertSame( count( $devices ), substr_count( $css, '--bodylineheight: ' . self::LINE_HEIGHT . ';' ) ); + $this->assertStringNotContainsString( '--bodylineheight: ' . self::LINE_HEIGHT . 'px;', $css ); + } + + /** + * Legacy scalar directional settings expand across all directions. + */ + public function test_scalar_directional_values_are_preserved(): void { + set_theme_mod( 'neve_form_fields_padding', 12 ); + set_theme_mod( 'neve_form_fields_border_width', 2 ); + set_theme_mod( 'neve_form_fields_border_radius', 3 ); + set_theme_mod( 'neve_scroll_to_top_padding', 4 ); + set_theme_mod( 'neve_scroll_to_top_status', '1' ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertStringContainsString( '--formfieldpadding:12px;', $css ); + $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); + $this->assertStringContainsString( '--formfieldborderradius:3px;', $css ); + $this->assertSame( 3, substr_count( $css, '--padding:4px;' ) ); + } + + /** + * A spacing value that would close its CSS declaration is rejected and the stored value kept. + */ + public function test_site_set_theme_mod_rejects_injected_spacing(): void { + set_theme_mod( self::FORM_PADDING, 'previous' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::FORM_PADDING, + 'value' => array( + 'top' => '10;}body{background:lime}/*', + 'right' => '10', + 'bottom' => '10', + 'left' => '10', + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( 'previous', get_theme_mod( self::FORM_PADDING ) ); + } + + /** + * An invalid row color keeps the stored color. + */ + public function test_header_configure_keeps_color_on_invalid_value(): void { + set_theme_mod( self::HEADER_TEXT, '#ff0000' ); + + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'notacolor', + ), + ), + ) + ); + + $this->assertSame( '#ff0000', get_theme_mod( self::HEADER_TEXT ) ); + } + + /** + * Named row text colors remain supported by both ability paths. + */ + public function test_abilities_accept_named_row_text_colors(): void { + ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'red', + ), + ), + ) + ); + $this->assertSame( 'red', get_theme_mod( self::HEADER_TEXT ) ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => 'red', + ) + ); + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'red', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Malformed functional colors are rejected and keep the stored color. + */ + public function test_site_set_theme_mod_rejects_malformed_function_color(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#123456' ); + + foreach ( array( 'rgb(,)', 'rgb(255, 0%, 0)', 'rgb(10%,0,0)', 'hsl(0,0,0)', 'hsl(120, 50, 50)' ) as $invalid_color ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $invalid_color, + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( '#123456', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + } + + /** + * Invalid Appearance Update button colors keep the stored appearance. + */ + public function test_appearance_update_rejects_invalid_button_hex_color(): void { + $stored = array( 'background' => '#123456' ); + set_theme_mod( 'neve_button_appearance', $stored ); + + $result = ( new Appearance_Update() )->execute( + array( + 'buttons' => array( + 'primary' => array( 'background' => '#12345' ), + ), + ) + ); + + $this->assertWPError( $result ); + $this->assertSame( $stored, get_theme_mod( 'neve_button_appearance' ) ); + } + + /** + * Alpha-hex button colors accepted by the ability remain available to CSS output. + */ + public function test_appearance_update_renders_alpha_hex_button_colors(): void { + $result = ( new Appearance_Update() )->execute( + array( + 'buttons' => array( + 'primary' => array( + 'background' => '#0008', + 'hover' => array( 'background' => '#11223344' ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $stored = get_theme_mod( 'neve_button_appearance' ); + $this->assertSame( '#0008', $stored['background'] ); + $this->assertSame( '#11223344', $stored['backgroundHover'] ); + $this->assertSame( ' --primarybtnbg: #0008;', neve_sanitize_color_css_rule( '--primarybtnbg', $stored['background'] ) ); + $this->assertSame( ' --primarybtnhoverbg: #11223344;', neve_sanitize_color_css_rule( '--primarybtnhoverbg', $stored['backgroundHover'] ) ); + } + + /** + * Typography and font-family values are sanitized on write and output. + */ + public function test_typography_values_are_sanitized_on_write_and_output(): void { + set_theme_mod( 'neve_body_font_family', 'Arial' ); + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'body_font' => 'Arial;}body{background:lime}/*', + 'element' => array( + 'target' => 'body', + 'fontSize' => array( + 'desktop' => '10;}body{background:lime}/*', + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'Arial', get_theme_mod( 'neve_body_font_family' ) ); + $typeface = get_theme_mod( 'neve_typeface_general' ); + $this->assertArrayNotHasKey( 'desktop', $typeface['fontSize'] ); + $this->assertSame( 'px', $typeface['fontSize']['suffix']['desktop'] ); + + $json_font = '{"x\\";}}body{background:lime}/*":0}'; + ( new Appearance_Update() )->execute( + array( + 'typography' => array( 'body_font' => $json_font ), + ) + ); + $this->assertSame( 'Arial', get_theme_mod( 'neve_body_font_family' ) ); + + set_theme_mod( 'neve_body_font_family', $json_font ); + set_theme_mod( 'neve_archive_typography_post_title', array( 'textTransform' => self::INJECTED_CSS ) ); + set_theme_mod( + 'neve_typeface_general', + array( + 'fontSize' => array( + 'desktop' => '10;}body{background:lime}/*', + 'suffix' => array( 'desktop' => 'px;}body{background:lime}/*' ), + ), + 'lineHeight' => array( 'desktop' => '10;}body{background:lime}/*' ), + 'letterSpacing' => array( 'desktop' => '10;}body{background:lime}/*' ), + 'fontWeight' => '10;}body{background:lime}/*', + 'textTransform' => 'none;}body{background:lime}/*', + ) + ); + $css = (string) ( new Frontend() )->generate(); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + + /** + * Stored keyword font weights render and survive partial updates. + */ + public function test_keyword_font_weight_survives_partial_typography_update(): void { + $typeface = Mods::get_alternative_mod_default( 'neve_typeface_general' ); + $typeface = is_array( $typeface ) ? $typeface : array(); + $typeface['fontWeight'] = 'bold'; + set_theme_mod( 'neve_typeface_general', $typeface ); + + $css = (string) ( new Frontend() )->generate(); + $this->assertStringContainsString( '--bodyfontweight: bold;', $css ); + + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'element' => array( + 'target' => 'body', + 'fontSize' => array( 'desktop' => 24 ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( 'bold', get_theme_mod( 'neve_typeface_general' )['fontWeight'] ); + } + + /** + * Partial typography updates retain the target's defaults. + */ + public function test_partial_typography_updates_retain_defaults(): void { + $targets = array( + 'body' => array( 'neve_typeface_general', '400' ), + 'h1' => array( 'neve_h1_typeface_general', '700' ), + ); + + foreach ( $targets as $target => $expected ) { + remove_theme_mod( $expected[0] ); + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'element' => array( + 'target' => $target, + 'fontSize' => array( 'desktop' => 24 ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $typeface = get_theme_mod( $expected[0] ); + $this->assertSame( $expected[1], $typeface['fontWeight'] ); + $this->assertSame( 24, $typeface['fontSize']['desktop'] ); + $this->assertArrayHasKey( 'mobile', $typeface['fontSize'] ); + } + } + + /** + * Empty font families remain valid reset values for the ability. + */ + public function test_typography_font_families_can_be_cleared(): void { + set_theme_mod( 'neve_body_font_family', 'Arial' ); + set_theme_mod( 'neve_headings_font_family', 'Georgia' ); + + $result = ( new Appearance_Update() )->execute( + array( + 'typography' => array( + 'body_font' => '', + 'headings_font' => '', + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( '', get_theme_mod( 'neve_body_font_family' ) ); + $this->assertSame( '', get_theme_mod( 'neve_headings_font_family' ) ); + } + + /** + * Previously stored fallback fonts are sanitized at their CSS sink. + */ + public function test_stored_fallback_font_is_sanitized_when_css_is_generated(): void { + set_theme_mod( 'neve_fallback_font_family', 'Arial;}body{background:lime}/*' ); + + $css = \Neve\Core\Dynamic_Css::get_fallback_font(); + + $this->assertSame( '--nv-fallback-ff:Arial, Helvetica, sans-serif;', $css ); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + + /** + * Stored header component spacing is sanitized at every directional sink. + */ + public function test_stored_header_component_spacing_is_sanitized(): void { + $invalid = array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'tablet' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'desktop' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '5;}body{background:lime}/*' ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + $mods = array( + 'primary-menu_component_padding', + 'primary-menu_component_margin', + 'header_search_field_border_width', + 'header_search_field_border_radius', + 'header_search_responsive_field_border_width', + 'header_search_responsive_field_border_radius', + 'header_menu_icon_label_margin', + ); + foreach ( $mods as $mod ) { + set_theme_mod( $mod, $invalid ); + } + set_theme_mod( 'primary-menu_component_font_family', 'Arial;}body{background:lime}/*' ); + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + foreach ( array( 'mobile', 'tablet', 'desktop' ) as $device ) { + $selector = new Dynamic_Selector( $builder->add_style( array() ) ); + $method = 'for_' . $device; + $css = (string) $selector->$method(); + $this->assertStringNotContainsString( 'background:lime', $css ); + } + } + + /** + * An invalid color is rejected and the stored color kept. + */ + public function test_site_set_theme_mod_rejects_invalid_color(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); + + $invalid_colors = array( + 'notacolor', + 'linear-gradient()', + 'linear-gradient(foo)', + 'linear-gradient(red garbage, blue)', + 'linear-gradient(to left right, red, blue)', + 'radial-gradient(10, red, blue)', + 'linear-gradient(red 10deg, blue)', + 'conic-gradient(red 10px, blue)', + 'linear-gradient(rgb(1 / 2 / 3), blue)', + 'radial-gradient(circle 10px 20px, red, blue)', + 'radial-gradient(ellipse -10px 20px, red, blue)', + 'linear-gradient(1.deg, red, blue)', + 'linear-gradient(rgb(1.,2,3), blue)', + 'linear-gradient(rgb(255, 0%, 0), blue)', + 'var(--c5-missing,rgb(foo))', + 'linear-gradient(var(--c5-missing,rgb(foo)),#fff)', + ); + foreach ( $invalid_colors as $invalid_color ) { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $invalid_color, + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( '#ff0000', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + } + + /** + * Modern gradient colors and computed stops remain valid ability values. + */ + public function test_site_set_theme_mod_keeps_modern_gradient_color(): void { + $gradient = 'linear-gradient(hwb(30 40% 20%), color-mix(in oklab, red 40%, blue) min(20%, 100px))'; + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => $gradient, + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( $gradient, get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Invalid button appearance colors keep the stored appearance. + */ + public function test_site_set_theme_mod_rejects_invalid_button_appearance_color(): void { + $stored = array( + 'background' => '#123456', + 'text' => '#ffffff', + ); + set_theme_mod( self::NAV_BUTTON, $stored ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => array( 'background' => 'notacolor' ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertSame( $stored, get_theme_mod( self::NAV_BUTTON ) ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => 'text_button', + ) + ); + + $this->assertIsArray( $result ); + $this->assertFalse( $result['success'] ); + $this->assertSame( $stored, get_theme_mod( self::NAV_BUTTON ) ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::NAV_BUTTON, + 'value' => array( 'background' => 'transparent' ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'transparent', get_theme_mod( self::NAV_BUTTON )['background'] ); + } + + /** + * Scalar button mode settings keep the generic allowlisted write path. + */ + public function test_site_set_theme_mod_accepts_scalar_button_appearance_mode(): void { + $allow_search_button = static function ( $allowlist ) { + $allowlist[] = 'header_search_button_appearance'; + + return $allowlist; + }; + add_filter( 'neve_abilities_theme_mod_allowlist', $allow_search_button ); + try { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'header_search_button_appearance', + 'value' => 'text_button', + ) + ); + } finally { + remove_filter( 'neve_abilities_theme_mod_allowlist', $allow_search_button ); + } + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'text_button', get_theme_mod( 'header_search_button_appearance' ) ); + } + + /** + * An empty color clears the stored color. + */ + public function test_site_set_theme_mod_clears_color_with_empty_value(): void { + set_theme_mod( self::SIDEBAR_TEXT, '#ff0000' ); + + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => '', + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( '', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Color settings are sanitized. + */ + public function test_site_set_theme_mod_sanitizes_colors(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => self::SIDEBAR_TEXT, + 'value' => self::INJECTED_CSS, + ) + ); + + $this->assertIsArray( $result ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'rgba(1,2,3,1)', get_theme_mod( self::SIDEBAR_TEXT ) ); + } + + /** + * Previously stored ability color settings are sanitized when CSS is generated. + */ + public function test_stored_ability_styles_are_sanitized_when_css_is_generated(): void { + $color_mods = array( + 'neve_form_fields_background_color', + 'neve_form_fields_border_color', + 'neve_input_text_color', + 'neve_scroll_to_top_icon_color', + 'neve_scroll_to_top_icon_hover_color', + 'neve_scroll_to_top_background_color', + 'neve_scroll_to_top_background_hover_color', + ); + foreach ( $color_mods as $mod ) { + set_theme_mod( $mod, self::INJECTED_CSS ); + } + $directional = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 2 ); + $directional['unit'] = self::INJECTED_CSS; + $invalid_directional = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '2;}body{background:lime}/*' ); + $invalid_directional['unit'] = 'px'; + set_theme_mod( 'neve_form_fields_border_width', $directional ); + set_theme_mod( 'neve_form_fields_border_radius', $invalid_directional ); + set_theme_mod( 'neve_form_fields_padding', $directional ); + $scroll_padding = array( + 'mobile' => $directional, + 'tablet' => $directional, + 'desktop' => $directional, + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + set_theme_mod( 'neve_scroll_to_top_padding', $scroll_padding ); + set_theme_mod( 'neve_scroll_to_top_border_radius', '3;}body{background:lime}/*' ); + set_theme_mod( 'neve_scroll_to_top_status', '1' ); + + $responsive_directional = array( + 'mobile' => $invalid_directional, + 'tablet' => $invalid_directional, + 'desktop' => $invalid_directional, + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ); + foreach ( array( 'neve_content_vspacing', 'neve_blog_content_padding', 'neve_comments_boxed_padding', 'neve_comments_form_boxed_padding', 'neve_post_cover_padding', 'neve_post_cover_title_boxed_padding', 'neve_post_content_vspacing' ) as $mod ) { + set_theme_mod( $mod, $responsive_directional ); + } + set_theme_mod( 'neve_post_inherit_vspacing', 'individual' ); + $post_id = self::factory()->post->create(); + $this->go_to( get_permalink( $post_id ) ); + + $css = (string) ( new Frontend() )->generate(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--formfieldbgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldbordercolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--formfieldborderwidth:2px;', $css ); + $this->assertStringNotContainsString( '--formfieldborderradius:', $css ); + $this->assertStringContainsString( '--formfieldpadding:2px;', $css ); + $this->assertStringContainsString( '--padding:2px;', $css ); + $this->assertStringContainsString( '--borderradius: 3px;', $css ); + $this->assertStringContainsString( '--hoverbgcolor: rgba(1,2,3,1);', $css ); + } + +} diff --git a/tests/test-neve-button-appearance-css.php b/tests/test-neve-button-appearance-css.php new file mode 100644 index 0000000000..576c4151ea --- /dev/null +++ b/tests/test-neve-button-appearance-css.php @@ -0,0 +1,236 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods and read them uncached. + */ + public function setUp(): void { + parent::setUp(); + + $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; + + parent::tearDown(); + } + + /** + * Button appearance saves sanitize every field that is later printed as a color. + */ + public function test_button_appearance_sanitizes_color_fields(): void { + $appearance = neve_sanitize_button_appearance( $this->injected_appearance() ); + + foreach ( $this->color_fields() as $field ) { + $this->assertSame( 'rgba(1,2,3,1)', $appearance[ $field ] ); + } + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + $this->assertSame( array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 0 ), $appearance[ $field ] ); + } + foreach ( array( 'shadowProperties', 'shadowPropertiesHover' ) as $field ) { + $this->assertSame( array( 'blur' => 5, 'width' => 0, 'height' => 0 ), $appearance[ $field ] ); + } + $this->assertSame( 'outline', $appearance['type'] ); + $this->assertSame( array(), neve_sanitize_button_appearance( 'invalid' ) ); + $this->assertSame( '', neve_sanitize_color_css_rule( '--color', 'invalid' ) ); + $this->assertSame( ' --color: transparent;', neve_sanitize_color_css_rule( '--color', 'transparent' ) ); + $this->assertSame( array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), 8 ), neve_sanitize_button_dimension( 8 ) ); + $this->assertSame( '--shadow:0px 0px 0px none;', neve_sanitize_shadow_css_rule( '--shadow', 'invalid', 0, 0, 0 ) ); + } + + /** + * Previously stored button colors are sanitized when global CSS is generated. + */ + public function test_global_button_css_sanitizes_stored_colors(): void { + $appearance = $this->injected_appearance(); + set_theme_mod( 'neve_button_appearance', $appearance ); + set_theme_mod( 'neve_secondary_button_appearance', $appearance ); + + $rules = ( new NeveButtonRulesWrapperForTest() )->get_button_rules(); + $css = (string) new Dynamic_Selector( array( ':root' => $rules ) ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--secondarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnborderradius:0;', $css ); + $this->assertStringContainsString( '--primarybtnborderwidth:0;', $css ); + } + + /** + * Malformed stored button shadows and padding are normalized before output. + */ + public function test_global_button_css_handles_malformed_stored_structures(): void { + $appearance = neve_get_button_appearance_default(); + $appearance['useShadow'] = true; + $appearance['useShadowHover'] = true; + $appearance['shadowProperties'] = 'invalid'; + $appearance['shadowPropertiesHover'] = array( 'blur' => self::INJECTED_CSS ); + set_theme_mod( 'neve_button_appearance', $appearance ); + set_theme_mod( 'neve_secondary_button_appearance', $appearance ); + set_theme_mod( + 'neve_button_padding', + array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'tablet' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'desktop' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => self::INJECTED_CSS, + 'desktop-unit' => self::INJECTED_CSS, + ) + ); + + $rules = ( new NeveButtonRulesWrapperForTest() )->get_button_rules(); + $css = (string) new Dynamic_Selector( array( ':root' => $rules ) ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--primarybtnshadow:0px 0px 0px', $css ); + $this->assertStringContainsString( '--secondarybtnhovershadow:0px 0px 0px', $css ); + $this->assertStringNotContainsString( '--btnpadding:', $css ); + } + + /** + * The Customizer setting sanitizes button padding before storage. + */ + public function test_button_padding_customizer_setting_sanitizes_input(): void { + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; + + $wp_customize = new WP_Customize_Manager(); + ( new \Neve\Customizer\Options\Buttons() )->register_controls_callback( $wp_customize ); + $setting = $wp_customize->get_setting( 'neve_button_padding' ); + + $this->assertNotNull( $setting ); + $input = array( + 'mobile' => array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), self::INJECTED_CSS ), + 'tablet' => array( 'top' => 12, 'right' => self::INJECTED_CSS ), + 'desktop' => array( 'top' => 10, 'right' => 20, 'bottom' => 30, 'left' => 40 ), + 'mobile-unit' => self::INJECTED_CSS, + 'tablet-unit' => 'em', + 'desktop-unit' => 'px', + ); + + $this->assertSame( + array( + 'mobile' => array(), + 'tablet' => array( 'top' => 12 ), + 'desktop' => array( 'top' => 10, 'right' => 20, 'bottom' => 30, 'left' => 40 ), + 'mobile-unit' => 'px', + 'tablet-unit' => 'em', + 'desktop-unit' => 'px', + ), + $setting->sanitize( $input ) + ); + } + + /** + * Previously stored component colors are sanitized when header CSS is generated. + */ + public function test_header_component_css_sanitizes_stored_colors(): void { + $appearance = $this->injected_appearance(); + set_theme_mod( 'button_base_style_setting', $appearance ); + set_theme_mod( 'nav-icon_button_appearance', $appearance ); + set_theme_mod( 'header_search_button_appearance', 'text_button' ); + set_theme_mod( 'header_search_button_style', $appearance ); + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $styles = $builder->add_style( array() ); + foreach ( $builder->get_components() as $component ) { + if ( in_array( $component->get_id(), array( 'button_base', 'header_search' ), true ) ) { + $styles = $component->add_style( $styles ); + } + } + + $css = (string) new Dynamic_Selector( $styles ); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--bgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnbg: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnhovershadow:0px 0px 5px rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--primarybtnborderradius:0;', $css ); + } + + /** + * Malformed stored component shadows are normalized before output. + */ + public function test_header_component_css_handles_malformed_stored_shadow(): void { + $appearance = neve_get_button_appearance_default(); + $appearance['useShadow'] = true; + $appearance['shadowProperties'] = 'invalid'; + set_theme_mod( 'button_base_style_setting', $appearance ); + + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + $styles = $builder->add_style( array() ); + foreach ( $builder->get_components() as $component ) { + if ( 'button_base' === $component->get_id() ) { + $styles = $component->add_style( $styles ); + } + } + + $css = (string) new Dynamic_Selector( $styles ); + + $this->assertStringContainsString( '--primarybtnshadow:0px 0px 0px', $css ); + } + + /** + * Button appearance whose color fields can close their CSS declaration. + * + * @return array + */ + private function injected_appearance(): array { + $appearance = neve_get_button_appearance_default(); + foreach ( $this->color_fields() as $field ) { + $appearance[ $field ] = self::INJECTED_CSS; + } + foreach ( array( 'borderRadius', 'borderWidth' ) as $field ) { + $appearance[ $field ] = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '10;}body{background:lime}/*' ); + } + $appearance['type'] = 'outline'; + $appearance['useShadow'] = true; + $appearance['useShadowHover'] = true; + + return $appearance; + } + + /** + * Color fields stored by the button appearance control. + * + * @return string[] + */ + private function color_fields(): array { + return array( 'background', 'backgroundHover', 'text', 'textHover', 'shadowColor', 'shadowColorHover' ); + } +} diff --git a/tests/test-neve-row-color-css.php b/tests/test-neve-row-color-css.php new file mode 100644 index 0000000000..429fe1bb36 --- /dev/null +++ b/tests/test-neve-row-color-css.php @@ -0,0 +1,129 @@ +|false + */ + private $theme_mods; + + /** + * Snapshot the theme mods and read them uncached. + */ + public function setUp(): void { + parent::setUp(); + + $this->theme_mods = get_theme_mods(); + Mods::$no_cache = true; + } + + /** + * Restore the theme mods. + */ + public function tearDown(): void { + update_option( 'theme_mods_' . get_option( 'stylesheet' ), $this->theme_mods ); + Mods::$no_cache = false; + + parent::tearDown(); + } + + /** + * Stored row colors that would close their declaration are printed sanitized; other colors as stored. + */ + public function test_injected_row_colors_are_printed_sanitized(): void { + set_theme_mod( 'hfg_header_layout_main_border_color', self::INJECTED_CSS ); + set_theme_mod( 'hfg_header_layout_main_new_text_color', 'red' ); + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => self::INJECTED_CSS, + ) + ); + + $css = $this->header_css(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--rowbcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--bgcolor: rgba(1,2,3,1);', $css ); + $this->assertStringContainsString( '--color: red;', $css ); + } + + /** + * Stored image overlay colors are sanitized when printed. + */ + public function test_injected_row_overlay_color_is_printed_sanitized(): void { + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'image', + 'imageUrl' => '', + 'focusPoint' => array( 'x' => 0.5, 'y' => 0.5 ), + 'overlayColorValue' => self::INJECTED_CSS, + 'useFeatured' => false, + ) + ); + + $css = $this->header_css(); + + $this->assertStringNotContainsString( 'background:lime', $css ); + $this->assertStringContainsString( '--overlaycolor: rgba(1,2,3,1);', $css ); + } + + /** + * A malformed legacy row color is omitted from generated CSS. + */ + public function test_malformed_stored_row_color_is_not_printed(): void { + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => 'linear-gradient()', + ) + ); + + $this->assertSame( 0, preg_match( '/\.header-main\{[^}]*--bgcolor:/s', $this->header_css() ) ); + } + + /** + * Valid modern stored gradients remain present in generated CSS. + */ + public function test_modern_stored_gradient_is_printed(): void { + $gradient = 'linear-gradient(hwb(30 40% 20%), color-mix(in oklab, red 40%, blue) min(20%, 100px))'; + set_theme_mod( + 'hfg_header_layout_main_background', + array( + 'type' => 'color', + 'colorValue' => $gradient, + ) + ); + + $this->assertStringContainsString( '--bgcolor: ' . $gradient . ';', $this->header_css() ); + } + + /** + * Render the header builder's dynamic CSS. + * + * @return string + */ + private function header_css(): string { + $builder = \HFG\Main::get_instance()->get_builder( 'header' ); + + return (string) new Dynamic_Selector( $builder->add_style( array() ) ); + } +} diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index 787f649664..de1156469b 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -191,10 +191,62 @@ public function test_sanitize_colors_with_non_string_values() { // Valid values keep working as before. $this->assertSame( '#ff0000', neve_sanitize_colors( '#ff0000' ) ); + $this->assertSame( '#0008', neve_sanitize_colors( '#0008' ) ); + $this->assertSame( '#11223344', neve_sanitize_colors( ' #11223344 ' ) ); $this->assertSame( 'rgba(255, 0, 0, 1)', neve_sanitize_colors( 'rgba(255, 0, 0, 1)' ) ); $this->assertSame( 'var(--nv-primary-accent)', neve_sanitize_colors( 'var(--nv-primary-accent)' ) ); } + /** + * Optional typography controls retain their supported inherited weight. + */ + public function test_typography_sanitizer_keeps_inherited_font_weight() { + $value = array( + 'fontWeight' => 'none', + 'textTransform' => 'none', + 'fontSize' => array(), + 'lineHeight' => array(), + 'letterSpacing' => array(), + ); + + $this->assertSame( $value, neve_sanitize_typography_control( $value ) ); + foreach ( array( 'normal', 'bold', 'bolder', 'lighter' ) as $font_weight ) { + $value['fontWeight'] = $font_weight; + $this->assertSame( $font_weight, neve_sanitize_typography_control( $value )['fontWeight'] ); + } + + unset( $value['fontWeight'] ); + $this->assertArrayNotHasKey( 'fontWeight', neve_sanitize_typography_control( $value ) ); + } + + /** + * Blank spacing remains blank instead of overriding inherited spacing with zero. + */ + public function test_directional_css_sanitizer_keeps_blank_spacing() { + $value = array_fill_keys( array( 'top', 'right', 'bottom', 'left' ), '' ); + $meta = array( + 'key' => 'blank_spacing', + 'suffix' => 'px', + 'directional-prop' => \Neve\Core\Settings\Config::CSS_PROP_PADDING, + ); + + $this->assertSame( '', neve_sanitize_directional_css_rule( '--padding', $value, $meta, 'desktop' ) ); + } + + /** + * Declaration checks inspect JSON object keys as well as values. + */ + public function test_css_declaration_check_inspects_json_keys() { + $this->assertTrue( neve_closes_css_declaration( '{"x\\";}}body{background:lime}/*":0}' ) ); + } + + /** + * Valid font families remain available to component custom properties. + */ + public function test_font_family_css_rule_preserves_valid_fonts() { + $this->assertSame( ' --inheritedff: Arial;', neve_sanitize_font_family_css_rule( '--inheritedff', 'Arial' ) ); + } + /** * Logo data can already be decoded when it reaches the sanitizer. */ @@ -218,6 +270,8 @@ public function test_is_css_var() { $valid = [ 'var(--nv-primary-accent)', 'var(--nv-c-1,#E5E7EB)', + 'var(--nv-c-1, #0008)', + 'var(--nv-c-1, #11223344)', 'var(--nv-c-1, #e5e7eb)', 'var( --nv-site-bg , #fff )', 'var(--secondarybtnbg, transparent)', @@ -243,6 +297,11 @@ public function test_is_css_var() { 'var(--x)}body{background:red}', 'var(--x, url(evil.css))', 'var(--x, "quoted")', + 'var(--c5-missing,rgb(foo))', + 'var(--x, rgba(1,2,3,1)junk)', + 'var(--x, rgba(1,2,3,1);}body{background:lime}/*)', + 'var(--x, rgba(1,2,3,1)/*(*/);}body{background:lime}/*)', + 'var(--x, rgba(1,2,3,1))', 'linear-gradient(var(--a), var(--b))', '', [ 'var(--x)' ], @@ -278,10 +337,40 @@ public function test_sanitize_colors_gradients(): void { 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', 'radial-gradient(rgb(6,147,227) 0%,rgb(155,81,224) 100%)', 'radial-gradient(circle at center, #fff 0%, #000 100%)', + 'radial-gradient(at center top, red, blue)', + 'radial-gradient(at left 10px top 20px, red, blue)', + 'radial-gradient(circle at left 10px top, red, blue)', + 'radial-gradient(circle at top left 10px, red, blue)', + 'radial-gradient(at left 10px center, red, blue)', + 'radial-gradient(5em circle at top left, yellow, blue)', + 'radial-gradient(circle closest-side, red, blue)', 'conic-gradient(from 90deg, red, blue)', 'repeating-linear-gradient(45deg, #FFF 0 10px, transparent 10px 20px)', 'linear-gradient(var(--nv-primary-accent), var(--nv_c-1, #fff))', 'linear-gradient(hsl(120 50% 50% / .5), transparent)', + 'linear-gradient(red 0%, 50%, blue 100%)', + 'radial-gradient(red 0%, 25px, blue 100%)', + 'conic-gradient(red 0deg, 25%, blue 180deg)', + 'linear-gradient(in oklab, red, blue)', + 'linear-gradient(in oklch longer hue to right, red, blue)', + 'linear-gradient(to right in oklab, red, blue)', + 'radial-gradient(in display-p3 circle at center, red, blue)', + 'conic-gradient(in hsl longer hue from 45deg at center, red, blue)', + 'linear-gradient(red calc(10% - 1px), blue)', + 'linear-gradient(red calc(10% + 1px), blue)', + 'linear-gradient(hwb(30 40% 20%), blue)', + 'linear-gradient(lab(50% 20 30 / .5), oklch(60% .2 120))', + 'linear-gradient(color(display-p3 1 0 0), device-cmyk(0 1 1 0))', + 'linear-gradient(color-mix(in oklab, red 40%, blue), light-dark(white, black))', + 'linear-gradient(red min(20%, 100px), blue)', + 'linear-gradient(red max(10%, 2rem), blue)', + 'linear-gradient(red clamp(10%, 20vw, 50%), blue)', + 'linear-gradient(red calc(var(--stop) - 1px), blue)', + 'linear-gradient(red min(calc(20% - 1px), var(--stop, 10%)), blue)', + 'linear-gradient(red 10dvh, blue)', + 'linear-gradient(red calc(100% - 1lh), blue)', + 'radial-gradient(ellipse 10cqw 20cqh, red, blue)', + 'radial-gradient(circle +10px, red, blue)', ]; foreach ( $valid as $value ) { @@ -291,6 +380,49 @@ public function test_sanitize_colors_gradients(): void { $this->assertSame( 'linear-gradient(red, blue)', neve_sanitize_colors( ' linear-gradient(red, blue) ' ) ); $invalid = [ + 'linear-gradient()', + 'linear-gradient(foo)', + 'linear-gradient(red)', + 'linear-gradient(foo, red, blue)', + 'linear-gradient(red garbage, blue)', + 'linear-gradient(to left right, red, blue)', + 'radial-gradient(10, red, blue)', + 'linear-gradient(red 10deg, blue)', + 'conic-gradient(red 10px, blue)', + 'linear-gradient(rgb(1 / 2 / 3), blue)', + 'radial-gradient(circle 10px 20px, red, blue)', + 'radial-gradient(circle -10px, red, blue)', + 'radial-gradient(at left right, red, blue)', + 'radial-gradient(10%, red, blue)', + 'radial-gradient(ellipse -10px 20px, red, blue)', + 'linear-gradient(1.deg, red, blue)', + 'linear-gradient(rgb(1.,2,3), blue)', + 'linear-gradient(rgb(255, 0%, 0), blue)', + 'linear-gradient(var(--c5-missing,rgb(foo)),#fff)', + 'linear-gradient(50%, red, blue)', + 'linear-gradient(red, 50%)', + 'linear-gradient(red, 50%, 60%, blue)', + 'linear-gradient(red, 50% 60%, blue)', + 'conic-gradient(red, 10px, blue)', + 'linear-gradient(in banana, red, blue)', + 'linear-gradient(red calc(foo - 1px), blue)', + 'linear-gradient(red calc(10% - ), blue)', + 'linear-gradient(hwb(30 40%), blue)', + 'linear-gradient(lab(50% 20), blue)', + 'linear-gradient(color(display-p3 1 0), blue)', + 'linear-gradient(color-mix(in banana, red, blue), white)', + 'linear-gradient(color-mix(in oklab, red, url(foo)), white)', + 'linear-gradient(red min(20%, foo), blue)', + 'linear-gradient(red clamp(10%, 20%), blue)', + 'linear-gradient(red calc(var(bad) - 1px), blue)', + 'linear-gradient(red calc(10%-1px), blue)', + 'linear-gradient(red calc(1px + 2), blue)', + 'linear-gradient(red calc(1px + 0), blue)', + 'linear-gradient(red min(10%, 2), blue)', + 'linear-gradient(red var(--stop, 2), blue)', + 'linear-gradient(color-mix(in oklab, red -10%, blue), white)', + 'linear-gradient(color-mix(in oklab, red 110%, blue), white)', + 'linear-gradient(color-mix(in oklab, red 0%, blue 0%), white)', 'linear-gradient(red,blue)', 'linear-gradient(red,blue);}body{display:none', 'linear-gradient(red,blue),url(//example.com/t.png)', @@ -307,7 +439,7 @@ public function test_sanitize_colors_gradients(): void { } /** - * Standalone rgb(), hsl() and hsla() colors are kept; anything around them is rejected. + * Standalone CSS function colors are kept; anything around them is rejected. */ public function test_sanitize_colors_keeps_function_colors(): void { $valid = [ @@ -318,7 +450,13 @@ public function test_sanitize_colors_keeps_function_colors(): void { 'rgba(1,2,3)', 'rgba(255,0,0,.5)', 'hsl(120, 50%, 50%)', + 'hsl(120deg, 50%, 50%)', 'hsla(120, 50%, 50%, 0.5)', + 'hwb(30 40% 20%)', + 'lab(50% 40 59)', + 'oklch(60% .2 120)', + 'color(display-p3 1 0 0)', + 'color-mix(in oklab, red 40%, blue)', ]; foreach ( $valid as $value ) { @@ -330,14 +468,18 @@ public function test_sanitize_colors_keeps_function_colors(): void { $this->assertSame( 'rgba(1,2,3,0.5)', neve_sanitize_colors( 'rgba(1,2,3,.5);}body{display:none' ) ); $invalid = [ + 'rgb(,)', + 'rgba(1,)', 'rgb(1,2,3);}body{display:none', 'rgb(1,2,3)),url(//example.com/t.png)', 'rgb(1,2,3)', 'rgb(var(--x),2,3)', + 'rgb(255, 0%, 0)', + 'rgb(10%,0,0)', + 'hsl(0,0,0)', + 'hsl(120, 50, 50)', 'rgb(1,2,3', '#rgb(1,2,3)', - 'hsl(120deg, 50%, 50%)', - 'lab(50% 40 59)', ]; foreach ( $invalid as $value ) {