<commit_before><?php

$theme_name = next(explode('/themes/', get_stylesheet_directory()));
$theme_data = get_theme_data(ABSPATH . 'wp-content/themes/' . $theme_name . '/style.css');

// Rewrites DO NOT happen for child themes
// rewrite /wp-content/themes/roots/css/ to /css/
// rewrite /wp-content/themes/roots/js/  to /js/
// rewrite /wp-content/themes/roots/img/ to /js/
// rewrite /wp-content/plugins/ to /plugins/

function roots_flush_rewrites() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

function roots_add_rewrites($content) {
	$theme_name = next(explode('/themes/', get_stylesheet_directory()));
	global $wp_rewrite;
	$roots_new_non_wp_rules = array(
		'css/(.*)'      => 'wp-content/themes/'. $theme_name . '/css/$1',
		'js/(.*)'       => 'wp-content/themes/'. $theme_name . '/js/$1',
		'img/(.*)'      => 'wp-content/themes/'. $theme_name . '/img/$1',
		'plugins/(.*)'  => 'wp-content/plugins/$1'
	);
	$wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}

add_action('admin_init', 'roots_flush_rewrites');

function roots_clean_assets($content) {
    $theme_name = next(explode('/themes/', $content));
    $current_path = '/wp-content/themes/' . $theme_name;
    $new_path = '';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}

function roots_clean_plugins($content) {
    $current_path = '/wp-content/plugins';
    $new_path = '/plugins';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}

// only use clean urls if the theme isn't a child or an MU (Network) install
if ((!defined('WP_ALLOW_MULTISITE') || (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE !== true)) && $theme_data['Template'] === '') {
	add_action('generate_rewrite_rules', 'roots_add_rewrites');
	add_filter('plugins_url', 'roots_clean_plugins');
	add_filter('bloginfo', 'roots_clean_assets');
	add_filter('stylesheet_directory_uri', 'roots_clean_assets');
	add_filter('template_directory_uri', 'roots_clean_assets');
}

// redirect /?s to /search/
// http://txfx.net/wordpress-plugins/nice-search/
function roots_nice_search_redirect() {
	if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
		wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var( 's' )))), 301);
    exit();
	}
}
add_action('template_redirect', 'roots_nice_search_redirect');

function roots_search_query($escaped = true) {
	$query = apply_filters('roots_search_query', get_query_var('s'));
	if ($escaped) {
    	$query = esc_attr( $query );
	}
  	return urldecode($query);
}

add_filter('get_search_query', 'roots_search_query');

// root relative URLs for everything
// inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
// thanks to <NAME> (scottwalkinshaw.com)
function roots_root_relative_url($input) {
	preg_match('/(https?:\/\/[^\/]+)/', $input, $matches);
	return str_replace(end($matches), '', $input);
}

add_filter('bloginfo_url', 'roots_root_relative_url');
add_filter('theme_root_uri', 'roots_root_relative_url');
add_filter('stylesheet_directory_uri', 'roots_root_relative_url');
add_filter('template_directory_uri', 'roots_root_relative_url');
add_filter('the_permalink', 'roots_root_relative_url');
add_filter('wp_list_pages', 'roots_root_relative_url');
add_filter('wp_list_categories', 'roots_root_relative_url');
add_filter('wp_nav_menu', 'roots_root_relative_url');
add_filter('wp_get_attachment_url', 'roots_root_relative_url');
add_filter('wp_get_attachment_link', 'roots_root_relative_url');
add_filter('the_content_more_link', 'roots_root_relative_url');
add_filter('the_tags', 'roots_root_relative_url');
add_filter('get_pagenum_link', 'roots_root_relative_url');
add_filter('get_comment_link', 'roots_root_relative_url');
add_filter('month_link', 'roots_root_relative_url');
add_filter('day_link', 'roots_root_relative_url');
add_filter('year_link', 'roots_root_relative_url');
add_filter('tag_link', 'roots_root_relative_url');

// Leaving plugins_url alone in admin to avoid potential issues (such as Gravity Forms)
if (!is_admin()) {
  add_filter('plugins_url', 'roots_root_relative_url');
}

// remove root relative URLs on any attachments in the feed
function roots_relative_feed_urls() {
	global $wp_query;
	if (is_feed()) {
		remove_filter('wp_get_attachment_url', 'roots_root_relative_url');
		remove_filter('wp_get_attachment_link', 'roots_root_relative_url');
	}
}

add_action('pre_get_posts', 'roots_relative_feed_urls' );

// remove dir and set lang="en" as default (rather than en-US)
function roots_language_attributes() {
	$attributes = array();
	$output = '';
	if (!defined('WP_LANG')) {
		$attributes[] = "lang=\"en\"";
	} else if ($lang = get_bloginfo('language')) {
		$attributes[] = "lang=\"$lang\"";
	}

	$output = implode(' ', $attributes);
	$output = apply_filters('roots_language_attributes', $output);
	echo $output;
}

add_filter('language_attributes', 'roots_language_attributes');

// remove WordPress version from RSS feed
function roots_no_generator() { return ''; }
add_filter('the_generator', 'roots_no_generator');

// cleanup wp_head
function roots_noindex() {
	if ('0' == get_option('blog_public'))
	echo "<meta name=\"robots\" content=\"noindex,nofollow\">\n";
}	

function roots_rel_canonical() {
	if (!is_singular())
		return;
	global $wp_the_query;
	if (!$id = $wp_the_query->get_queried_object_id())
		return;
	$link = get_permalink($id);
	echo "<link rel=\"canonical\" href=\"$link\">\n";
}

// remove CSS from recent comments widget
function roots_remove_recent_comments_style() {
	global $wp_widget_factory;
	remove_action( 'wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style') );
}

// remove CSS from gallery
function roots_gallery_style($css) {
	return preg_replace("#<style type='text/css'>(.*?)</style>#s", '', $css);
}

function roots_head_cleanup() {
	// http://wpengineer.com/1438/wordpress-header/
	remove_action('wp_head', 'feed_links', 2);
	remove_action('wp_head', 'feed_links_extra', 3);
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'index_rel_link');
	remove_action('wp_head', 'parent_post_rel_link', 10, 0);<commit_msg>removing undefined index error on the removal of css for the recent comments widget<commit_after><?php

$theme_name = next(explode('/themes/', get_stylesheet_directory()));
$theme_data = get_theme_data(ABSPATH . 'wp-content/themes/' . $theme_name . '/style.css');

// Rewrites DO NOT happen for child themes
// rewrite /wp-content/themes/roots/css/ to /css/
// rewrite /wp-content/themes/roots/js/  to /js/
// rewrite /wp-content/themes/roots/img/ to /js/
// rewrite /wp-content/plugins/ to /plugins/

function roots_flush_rewrites() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

function roots_add_rewrites($content) {
	$theme_name = next(explode('/themes/', get_stylesheet_directory()));
	global $wp_rewrite;
	$roots_new_non_wp_rules = array(
		'css/(.*)'      => 'wp-content/themes/'. $theme_name . '/css/$1',
		'js/(.*)'       => 'wp-content/themes/'. $theme_name . '/js/$1',
		'img/(.*)'      => 'wp-content/themes/'. $theme_name . '/img/$1',
		'plugins/(.*)'  => 'wp-content/plugins/$1'
	);
	$wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}

add_action('admin_init', 'roots_flush_rewrites');

function roots_clean_assets($content) {
    $theme_name = next(explode('/themes/', $content));
    $current_path = '/wp-content/themes/' . $theme_name;
    $new_path = '';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}

function roots_clean_plugins($content) {
    $current_path = '/wp-content/plugins';
    $new_path = '/plugins';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}

// only use clean urls if the theme isn't a child or an MU (Network) install
if ((!defined('WP_ALLOW_MULTISITE') || (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE !== true)) && $theme_data['Template'] === '') {
	add_action('generate_rewrite_rules', 'roots_add_rewrites');
	add_filter('plugins_url', 'roots_clean_plugins');
	add_filter('bloginfo', 'roots_clean_assets');
	add_filter('stylesheet_directory_uri', 'roots_clean_assets');
	add_filter('template_directory_uri', 'roots_clean_assets');
}

// redirect /?s to /search/
// http://txfx.net/wordpress-plugins/nice-search/
function roots_nice_search_redirect() {
	if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
		wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var( 's' )))), 301);
    exit();
	}
}
add_action('template_redirect', 'roots_nice_search_redirect');

function roots_search_query($escaped = true) {
	$query = apply_filters('roots_search_query', get_query_var('s'));
	if ($escaped) {
    	$query = esc_attr( $query );
	}
  	return urldecode($query);
}

add_filter('get_search_query', 'roots_search_query');

// root relative URLs for everything
// inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
// thanks to <NAME> (s<EMAIL>)
function roots_root_relative_url($input) {
	preg_match('/(https?:\/\/[^\/]+)/', $input, $matches);
	return str_replace(end($matches), '', $input);
}

add_filter('bloginfo_url', 'roots_root_relative_url');
add_filter('theme_root_uri', 'roots_root_relative_url');
add_filter('stylesheet_directory_uri', 'roots_root_relative_url');
add_filter('template_directory_uri', 'roots_root_relative_url');
add_filter('the_permalink', 'roots_root_relative_url');
add_filter('wp_list_pages', 'roots_root_relative_url');
add_filter('wp_list_categories', 'roots_root_relative_url');
add_filter('wp_nav_menu', 'roots_root_relative_url');
add_filter('wp_get_attachment_url', 'roots_root_relative_url');
add_filter('wp_get_attachment_link', 'roots_root_relative_url');
add_filter('the_content_more_link', 'roots_root_relative_url');
add_filter('the_tags', 'roots_root_relative_url');
add_filter('get_pagenum_link', 'roots_root_relative_url');
add_filter('get_comment_link', 'roots_root_relative_url');
add_filter('month_link', 'roots_root_relative_url');
add_filter('day_link', 'roots_root_relative_url');
add_filter('year_link', 'roots_root_relative_url');
add_filter('tag_link', 'roots_root_relative_url');

// Leaving plugins_url alone in admin to avoid potential issues (such as Gravity Forms)
if (!is_admin()) {
  add_filter('plugins_url', 'roots_root_relative_url');
}

// remove root relative URLs on any attachments in the feed
function roots_relative_feed_urls() {
	global $wp_query;
	if (is_feed()) {
		remove_filter('wp_get_attachment_url', 'roots_root_relative_url');
		remove_filter('wp_get_attachment_link', 'roots_root_relative_url');
	}
}

add_action('pre_get_posts', 'roots_relative_feed_urls' );

// remove dir and set lang="en" as default (rather than en-US)
function roots_language_attributes() {
	$attributes = array();
	$output = '';
	if (!defined('WP_LANG')) {
		$attributes[] = "lang=\"en\"";
	} else if ($lang = get_bloginfo('language')) {
		$attributes[] = "lang=\"$lang\"";
	}

	$output = implode(' ', $attributes);
	$output = apply_filters('roots_language_attributes', $output);
	echo $output;
}

add_filter('language_attributes', 'roots_language_attributes');

// remove WordPress version from RSS feed
function roots_no_generator() { return ''; }
add_filter('the_generator', 'roots_no_generator');

// cleanup wp_head
function roots_noindex() {
	if ('0' == get_option('blog_public'))
	echo "<meta name=\"robots\" content=\"noindex,nofollow\">\n";
}	

function roots_rel_canonical() {
	if (!is_singular())
		return;
	global $wp_the_query;
	if (!$id = $wp_the_query->get_queried_object_id())
		return;
	$link = get_permalink($id);
	echo "<link rel=\"canonical\" href=\"$link\">\n";
}

// remove CSS from recent comments widget
function roots_remove_recent_comments_style() {
	global $wp_widget_factory;
	if (isset($wp_widget_factory->widgets['WP_Widget_Recent_Comments'])) {
		remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
	}
}

// remove CSS from gallery
function roots_gallery_style($css) {
	return preg_replace("#<style type='text/css'>(.*?)</style>#s", '', $css);
}

function roots_head_cleanup() {
	// http://wpengineer.com/1438/wordpress-header/
	remove_action('wp_head', 'feed_links', 2);
	remove_action('wp_head', 'feed_links_extra', 3);
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'index_rel_link');
	remove_action('wp_head', 'parent_post_rel_link', 10, 0);
{'fraction_non_alphanumeric': 0.1727123800608472, 'fraction_numerical': 0.0043685154848272095, 'mean_word_length': 6.492694330800702, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6161648', 'n_tokens_mistral': 4639, 'n_tokens_neox': 4411, 'n_words': 761}