Files
sage/inc/roots-cleanup.php
2012-02-27 11:40:01 -07:00

650 lines
22 KiB
PHP

<?php
// 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');
// fix for empty search query
// http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
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');
// root relative URLs for everything
// inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
// thanks to Scott Walkinshaw (scottwalkinshaw.com)
function roots_root_relative_url($input) {
$output = preg_replace_callback(
'!(https?://[^/|"]+)([^"]+)?!',
create_function(
'$matches',
// if full URL is site_url, return a slash for relative root
'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
// if domain is equal to site_url, then make URL relative
'} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
// if domain is not equal to site_url, do not make external link relative
'} else { return $matches[0]; };'
),
$input
);
return $output;
}
if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
$tags = array(
'bloginfo_url',
'theme_root_uri',
'stylesheet_directory_uri',
'template_directory_uri',
'script_loader_src',
'style_loader_src',
'plugins_url',
'the_permalink',
'wp_list_pages',
'wp_list_categories',
'wp_nav_menu',
'the_content_more_link',
'the_tags',
'get_pagenum_link',
'get_comment_link',
'month_link',
'day_link',
'year_link',
'tag_link',
'the_author_posts_link'
);
add_filters($tags, 'roots_root_relative_url');
}
// remove root relative URLs on any attachments in the feed
function roots_root_relative_attachment_urls() {
if (!is_feed()) {
add_filter('wp_get_attachment_url', 'roots_root_relative_url');
add_filter('wp_get_attachment_link', 'roots_root_relative_url');
}
}
add_action('pre_get_posts', 'roots_root_relative_attachment_urls');
// set lang="en" as default (rather than en-US)
function roots_language_attributes() {
$attributes = array();
$output = '';
if (function_exists('is_rtl')) {
if (is_rtl() == 'rtl') {
$attributes[] = 'dir="rtl"';
}
}
$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('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 (get_option('blog_public') === '0') {
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 "\t<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);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
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);
remove_action('wp_head', 'noindex', 1);
add_action('wp_head', 'roots_noindex');
add_action('wp_head', 'roots_remove_recent_comments_style', 1);
add_filter('gallery_style', 'roots_gallery_style');
if (!class_exists('WPSEO_Frontend')) {
remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'roots_rel_canonical');
}
}
add_action('init', 'roots_head_cleanup');
// cleanup gallery_shortcode()
function roots_gallery_shortcode($attr) {
global $post, $wp_locale;
static $instance = 0;
$instance++;
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ($output != '') {
return $output;
}
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby']) {
unset($attr['orderby']);
}
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'icontag' => 'li',
'captiontag' => 'p',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order) {
$orderby = 'none';
}
if (!empty($include)) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif (!empty($exclude)) {
$exclude = preg_replace('/[^0-9,]+/', '', $exclude);
$attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
} else {
$attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
}
if (empty($attachments)) {
return '';
}
if (is_feed()) {
$output = "\n";
foreach ($attachments as $att_id => $attachment)
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$captiontag = tag_escape($captiontag);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if (apply_filters('use_default_gallery_style', true)) {
$gallery_style = "";
}
$size_class = sanitize_html_class($size);
$gallery_div = "<ul id='$selector' class='thumbnails gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters('gallery_style', $gallery_style . "\n\t\t" . $gallery_div);
$i = 0;
foreach ($attachments as $id => $attachment) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "
<{$icontag} class=\"gallery-item\">
$link
";
if ($captiontag && trim($attachment->post_excerpt)) {
$output .= "
<{$captiontag} class=\"gallery-caption hidden\">
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$icontag}>";
if ($columns > 0 && ++$i % $columns == 0) {
$output .= '';
}
}
$output .= "</ul>\n";
return $output;
}
remove_shortcode('gallery');
add_shortcode('gallery', 'roots_gallery_shortcode');
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);
// http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
function roots_caption($output, $attr, $content) {
/* We're not worried abut captions in feeds, so just return the output here. */
if ( is_feed()) {
return $output;
}
/* Set up the default arguments. */
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
/* Merge the defaults with user input. */
$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"';
/* Open the caption <div>. */
$output = '<div' . $attributes .'>';
/* Allow shortcodes for the content the caption was created for. */
$output .= do_shortcode($content);
/* Append the caption text. */
$output .= '<div class="caption"><p class="wp-caption-text">' . $attr['caption'] . '</p></div>';
/* Close the caption </div>. */
$output .= '</div>';
/* Return the formatted, clean caption. */
return $output;
}
add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
// http://www.deluxeblogtips.com/2011/01/remove-dashboard-widgets-in-wordpress.html
function roots_remove_dashboard_widgets() {
remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
remove_meta_box('dashboard_primary', 'dashboard', 'normal');
remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
}
add_action('admin_init', 'roots_remove_dashboard_widgets');
// excerpt cleanup
function roots_excerpt_length($length) {
return POST_EXCERPT_LENGTH;
}
function roots_excerpt_more($more) {
return ' &hellip; <a href="' . get_permalink() . '">' . __( 'Continued', 'roots' ) . '</a>';
}
add_filter('excerpt_length', 'roots_excerpt_length');
add_filter('excerpt_more', 'roots_excerpt_more');
class Roots_Nav_Walker extends Walker_Nav_Menu {
function check_current($val) {
return preg_match('/(current-)/', $val);
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$slug = sanitize_title($item->title);
$id = apply_filters('nav_menu_item_id', 'menu-' . $slug, $item, $args);
$id = strlen($id) ? '' . esc_attr( $id ) . '' : '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$classes = array_filter($classes, array(&$this, 'check_current'));
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
$class_names = $class_names ? ' class="' . $id . ' ' . esc_attr($class_names) . '"' : ' class="' . $id . '"';
$output .= $indent . '<li' . $class_names . '>';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$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 ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
}
class Roots_Navbar_Nav_Walker extends Walker_Nav_Menu {
function check_current($val) {
return preg_match('/(current-)|active|dropdown/', $val);
}
function start_lvl(&$output, $depth) {
$output .= "\n<ul class=\"dropdown-menu\">\n";
}
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$slug = sanitize_title($item->title);
$id = apply_filters('nav_menu_item_id', 'menu-' . $slug, $item, $args);
$id = strlen($id) ? '' . esc_attr( $id ) . '' : '';
$li_attributes = '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
if ($args->has_children) {
$classes[] = 'dropdown';
$li_attributes .= ' data-dropdown="dropdown"';
}
$classes[] = ($item->current) ? 'active' : '';
$classes = array_filter($classes, array(&$this, 'check_current'));
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
$class_names = $class_names ? ' class="' . $id . ' ' . esc_attr($class_names) . '"' : ' class="' . $id . '"';
$output .= $indent . '<li' . $class_names . $li_attributes . '>';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$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"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= ($args->has_children) ? ' <b class="caret"></b>' : '';
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
if (!$element) { return; }
$id_field = $this->db_fields['id'];
// display this element
if (is_array($args[0])) {
$args[0]['has_children'] = !empty($children_elements[$element->$id_field]);
} elseif (is_object($args[0])) {
$args[0]->has_children = !empty($children_elements[$element->$id_field]);
}
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
$id = $element->$id_field;
// descend only when the depth is right and there are childrens for this element
if (($max_depth == 0 || $max_depth > $depth+1) && isset($children_elements[$id])) {
foreach ($children_elements[$id] as $child) {
if (!isset($newlevel)) {
$newlevel = true;
// start the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
$this->display_element($child, $children_elements, $max_depth, $depth + 1, $args, $output);
}
unset($children_elements[$id]);
}
if (isset($newlevel) && $newlevel) {
// end the child delimiter
$cb_args = array_merge(array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
// end this element
$cb_args = array_merge(array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}
}
function roots_nav_menu_args($args = '') {
$args['container'] = false;
$args['depth'] = 2;
$args['items_wrap'] = '<ul class="nav">%3$s</ul>';
if (!$args['walker']) {
$args['walker'] = new Roots_Nav_Walker();
}
return $args;
}
add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
// we don't need to self-close these tags in html5:
// <img>, <input>
function roots_remove_self_closing_tags($input) {
return str_replace(' />', '>', $input);
}
add_filter('get_avatar', 'roots_remove_self_closing_tags');
add_filter('comment_id_fields', 'roots_remove_self_closing_tags');
add_filter('post_thumbnail_html', 'roots_remove_self_closing_tags');
// check to see if the tagline is set to default
// show an admin notice to update if it hasn't been changed
// you want to change this or remove it because it's used as the description in the RSS feed
function roots_notice_tagline() {
global $current_user;
$user_id = $current_user->ID;
if (!get_user_meta($user_id, 'ignore_tagline_notice')) {
echo '<div class="error">';
echo '<p>', sprintf(__('Please update your <a href="%s">site tagline</a> <a href="%s" style="float: right;">Hide Notice</a>', 'roots'), admin_url('options-general.php'), '?tagline_notice_ignore=0'), '</p>';
echo '</div>';
}
}
if ((get_option('blogdescription') === 'Just another WordPress site') && isset($_GET['page']) != 'theme_activation_options') {
add_action('admin_notices', 'roots_notice_tagline');
}
function roots_notice_tagline_ignore() {
global $current_user;
$user_id = $current_user->ID;
if (isset($_GET['tagline_notice_ignore']) && '0' == $_GET['tagline_notice_ignore']) {
add_user_meta($user_id, 'ignore_tagline_notice', 'true', true);
}
}
add_action('admin_init', 'roots_notice_tagline_ignore');
// set the post revisions to 5 unless the constant
// was set in wp-config.php to avoid DB bloat
if (!defined('WP_POST_REVISIONS')) { define('WP_POST_REVISIONS', 5); }
// allow more tags in TinyMCE including <iframe> and <script>
function roots_change_mce_options($options) {
$ext = 'pre[id|name|class|style],iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src],script[charset|defer|language|src|type]';
if (isset($initArray['extended_valid_elements'])) {
$options['extended_valid_elements'] .= ',' . $ext;
} else {
$options['extended_valid_elements'] = $ext;
}
return $options;
}
add_filter('tiny_mce_before_init', 'roots_change_mce_options');
//clean up the default WordPress style 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";
}
function roots_body_class() {
$term = get_queried_object();
if (is_single()) {
$cat = get_the_category();
}
if(!empty($cat)) {
return $cat[0]->slug;
} elseif (isset($term->slug)) {
return $term->slug;
} elseif (isset($term->page_name)) {
return $term->page_name;
} elseif (isset($term->post_name)) {
return $term->post_name;
} else {
return;
}
}
// first and last classes for widgets
// http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets
function roots_widget_first_last_classes($params) {
global $my_widget_num;
$this_id = $params[0]['id'];
$arr_registered_widgets = wp_get_sidebars_widgets();
if (!$my_widget_num) {
$my_widget_num = array();
}
if (!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) {
return $params;
}
if (isset($my_widget_num[$this_id])) {
$my_widget_num[$this_id] ++;
} else {
$my_widget_num[$this_id] = 1;
}
$class = 'class="widget-' . $my_widget_num[$this_id] . ' ';
if ($my_widget_num[$this_id] == 1) {
$class .= 'widget-first ';
} elseif ($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) {
$class .= 'widget-last ';
}
$params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
return $params;
}
add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
// apply Bootstrap markup/classes to Gravity Forms (in progress)
if (class_exists('RGForms')) {
update_option('rg_gforms_disable_css', 1);
// error message class
function roots_gform_validation_message($message, $form) {
$message = '<div class="alert alert-error fade in">';
$message .= '<a class="close" data-dismiss="alert">&times;</a>';
$message .= '<strong>There was a problem with your submission. Errors have been highlighted below.</strong>';
$message .= '</div>';
return $message;
}
add_filter('gform_validation_message', 'roots_gform_validation_message', 10, 2);
// field class
function roots_gform_field_css_class($classes, $field, $form) {
$classes .= " control-group";
return $classes;
}
add_action('gform_field_css_class', 'roots_gform_field_css_class', 10, 3);
// button class
function roots_gform_submit_button($button, $form) {
return "<button class='btn btn-primary' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
}
add_filter('gform_submit_button', 'roots_gform_submit_button', 10, 2);
}