diff --git a/globals/sanitize-functions.php b/globals/sanitize-functions.php index 1110624ae2..c3103881bd 100644 --- a/globals/sanitize-functions.php +++ b/globals/sanitize-functions.php @@ -56,7 +56,11 @@ 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 ) ); + $number = '-?(?:\d+(?:\.\d*)?|\.\d+)%?'; + // Three or four comma-separated components, or three space-separated ones. + $args = "{$number}\s*,\s*{$number}\s*,\s*{$number}(?:\s*,\s*{$number})?|{$number}\s+{$number}\s+{$number}"; + + return (bool) preg_match( '/^(?:rgb|rgba|hsl|hsla)\(\s*(?:' . $args . ')\s*\)$/', trim( $value ) ); } /** @@ -246,11 +250,21 @@ 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 mixed The value with every color field sanitized ('' when invalid); non-array values unchanged. */ function neve_sanitize_button_appearance( $value ) { + if ( ! is_array( $value ) ) { + return $value; + } + + foreach ( array( 'background', 'text', 'backgroundHover', 'textHover', 'shadowColor', 'shadowColorHover' ) as $field ) { + if ( isset( $value[ $field ] ) ) { + $value[ $field ] = neve_sanitize_colors( $value[ $field ] ); + } + } + return $value; } diff --git a/inc/abilities/abstract_ability.php b/inc/abilities/abstract_ability.php index ff6c2b0598..ca03598c46 100644 --- a/inc/abilities/abstract_ability.php +++ b/inc/abilities/abstract_ability.php @@ -236,6 +236,73 @@ protected function validation_issue( $code, $message, $field = '' ) { ); } + /** + * Sanitize a color sent to an ability the way the Customizer color controls do. + * + * @param mixed $value Incoming color. + * @return string|null Sanitized color ('' clears it), or null when a non-empty value is not a valid color. + */ + protected function sanitize_color_input( $value ) { + if ( ! is_string( $value ) ) { + return null; + } + + $color = neve_sanitize_colors( $value ); + + return ( '' === $color && '' !== trim( $value ) ) ? null : $color; + } + + /** + * Sanitize a builder background sent to an ability the way the Customizer background control does. + * + * @param mixed $value Incoming background. + * @return array|null Sanitized background, or null when it is invalid. + */ + protected function sanitize_background_input( $value ) { + if ( ! is_array( $value ) ) { + return null; + } + + $value = map_deep( $value, 'sanitize_text_field' ); + $background = neve_sanitize_background( $value ); + + return is_wp_error( $background ) || $this->blanks_sent_field( $value, $background ) ? null : $background; + } + + /** + * Sanitize a button appearance sent to an ability the way the Customizer button control does. + * + * @param mixed $value Incoming button appearance. + * @return array|null Sanitized appearance, or null when a color is invalid. + */ + protected function sanitize_button_appearance_input( $value ) { + if ( ! is_array( $value ) ) { + return null; + } + + $value = map_deep( $value, 'sanitize_text_field' ); + $appearance = neve_sanitize_button_appearance( $value ); + + return $this->blanks_sent_field( $value, $appearance ) ? null : $appearance; + } + + /** + * Whether sanitizing emptied a field that was sent with a value, i.e. the sanitizer rejected it. + * + * @param array $sent Value as sent. + * @param array $sanitized Value after the Customizer sanitizer. + * @return bool + */ + private function blanks_sent_field( array $sent, array $sanitized ) { + foreach ( $sanitized as $field => $field_value ) { + if ( '' === $field_value && isset( $sent[ $field ] ) && '' !== $sent[ $field ] ) { + return true; + } + } + + return false; + } + /** * Build a structured non-validation error payload for clients that collapse * WP_Error details into a generic transport error. diff --git a/inc/abilities/builder_configure.php b/inc/abilities/builder_configure.php index 894f753ce4..c2f9d86718 100644 --- a/inc/abilities/builder_configure.php +++ b/inc/abilities/builder_configure.php @@ -175,9 +175,10 @@ function ( $item ) use ( $component ) { * @param string $builder_id Builder id. * @param string $row Row key. * @param array $style Style block. + * @param string[] $warnings Receives a warning for each skipped field. * @return string[] Affected theme_mod keys. */ - protected function apply_row_style( $builder_id, $row, array $style ) { + protected function apply_row_style( $builder_id, $row, array $style, array &$warnings ) { $prefix = 'hfg_' . $builder_id . '_layout_' . $row; $affected = array(); @@ -186,12 +187,24 @@ protected function apply_row_style( $builder_id, $row, array $style ) { $affected[] = $prefix . '_height'; } if ( isset( $style['background'] ) ) { - set_theme_mod( $prefix . '_background', map_deep( $style['background'], 'sanitize_text_field' ) ); - $affected[] = $prefix . '_background'; + $background = $this->sanitize_background_input( $style['background'] ); + if ( null === $background ) { + /* translators: %s: input field name */ + $warnings[] = sprintf( __( 'Invalid value for "%s".', 'neve' ), 'row_style.' . $row . '.background' ); + } else { + set_theme_mod( $prefix . '_background', $background ); + $affected[] = $prefix . '_background'; + } } if ( isset( $style['text_color'] ) ) { - set_theme_mod( $prefix . '_new_text_color', sanitize_text_field( $style['text_color'] ) ); - $affected[] = $prefix . '_new_text_color'; + $text_color = $this->sanitize_color_input( $style['text_color'] ); + if ( null === $text_color ) { + /* translators: %s: input field name */ + $warnings[] = sprintf( __( 'Invalid value for "%s".', 'neve' ), 'row_style.' . $row . '.text_color' ); + } else { + set_theme_mod( $prefix . '_new_text_color', $text_color ); + $affected[] = $prefix . '_new_text_color'; + } } return $affected; diff --git a/inc/abilities/footer_configure.php b/inc/abilities/footer_configure.php index 0823ae157d..02045156fb 100644 --- a/inc/abilities/footer_configure.php +++ b/inc/abilities/footer_configure.php @@ -206,7 +206,7 @@ protected function run( array $input ) { if ( ! $dry_run && ! empty( $input['row_style'] ) && is_array( $input['row_style'] ) ) { foreach ( $input['row_style'] as $style ) { if ( isset( $style['row'] ) ) { - $this->apply_row_style( 'footer', $style['row'], $style ); + $this->apply_row_style( 'footer', $style['row'], $style, $warnings ); } } } diff --git a/inc/abilities/header_configure.php b/inc/abilities/header_configure.php index 81c2ccd9a4..d6d7d035e8 100644 --- a/inc/abilities/header_configure.php +++ b/inc/abilities/header_configure.php @@ -214,7 +214,7 @@ protected function run( array $input ) { if ( ! isset( $style['row'] ) ) { continue; } - $this->apply_row_style( 'header', $style['row'], $style ); + $this->apply_row_style( 'header', $style['row'], $style, $warnings ); } } diff --git a/inc/abilities/site_set_theme_mod.php b/inc/abilities/site_set_theme_mod.php index dc7b1c3a6a..70c026009d 100644 --- a/inc/abilities/site_set_theme_mod.php +++ b/inc/abilities/site_set_theme_mod.php @@ -172,7 +172,17 @@ protected function run( array $input ) { ); } - set_theme_mod( $key, $this->sanitize_value( $input['value'] ) ); + $value = $this->sanitize_setting( $key, $input['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( @@ -182,6 +192,29 @@ protected function run( array $input ) { ); } + /** + * Sanitize a value for its setting: colors and backgrounds as the Customizer does, the rest as text. + * + * @param string $key The allowlisted theme_mod key. + * @param mixed $value The incoming value. + * @return mixed Sanitized value, or null when a color or background is invalid. + */ + private function sanitize_setting( $key, $value ) { + if ( '_color' === substr( $key, -6 ) ) { + return $this->sanitize_color_input( $value ); + } + + if ( '_background' === substr( $key, -11 ) ) { + return $this->sanitize_background_input( $value ); + } + + if ( '_button_appearance' === substr( $key, -18 ) ) { + return $this->sanitize_button_appearance_input( $value ); + } + + return $this->sanitize_value( $value ); + } + /** * Lightly sanitize a value, preserving structured (array) settings. * diff --git a/tests/test-neve-ability-color-inputs.php b/tests/test-neve-ability-color-inputs.php new file mode 100644 index 0000000000..11d328bbd5 --- /dev/null +++ b/tests/test-neve-ability-color-inputs.php @@ -0,0 +1,233 @@ +execute( + array( + 'action' => 'set', + 'key' => 'neve_scroll_to_top_icon_color', + 'value' => self::MALFORMED_COLOR, + ) + ); + + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertFalse( get_theme_mod( 'neve_scroll_to_top_icon_color' ) ); + } + + public function test_set_theme_mod_saves_valid_and_empty_colors(): void { + $ability = new Site_Set_Theme_Mod(); + + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'neve_scroll_to_top_icon_color', + 'value' => ' rgb(255, 0, 0) ', + ) + ); + $this->assertTrue( $result['success'] ); + $this->assertSame( 'rgb(255, 0, 0)', get_theme_mod( 'neve_scroll_to_top_icon_color' ) ); + + $result = $ability->execute( + array( + 'action' => 'set', + 'key' => 'neve_scroll_to_top_icon_color', + 'value' => '', + ) + ); + $this->assertTrue( $result['success'] ); + $this->assertSame( '', get_theme_mod( 'neve_scroll_to_top_icon_color' ) ); + } + + public function test_set_theme_mod_rejects_non_string_color(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'neve_scroll_to_top_icon_color', + 'value' => array( '#fff' ), + ) + ); + + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertFalse( get_theme_mod( 'neve_scroll_to_top_icon_color' ) ); + } + + public function test_set_theme_mod_rejects_button_appearance_with_malformed_color(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'nav-icon_button_appearance', + 'value' => array( + 'type' => 'fill', + 'background' => self::MALFORMED_COLOR, + ), + ) + ); + + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertFalse( get_theme_mod( 'nav-icon_button_appearance' ) ); + } + + public function test_set_theme_mod_saves_valid_button_appearance(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'nav-icon_button_appearance', + 'value' => array( + 'type' => 'fill', + 'background' => 'var(--nv-primary-accent)', + 'text' => '#ffffff', + 'textHover' => '', + ), + ) + ); + + $this->assertTrue( $result['success'] ); + $appearance = get_theme_mod( 'nav-icon_button_appearance' ); + $this->assertSame( 'var(--nv-primary-accent)', $appearance['background'] ); + $this->assertSame( '#ffffff', $appearance['text'] ); + $this->assertSame( '', $appearance['textHover'] ); + } + + public function test_button_appearance_sanitizer_blanks_malformed_colors(): void { + $appearance = neve_sanitize_button_appearance( + array( + 'type' => 'fill', + 'background' => self::MALFORMED_COLOR, + 'text' => '#ffffff', + 'backgroundHover' => 'red;}body{display:none', + 'textHover' => 'rgba(0,0,0,0.5)', + 'shadowColor' => self::MALFORMED_COLOR, + 'shadowColorHover' => 'var(--nv-text-color)', + ) + ); + + $this->assertSame( 'fill', $appearance['type'] ); + $this->assertSame( '', $appearance['background'] ); + $this->assertSame( '#ffffff', $appearance['text'] ); + $this->assertSame( '', $appearance['backgroundHover'] ); + $this->assertSame( 'rgba(0,0,0,0.5)', $appearance['textHover'] ); + $this->assertSame( '', $appearance['shadowColor'] ); + $this->assertSame( 'var(--nv-text-color)', $appearance['shadowColorHover'] ); + } + + public function test_set_theme_mod_rejects_background_with_malformed_color(): void { + $result = ( new Site_Set_Theme_Mod() )->execute( + array( + 'action' => 'set', + 'key' => 'hfg_header_layout_sidebar_background', + 'value' => array( + 'type' => 'color', + 'colorValue' => self::MALFORMED_COLOR, + ), + ) + ); + + $this->assertFalse( $result['success'] ); + $this->assertSame( 'invalid_value', $result['error']['code'] ); + $this->assertFalse( get_theme_mod( 'hfg_header_layout_sidebar_background' ) ); + } + + public function test_header_row_style_skips_malformed_colors(): void { + $result = ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => self::MALFORMED_COLOR, + 'background' => array( + 'type' => 'color', + 'colorValue' => '#ffffff', + 'overlayColorValue' => self::MALFORMED_COLOR, + ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertContains( 'Invalid value for "row_style.main.text_color".', $result['warnings'] ); + $this->assertContains( 'Invalid value for "row_style.main.background".', $result['warnings'] ); + $this->assertFalse( get_theme_mod( 'hfg_header_layout_main_new_text_color' ) ); + $this->assertFalse( get_theme_mod( 'hfg_header_layout_main_background' ) ); + } + + public function test_header_row_style_saves_valid_colors(): void { + $result = ( new Header_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'main', + 'text_color' => 'var(--nv-text-color)', + 'background' => array( + 'type' => 'color', + 'colorValue' => '#ffffff', + ), + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertSame( array(), $result['warnings'] ); + $this->assertSame( 'var(--nv-text-color)', get_theme_mod( 'hfg_header_layout_main_new_text_color' ) ); + $this->assertSame( '#ffffff', get_theme_mod( 'hfg_header_layout_main_background' )['colorValue'] ); + } + + public function test_footer_row_style_skips_malformed_text_color(): void { + $result = ( new Footer_Configure() )->execute( + array( + 'row_style' => array( + array( + 'row' => 'bottom', + 'text_color' => self::MALFORMED_COLOR, + ), + ), + ) + ); + + $this->assertIsArray( $result ); + $this->assertContains( 'Invalid value for "row_style.bottom.text_color".', $result['warnings'] ); + $this->assertFalse( get_theme_mod( 'hfg_footer_layout_bottom_new_text_color' ) ); + } +} diff --git a/tests/test-neve-sanitization.php b/tests/test-neve-sanitization.php index ae5c1f4bfb..33d2fa5560 100644 --- a/tests/test-neve-sanitization.php +++ b/tests/test-neve-sanitization.php @@ -379,26 +379,40 @@ public function test_global_color_slugs_must_be_css_identifiers(): void { } /** - * Incomplete or non-finite rgba() colors are rejected; complete ones are normalized. + * Incomplete, malformed or non-finite function colors are rejected; complete ones are kept. */ public function test_sanitize_colors_rejects_incomplete_rgba(): void { $invalid = [ - 'rgba(1,2,3)', 'rgba(1,2)', 'rgba(', 'rgba', 'xrgba(1,2,3,4)', - 'rgba(1.5,2,3,1)', 'rgba(1,2,3,1e999)', + 'rgb(1,2,3,4,5)', + 'rgba(1,,2,3)', + 'rgb(1)', + 'rgb(,,)', + 'rgb(-)', + 'rgb(1.2.3,4,5)', + 'hsl(1 2 3 4)', ]; foreach ( $invalid as $value ) { + $this->assertFalse( neve_is_css_function_color( $value ), $value . ' should not be a function color' ); $this->assertSame( '', neve_sanitize_colors( $value ), $value . ' should be rejected' ); } - $this->assertSame( 'rgba(255,0,0,1)', neve_sanitize_colors( 'rgba( 255, 0, 0, 1 )' ) ); - $this->assertSame( 'rgba(1,2,3,0.5)', neve_sanitize_colors( 'rgba(1,2,3,.5)' ) ); - $this->assertSame( 'rgba(0,0,0,0)', neve_sanitize_colors( 'rgba(0,0,0,0)' ) ); + $valid = [ + 'rgba(255, 0, 0, 1)', + 'rgba(1.5,2,3,1)', + 'rgba(0,0,0,0)', + 'rgb(-1,0,0)', + 'rgb(255 0 0)', + ]; + + foreach ( $valid as $value ) { + $this->assertSame( $value, neve_sanitize_colors( $value ), $value . ' should be kept' ); + } } /** @@ -406,7 +420,10 @@ public function test_sanitize_colors_rejects_incomplete_rgba(): void { */ public function test_incomplete_rgba_border_color_uses_default(): void { $key = 'hfg_header_layout_main_border_color'; - set_theme_mod( $key, neve_sanitize_colors( 'rgba(1,2,3)' ) ); + set_theme_mod( $key, neve_sanitize_colors( 'rgba(1,2)' ) ); + // Read the saved mod, not a value cached by an earlier test. + $no_cache = \Neve\Core\Settings\Mods::$no_cache; + \Neve\Core\Settings\Mods::$no_cache = true; try { $css = (string) new \Neve\Core\Styles\Dynamic_Selector( @@ -420,6 +437,8 @@ public function test_incomplete_rgba_border_color_uses_default(): void { ] ); } finally { + \Neve\Core\Settings\Mods::$no_cache = $no_cache; + remove_theme_mod( $key ); \Neve\Core\Settings\Mods::set( $key, null ); }