Last active
October 2, 2019 11:13
-
-
Save pyronaur/5387539 to your computer and use it in GitHub Desktop.
sanitize_html_class works just fine for a single class
But sometimes le wild `<span class="blue hedgehog">` appears, which is when you need this function, to validate both blue and hedgehog, Because sanitize_html_class doesn't allow spaces (because it's meant for a single class)
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
<?php | |
if ( ! function_exists( "sanitize_html_classes" ) && function_exists( "sanitize_html_class" ) ) { | |
/** | |
* sanitize_html_class works just fine for a single class | |
* Some times le wild <span class="blue hedgehog"> appears, which is when you need this function, | |
* to validate both blue and hedgehog, | |
* Because sanitize_html_class doesn't allow spaces. | |
* | |
* @uses sanitize_html_class | |
* @param (mixed: string/array) $class "blue hedgehog goes shopping" or array("blue", "hedgehog", "goes", "shopping") | |
* @param (mixed) $fallback Anything you want returned in case of a failure | |
* @return (mixed: string / $fallback ) | |
*/ | |
function sanitize_html_classes( $class, $fallback = null ) { | |
// Explode it, if it's a string | |
if ( is_string( $class ) ) { | |
$class = explode(" ", $class); | |
} | |
if ( is_array( $class ) && count( $class ) > 0 ) { | |
$class = array_map("sanitize_html_class", $class); | |
return implode(" ", $class); | |
} | |
else { | |
return sanitize_html_class( $class, $fallback ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!