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

Font Library: Add sanitize from schema util #58571

Merged
merged 14 commits into from
Feb 2, 2024
81 changes: 79 additions & 2 deletions lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php
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
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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.
Comment on lines +148 to +149
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param array $tree The data to sanitize.
* @param array $schema The schema used for sanitization.
* @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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @since 6.5.0
* @param mixed $value The value to sanitize.
* @since 6.5.0
* @param mixed $value The value to sanitize.

* @param mixed $sanitizer The sanitizer to apply.
Comment on lines +201 to +202
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param mixed $value The value to sanitize.
* @param mixed $sanitizer The sanitizer to apply.
* @param mixed $value The value to sanitize.
* @param mixed $sanitizer The sanitizer to apply.

*
* @return mixed The sanitized value.
*/
private static function apply_sanitizer( $value, $sanitizer ) {
if ( null === $sanitizer ) {
return $value;

}
return call_user_func( $sanitizer, $value );
}
}
}
Loading
Loading