Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boost: Update cache exceptions UI to indicate if there's an invalid pattern #35984

Merged
merged 4 commits into from
Feb 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ const BypassPatterns = ( {
// @todo - add proper link.
const exclusionsLink = 'https://jetpack.com';

const validateInputValue = ( value: string ) => {
setInputValue( value );
setInputInvalid( ! validatePatterns( value ) );
}

const validatePatterns = ( value: string ) => {
const lines = value.split( '\n' ).map( line => line.trim() ).filter( line => line.trim() !== '' );

// check if it's a valid regex
try {
lines.forEach( line => new RegExp( line ) );
} catch ( e ) {
return false;
}

return true;
}

useEffect( () => {
setInputValue( patterns );
}, [ patterns ] );
Expand All @@ -182,7 +200,7 @@ const BypassPatterns = ( {
<textarea
value={ inputValue }
rows={ 3 }
onChange={ e => setInputValue( e.target.value ) }
onChange={ e => validateInputValue( e.target.value ) }
id="jb-cache-exceptions"
/>
<p className={ classNames( styles.description, styles[ 'error-message' ] ) }>
Expand Down Expand Up @@ -212,7 +230,7 @@ const BypassPatterns = ( {
{ __( 'An error occurred while saving changes. Please, try again.', 'jetpack-boost' ) }
</Notice>
) }
<Button disabled={ patterns === inputValue } onClick={ save } className={ styles.button }>
<Button disabled={ patterns === inputValue || inputInvalid } onClick={ save } className={ styles.button }>
{ __( 'Save', 'jetpack-boost' ) }
</Button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,72 @@ public function set( $value ) {
}

/**
* Sanitizes the given value, ensuring that it is a comma-separated list of unique, trimmed strings.
* Sanitizes the given value, ensuring that it is list of valid patterns.
*
* @param mixed $value The value to sanitize.
*
* @return string The sanitized value, as a comma-separated list of unique, trimmed strings.
* @return string The sanitized value, as a list.
*/
private function sanitize_value( $value ) {
if ( is_array( $value ) ) {
$value = array_values( array_unique( array_filter( array_map( 'trim', $value ) ) ) );
$value = array_values( array_unique( array_filter( array_map( 'trim', array_map( 'strtolower', $value ) ) ) ) );

$home_url = home_url( '/' );

foreach ( $value as &$path ) {
// Strip home URL (both secure and non-secure).
$path = str_ireplace(
array(
$home_url,
str_replace( 'http:', 'https:', $home_url ),
),
array(
'/',
'/',
),
$path
);

// Remove double shashes.
$path = str_replace( '//', '/', $path );

// Make sure there's a leading slash.
$path = '/' . ltrim( $path, '/' );

// Fix up any wildcards.
$path = $this->sanitize_wildcards( $path );
}
} else {
$value = array();
}

return $value;
}

/**
* Sanitize wildcards in a given path.
*
* @param string $path The path to sanitize.
* @return string The sanitized path.
*/
private function sanitize_wildcards( $path ) {
if ( ! $path ) {
return '';
}

$path_components = explode( '/', $path );
$arr = array(
'.*' => '(.*)',
'*' => '(.*)',
'(*)' => '(.*)',
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

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

Cheese* is a valid regexp which will match chees, cheese, cheeseeee, etc.

However, it seems like a pattern that users are very unlikely to want.

I think I'm ok with the guard-rails you've put here, because for most users this will put them on the right path. But I did want to note this will make some esoteric regexps impossible to enter.

'(.*)' => '(.*)',
);

foreach ( $path_components as &$path_component ) {
$path_component = strtr( $path_component, $arr );
}
$path = implode( '/', $path_components );

return $path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Update cache exceptions UI to show an error if there's an invalid pattern.


Loading