PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/roots-cleanup.php

http://github.com/retlehs/roots
PHP | 537 lines | 310 code | 64 blank | 163 comment | 44 complexity | e6e214079900d68b8e01cc3eb1804b46 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. $roots_options = roots_get_theme_options();
  3. // redirect /?s to /search/
  4. // http://txfx.net/wordpress-plugins/nice-search/
  5. function roots_nice_search_redirect() {
  6. if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
  7. wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var('s')))), 301);
  8. exit();
  9. }
  10. }
  11. add_action('template_redirect', 'roots_nice_search_redirect');
  12. function roots_search_query($escaped = true) {
  13. $query = apply_filters('roots_search_query', get_query_var('s'));
  14. if ($escaped) {
  15. $query = esc_attr($query);
  16. }
  17. return urldecode($query);
  18. }
  19. add_filter('get_search_query', 'roots_search_query');
  20. // fix for empty search query
  21. // http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
  22. function roots_request_filter($query_vars) {
  23. if (isset($_GET['s']) && empty($_GET['s'])) {
  24. $query_vars['s'] = " ";
  25. }
  26. return $query_vars;
  27. }
  28. add_filter('request', 'roots_request_filter');
  29. // root relative URLs for everything
  30. // inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
  31. // thanks to Scott Walkinshaw (scottwalkinshaw.com)
  32. function roots_root_relative_url($input) {
  33. $output = preg_replace_callback(
  34. '!(https?://[^/|"]+)([^"]+)?!',
  35. create_function(
  36. '$matches',
  37. // if full URL is site_url, return a slash for relative root
  38. 'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
  39. // if domain is equal to site_url, then make URL relative
  40. '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
  41. // if domain is not equal to site_url, do not make external link relative
  42. '} else { return $matches[0]; };'
  43. ),
  44. $input
  45. );
  46. return $output;
  47. }
  48. // Terrible workaround to remove the duplicate subfolder in the src of JS/CSS tags
  49. // Example: /subfolder/subfolder/css/style.css
  50. function roots_fix_duplicate_subfolder_urls($input) {
  51. $output = roots_root_relative_url($input);
  52. preg_match_all('!([^/]+)/([^/]+)!', $output, $matches);
  53. if (isset($matches[1]) && isset($matches[2])) {
  54. if ($matches[1][0] === $matches[2][0]) {
  55. $output = substr($output, strlen($matches[1][0]) + 1);
  56. }
  57. }
  58. return $output;
  59. }
  60. if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) && $roots_options['root_relative_urls']) {
  61. add_filter('bloginfo_url', 'roots_root_relative_url');
  62. add_filter('theme_root_uri', 'roots_root_relative_url');
  63. add_filter('stylesheet_directory_uri', 'roots_root_relative_url');
  64. add_filter('template_directory_uri', 'roots_root_relative_url');
  65. add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls');
  66. add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls');
  67. add_filter('plugins_url', 'roots_root_relative_url');
  68. add_filter('the_permalink', 'roots_root_relative_url');
  69. add_filter('wp_list_pages', 'roots_root_relative_url');
  70. add_filter('wp_list_categories', 'roots_root_relative_url');
  71. add_filter('wp_nav_menu', 'roots_root_relative_url');
  72. add_filter('the_content_more_link', 'roots_root_relative_url');
  73. add_filter('the_tags', 'roots_root_relative_url');
  74. add_filter('get_pagenum_link', 'roots_root_relative_url');
  75. add_filter('get_comment_link', 'roots_root_relative_url');
  76. add_filter('month_link', 'roots_root_relative_url');
  77. add_filter('day_link', 'roots_root_relative_url');
  78. add_filter('year_link', 'roots_root_relative_url');
  79. add_filter('tag_link', 'roots_root_relative_url');
  80. add_filter('the_author_posts_link', 'roots_root_relative_url');
  81. }
  82. // remove root relative URLs on any attachments in the feed
  83. function roots_root_relative_attachment_urls() {
  84. global $roots_options;
  85. if (!is_feed() && $roots_options['root_relative_urls']) {
  86. add_filter('wp_get_attachment_url', 'roots_root_relative_url');
  87. add_filter('wp_get_attachment_link', 'roots_root_relative_url');
  88. }
  89. }
  90. add_action('pre_get_posts', 'roots_root_relative_attachment_urls');
  91. // set lang="en" as default (rather than en-US)
  92. function roots_language_attributes() {
  93. $attributes = array();
  94. $output = '';
  95. if (function_exists('is_rtl')) {
  96. if (is_rtl() == 'rtl') {
  97. $attributes[] = 'dir="rtl"';
  98. }
  99. }
  100. $lang = get_bloginfo('language');
  101. if ($lang && $lang !== 'en-US') {
  102. $attributes[] = "lang=\"$lang\"";
  103. } else {
  104. $attributes[] = 'lang="en"';
  105. }
  106. $output = implode(' ', $attributes);
  107. $output = apply_filters('roots_language_attributes', $output);
  108. return $output;
  109. }
  110. add_filter('language_attributes', 'roots_language_attributes');
  111. // remove WordPress version from RSS feed
  112. function roots_no_generator() { return ''; }
  113. add_filter('the_generator', 'roots_no_generator');
  114. // cleanup wp_head
  115. function roots_noindex() {
  116. if (get_option('blog_public') === '0') {
  117. echo '<meta name="robots" content="noindex,nofollow">', "\n";
  118. }
  119. }
  120. function roots_rel_canonical() {
  121. if (!is_singular()) {
  122. return;
  123. }
  124. global $wp_the_query;
  125. if (!$id = $wp_the_query->get_queried_object_id()) {
  126. return;
  127. }
  128. $link = get_permalink($id);
  129. echo "\t<link rel=\"canonical\" href=\"$link\">\n";
  130. }
  131. // remove CSS from recent comments widget
  132. function roots_remove_recent_comments_style() {
  133. global $wp_widget_factory;
  134. if (isset($wp_widget_factory->widgets['WP_Widget_Recent_Comments'])) {
  135. remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
  136. }
  137. }
  138. // remove CSS from gallery
  139. function roots_gallery_style($css) {
  140. return preg_replace("!<style type='text/css'>(.*?)</style>!s", '', $css);
  141. }
  142. function roots_head_cleanup() {
  143. // http://wpengineer.com/1438/wordpress-header/
  144. remove_action('wp_head', 'feed_links', 2);
  145. remove_action('wp_head', 'feed_links_extra', 3);
  146. remove_action('wp_head', 'rsd_link');
  147. remove_action('wp_head', 'wlwmanifest_link');
  148. remove_action('wp_head', 'index_rel_link');
  149. remove_action('wp_head', 'parent_post_rel_link', 10, 0);
  150. remove_action('wp_head', 'start_post_rel_link', 10, 0);
  151. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  152. remove_action('wp_head', 'wp_generator');
  153. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  154. remove_action('wp_head', 'noindex', 1);
  155. add_action('wp_head', 'roots_noindex');
  156. remove_action('wp_head', 'rel_canonical');
  157. add_action('wp_head', 'roots_rel_canonical');
  158. add_action('wp_head', 'roots_remove_recent_comments_style', 1);
  159. add_filter('gallery_style', 'roots_gallery_style');
  160. // stop Gravity Forms from outputting CSS since it's linked in header.php
  161. if (class_exists('RGForms')) {
  162. update_option('rg_gforms_disable_css', 1);
  163. }
  164. // deregister l10n.js (new since WordPress 3.1)
  165. // why you might want to keep it: http://wordpress.stackexchange.com/questions/5451/what-does-l10n-js-do-in-wordpress-3-1-and-how-do-i-remove-it/5484#5484
  166. // don't load jQuery through WordPress since it's linked in header.php
  167. if (!is_admin()) {
  168. wp_deregister_script('l10n');
  169. wp_deregister_script('jquery');
  170. wp_register_script('jquery', '', '', '', true);
  171. }
  172. }
  173. add_action('init', 'roots_head_cleanup');
  174. // cleanup gallery_shortcode()
  175. function roots_gallery_shortcode($attr) {
  176. global $post, $wp_locale;
  177. static $instance = 0;
  178. $instance++;
  179. // Allow plugins/themes to override the default gallery template.
  180. $output = apply_filters('post_gallery', '', $attr);
  181. if ($output != '') {
  182. return $output;
  183. }
  184. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  185. if (isset($attr['orderby'])) {
  186. $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
  187. if (!$attr['orderby']) {
  188. unset($attr['orderby']);
  189. }
  190. }
  191. extract(shortcode_atts(array(
  192. 'order' => 'ASC',
  193. 'orderby' => 'menu_order ID',
  194. 'id' => $post->ID,
  195. 'icontag' => 'figure',
  196. 'captiontag' => 'figcaption',
  197. 'columns' => 3,
  198. 'size' => 'thumbnail',
  199. 'include' => '',
  200. 'exclude' => ''
  201. ), $attr));
  202. $id = intval($id);
  203. if ('RAND' == $order) {
  204. $orderby = 'none';
  205. }
  206. if (!empty($include)) {
  207. $include = preg_replace( '/[^0-9,]+/', '', $include );
  208. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  209. $attachments = array();
  210. foreach ($_attachments as $key => $val) {
  211. $attachments[$val->ID] = $_attachments[$key];
  212. }
  213. } elseif (!empty($exclude)) {
  214. $exclude = preg_replace('/[^0-9,]+/', '', $exclude);
  215. $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
  216. } else {
  217. $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
  218. }
  219. if (empty($attachments)) {
  220. return '';
  221. }
  222. if (is_feed()) {
  223. $output = "\n";
  224. foreach ($attachments as $att_id => $attachment)
  225. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  226. return $output;
  227. }
  228. $captiontag = tag_escape($captiontag);
  229. $columns = intval($columns);
  230. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  231. $float = is_rtl() ? 'right' : 'left';
  232. $selector = "gallery-{$instance}";
  233. $gallery_style = $gallery_div = '';
  234. if (apply_filters('use_default_gallery_style', true)) {
  235. $gallery_style = "";
  236. }
  237. $size_class = sanitize_html_class($size);
  238. $gallery_div = "<section id='$selector' class='clearfix gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  239. $output = apply_filters('gallery_style', $gallery_style . "\n\t\t" . $gallery_div);
  240. $i = 0;
  241. foreach ($attachments as $id => $attachment) {
  242. // make the gallery link to the file by default instead of the attachment
  243. // thanks to Matt Price (countingrows.com)
  244. $link = isset($attr['link']) && $attr['link'] === 'attachment' ?
  245. wp_get_attachment_link($id, $size, true, false) :
  246. wp_get_attachment_link($id, $size, false, false);
  247. $output .= "
  248. <{$icontag} class=\"gallery-item\">
  249. $link
  250. ";
  251. if ($captiontag && trim($attachment->post_excerpt)) {
  252. $output .= "
  253. <{$captiontag} class=\"gallery-caption\">
  254. " . wptexturize($attachment->post_excerpt) . "
  255. </{$captiontag}>";
  256. }
  257. $output .= "</{$icontag}>";
  258. if ($columns > 0 && ++$i % $columns == 0) {
  259. $output .= '';
  260. }
  261. }
  262. $output .= "</section>\n";
  263. return $output;
  264. }
  265. remove_shortcode('gallery');
  266. add_shortcode('gallery', 'roots_gallery_shortcode');
  267. // http://www.deluxeblogtips.com/2011/01/remove-dashboard-widgets-in-wordpress.html
  268. function roots_remove_dashboard_widgets() {
  269. remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
  270. remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
  271. remove_meta_box('dashboard_primary', 'dashboard', 'normal');
  272. remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
  273. }
  274. add_action('admin_init', 'roots_remove_dashboard_widgets');
  275. // excerpt cleanup
  276. function roots_excerpt_length($length) {
  277. return 40;
  278. }
  279. function roots_excerpt_more($more) {
  280. return ' &hellip; <a href="' . get_permalink() . '">' . __( 'Continued', 'roots' ) . '</a>';
  281. }
  282. add_filter('excerpt_length', 'roots_excerpt_length');
  283. add_filter('excerpt_more', 'roots_excerpt_more');
  284. // remove container from menus
  285. function roots_nav_menu_args($args = '') {
  286. $args['container'] = false;
  287. return $args;
  288. }
  289. add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
  290. function roots_check_current($val) {
  291. return preg_match('/current-/', $val);
  292. }
  293. class Roots_Nav_Walker extends Walker_Nav_Menu {
  294. function start_el(&$output, $item, $depth, $args) {
  295. global $wp_query;
  296. $indent = ($depth) ? str_repeat("\t", $depth) : '';
  297. $slug = sanitize_title($item->title);
  298. $class_names = $value = '';
  299. $classes = empty($item->classes) ? array() : (array) $item->classes;
  300. $classes = array_filter($classes, 'roots_check_current');
  301. $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item));
  302. $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
  303. $id = apply_filters('nav_menu_item_id', 'menu-' . $slug, $item, $args);
  304. $id = strlen($id) ? ' id="' . esc_attr( $id ) . '"' : '';
  305. $output .= $indent . '<li' . $id . $class_names . '>';
  306. $attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
  307. $attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
  308. $attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
  309. $attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
  310. $item_output = $args->before;
  311. $item_output .= '<a'. $attributes .'>';
  312. $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
  313. $item_output .= '</a>';
  314. $item_output .= $args->after;
  315. $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  316. }
  317. }
  318. // use the clean nav menu walker for all nav menus if the option is set
  319. function roots_default_wp_nav_menu_walker($args = '') {
  320. global $roots_options;
  321. if ($roots_options['clean_menu']) {
  322. $args['walker'] = new Roots_Nav_Walker();
  323. }
  324. return $args;
  325. }
  326. add_filter('wp_nav_menu_args', 'roots_default_wp_nav_menu_walker');
  327. // add to robots.txt
  328. // http://codex.wordpress.org/Search_Engine_Optimization_for_WordPress#Robots.txt_Optimization
  329. function roots_robots() {
  330. echo "Disallow: /cgi-bin\n";
  331. echo "Disallow: /wp-admin\n";
  332. echo "Disallow: /wp-includes\n";
  333. echo "Disallow: /wp-content/plugins\n";
  334. echo "Disallow: /plugins\n";
  335. echo "Disallow: /wp-content/cache\n";
  336. echo "Disallow: /wp-content/themes\n";
  337. echo "Disallow: /trackback\n";
  338. echo "Disallow: /feed\n";
  339. echo "Disallow: /comments\n";
  340. echo "Disallow: /category/*/*\n";
  341. echo "Disallow: */trackback\n";
  342. echo "Disallow: */feed\n";
  343. echo "Disallow: */comments\n";
  344. echo "Disallow: /*?*\n";
  345. echo "Disallow: /*?\n";
  346. echo "Allow: /wp-content/uploads\n";
  347. echo "Allow: /assets";
  348. }
  349. add_action('do_robots', 'roots_robots');
  350. // we don't need to self-close these tags in html5:
  351. // <img>, <input>
  352. function roots_remove_self_closing_tags($input) {
  353. return str_replace(' />', '>', $input);
  354. }
  355. add_filter('get_avatar', 'roots_remove_self_closing_tags');
  356. add_filter('comment_id_fields', 'roots_remove_self_closing_tags');
  357. add_filter('post_thumbnail_html', 'roots_remove_self_closing_tags');
  358. // check to see if the tagline is set to default
  359. // show an admin notice to update if it hasn't been changed
  360. // you want to change this or remove it because it's used as the description in the RSS feed
  361. function roots_notice_tagline() {
  362. global $current_user;
  363. $user_id = $current_user->ID;
  364. if (!get_user_meta($user_id, 'ignore_tagline_notice')) {
  365. echo '<div class="error">';
  366. 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>';
  367. echo '</div>';
  368. }
  369. }
  370. if ((get_option('blogdescription') === 'Just another WordPress site') && isset($_GET['page']) != 'theme_activation_options') {
  371. add_action('admin_notices', 'roots_notice_tagline');
  372. }
  373. function roots_notice_tagline_ignore() {
  374. global $current_user;
  375. $user_id = $current_user->ID;
  376. if (isset($_GET['tagline_notice_ignore']) && '0' == $_GET['tagline_notice_ignore']) {
  377. add_user_meta($user_id, 'ignore_tagline_notice', 'true', true);
  378. }
  379. }
  380. add_action('admin_init', 'roots_notice_tagline_ignore');
  381. // set the post revisions to 5 unless the constant
  382. // was set in wp-config.php to avoid DB bloat
  383. if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);
  384. // allow more tags in TinyMCE including <iframe> and <script>
  385. function roots_change_mce_options($options) {
  386. $ext = 'pre[id|name|class|style],iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src],script[charset|defer|language|src|type]';
  387. if (isset($initArray['extended_valid_elements'])) {
  388. $options['extended_valid_elements'] .= ',' . $ext;
  389. } else {
  390. $options['extended_valid_elements'] = $ext;
  391. }
  392. return $options;
  393. }
  394. add_filter('tiny_mce_before_init', 'roots_change_mce_options');
  395. //clean up the default WordPress style tags
  396. add_filter('style_loader_tag', 'roots_clean_style_tag');
  397. function roots_clean_style_tag($input) {
  398. preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
  399. //only display media if it's print
  400. $media = $matches[3][0] === 'print' ? ' media="print"' : '';
  401. return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
  402. }
  403. function roots_body_class() {
  404. $term = get_queried_object();
  405. if (is_single()) {
  406. $cat = get_the_category();
  407. }
  408. if(!empty($cat)) {
  409. return $cat[0]->slug;
  410. } elseif(isset($term->slug)) {
  411. return $term->slug;
  412. } elseif(isset($term->page_name)) {
  413. return $term->page_name;
  414. } elseif(isset($term->post_name)) {
  415. return $term->post_name;
  416. } else {
  417. return;
  418. }
  419. }
  420. // first and last classes for widgets
  421. // http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets
  422. function roots_widget_first_last_classes($params) {
  423. global $my_widget_num;
  424. $this_id = $params[0]['id'];
  425. $arr_registered_widgets = wp_get_sidebars_widgets();
  426. if (!$my_widget_num) {
  427. $my_widget_num = array();
  428. }
  429. if (!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) {
  430. return $params;
  431. }
  432. if (isset($my_widget_num[$this_id])) {
  433. $my_widget_num[$this_id] ++;
  434. } else {
  435. $my_widget_num[$this_id] = 1;
  436. }
  437. $class = 'class="widget-' . $my_widget_num[$this_id] . ' ';
  438. if ($my_widget_num[$this_id] == 1) {
  439. $class .= 'widget-first ';
  440. } elseif ($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) {
  441. $class .= 'widget-last ';
  442. }
  443. $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
  444. return $params;
  445. }
  446. add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
  447. ?>