Make WordPress Core


Ignore:
Timestamp:
05/22/2009 05:44:26 PM (15 years ago)
Author:
westi
Message:

Introduce sanitise_css_classname() and use it to give categories, tags, users etc meaningful classnames where possible. Falls back to the id if not. Fixes #8446.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r11414 r11433  
    717717        return false;
    718718    return $orderby;
     719}
     720
     721/**
     722 * Santises a css classname to ensure it only contains valid characters
     723 *
     724 * Strips the classname down to A-Z,a-z,0-9,'-' if this results in an empty
     725 * string then it will return the alternative value supplied.
     726 * 
     727 * @param string $classname The classname to be sanitised
     728 * @param string $alternative The value to return if the sanitisation end's up as an empty string.
     729 * @return string The sanitised value
     730 */
     731function sanitise_css_classname($classname, $alternative){
     732    //Strip out any % encoded octets
     733    $sanitised = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $classname);
     734   
     735    //Limit to A-Z,a-z,0-9,'-'
     736    $sanitised = preg_replace('/[^A-Za-z0-9-]/', '', $sanitised);
     737   
     738    if ('' == $sanitised)
     739        $sanitised = $alternative;
     740   
     741    return apply_filters('sanitise_css_classname',$sanitised, $classname, $alternative);   
    719742}
    720743
Note: See TracChangeset for help on using the changeset viewer.