Make WordPress Core

Ticket #8446: 8446-valid-classnames-for-post-categories-and-tags.patch

File 8446-valid-classnames-for-post-categories-and-tags.patch, 2.2 KB (added by hakre, 16 years ago)

extended patch, incl. a new function to filter classname(s), adding valid classes for tags and categories, ids preserved in their own classes.

  • wp-includes/formatting.php

     
    7676}
    7777
    7878/**
     79 * clean invalid chars from css class names
     80 *
     81 * invalid chars will be replaced with '_', if the class
     82 * should not be treated as a suffix for an existing class,
     83 * then invalid classes will be prefixed with 'a_' to
     84 * become valid again.
     85 *
     86 * @since 2.7.2
     87 *
     88 * @param  array|string $class  array or string with class(es)
     89 * @param  bool         $suffix optional wether or not class(es)
     90 *                                                              are treated as being a suffix
     91 *                                                              (defaults to true)
     92 * @return string       classname(es) cleaned 
     93 */
     94function clean_css_classnames($class, $suffix = true)
     95{
     96        if (is_array($class)) {
     97                $return = array();
     98                foreach($class as $single)
     99                        $return[] = clean_css_classnames($single, $suffix);
     100                return $return;
     101        }       
     102       
     103        $class = preg_replace('|[^_a-z0-9-]|i', '_', $class);
     104       
     105        if (false == $suffix && 0 == preg_match('|^-?[_a-z]+[_a-z0-9-]*$|i', $class))
     106                $class = 'a_' . $class;
     107                       
     108        return $class;
     109}
     110
     111/**
    79112 * Accepts matches array from preg_replace_callback in wpautop() or a string.
    80113 *
    81114 * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
  • wp-includes/post-template.php

     
    320320                if ( empty($cat->slug ) )
    321321                        continue;
    322322                $classes[] = 'category-' . $cat->slug;
     323                $classes[] = 'categoryid-' . $cat->term_id;
    323324        }
    324325
    325326        // Tags
    326327        foreach ( (array) get_the_tags($post->ID) as $tag ) {
    327328                if ( empty($tag->slug ) )
    328329                        continue;
    329                 $classes[] = 'tag-' . $tag->slug;
     330                $classes[] = 'tag-' . $tag->slug;               
     331                $classes[] = 'tagid-' . $tag->term_id;
    330332        }
    331333
    332334        if ( !empty($class) ) {
     
    334336                        $class = preg_split('#\s+#', $class);
    335337                $classes = array_merge($classes, $class);
    336338        }
     339       
     340        $classes = clean_css_classnames($classes, false);
    337341
    338342        return apply_filters('post_class', $classes, $class, $post_id);
    339343}