<?php
// ===== Widgets ======================================================
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '<ul><li>',
        'after_widget' => '</li></ul>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
	));	

// ===== Code Markup ==================================================
add_filter('the_content', 'tguy_cmu_encode_xml', '1');
add_filter('the_content', 'tguy_cmu_tidy_code', '11');
function tguy_cmu_encode_xml($content) {
global $wp_version;
// $content = preg_replace('%(?<!<pre[^>]*>[^>]*)(<code[^>]*>.*?</code>)%ims', '<pre class="code-markup">\n$1\n</pre>', $content);
if ($wp_version < '2.3') {
$content = preg_replace_callback('!<pre([^>]*)>(.*?)</pre>!ims', 'tguy_cmu_escape_backslash_callback', $content);
}
$content = preg_replace_callback('!<code([^>]*)>(?:\r\n|\n|\r|)(.*?)(?:\r\n|\n|\r|)</code>!ims', 'tguy_cmu_encode_xml_callback', $content);
	return $content;
}
function tguy_cmu_tidy_code($content) {
$content = preg_replace_callback('!<pre([^>]*)>(.*?)</pre>!ims', 'tguy_cmu_unescape_qq_callback', $content);
$content = preg_replace_callback('!<code([^>]*)>(.*?)</code>!ims', 'tguy_cmu_untexturize_code_callback', $content);
	return $content;
}
function tguy_cmu_encode_xml_callback($matches) {
$attributes = $matches[1];
$escapedContent = $matches[2];
$attrMatches = array();
$markup = 'default';
if (0 < preg_match('!^(.*?)\s+(?:markup|allow)="([^"]*)"(.*)$!i', $attributes, $attrMatches)) {
$markup = strtolower($attrMatches[2]);
$attributes = $attrMatches[1] . $attrMatches[3];
}
if ($markup == 'default') {
if (0 < preg_match('!^(.*)lang="([^"]*)"(.*)$!i', $attributes, $attrMatches)) {
$lang = strtolower($attrMatches[2]);
$attributes = $attrMatches[1] . $attrMatches[3];
if ($lang == 'html' || $lang == 'xhtml') {
$markup = 'none';
			}
		}
	}
if ($markup == 'all') {
} else {
if ($markup == 'none' || $markup == '') {
$allowedTags = '';
} else if ($markup == 'default' || $markup == 'tags') { // 'tags' allowed for backward compatibility
$allowedTags = 'em|strong|b|i|ins|del|a|span|comment';
} else {
$allowedTags = preg_replace('!\s+!', '|', trim($markup));
}
$escapedContent = htmlspecialchars($escapedContent, ENT_NOQUOTES);
if ($allowedTags != '') {
$escapedContent = preg_replace_callback('!&lt;/?('.$allowedTags.')( .*?)?&gt;!is',
'tguy_cmu_unescape_tag', $escapedContent);
if (false !== strpos($allowedTags, 'comment')) {
$escapedContent = preg_replace_callback('|&lt;!--.*?--&gt;|is',
'tguy_cmu_unescape_tag', $escapedContent);
			}
		}
	}
return "<code$attributes>$escapedContent</code>";
}
function tguy_cmu_unescape_tag($matches) {
return str_replace(
array("&gt;", "&lt;", "&quot;", "&amp;"),
array(">", "<", "\"", "&"),
$matches[0]);
}
function tguy_cmu_escape_backslash_callback($matches) {
return "<pre{$matches[1]}>".str_replace('\\', '\\\\', $matches[2])."</pre>";
}
function tguy_cmu_unescape_qq_callback($matches) {
return "<pre{$matches[1]}>".str_replace('\"', '"', $matches[2])."</pre>";
}
function tguy_cmu_untexturize_code_callback($matches) {
$fancy = array('&#215;', '&#8216;', '&#8217;', '&#8242;', '&#8220;', '&#8221;', '&#8243;', '&#8212;', '&#8211;', '&#8230;', '&#8220;');
$plain = array('x'     ,'\''     , '\''     , '\''     , '"'      , '"'      , '"'      , '--'     , '--'     , '...'    , '``'     );
return "<code{$matches[1]}>".str_replace($fancy, $plain, $matches[2])."</code>";
}

// ===== Add Meta Noindex Rules on Singular Comment Page Section ======
function wpi_comment_paging_noindex_meta()
{	global $wp_query;
if (version_compare( (float) get_bloginfo('version'), 2.7, '>=') ){
if ($wp_query->is_singular && get_option('page_comments')){ // comments paging enabled
if (isset($wp_query->query['cpage'])
&& absint($wp_query->query['cpage']) >= 1 ){
echo '<meta name="robots" content="noindex" />'.PHP_EOL;
			}
		}
	}
}
add_action('wp_head','wpi_comment_paging_noindex_meta');	

// ===== Add a Microid to all Comments ================================
function comment_add_microid($classes) {
	$c_email=get_comment_author_email();
	$c_url=get_comment_author_url();
	if (!empty($c_email) && !empty($c_url)) {
		$microid = 'microid-mailto+http:sha1:' . sha1(sha1('mailto:'.$c_email).sha1($c_url));
		$classes[] = $microid;
	}
	return $classes;
}
add_filter('comment_class','comment_add_microid');

// ===== Comments Loop ================================================
function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
<?php if(function_exists('get_avatar')){ echo get_avatar($comment, '35'); } ?>
			<cite><?php comment_author_link() ?></cite>
			<?php if ($comment->comment_approved == '0') : ?>
			<em>Your comment is awaiting moderation.</em>
			<?php endif; ?>
			<br />
			<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('l, M jS, Y') ?> at <?php comment_time('G:i') ?></a> <?php edit_comment_link('Edit',' | ',''); ?></small>
			<?php comment_text() ?>
			<small class="commentmetadata">
			<?php if (!get_option('thread_comments')) : ?>
					<a href="javascript:void(0);" onclick="MGJS_CMT.reply('commentauthor-<?php comment_ID() ?>', 'comment-<?php comment_ID() ?>', 'comment');"><?php _e('', ''); ?></a>
				<?php else : ?>
					<?php comment_reply_link(array('depth' => $depth, 'max_depth'=> $args['max_depth'], 'reply_text' => __('Reply', ''), 'after' => ''));?>
				<?php endif; ?>
			</small>
<?php
        }
?>