Cleanup clean up, reorganization

This commit is contained in:
Ben Word
2012-09-03 09:43:38 -05:00
parent 141cb513a5
commit f538aa0116

View File

@@ -1,49 +1,127 @@
<?php
/**
* Redirects search results from /?s=query to /search/query/, converts %20 to +
* Clean up wp_head()
*
* @link http://txfx.net/wordpress-plugins/nice-search/
* Remove unnecessary <link>'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<link rel=\"canonical\" href=\"$link\">\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 <html> 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 <link> tags
*/
function roots_clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
// Only display media if it's print
$media = $matches[3][0] === 'print' ? ' media="print"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\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 <html> 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 '<div class="entry-content-asset">' . $cache . '</div>';
}
if (function_exists('is_rtl')) {
if (is_rtl() == 'rtl') {
$attributes[] = 'dir="rtl"';
}
add_filter('embed_oembed_html', 'roots_embed_wrap', 10, 4);
add_filter('embed_googlevideo', 'roots_embed_wrap', 10, 2);
/**
* Add class="thumbnail" to attachment items
*/
function roots_attachment_link_class($html) {
$postid = get_the_ID();
$html = str_replace('<a', '<a class="thumbnail"', $html);
return $html;
}
add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1);
/**
* Add Bootstrap thumbnail styling to images with captions
* Use <figure> and <figcaption>
*
* @link http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
*/
function roots_caption($output, $attr, $content) {
if (is_feed()) {
return $output;
}
$lang = get_bloginfo('language');
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
if ($lang && $lang !== 'en-US') {
$attributes[] = "lang=\"$lang\"";
} else {
$attributes[] = 'lang="en"';
$attr = shortcode_atts($defaults, $attr);
// If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags
if (1 > $attr['width'] || empty($attr['caption'])) {
return $content;
}
$output = implode(' ', $attributes);
$output = apply_filters('roots_language_attributes', $output);
// Set up the attributes for the caption <figure>
$attributes = (!empty($attr['id']) ? ' id="' . esc_attr($attr['id']) . '"' : '' );
$attributes .= ' class="thumbnail wp-caption ' . esc_attr($attr['align']) . '"';
$attributes .= ' style="width: ' . esc_attr($attr['width']) . 'px"';
$output = '<figure' . $attributes .'>';
$output .= do_shortcode($content);
$output .= '<figcaption class="caption wp-caption-text">' . $attr['caption'] . '</figcaption>';
$output .= '</figure>';
return $output;
}
add_filter('language_attributes', 'roots_language_attributes');
add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
/**
* Remove the WordPress version from RSS feeds
*/
function roots_remove_generator() { return; }
add_filter('the_generator', 'roots_remove_generator');
/**
* Cleanup wp_head()
*
* Remove unnecessary <link>'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_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', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
add_action('wp_head', 'roots_remove_recent_comments_style', 1);
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');
}
}
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<link rel=\"canonical\" href=\"$link\">\n";
}
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'));
}
}
add_action('init', 'roots_head_cleanup');
/**
* Cleanup gallery_shortcode()
* Clean up gallery_shortcode()
*
* Re-create the [gallery] shortcode and use thumbnails styling from Bootstrap
*
@@ -328,57 +379,6 @@ function roots_gallery($attr) {
remove_shortcode('gallery');
add_shortcode('gallery', 'roots_gallery');
/**
* Add class="thumbnail" to attachment items
*/
function roots_attachment_link_class($html) {
$postid = get_the_ID();
$html = str_replace('<a', '<a class="thumbnail"', $html);
return $html;
}
add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1);
/**
* Add Bootstrap thumbnail styling to images with captions
* Use <figure> and <figcaption>
*
* @link http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
*/
function roots_caption($output, $attr, $content) {
if (is_feed()) {
return $output;
}
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
$attr = shortcode_atts($defaults, $attr);
// If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags
if (1 > $attr['width'] || empty($attr['caption'])) {
return $content;
}
// Set up the attributes for the caption <div>
$attributes = (!empty($attr['id']) ? ' id="' . esc_attr($attr['id']) . '"' : '' );
$attributes .= ' class="thumbnail wp-caption ' . esc_attr($attr['align']) . '"';
$attributes .= ' style="width: ' . esc_attr($attr['width']) . 'px"';
$output = '<figure' . $attributes .'>';
$output .= do_shortcode($content);
$output .= '<figcaption class="caption wp-caption-text">' . $attr['caption'] . '</figcaption>';
$output .= '</figure>';
return $output;
}
add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
/**
* Remove unnecessary dashboard widgets
*
@@ -394,7 +394,7 @@ function roots_remove_dashboard_widgets() {
add_action('admin_init', 'roots_remove_dashboard_widgets');
/**
* Cleanup the_excerpt()
* Clean up the_excerpt()
*/
function roots_excerpt_length($length) {
return POST_EXCERPT_LENGTH;
@@ -471,7 +471,7 @@ class Roots_Nav_Walker extends Walker_Nav_Menu {
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
$attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
$attributes .= ($args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown" data-target="#"' : '';
$attributes .= ($args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown" data-target="#"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
@@ -522,18 +522,19 @@ class Roots_Nav_Walker extends Walker_Nav_Menu {
}
/**
* Cleanup wp_nav_menu_args
* Clean up wp_nav_menu_args
*
* Remove the container
* Use Roots_Nav_Walker() by default
*/
function roots_nav_menu_args($args = '') {
$roots_nav_menu_args['container'] = false;
$roots_nav_menu_args['container'] = false;
if (!$args['items_wrap']) {
$roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
}
// Bootstrap's navbar doesn't support multi-level dropdowns
if (current_theme_supports('bootstrap-top-navbar')) {
$roots_nav_menu_args['depth'] = 2;
}
@@ -547,7 +548,6 @@ function roots_nav_menu_args($args = '') {
add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
/**
* Remove unnecessary self-closing tags
*/
@@ -587,31 +587,6 @@ function roots_change_mce_options($options) {
add_filter('tiny_mce_before_init', 'roots_change_mce_options');
/**
* Cleanup output of stylesheet <link> tags
*/
add_filter('style_loader_tag', 'roots_clean_style_tag');
function roots_clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
// Only display media if it's print
$media = $matches[3][0] === 'print' ? ' media="print"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}
/**
* Apply filters to body_class()
*/
function roots_body_class_filter($classes) {
// Add 'top-navbar' class if using Bootstrap's Navbar
if (current_theme_supports('bootstrap-top-navbar')) {
$classes[] = 'top-navbar';
}
return $classes;
}
add_filter('body_class', 'roots_body_class_filter');
/**
* Add additional classes onto widgets
*
@@ -648,23 +623,54 @@ function roots_widget_first_last_classes($params) {
$params[0]['before_widget'] = preg_replace('/class=\"/', "$class", $params[0]['before_widget'], 1);
return $params;
}
add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
/**
* Wrap embedded media as suggested by Readability
* Redirects search results from /?s=query to /search/query/, converts %20 to +
*
* @link https://gist.github.com/965956
* @link http://www.readability.com/publishers/guidelines#publisher
* @link http://txfx.net/wordpress-plugins/nice-search/
*/
function roots_embed_wrap($cache, $url, $attr = '', $post_ID = '') {
return '<div class="entry-content-asset">' . $cache . '</div>';
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_filter('embed_oembed_html', 'roots_embed_wrap', 10, 4);
add_filter('embed_googlevideo', 'roots_embed_wrap', 10, 2);
add_action('template_redirect', 'roots_nice_search_redirect');
/**
* Fix for get_search_query() returning +'s between search terms
*/
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');
/**
* Fix for empty search queries redirecting to home page
*
* @link http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
* @link http://core.trac.wordpress.org/ticket/11330
*/
function roots_request_filter($query_vars) {
if (isset($_GET['s']) && empty($_GET['s'])) {
$query_vars['s'] = ' ';
}
return $query_vars;
}
add_filter('request', 'roots_request_filter');
/**
* Tell WordPress to use searchform.php from the templates/ directory