Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions globals/sanitize-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
67 changes: 67 additions & 0 deletions inc/abilities/abstract_ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>|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<string, mixed>|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<string, mixed> $sent Value as sent.
* @param array<string, mixed> $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.
Expand Down
23 changes: 18 additions & 5 deletions inc/abilities/builder_configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ function ( $item ) use ( $component ) {
* @param string $builder_id Builder id.
* @param string $row Row key.
* @param array<mixed> $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();

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion inc/abilities/footer_configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion inc/abilities/header_configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down
35 changes: 34 additions & 1 deletion inc/abilities/site_set_theme_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Comment on lines +196 to +200

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments The sanitize_setting() docblock is now stale. It says "colors and backgrounds as the Customizer does, the rest as text" and "null when a color or background is invalid". The new _button_appearance branch at line 211 follows neither sentence. Write "colors, backgrounds and button appearances as the Customizer does, the rest as text". Change the @return text to "null when a color, background or button appearance 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 );
}
Comment thread
pirate-bot marked this conversation as resolved.
Resolved

/**
* Lightly sanitize a value, preserving structured (array) settings.
*
Expand Down
Loading
Loading