PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/bladencountyrecords/inc/roots-cleanup.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 515 lines | 296 code | 62 blank | 157 comment | 43 complexity | 78d15856878192e0839224ab087d5add MD5 | raw file
  1. <?php
  2. // redirect /?s to /search/
  3. // http://txfx.net/wordpress-plugins/nice-search/
  4. function roots_nice_search_redirect() {
  5. if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
  6. wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var( 's' )))), 301);
  7. exit();
  8. }
  9. }
  10. add_action('template_redirect', 'roots_nice_search_redirect');
  11. function roots_search_query($escaped = true) {
  12. $query = apply_filters('roots_search_query', get_query_var('s'));
  13. if ($escaped) {
  14. $query = esc_attr($query);
  15. }
  16. return urldecode($query);
  17. }
  18. add_filter('get_search_query', 'roots_search_query');
  19. // fix for empty search query
  20. // http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
  21. function roots_request_filter($query_vars) {
  22. if (isset($_GET['s']) && empty($_GET['s'])) {
  23. $query_vars['s'] = " ";
  24. }
  25. return $query_vars;
  26. }
  27. add_filter('request', 'roots_request_filter');
  28. // root relative URLs for everything
  29. // inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
  30. // thanks to Scott Walkinshaw (scottwalkinshaw.com)
  31. function roots_root_relative_url($input) {
  32. $output = preg_replace_callback(
  33. '!(https?://[^/|"]+)([^"]+)?!',
  34. create_function(
  35. '$matches',
  36. // if full URL is site_url, return a slash for relative root
  37. 'if (isset($matches[0]) && $matches[0] === site_url()) { return "/";' .
  38. // if domain is equal to site_url, then make URL relative
  39. '} elseif (isset($matches[0]) && strpos($matches[0], site_url()) !== false) { return $matches[2];' .
  40. // if domain is not equal to site_url, do not make external link relative
  41. '} else { return $matches[0]; };'
  42. ),
  43. $input
  44. );
  45. return $output;
  46. }
  47. // Terrible workaround to remove the duplicate subfolder in the src of JS/CSS tags
  48. // Example: /subfolder/subfolder/css/style.css
  49. function roots_fix_duplicate_subfolder_urls($input) {
  50. $output = roots_root_relative_url($input);
  51. preg_match_all('!([^/]+)/([^/]+)!', $output, $matches);
  52. if (isset($matches[1]) && isset($matches[2])) {
  53. if ($matches[1][0] === $matches[2][0]) {
  54. $output = substr($output, strlen($matches[1][0]) + 1);
  55. }
  56. }
  57. return $output;
  58. }
  59. $roots_options = roots_get_theme_options();
  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. $roots_options = roots_get_theme_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. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  184. if ( isset( $attr['orderby'] ) ) {
  185. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  186. if ( !$attr['orderby'] )
  187. unset( $attr['orderby'] );
  188. }
  189. extract(shortcode_atts(array(
  190. 'order' => 'ASC',
  191. 'orderby' => 'menu_order ID',
  192. 'id' => $post->ID,
  193. 'icontag' => 'figure',
  194. 'captiontag' => 'figcaption',
  195. 'columns' => 3,
  196. 'size' => 'thumbnail',
  197. 'include' => '',
  198. 'exclude' => ''
  199. ), $attr));
  200. $id = intval($id);
  201. if ( 'RAND' == $order )
  202. $orderby = 'none';
  203. if ( !empty($include) ) {
  204. $include = preg_replace( '/[^0-9,]+/', '', $include );
  205. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  206. $attachments = array();
  207. foreach ( $_attachments as $key => $val ) {
  208. $attachments[$val->ID] = $_attachments[$key];
  209. }
  210. } elseif ( !empty($exclude) ) {
  211. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  212. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  213. } else {
  214. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  215. }
  216. if ( empty($attachments) )
  217. return '';
  218. if ( is_feed() ) {
  219. $output = "\n";
  220. foreach ( $attachments as $att_id => $attachment )
  221. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  222. return $output;
  223. }
  224. $captiontag = tag_escape($captiontag);
  225. $columns = intval($columns);
  226. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  227. $float = is_rtl() ? 'right' : 'left';
  228. $selector = "gallery-{$instance}";
  229. $gallery_style = $gallery_div = '';
  230. if ( apply_filters( 'use_default_gallery_style', true ) )
  231. $gallery_style = "";
  232. $size_class = sanitize_html_class( $size );
  233. $gallery_div = "<section id='$selector' class='clearfix gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  234. $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  235. $i = 0;
  236. foreach ( $attachments as $id => $attachment ) {
  237. // make the gallery link to the file by default instead of the attachment
  238. // thanks to Matt Price (countingrows.com)
  239. $link = isset($attr['link']) && $attr['link'] === 'attachment' ?
  240. wp_get_attachment_link($id, $size, true, false) :
  241. wp_get_attachment_link($id, $size, false, false);
  242. $output .= "
  243. <{$icontag} class=\"gallery-item\">
  244. $link
  245. ";
  246. if ( $captiontag && trim($attachment->post_excerpt) ) {
  247. $output .= "
  248. <{$captiontag} class=\"gallery-caption\">
  249. " . wptexturize($attachment->post_excerpt) . "
  250. </{$captiontag}>";
  251. }
  252. $output .= "</{$icontag}>";
  253. if ( $columns > 0 && ++$i % $columns == 0 )
  254. $output .= '';
  255. }
  256. $output .= "</section>\n";
  257. return $output;
  258. }
  259. remove_shortcode('gallery');
  260. add_shortcode('gallery', 'roots_gallery_shortcode');
  261. // http://www.deluxeblogtips.com/2011/01/remove-dashboard-widgets-in-wordpress.html
  262. function roots_remove_dashboard_widgets() {
  263. remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
  264. remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
  265. remove_meta_box('dashboard_primary', 'dashboard', 'normal');
  266. remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
  267. }
  268. add_action('admin_init', 'roots_remove_dashboard_widgets');
  269. // excerpt cleanup
  270. function roots_excerpt_length($length) {
  271. return 40;
  272. }
  273. function roots_excerpt_more($more) {
  274. return ' &hellip; <a href="' . get_permalink() . '">' . __( 'Continued', 'roots' ) . '</a>';
  275. }
  276. add_filter('excerpt_length', 'roots_excerpt_length');
  277. add_filter('excerpt_more', 'roots_excerpt_more');
  278. // remove container from menus
  279. function roots_nav_menu_args($args = '') {
  280. $args['container'] = false;
  281. return $args;
  282. }
  283. add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
  284. class roots_nav_walker extends Walker_Nav_Menu {
  285. function start_el(&$output, $item, $depth, $args) {
  286. global $wp_query;
  287. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  288. $slug = sanitize_title($item->title);
  289. $class_names = $value = '';
  290. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  291. $classes = array_filter($classes, 'roots_check_current');
  292. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  293. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  294. $id = apply_filters( 'nav_menu_item_id', 'menu-' . $slug, $item, $args );
  295. $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  296. $output .= $indent . '<li' . $id . $class_names . '>';
  297. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  298. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  299. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  300. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  301. $item_output = $args->before;
  302. $item_output .= '<a'. $attributes .'>';
  303. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  304. $item_output .= '</a>';
  305. $item_output .= $args->after;
  306. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  307. }
  308. }
  309. function roots_check_current($val) {
  310. return preg_match('/current-menu/', $val);
  311. }
  312. // add to robots.txt
  313. // http://codex.wordpress.org/Search_Engine_Optimization_for_WordPress#Robots.txt_Optimization
  314. function roots_robots() {
  315. echo "Disallow: /cgi-bin\n";
  316. echo "Disallow: /wp-admin\n";
  317. echo "Disallow: /wp-includes\n";
  318. echo "Disallow: /wp-content/plugins\n";
  319. echo "Disallow: /plugins\n";
  320. echo "Disallow: /wp-content/cache\n";
  321. echo "Disallow: /wp-content/themes\n";
  322. echo "Disallow: /trackback\n";
  323. echo "Disallow: /feed\n";
  324. echo "Disallow: /comments\n";
  325. echo "Disallow: /category/*/*\n";
  326. echo "Disallow: */trackback\n";
  327. echo "Disallow: */feed\n";
  328. echo "Disallow: */comments\n";
  329. echo "Disallow: /*?*\n";
  330. echo "Disallow: /*?\n";
  331. echo "Allow: /wp-content/uploads\n";
  332. echo "Allow: /assets";
  333. }
  334. add_action('do_robots', 'roots_robots');
  335. // we don't need to self-close these tags in html5:
  336. // <img>, <input>
  337. function roots_remove_self_closing_tags($input) {
  338. return str_replace(' />', '>', $input);
  339. }
  340. add_filter('get_avatar', 'roots_remove_self_closing_tags');
  341. add_filter('comment_id_fields', 'roots_remove_self_closing_tags');
  342. add_filter('post_thumbnail_html', 'roots_remove_self_closing_tags');
  343. // check to see if the tagline is set to default
  344. // show an admin notice to update if it hasn't been changed
  345. // you want to change this or remove it because it's used as the description in the RSS feed
  346. function roots_notice_tagline() {
  347. global $current_user;
  348. $user_id = $current_user->ID;
  349. if (!get_user_meta($user_id, 'ignore_tagline_notice')) {
  350. echo '<div class="error">';
  351. 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>';
  352. echo '</div>';
  353. }
  354. }
  355. if (get_option('blogdescription') === 'Just another WordPress site') {
  356. add_action('admin_notices', 'roots_notice_tagline');
  357. }
  358. function roots_notice_tagline_ignore() {
  359. global $current_user;
  360. $user_id = $current_user->ID;
  361. if (isset($_GET['tagline_notice_ignore']) && '0' == $_GET['tagline_notice_ignore']) {
  362. add_user_meta($user_id, 'ignore_tagline_notice', 'true', true);
  363. }
  364. }
  365. add_action('admin_init', 'roots_notice_tagline_ignore');
  366. // set the post revisions to 5 unless the constant
  367. // was set in wp-config.php to avoid DB bloat
  368. if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);
  369. // allow more tags in TinyMCE including iframes
  370. function roots_change_mce_options($options) {
  371. $ext = 'pre[id|name|class|style],iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src]';
  372. if (isset($initArray['extended_valid_elements'])) {
  373. $options['extended_valid_elements'] .= ',' . $ext;
  374. } else {
  375. $options['extended_valid_elements'] = $ext;
  376. }
  377. return $options;
  378. }
  379. add_filter('tiny_mce_before_init', 'roots_change_mce_options');
  380. //clean up the default WordPress style tags
  381. add_filter('style_loader_tag', 'roots_clean_style_tag');
  382. function roots_clean_style_tag($input) {
  383. preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
  384. //only display media if it's print
  385. $media = $matches[3][0] === 'print' ? ' media="print"' : '';
  386. return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
  387. }
  388. function roots_body_class() {
  389. $term = get_queried_object();
  390. if (is_single())
  391. $cat = get_the_category();
  392. if(!empty($cat))
  393. return $cat[0]->slug;
  394. elseif(isset($term->slug))
  395. return $term->slug;
  396. elseif(isset($term->page_name))
  397. return $term->page_name;
  398. elseif(isset($term->post_name))
  399. return $term->post_name;
  400. else
  401. return;
  402. }
  403. // first and last classes for widgets
  404. // http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets
  405. function roots_widget_first_last_classes($params) {
  406. global $my_widget_num;
  407. $this_id = $params[0]['id'];
  408. $arr_registered_widgets = wp_get_sidebars_widgets();
  409. if (!$my_widget_num) {
  410. $my_widget_num = array();
  411. }
  412. if (!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) {
  413. return $params;
  414. }
  415. if (isset($my_widget_num[$this_id])) {
  416. $my_widget_num[$this_id] ++;
  417. } else {
  418. $my_widget_num[$this_id] = 1;
  419. }
  420. $class = 'class="widget-' . $my_widget_num[$this_id] . ' ';
  421. if ($my_widget_num[$this_id] == 1) {
  422. $class .= 'widget-first ';
  423. } elseif ($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) {
  424. $class .= 'widget-last ';
  425. }
  426. $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
  427. return $params;
  428. }
  429. add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
  430. ?>