Skip to content

Instantly share code, notes, and snippets.

@mintindeed
Created May 24, 2012 22:07
Show Gist options
  • Save mintindeed/2784483 to your computer and use it in GitHub Desktop.
Save mintindeed/2784483 to your computer and use it in GitHub Desktop.
/**
* function to implement [pmc_post_top_tag_names] shortcode to fetch top N tag names for a post
*/
public function post_top_tag_names($attrs) {
extract(shortcode_atts(array(
'post' => 0,
'count' => 3,
'tags' => 'strong,em'
), $attrs));
$_tags_enum = array('strong', 'em', 'b', 'i'); //html tag whitelist
$post = intval($post);
$count = intval($count);
$tags = trim(strip_tags($tags)); //lets strip out any HTML/PHP tags in there
$tags = str_replace('"', '', str_replace("'", '', $tags)); //replace any quotes etc
if( empty($tags) ) {
return '';
}
$top_tags = $this->_get_post_top_tag_names($post, $count);
if( ! is_array($top_tags) ) {
$top_tags = explode(', ', $this->_defaults['pmc_post_top_tag_names']['tags']);
}
$tags = explode(',', $tags);
foreach( $top_tags as $key => $value ) {
foreach( $tags as $tag ) {
$tag = trim($tag);
if( ! in_array($tag, $_tags_enum) ) {
continue; //if HTML tag is not in whitelist then we don't apply it
}
$top_tags[$key] = '<' . $tag . '>' . esc_html($value) . '</' . $tag . '>';
}
}
//if there are just two tag names then use "and" between them else make them comma separated
$top_tags_str = ( count($top_tags) == 2 ) ? implode(' and ', $top_tags) : implode(', ', $top_tags);
unset($post, $count, $tags, $top_tags);
return $top_tags_str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment