From c5f59ea10432723437909a5859fb003f932674c9 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 14:04:28 -0300 Subject: [PATCH 01/13] add sanitize_from_schema util function --- .../font-library/class-wp-font-utils.php | 73 +++++ .../wpFontUtils/sanitizeFromSchema.php | 269 ++++++++++++++++++ 2 files changed, 342 insertions(+) create mode 100644 phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index e6fdc5ae08f5d..a09f57ba12a59 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -133,5 +133,78 @@ 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 sanitizator to the values. + * + * @since 6.5.0 + * + * @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 ) ) { + return $tree; + } + + if ( ! 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; + } + + // Check if the value is an array and requires further processing. + if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { + + if ( ! wp_is_numeric_array( $value ) ) { + // If it is an associative or indexed array., process as a single object. + $tree[ $key ] = self::sanitize_from_schema( $value, $schema[ $key ] ); + + if ( empty( $tree[ $key ] ) ) { + unset( $tree[ $key ] ); + } + } else { + // If indexed, process each item in the array. + foreach ( $value as $item_key => $item_value ) { + if ( isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] ) ) { + $tree[ $key ][ $item_key ] = self::sanitize_from_schema( $item_value, $schema[ $key ][0] ); + } else { + $tree[ $key ][ $item_key ] = self::apply_sanitizator( $item_value, $schema[ $key ][0] ); + } + } + } + } elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $key ] ) ) { + unset( $tree[ $key ] ); + } else { + $tree[ $key ] = self::apply_sanitizator( $tree[ $key ], $schema[ $key ] ); + } + } + + return $tree; + } + + /** + * Apply the sanitizator to the value. + * + * @since 6.5.0 + * @param mixed $value The value to sanitize. + * @param mixed $sanitizator The sanitizator to apply. + * @return mixed The sanitized value. + */ + private static function apply_sanitizator( $value, $sanitizator ) { + if ( $sanitizator === null ) { + return $value; + + } + return call_user_func( $sanitizator, $value ); + } } } diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php new file mode 100644 index 0000000000000..98a80b607efe4 --- /dev/null +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -0,0 +1,269 @@ +assertSame( $result, $expected ); + } + + public function data_sanitize_from_schema() { + return array( + 'One level associative array' => array( + 'data' => array( + 'slug' => 'open - sans', + 'fontFamily' => 'Open Sans, sans-serif', + 'name' => 'Open Sans', + 'src' => 'https://wordpress.org/example.json', + ), + 'schema' => array( + 'slug' => 'sanitize_title', + 'name' => 'sanitize_text_field', + 'fontFamily' => 'sanitize_text_field', + 'src' => 'sanitize_url', + ), + 'expected' => array( + 'slug' => 'open-sansalertxss', + 'fontFamily' => 'Open Sans, sans-serif', + 'name' => 'Open Sans', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + ), + ), + 'Nested associative arrays' => array( + 'data' => array( + 'slug' => 'open - sans', + 'fontFamily' => 'Open Sans, sans-serif', + 'name' => 'Open Sans', + 'src' => 'https://wordpress.org/example.json', + 'nested' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'nested2' => array( + 'key3' => 'value3', + 'key4' => 'value4', + ), + ), + ), + 'schema' => array( + 'slug' => 'sanitize_title', + 'name' => 'sanitize_text_field', + 'fontFamily' => 'sanitize_text_field', + 'src' => 'sanitize_url', + 'nested' => array( + 'key1' => 'sanitize_text_field', + 'key2' => 'sanitize_text_field', + 'nested2' => array( + 'key3' => 'sanitize_text_field', + 'key4' => 'sanitize_text_field', + ), + ), + ), + 'expected' => array( + 'slug' => 'open-sansalertxss', + 'fontFamily' => 'Open Sans, sans-serif', + 'name' => 'Open Sans', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + 'nested' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'nested2' => array( + 'key3' => 'value3', + 'key4' => 'value4', + ), + ), + ), + ), + + 'Indexed arrays' => array( + 'data' => array( + 'slug' => 'oPeN SaNs', + 'enum' => array( + 'value1', + 'value2', + 'value3', + ), + ), + 'schema' => array( + 'slug' => 'sanitize_title', + 'enum' => array( 'sanitize_text_field' ), + ), + 'expected' => array( + 'slug' => 'open-sans', + 'enum' => array( 'value1', 'value2', 'value3' ), + ), + ), + + 'Nested indexed arrays' => array( + 'data' => array( + 'slug' => 'OPEN-SANS', + 'name' => 'Open Sans', + 'fontFace' => array( + array( + 'fontFamily' => 'Open Sans, sans-serif', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + ), + array( + 'fontFamily' => 'Open Sans, sans-serif', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + ), + ), + ), + 'schema' => array( + 'slug' => 'sanitize_title', + 'name' => 'sanitize_text_field', + 'fontFace' => array( + array( + 'fontFamily' => 'sanitize_text_field', + 'src' => 'sanitize_url', + ), + ), + ), + 'expected' => array( + 'slug' => 'open-sans', + 'name' => 'Open Sans', + 'fontFace' => array( + array( + 'fontFamily' => 'Open Sans, sans-serif', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + ), + array( + 'fontFamily' => 'Open Sans, sans-serif', + 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', + ), + ), + ), + ), + + 'Custom sanitization function' => array( + 'data' => array( + 'key1' => 'abc123edf456ghi789', + 'key2' => 'value2', + ), + 'schema' => array( + 'key1' => function ( $value ) { + // Remove the six first character. + return substr( $value, 6 ); + }, + 'key2' => function ( $value ) { + // Capitalize the value. + return strtoupper( $value ); + }, + ), + 'expected' => array( + 'key1' => 'edf456ghi789', + 'key2' => 'VALUE2', + ), + ), + + 'Null as schema value' => array( + 'data' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'nested' => array( + 'key3' => 'value3', + 'key4' => 'value4', + ), + ), + 'schema' => array( + 'key1' => null, + 'key2' => 'sanitize_text_field', + 'nested' => null, + ), + 'expected' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'nested' => array( + 'key3' => 'value3', + 'key4' => 'value4', + ), + ), + ), + + 'Keys to remove' => array( + 'data' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'unwanted1' => 'value', + 'unwanted2' => 'value', + 'nestedAssociative' => array( + 'key5' => 'value5', + 'unwanted3' => 'value', + ), + 'nestedIndexed' => array( + array( + 'key6' => 'value7', + 'unwanted4' => 'value', + ), + array( + 'key6' => 'value7', + 'unwanted5' => 'value', + ), + ), + + ), + 'schema' => array( + 'key1' => 'sanitize_text_field', + 'key2' => 'sanitize_text_field', + 'nestedAssociative' => array( + 'key5' => 'sanitize_text_field', + ), + 'nestedIndexed' => array( + array( + 'key6' => 'sanitize_text_field', + ), + ), + ), + 'expected' => array( + 'key1' => 'value1', + 'key2' => 'value2', + 'nestedAssociative' => array( + 'key5' => 'value5', + ), + 'nestedIndexed' => array( + array( + 'key6' => 'value7', + ), + array( + 'key6' => 'value7', + ), + ), + ), + ), + ); + } + + function test_sanitize_from_schema_with_invalid_data() { + $data = 'invalid data'; + $schema = array( + 'key1' => 'sanitize_text_field', + 'key2' => 'sanitize_text_field', + ); + + $result = WP_Font_Utils::sanitize_from_schema( $data, $schema ); + + $this->assertSame( $result, $data ); + } + + + function test_sanitize_from_schema_with_invalid_schema() { + $data = array( + 'key1' => 'value1', + 'key2' => 'value2', + ); + $schema = 'invalid schema'; + + $result = WP_Font_Utils::sanitize_from_schema( $data, $schema ); + + $this->assertSame( $result, array() ); + } +} From f1be42d0ffc2b003f326cf21cbf17bddd8dac9ba Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 14:14:24 -0300 Subject: [PATCH 02/13] simplify sanitize_from_schema --- .../font-library/class-wp-font-utils.php | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index a09f57ba12a59..168d1813eced2 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -146,12 +146,8 @@ function ( $elem ) { * @return array The sanitized data. */ public static function sanitize_from_schema( $tree, $schema ) { - if ( ! is_array( $tree ) ) { - return $tree; - } - - if ( ! is_array( $schema ) ) { - return array(); + if ( ! is_array( $tree ) || ! is_array( $schema ) ) { + return is_array( $tree ) ? array() : $tree; } foreach ( $tree as $key => $value ) { @@ -161,30 +157,30 @@ public static function sanitize_from_schema( $tree, $schema ) { continue; } - // Check if the value is an array and requires further processing. - if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { + $is_value_array = is_array( $value ); + $is_schema_array = is_array( $schema[ $key ] ); - if ( ! wp_is_numeric_array( $value ) ) { + 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_sanitizator( $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 ] ); - if ( empty( $tree[ $key ] ) ) { unset( $tree[ $key ] ); } - } else { - // If indexed, process each item in the array. - foreach ( $value as $item_key => $item_value ) { - if ( isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] ) ) { - $tree[ $key ][ $item_key ] = self::sanitize_from_schema( $item_value, $schema[ $key ][0] ); - } else { - $tree[ $key ][ $item_key ] = self::apply_sanitizator( $item_value, $schema[ $key ][0] ); - } - } } - } elseif ( is_array( $schema[ $key ] ) && ! is_array( $tree[ $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 ] ); - } else { - $tree[ $key ] = self::apply_sanitizator( $tree[ $key ], $schema[ $key ] ); + } elseif ( ! $is_schema_array ) { + // If the schema is not an array, apply the sanitizator to the value. + $tree[ $key ] = self::apply_sanitizator( $value, $schema[ $key ] ); } } From 60a4b2c141d61f13fe592dc2819de94d865c22d0 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 14:21:11 -0300 Subject: [PATCH 03/13] make test shorter --- .../fonts/font-library/wpFontUtils/sanitizeFromSchema.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index 98a80b607efe4..5bb164d5bc695 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -23,19 +23,16 @@ public function data_sanitize_from_schema() { 'data' => array( 'slug' => 'open - sans', 'fontFamily' => 'Open Sans, sans-serif', - 'name' => 'Open Sans', 'src' => 'https://wordpress.org/example.json', ), 'schema' => array( 'slug' => 'sanitize_title', - 'name' => 'sanitize_text_field', 'fontFamily' => 'sanitize_text_field', 'src' => 'sanitize_url', ), 'expected' => array( 'slug' => 'open-sansalertxss', 'fontFamily' => 'Open Sans, sans-serif', - 'name' => 'Open Sans', 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', ), ), @@ -43,7 +40,6 @@ public function data_sanitize_from_schema() { 'data' => array( 'slug' => 'open - sans', 'fontFamily' => 'Open Sans, sans-serif', - 'name' => 'Open Sans', 'src' => 'https://wordpress.org/example.json', 'nested' => array( 'key1' => 'value1', @@ -56,7 +52,6 @@ public function data_sanitize_from_schema() { ), 'schema' => array( 'slug' => 'sanitize_title', - 'name' => 'sanitize_text_field', 'fontFamily' => 'sanitize_text_field', 'src' => 'sanitize_url', 'nested' => array( @@ -71,7 +66,6 @@ public function data_sanitize_from_schema() { 'expected' => array( 'slug' => 'open-sansalertxss', 'fontFamily' => 'Open Sans, sans-serif', - 'name' => 'Open Sans', 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', 'nested' => array( 'key1' => 'value1', From 39b8b91d052b22ab938f34120ee0d9f36b4a6bd7 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 14:22:43 -0300 Subject: [PATCH 04/13] lint php --- .../fonts/font-library/wpFontUtils/sanitizeFromSchema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index 5bb164d5bc695..f9ca6e3460888 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -236,7 +236,7 @@ public function data_sanitize_from_schema() { ); } - function test_sanitize_from_schema_with_invalid_data() { + public function test_sanitize_from_schema_with_invalid_data() { $data = 'invalid data'; $schema = array( 'key1' => 'sanitize_text_field', @@ -249,7 +249,7 @@ function test_sanitize_from_schema_with_invalid_data() { } - function test_sanitize_from_schema_with_invalid_schema() { + public function test_sanitize_from_schema_with_invalid_schema() { $data = array( 'key1' => 'value1', 'key2' => 'value2', From 6d9be46b43afbd03d33a516283ae4365332138d6 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 15:34:08 -0300 Subject: [PATCH 05/13] yoda style --- lib/experimental/fonts/font-library/class-wp-font-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index 168d1813eced2..998b88eaf669d 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -196,7 +196,7 @@ public static function sanitize_from_schema( $tree, $schema ) { * @return mixed The sanitized value. */ private static function apply_sanitizator( $value, $sanitizator ) { - if ( $sanitizator === null ) { + if ( null === $sanitizator ) { return $value; } From b25aec7b115c1725ff372f2171ac191ecd1686eb Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 1 Feb 2024 15:35:25 -0300 Subject: [PATCH 06/13] update comment --- lib/experimental/fonts/font-library/class-wp-font-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index 998b88eaf669d..15a1924a65bee 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -1,8 +1,8 @@ Date: Fri, 2 Feb 2024 11:01:08 -0300 Subject: [PATCH 07/13] fix word non -existing in english --- .../fonts/font-library/class-wp-font-utils.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index 15a1924a65bee..75200fa3ae581 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -137,7 +137,7 @@ function ( $elem ) { /** * 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 sanitizator to the values. + * It removes the keys not in the schema and applies the sanitizer to the values. * * @since 6.5.0 * @@ -166,7 +166,7 @@ public static function sanitize_from_schema( $tree, $schema ) { 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_sanitizator( $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. @@ -179,8 +179,8 @@ public static function sanitize_from_schema( $tree, $schema ) { // 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 sanitizator to the value. - $tree[ $key ] = self::apply_sanitizator( $value, $schema[ $key ] ); + // If the schema is not an array, apply the sanitizer to the value. + $tree[ $key ] = self::apply_sanitizer( $value, $schema[ $key ] ); } } @@ -188,19 +188,19 @@ public static function sanitize_from_schema( $tree, $schema ) { } /** - * Apply the sanitizator to the value. + * Apply the sanitizer to the value. * * @since 6.5.0 * @param mixed $value The value to sanitize. - * @param mixed $sanitizator The sanitizator to apply. + * @param mixed $sanitizer The sanitizer to apply. * @return mixed The sanitized value. */ - private static function apply_sanitizator( $value, $sanitizator ) { - if ( null === $sanitizator ) { + private static function apply_sanitizer( $value, $sanitizer ) { + if ( null === $sanitizer ) { return $value; } - return call_user_func( $sanitizator, $value ); + return call_user_func( $sanitizer, $value ); } } } From df387c0ccfd9f1772c7b194f2a4751d69a931c60 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:03:33 -0300 Subject: [PATCH 08/13] docblock formatting --- lib/experimental/fonts/font-library/class-wp-font-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index 75200fa3ae581..fc6dc514bf791 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -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 ) { @@ -143,6 +145,7 @@ function ( $elem ) { * * @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 ) { @@ -193,6 +196,7 @@ public static function sanitize_from_schema( $tree, $schema ) { * @since 6.5.0 * @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 ) { From f5744ec79e13319831d786a67585fe1c64f702d4 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:06:44 -0300 Subject: [PATCH 09/13] adding params comment in test --- .../fonts/font-library/wpFontUtils/sanitizeFromSchema.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index f9ca6e3460888..23a40b74c2f35 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -10,6 +10,10 @@ class Tests_Fonts_WpFontUtils_SanitizeFromSchema extends WP_UnitTestCase { /** * @dataProvider data_sanitize_from_schema + * + * @param array $data Data to sanitize. + * @param array $schema Schema to use for sanitization. + * @param array $expected Expected result. */ public function test_sanitize_from_schema( $data, $schema, $expected ) { $result = WP_Font_Utils::sanitize_from_schema( $data, $schema ); From acee66fcc4a730baaf36d06b06b8b41f607138d1 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:24:21 -0300 Subject: [PATCH 10/13] add test case --- .../font-library/class-wp-font-utils.php | 10 +++-- .../wpFontUtils/sanitizeFromSchema.php | 43 ++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index fc6dc514bf791..a0f35c02b6e19 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -150,7 +150,7 @@ function ( $elem ) { */ public static function sanitize_from_schema( $tree, $schema ) { if ( ! is_array( $tree ) || ! is_array( $schema ) ) { - return is_array( $tree ) ? array() : $tree; + return array(); } foreach ( $tree as $key => $value ) { @@ -174,9 +174,6 @@ public static function sanitize_from_schema( $tree, $schema ) { } else { // If it is an associative or indexed array., process as a single object. $tree[ $key ] = self::sanitize_from_schema( $value, $schema[ $key ] ); - if ( empty( $tree[ $key ] ) ) { - unset( $tree[ $key ] ); - } } } elseif ( ! $is_value_array && $is_schema_array ) { // If the value is not an array but the schema is, remove the key. @@ -185,6 +182,11 @@ public static function sanitize_from_schema( $tree, $schema ) { // 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; diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index 23a40b74c2f35..3cb8366b3eca3 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -40,6 +40,7 @@ public function data_sanitize_from_schema() { 'src' => 'https://wordpress.org/example.json/stylescriptalert(xss)/script', ), ), + 'Nested associative arrays' => array( 'data' => array( 'slug' => 'open - sans', @@ -237,6 +238,46 @@ public function data_sanitize_from_schema() { ), ), ), + + 'With empty structure' => array( + 'data' => array( + 'slug' => 'open-sans', + 'nested' => array( + 'key1' => 'value', + 'nested2' => array( + 'key2' => 'value', + 'nested3' => array( + 'nested4' => array( + ), + ), + ), + ), + ), + 'schema' => array( + 'slug' => 'sanitize_title', + 'nested' => array( + 'key1' => 'sanitize_text_field', + 'nested2' => array( + 'key2' => 'sanitize_text_field', + 'nested3' => array( + 'key3' => 'sanitize_text_field', + 'nested4' => array( + 'key4' => 'sanitize_text_field', + ), + ), + ), + ), + ), + 'expected' => array( + 'slug' => 'open-sans', + 'nested' => array( + 'key1' => 'value', + 'nested2' => array( + 'key2' => 'value', + ), + ), + ), + ), ); } @@ -249,7 +290,7 @@ public function test_sanitize_from_schema_with_invalid_data() { $result = WP_Font_Utils::sanitize_from_schema( $data, $schema ); - $this->assertSame( $result, $data ); + $this->assertSame( $result, array() ); } From 78a0b848b437cdd7ccde0534ed4406afe1683f57 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:25:43 -0300 Subject: [PATCH 11/13] adding access private to sanitize_from_schema --- lib/experimental/fonts/font-library/class-wp-font-utils.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/experimental/fonts/font-library/class-wp-font-utils.php b/lib/experimental/fonts/font-library/class-wp-font-utils.php index a0f35c02b6e19..edf466fc034b0 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-utils.php +++ b/lib/experimental/fonts/font-library/class-wp-font-utils.php @@ -142,6 +142,8 @@ function ( $elem ) { * 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. From 779189c3a0e2ac3c9f701eceb98330c7d3f3026e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:32:11 -0300 Subject: [PATCH 12/13] php format --- .../fonts/class-wp-font-utils.php | 12 ++++----- .../wpFontUtils/sanitizeFromSchema.php | 25 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php index edf466fc034b0..72a362a3a42a4 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-utils.php @@ -27,7 +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 ) { @@ -76,7 +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 ) { @@ -142,12 +142,12 @@ function ( $elem ) { * It removes the keys not in the schema and applies the sanitizer to the values. * * @since 6.5.0 - * - * @access private + * + * @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 ) { @@ -200,7 +200,7 @@ public static function sanitize_from_schema( $tree, $schema ) { * @since 6.5.0 * @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 ) { diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index 3cb8366b3eca3..350bed7f6974e 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -10,7 +10,7 @@ class Tests_Fonts_WpFontUtils_SanitizeFromSchema extends WP_UnitTestCase { /** * @dataProvider data_sanitize_from_schema - * + * * @param array $data Data to sanitize. * @param array $schema Schema to use for sanitization. * @param array $expected Expected result. @@ -239,28 +239,27 @@ public function data_sanitize_from_schema() { ), ), - 'With empty structure' => array( + 'With empty structure' => array( 'data' => array( - 'slug' => 'open-sans', - 'nested' => array( + 'slug' => 'open-sans', + 'nested' => array( 'key1' => 'value', 'nested2' => array( - 'key2' => 'value', + 'key2' => 'value', 'nested3' => array( - 'nested4' => array( - ), + 'nested4' => array(), ), ), ), ), 'schema' => array( - 'slug' => 'sanitize_title', - 'nested' => array( + 'slug' => 'sanitize_title', + 'nested' => array( 'key1' => 'sanitize_text_field', 'nested2' => array( - 'key2' => 'sanitize_text_field', + 'key2' => 'sanitize_text_field', 'nested3' => array( - 'key3' => 'sanitize_text_field', + 'key3' => 'sanitize_text_field', 'nested4' => array( 'key4' => 'sanitize_text_field', ), @@ -269,8 +268,8 @@ public function data_sanitize_from_schema() { ), ), 'expected' => array( - 'slug' => 'open-sans', - 'nested' => array( + 'slug' => 'open-sans', + 'nested' => array( 'key1' => 'value', 'nested2' => array( 'key2' => 'value', From 7790faeccba482d4ba530f8a6245fba7b5ca7ca3 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 2 Feb 2024 11:33:29 -0300 Subject: [PATCH 13/13] adding test group comment --- .../fonts/font-library/wpFontUtils/sanitizeFromSchema.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php index 350bed7f6974e..88983fe15a14e 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/sanitizeFromSchema.php @@ -4,7 +4,10 @@ * * @package WordPress * @subpackage Font Library - * * + * + * @group fonts + * @group font-library + * * @covers WP_Font_Utils::sanitize_from_schema */ class Tests_Fonts_WpFontUtils_SanitizeFromSchema extends WP_UnitTestCase {