diff --git a/lib/cleanup.php b/lib/cleanup.php index ad0a6d3..790a54e 100644 --- a/lib/cleanup.php +++ b/lib/cleanup.php @@ -1,49 +1,127 @@ 's + * Remove inline CSS used by Recent Comments widget + * Remove inline CSS used by posts with galleries + * Remove self-closing tag and change ''s to "'s on rel_canonical() */ -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(); +function roots_head_cleanup() { + // Originally from 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', 'adjacent_posts_rel_link_wp_head', 10, 0); + remove_action('wp_head', 'wp_generator'); + remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); + + global $wp_widget_factory; + remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style')); + + add_filter('use_default_gallery_style', '__return_null'); + + if (!class_exists('WPSEO_Frontend')) { + remove_action('wp_head', 'rel_canonical'); + add_action('wp_head', 'roots_rel_canonical'); } } -add_action('template_redirect', 'roots_nice_search_redirect'); +function roots_rel_canonical() { + global $wp_the_query; + + if (!is_singular()) { + return; + } + + if (!$id = $wp_the_query->get_queried_object_id()) { + return; + } + + $link = get_permalink($id); + echo "\t\n"; +} + +add_action('init', 'roots_head_cleanup'); /** - * Fix for get_search_query() returning +'s between search terms + * Remove the WordPress version from RSS feeds */ -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'); +add_filter('the_generator', '__return_false'); /** - * Fix for empty search queries redirecting to home page + * Clean up language_attributes() used in tag * - * @link http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565 - * @link http://core.trac.wordpress.org/ticket/11330 + * Change lang="en-US" to lang="en" + * Remove dir="ltr" */ -function roots_request_filter($query_vars) { - if (isset($_GET['s']) && empty($_GET['s'])) { - $query_vars['s'] = ' '; +function roots_language_attributes() { + $attributes = array(); + $output = ''; + + if (function_exists('is_rtl')) { + if (is_rtl() == 'rtl') { + $attributes[] = 'dir="rtl"'; + } } - return $query_vars; + $lang = get_bloginfo('language'); + + if ($lang && $lang !== 'en-US') { + $attributes[] = "lang=\"$lang\""; + } else { + $attributes[] = 'lang="en"'; + } + + $output = implode(' ', $attributes); + $output = apply_filters('roots_language_attributes', $output); + + return $output; } -add_filter('request', 'roots_request_filter'); +add_filter('language_attributes', 'roots_language_attributes'); + +/** + * Clean up output of stylesheet tags + */ +function roots_clean_style_tag($input) { + preg_match_all("!!", $input, $matches); + // Only display media if it's print + $media = $matches[3][0] === 'print' ? ' media="print"' : ''; + return '' . "\n"; +} + +add_filter('style_loader_tag', 'roots_clean_style_tag'); + +/** + * Add and remove body_class() classes + */ +function roots_body_class($classes) { + // Add 'top-navbar' class if using Bootstrap's Navbar + // Used to add styling to account for the WordPress admin bar + if (current_theme_supports('bootstrap-top-navbar')) { + $classes[] = 'top-navbar'; + } + + // Add post/page slug + if (is_single() || is_page() && !is_front_page()) { + $classes[] = basename(get_permalink()); + } + + // Remove unnecessary classes + $home_id_class = 'page-id-' . get_option('page_on_front'); + $remove_classes = array( + 'page-template-default', + $home_id_class + ); + $classes = array_diff($classes, $remove_classes); + + return $classes; +} + +add_filter('body_class', 'roots_body_class'); /** * Root relative URLs @@ -91,11 +169,11 @@ function roots_fix_duplicate_subfolder_urls($input) { return $output; } -function enable_root_relative_urls() { +function roots_enable_root_relative_urls() { return !(is_admin() && in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) && current_theme_supports('root-relative-urls'); } -if (enable_root_relative_urls()) { +if (roots_enable_root_relative_urls()) { $root_rel_filters = array( 'bloginfo_url', 'theme_root_uri', @@ -124,98 +202,71 @@ if (enable_root_relative_urls()) { } /** - * Cleanup language_attributes() used in tag + * Wrap embedded media as suggested by Readability * - * Change lang="en-US" to lang="en" - * Remove dir="ltr" + * @link https://gist.github.com/965956 + * @link http://www.readability.com/publishers/guidelines#publisher */ -function roots_language_attributes() { - $attributes = array(); - $output = ''; +function roots_embed_wrap($cache, $url, $attr = '', $post_ID = '') { + return '