-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Font Library: Add sanitize from schema util #58571
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c5f59ea
add sanitize_from_schema util function
matiasbenedetto f1be42d
simplify sanitize_from_schema
matiasbenedetto 60a4b2c
make test shorter
matiasbenedetto 39b8b91
lint php
matiasbenedetto 6d9be46
yoda style
matiasbenedetto b25aec7
update comment
matiasbenedetto a436dcb
fix word non -existing in english
matiasbenedetto df387c0
docblock formatting
matiasbenedetto f5744ec
adding params comment in test
matiasbenedetto acee66f
add test case
matiasbenedetto 78a0b84
adding access private to sanitize_from_schema
matiasbenedetto 957b032
Merge branch 'trunk' into add/sanitize-from-schema
matiasbenedetto 779189c
php format
matiasbenedetto 7790fae
adding test group comment
matiasbenedetto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,8 @@ | ||||||||||||
<?php | ||||||||||||
/** | ||||||||||||
* Fonts Family Utils class. | ||||||||||||
* Font Utils class. | ||||||||||||
* | ||||||||||||
* This file contains utils fot Font Family class. | ||||||||||||
* This file contains utils related to the Font Library. | ||||||||||||
* | ||||||||||||
* @package WordPress | ||||||||||||
* @subpackage Font Library | ||||||||||||
|
@@ -27,6 +27,7 @@ class WP_Font_Utils { | |||||||||||
* @access private | ||||||||||||
* | ||||||||||||
* @param string $font_family Font family attribute. | ||||||||||||
* | ||||||||||||
* @return string The formatted font family attribute. | ||||||||||||
*/ | ||||||||||||
public static function format_font_family( $font_family ) { | ||||||||||||
|
@@ -75,6 +76,7 @@ function ( $family ) { | |||||||||||
* @type string $fontStretch Optional font stretch, defaults to '100%'. | ||||||||||||
* @type string $unicodeRange Optional unicode range, defaults to 'U+0-10FFFF'. | ||||||||||||
* } | ||||||||||||
* | ||||||||||||
* @return string Font face slug. | ||||||||||||
*/ | ||||||||||||
public static function get_font_face_slug( $settings ) { | ||||||||||||
|
@@ -133,5 +135,80 @@ function ( $elem ) { | |||||||||||
|
||||||||||||
return join( ';', $slug_elements ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Sanitize a tree of data using an schema that defines the sanitization to apply to each key. | ||||||||||||
* | ||||||||||||
* It removes the keys not in the schema and applies the sanitizer to the values. | ||||||||||||
* | ||||||||||||
* @since 6.5.0 | ||||||||||||
* | ||||||||||||
* @access private | ||||||||||||
* | ||||||||||||
* @param array $tree The data to sanitize. | ||||||||||||
* @param array $schema The schema used for sanitization. | ||||||||||||
* | ||||||||||||
* @return array The sanitized data. | ||||||||||||
*/ | ||||||||||||
public static function sanitize_from_schema( $tree, $schema ) { | ||||||||||||
if ( ! is_array( $tree ) || ! is_array( $schema ) ) { | ||||||||||||
return array(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
foreach ( $tree as $key => $value ) { | ||||||||||||
// Remove keys not in the schema or with null/empty values. | ||||||||||||
if ( ! array_key_exists( $key, $schema ) ) { | ||||||||||||
unset( $tree[ $key ] ); | ||||||||||||
continue; | ||||||||||||
} | ||||||||||||
|
||||||||||||
$is_value_array = is_array( $value ); | ||||||||||||
$is_schema_array = is_array( $schema[ $key ] ); | ||||||||||||
|
||||||||||||
if ( $is_value_array && $is_schema_array ) { | ||||||||||||
if ( wp_is_numeric_array( $value ) ) { | ||||||||||||
// If indexed, process each item in the array. | ||||||||||||
foreach ( $value as $item_key => $item_value ) { | ||||||||||||
$tree[ $key ][ $item_key ] = isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] ) | ||||||||||||
? self::sanitize_from_schema( $item_value, $schema[ $key ][0] ) | ||||||||||||
: self::apply_sanitizer( $item_value, $schema[ $key ][0] ); | ||||||||||||
} | ||||||||||||
} else { | ||||||||||||
// If it is an associative or indexed array., process as a single object. | ||||||||||||
$tree[ $key ] = self::sanitize_from_schema( $value, $schema[ $key ] ); | ||||||||||||
} | ||||||||||||
} elseif ( ! $is_value_array && $is_schema_array ) { | ||||||||||||
// If the value is not an array but the schema is, remove the key. | ||||||||||||
unset( $tree[ $key ] ); | ||||||||||||
} elseif ( ! $is_schema_array ) { | ||||||||||||
// If the schema is not an array, apply the sanitizer to the value. | ||||||||||||
$tree[ $key ] = self::apply_sanitizer( $value, $schema[ $key ] ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Remove keys with null/empty values. | ||||||||||||
if ( empty( $tree[ $key ] ) ) { | ||||||||||||
unset( $tree[ $key ] ); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return $tree; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Apply the sanitizer to the value. | ||||||||||||
* | ||||||||||||
* @since 6.5.0 | ||||||||||||
* @param mixed $value The value to sanitize. | ||||||||||||
Comment on lines
+200
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
* @param mixed $sanitizer The sanitizer to apply. | ||||||||||||
Comment on lines
+201
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
* | ||||||||||||
* @return mixed The sanitized value. | ||||||||||||
*/ | ||||||||||||
private static function apply_sanitizer( $value, $sanitizer ) { | ||||||||||||
if ( null === $sanitizer ) { | ||||||||||||
return $value; | ||||||||||||
|
||||||||||||
} | ||||||||||||
return call_user_func( $sanitizer, $value ); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.