PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/nonus/framework/roots/cleanup.php

https://github.com/alniko009/magic
PHP | 631 lines | 382 code | 118 blank | 131 comment | 63 complexity | e7e0138d7c562f3256f030fbfa49b1aa MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Clean up wp_head()
  4. *
  5. * Remove unnecessary <link>'s
  6. * Remove inline CSS used by Recent Comments widget
  7. * Remove inline CSS used by posts with galleries
  8. * Remove self-closing tag and change ''s to "'s on rel_canonical()
  9. */
  10. function roots_head_cleanup() {
  11. // Originally from http://wpengineer.com/1438/wordpress-header/
  12. remove_action('wp_head', 'feed_links', 2);
  13. remove_action('wp_head', 'feed_links_extra', 3);
  14. remove_action('wp_head', 'rsd_link');
  15. remove_action('wp_head', 'wlwmanifest_link');
  16. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  17. remove_action('wp_head', 'wp_generator');
  18. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  19. global $wp_widget_factory;
  20. remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
  21. add_filter('use_default_gallery_style', '__return_null');
  22. if (!class_exists('WPSEO_Frontend')) {
  23. remove_action('wp_head', 'rel_canonical');
  24. add_action('wp_head', 'roots_rel_canonical');
  25. }
  26. }
  27. function roots_rel_canonical() {
  28. global $wp_the_query;
  29. if (!is_singular()) {
  30. return;
  31. }
  32. if (!$id = $wp_the_query->get_queried_object_id()) {
  33. return;
  34. }
  35. $link = get_permalink($id);
  36. echo "\t<link rel=\"canonical\" href=\"$link\">\n";
  37. }
  38. add_action('init', 'roots_head_cleanup');
  39. /**
  40. * Remove the WordPress version from RSS feeds
  41. */
  42. add_filter('the_generator', '__return_false');
  43. /**
  44. * Clean up language_attributes() used in <html> tag
  45. *
  46. * Change lang="en-US" to lang="en"
  47. * Remove dir="ltr"
  48. */
  49. function roots_language_attributes() {
  50. $attributes = array();
  51. $output = '';
  52. if (function_exists('is_rtl')) {
  53. if (is_rtl() == 'rtl') {
  54. $attributes[] = 'dir="rtl"';
  55. }
  56. }
  57. $lang = get_bloginfo('language');
  58. if ($lang && $lang !== 'en-US') {
  59. $attributes[] = "lang=\"$lang\"";
  60. } else {
  61. $attributes[] = 'lang="en"';
  62. }
  63. $output = implode(' ', $attributes);
  64. $output = apply_filters('roots_language_attributes', $output);
  65. return $output;
  66. }
  67. add_filter('language_attributes', 'roots_language_attributes');
  68. /**
  69. * Clean up output of stylesheet <link> tags
  70. */
  71. function roots_clean_style_tag($input) {
  72. preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
  73. // Only display media if it's print
  74. $media = $matches[3][0] === 'print' ? ' media="print"' : '';
  75. return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
  76. }
  77. add_filter('style_loader_tag', 'roots_clean_style_tag');
  78. /**
  79. * Add and remove body_class() classes
  80. */
  81. function roots_body_class($classes) {
  82. // Add 'top-navbar' class if using Bootstrap's Navbar
  83. // Used to add styling to account for the WordPress admin bar
  84. if (current_theme_supports('bootstrap-top-navbar')) {
  85. $classes[] = 'top-navbar';
  86. }
  87. // Add post/page slug
  88. if (is_single() || is_page() && !is_front_page()) {
  89. $classes[] = basename(get_permalink());
  90. }
  91. // Remove unnecessary classes
  92. $home_id_class = 'page-id-' . get_option('page_on_front');
  93. $remove_classes = array(
  94. 'page-template-default',
  95. $home_id_class
  96. );
  97. $classes = array_diff($classes, $remove_classes);
  98. return $classes;
  99. }
  100. add_filter('body_class', 'roots_body_class');
  101. /**
  102. * Root relative URLs
  103. *
  104. * WordPress likes to use absolute URLs on everything - let's clean that up.
  105. * Inspired by http://www.456bereastreet.com/archive/201010/how_to_make_wordpress_urls_root_relative/
  106. *
  107. * You can enable/disable this feature in config.php:
  108. * current_theme_supports('root-relative-urls');
  109. *
  110. * @author Scott Walkinshaw <scott.walkinshaw@gmail.com>
  111. */
  112. function roots_root_relative_url($input) {
  113. $output = preg_replace_callback(
  114. '!(https?://[^/|"]+)([^"]+)?!',
  115. create_function(
  116. '$matches',
  117. // If full URL is home_url("/") and this isn't a subdir install, return a slash for relative root
  118. 'if (isset($matches[0]) && $matches[0] === home_url("/") && str_replace("http://", "", home_url("/", "http"))==$_SERVER["HTTP_HOST"]) { return "/";' .
  119. // If domain is equal to home_url("/"), then make URL relative
  120. '} elseif (isset($matches[0]) && strpos($matches[0], home_url("/")) !== false) { return $matches[2];' .
  121. // If domain is not equal to home_url("/"), do not make external link relative
  122. '} else { return $matches[0]; };'
  123. ),
  124. $input
  125. );
  126. return $output;
  127. }
  128. /**
  129. * Terrible workaround to remove the duplicate subfolder in the src of <script> and <link> tags
  130. * Example: /subfolder/subfolder/css/style.css
  131. */
  132. function roots_fix_duplicate_subfolder_urls($input) {
  133. $output = roots_root_relative_url($input);
  134. preg_match_all('!([^/]+)/([^/]+)!', $output, $matches);
  135. if (isset($matches[1]) && isset($matches[2])) {
  136. if ($matches[1][0] === $matches[2][0]) {
  137. $output = substr($output, strlen($matches[1][0]) + 1);
  138. }
  139. }
  140. return $output;
  141. }
  142. function roots_enable_root_relative_urls() {
  143. return !(is_admin() && in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) && current_theme_supports('root-relative-urls') && apply_filters('root-relative-urls',true);
  144. }
  145. if (roots_enable_root_relative_urls()) {
  146. $root_rel_filters = array(
  147. 'bloginfo_url',
  148. 'theme_root_uri',
  149. 'stylesheet_directory_uri',
  150. 'template_directory_uri',
  151. 'plugins_url',
  152. 'the_permalink',
  153. 'wp_list_pages',
  154. 'wp_list_categories',
  155. 'wp_nav_menu',
  156. 'the_content_more_link',
  157. 'the_tags',
  158. 'get_pagenum_link',
  159. 'get_comment_link',
  160. 'month_link',
  161. 'day_link',
  162. 'year_link',
  163. 'tag_link',
  164. 'the_author_posts_link'
  165. );
  166. add_filters($root_rel_filters, 'roots_root_relative_url');
  167. add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls');
  168. add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls');
  169. }
  170. /**
  171. * Wrap embedded media as suggested by Readability
  172. *
  173. * @link https://gist.github.com/965956
  174. * @link http://www.readability.com/publishers/guidelines#publisher
  175. */
  176. function roots_embed_wrap($cache, $url, $attr = '', $post_ID = '') {
  177. return '<div class="entry-content-asset">' . $cache . '</div>';
  178. }
  179. add_filter('embed_oembed_html', 'roots_embed_wrap', 10, 4);
  180. add_filter('embed_googlevideo', 'roots_embed_wrap', 10, 2);
  181. /**
  182. * Add class="thumbnail" to attachment items
  183. */
  184. function roots_attachment_link_class($html) {
  185. $postid = get_the_ID();
  186. $html = str_replace('<a', '<a class="thumbnail"', $html);
  187. return $html;
  188. }
  189. add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1);
  190. /**
  191. * Add Bootstrap thumbnail styling to images with captions
  192. * Use <figure> and <figcaption>
  193. *
  194. * @link http://justintadlock.com/archives/2011/07/01/captions-in-wordpress
  195. */
  196. function roots_caption($output, $attr, $content) {
  197. if (is_feed()) {
  198. return $output;
  199. }
  200. $defaults = array(
  201. 'id' => '',
  202. 'align' => 'alignnone',
  203. 'width' => '',
  204. 'caption' => ''
  205. );
  206. $attr = shortcode_atts($defaults, $attr);
  207. // If the width is less than 1 or there is no caption, return the content wrapped between the [caption] tags
  208. if ($attr['width'] < 1 || empty($attr['caption'])) {
  209. return $content;
  210. }
  211. // Set up the attributes for the caption <figure>
  212. $attributes = (!empty($attr['id']) ? ' id="' . esc_attr($attr['id']) . '"' : '' );
  213. $attributes .= ' class="thumbnail wp-caption ' . esc_attr($attr['align']) . '"';
  214. $attributes .= ' style="width: ' . esc_attr($attr['width']) . 'px"';
  215. $output = '<figure' . $attributes .'>';
  216. $output .= do_shortcode($content);
  217. $output .= '<figcaption class="caption wp-caption-text">' . $attr['caption'] . '</figcaption>';
  218. $output .= '</figure>';
  219. return $output;
  220. }
  221. add_filter('img_caption_shortcode', 'roots_caption', 10, 3);
  222. /**
  223. * Clean up gallery_shortcode()
  224. *
  225. * Re-create the [gallery] shortcode and use thumbnails styling from Bootstrap
  226. *
  227. * @link http://twitter.github.com/bootstrap/components.html#thumbnails
  228. */
  229. function roots_gallery($attr) {
  230. $post = get_post();
  231. static $instance = 0;
  232. $instance++;
  233. if (!empty($attr['ids'])) {
  234. if (empty($attr['orderby'])) {
  235. $attr['orderby'] = 'post__in';
  236. }
  237. $attr['include'] = $attr['ids'];
  238. }
  239. $output = apply_filters('post_gallery', '', $attr);
  240. if ($output != '') {
  241. return $output;
  242. }
  243. if (isset($attr['orderby'])) {
  244. $attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
  245. if (!$attr['orderby']) {
  246. unset($attr['orderby']);
  247. }
  248. }
  249. extract(shortcode_atts(array(
  250. 'order' => 'ASC',
  251. 'orderby' => 'menu_order ID',
  252. 'id' => $post->ID,
  253. 'itemtag' => '',
  254. 'icontag' => '',
  255. 'captiontag' => '',
  256. 'columns' => 3,
  257. 'size' => 'thumbnail',
  258. 'include' => '',
  259. 'exclude' => ''
  260. ), $attr));
  261. $id = intval($id);
  262. if ($order === 'RAND') {
  263. $orderby = 'none';
  264. }
  265. if (!empty($include)) {
  266. $_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
  267. $attachments = array();
  268. foreach ($_attachments as $key => $val) {
  269. $attachments[$val->ID] = $_attachments[$key];
  270. }
  271. } elseif (!empty($exclude)) {
  272. $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
  273. } else {
  274. $attachments = get_children(array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
  275. }
  276. if (empty($attachments)) {
  277. return '';
  278. }
  279. if (is_feed()) {
  280. $output = "\n";
  281. foreach ($attachments as $att_id => $attachment) {
  282. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  283. }
  284. return $output;
  285. }
  286. $output = '<ul class="thumbnails gallery">';
  287. $i = 0;
  288. foreach ($attachments as $id => $attachment) {
  289. $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  290. $output .= '<li>' . $link;
  291. if (trim($attachment->post_excerpt)) {
  292. $output .= '<div class="caption hidden">' . wptexturize($attachment->post_excerpt) . '</div>';
  293. }
  294. $output .= '</li>';
  295. }
  296. $output .= '</ul>';
  297. return $output;
  298. }
  299. remove_shortcode('gallery');
  300. add_shortcode('gallery', 'roots_gallery');
  301. /**
  302. * Remove unnecessary dashboard widgets
  303. *
  304. * @link http://www.deluxeblogtips.com/2011/01/remove-dashboard-widgets-in-wordpress.html
  305. */
  306. function roots_remove_dashboard_widgets() {
  307. remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
  308. remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
  309. remove_meta_box('dashboard_primary', 'dashboard', 'normal');
  310. remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
  311. }
  312. add_action('admin_init', 'roots_remove_dashboard_widgets');
  313. /**
  314. * Clean up the_excerpt()
  315. */
  316. function roots_excerpt_length($length) {
  317. return POST_EXCERPT_LENGTH;
  318. }
  319. function roots_excerpt_more($more) {
  320. return ' &hellip; <a href="' . get_permalink() . '">' . __('Continued', 'ct_theme') . '</a>';
  321. }
  322. add_filter('excerpt_length', 'roots_excerpt_length');
  323. add_filter('excerpt_more', 'roots_excerpt_more');
  324. /**
  325. * Cleaner walker for wp_nav_menu()
  326. *
  327. * Walker_Nav_Menu (WordPress default) example output:
  328. * <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="/">Home</a></li>
  329. * <li id="menu-item-9" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9"><a href="/sample-page/">Sample Page</a></l
  330. *
  331. * Roots_Nav_Walker example output:
  332. * <li class="menu-home"><a href="/">Home</a></li>
  333. * <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li>
  334. */
  335. class Roots_Nav_Walker extends Walker_Nav_Menu {
  336. function check_current($classes) {
  337. return preg_match('/(current[-_])|active|dropdown/', $classes);
  338. }
  339. function start_lvl(&$output, $depth = 0, $args = array()) {
  340. $output .= "\n<ul class=\"dropdown-menu unstyled\">\n";
  341. }
  342. function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
  343. $item_html = '';
  344. parent::start_el($item_html, $item, $depth, $args);
  345. if ($item->is_dropdown && ($depth === 0)) {
  346. $item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
  347. //$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
  348. }
  349. elseif (in_array('divider-vertical', $item->classes)) {
  350. $item_html = '<li class="divider-vertical">';
  351. }
  352. elseif (in_array('divider', $item->classes)) {
  353. $item_html = '<li class="divider">';
  354. }
  355. elseif (in_array('nav-header', $item->classes)) {
  356. $item_html = '<li class="nav-header">' . $item->title;
  357. }
  358. $output .= $item_html;
  359. }
  360. function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
  361. $element->is_dropdown = !empty($children_elements[$element->ID]);
  362. if ($element->is_dropdown) {
  363. if ($depth === 0) {
  364. $element->classes[] = 'dropdown';
  365. } elseif ($depth >= 1 && $max_depth-1 >1) {
  366. $element->classes[] = 'dropdown-submenu';
  367. }
  368. }
  369. parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
  370. }
  371. }
  372. /**
  373. * Remove the id="" on nav menu items
  374. * Return 'menu-slug' for nav menu classes
  375. */
  376. function roots_nav_menu_css_class($classes, $item) {
  377. $slug = sanitize_title($item->title);
  378. $classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', is_404()?'':'active', $classes);
  379. $classes = preg_replace('/((menu|page)[-_\w+]+)+/', '', $classes);
  380. $classes[] = 'menu-' . $slug;
  381. $classes = array_unique($classes);
  382. return array_filter($classes, 'is_element_empty');
  383. }
  384. add_filter('nav_menu_css_class', 'roots_nav_menu_css_class', 10, 2);
  385. add_filter('nav_menu_item_id', '__return_null');
  386. /**
  387. * Clean up wp_nav_menu_args
  388. *
  389. * Remove the container
  390. * Use Roots_Nav_Walker() by default
  391. */
  392. function roots_nav_menu_args($args = '') {
  393. $roots_nav_menu_args['container'] = false;
  394. if (!$args['items_wrap']) {
  395. $roots_nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
  396. }
  397. if (current_theme_supports('bootstrap-top-navbar')) {
  398. $roots_nav_menu_args['depth'] = ct_get_option('roots_navbar_depth',99);
  399. }
  400. if (!$args['walker']) {
  401. $roots_nav_menu_args['walker'] = new Roots_Nav_Walker();
  402. }
  403. return array_merge($args, $roots_nav_menu_args);
  404. }
  405. add_filter('wp_nav_menu_args', 'roots_nav_menu_args');
  406. /**
  407. * Remove unnecessary self-closing tags
  408. */
  409. function roots_remove_self_closing_tags($input) {
  410. return str_replace(' />', '>', $input);
  411. }
  412. add_filter('get_avatar', 'roots_remove_self_closing_tags'); // <img />
  413. add_filter('comment_id_fields', 'roots_remove_self_closing_tags'); // <input />
  414. add_filter('post_thumbnail_html', 'roots_remove_self_closing_tags'); // <img />
  415. /**
  416. * Don't return the default description in the RSS feed if it hasn't been changed
  417. */
  418. function roots_remove_default_description($bloginfo) {
  419. $default_tagline = 'Just another WordPress site';
  420. return ($bloginfo === $default_tagline) ? '' : $bloginfo;
  421. }
  422. add_filter('get_bloginfo_rss', 'roots_remove_default_description');
  423. /**
  424. * Allow more tags in TinyMCE including <iframe> and <script>
  425. */
  426. function roots_change_mce_options($options) {
  427. $ext = 'pre[id|name|class|style],iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src],script[charset|defer|language|src|type]';
  428. if (isset($initArray['extended_valid_elements'])) {
  429. $options['extended_valid_elements'] .= ',' . $ext;
  430. } else {
  431. $options['extended_valid_elements'] = $ext;
  432. }
  433. return $options;
  434. }
  435. add_filter('tiny_mce_before_init', 'roots_change_mce_options');
  436. /**
  437. * Add additional classes onto widgets
  438. *
  439. * @link http://wordpress.org/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets
  440. */
  441. function roots_widget_first_last_classes($params) {
  442. global $my_widget_num;
  443. $this_id = $params[0]['id'];
  444. $arr_registered_widgets = wp_get_sidebars_widgets();
  445. if (!$my_widget_num) {
  446. $my_widget_num = array();
  447. }
  448. if (!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) {
  449. return $params;
  450. }
  451. if (isset($my_widget_num[$this_id])) {
  452. $my_widget_num[$this_id] ++;
  453. } else {
  454. $my_widget_num[$this_id] = 1;
  455. }
  456. $class = 'class="widget-' . $my_widget_num[$this_id] . ' ';
  457. if ($my_widget_num[$this_id] == 1) {
  458. $class .= 'widget-first ';
  459. } elseif ($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) {
  460. $class .= 'widget-last ';
  461. }
  462. $params[0]['before_widget'] = preg_replace('/class=\"/', "$class", $params[0]['before_widget'], 1);
  463. return $params;
  464. }
  465. add_filter('dynamic_sidebar_params', 'roots_widget_first_last_classes');
  466. /**
  467. * Redirects search results from /?s=query to /search/query/, converts %20 to +
  468. *
  469. * @link http://txfx.net/wordpress-plugins/nice-search/
  470. */
  471. function roots_nice_search_redirect() {
  472. if (is_search() && strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === false && strpos($_SERVER['REQUEST_URI'], '/search/') === false) {
  473. if ( get_option('permalink_structure') != '' ) {
  474. wp_redirect(home_url('/search/' . str_replace(array(' ', '%20'), array('+', '+'), urlencode(get_query_var('s')))), 301);
  475. exit();
  476. }
  477. }
  478. }
  479. add_action('template_redirect', 'roots_nice_search_redirect');
  480. /**
  481. * Fix for get_search_query() returning +'s between search terms
  482. */
  483. function roots_search_query($escaped = true) {
  484. $query = apply_filters('roots_search_query', get_query_var('s'));
  485. if ($escaped) {
  486. $query = esc_attr($query);
  487. }
  488. return urldecode($query);
  489. }
  490. add_filter('get_search_query', 'roots_search_query');
  491. /**
  492. * Fix for empty search queries redirecting to home page
  493. *
  494. * @link http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage#post-1772565
  495. * @link http://core.trac.wordpress.org/ticket/11330
  496. */
  497. function roots_request_filter($query_vars) {
  498. if (isset($_GET['s']) && empty($_GET['s'])) {
  499. $query_vars['s'] = ' ';
  500. }
  501. return $query_vars;
  502. }
  503. add_filter('request', 'roots_request_filter');
  504. /**
  505. * Tell WordPress to use searchform.php from the templates/ directory
  506. */
  507. function roots_get_search_form($form) {
  508. ob_start();
  509. locate_template('/templates/searchform.php', true, false);
  510. $form = ob_get_clean();
  511. return $form;
  512. }
  513. add_filter('get_search_form', 'roots_get_search_form');