PageRenderTime 94ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/alniko009/magic
PHP | 96 lines | 64 code | 13 blank | 19 comment | 19 complexity | 9e2316e214d52f1b94d091850e3333f3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * URL rewriting and addition of HTML5 Boilerplate's .htaccess
  4. *
  5. * Rewrites currently do not happen for child themes (or network installs)
  6. * @todo https://github.com/retlehs/roots/issues/461
  7. *
  8. * Rewrite:
  9. * /wp-content/themes/themename/css/ to /css/
  10. * /wp-content/themes/themename/js/ to /js/
  11. * /wp-content/themes/themename/img/ to /img/
  12. * /wp-content/plugins/ to /plugins/
  13. *
  14. * If you aren't using Apache, alternate configuration settings can be found in the wiki.
  15. *
  16. * @link https://github.com/retlehs/roots/wiki/Nginx
  17. * @link https://github.com/retlehs/roots/wiki/Lighttpd
  18. */
  19. if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') || stristr($_SERVER['SERVER_SOFTWARE'], 'litespeed') !== false) {
  20. // Show an admin notice if .htaccess isn't writable
  21. function roots_htaccess_writable() {
  22. if (!is_writable(get_home_path() . '.htaccess')) {
  23. if (current_user_can('administrator')) {
  24. add_action('admin_notices', create_function('', "echo '<div class=\"error\"><p>" . sprintf(__('Please make sure your <a href="%s">.htaccess</a> file is writable ', 'roots'), admin_url('options-permalink.php')) . "</p></div>';"));
  25. }
  26. }
  27. }
  28. add_action('admin_init', 'roots_htaccess_writable');
  29. function roots_add_rewrites($content) {
  30. global $wp_rewrite;
  31. $roots_new_non_wp_rules = array(
  32. 'assets/css/(.*)' => THEME_PATH . '/assets/css/$1',
  33. 'assets/js/(.*)' => THEME_PATH . '/assets/js/$1',
  34. 'assets/img/(.*)' => THEME_PATH . '/assets/img/$1',
  35. 'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1'
  36. );
  37. $wp_rewrite->non_wp_rules = array_merge($wp_rewrite->non_wp_rules, $roots_new_non_wp_rules);
  38. return $content;
  39. }
  40. function roots_clean_urls($content) {
  41. if (strpos($content, FULL_RELATIVE_PLUGIN_PATH) === 0) {
  42. return str_replace(FULL_RELATIVE_PLUGIN_PATH, WP_BASE . '/plugins', $content);
  43. } else {
  44. return str_replace('/' . THEME_PATH, '', $content);
  45. }
  46. }
  47. if (!is_multisite() && !is_child_theme() && get_option('permalink_structure')) {
  48. if (current_theme_supports('rewrite-urls')) {
  49. add_action('generate_rewrite_rules', 'roots_add_rewrites');
  50. }
  51. if (current_theme_supports('h5bp-htaccess')) {
  52. add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
  53. }
  54. if (!is_admin() && current_theme_supports('rewrite-urls')) {
  55. $tags = array(
  56. 'plugins_url',
  57. 'bloginfo',
  58. 'stylesheet_directory_uri',
  59. 'template_directory_uri',
  60. 'script_loader_src',
  61. 'style_loader_src'
  62. );
  63. add_filters($tags, 'roots_clean_urls');
  64. }
  65. }
  66. // Add the contents of h5bp-htaccess into the .htaccess file
  67. function roots_add_h5bp_htaccess($content) {
  68. global $wp_rewrite;
  69. $home_path = function_exists('get_home_path') ? get_home_path() : ABSPATH;
  70. $htaccess_file = $home_path . '.htaccess';
  71. $mod_rewrite_enabled = function_exists('got_mod_rewrite') ? got_mod_rewrite() : false;
  72. if ((!file_exists($htaccess_file) && is_writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is_writable($htaccess_file)) {
  73. if ($mod_rewrite_enabled) {
  74. $h5bp_rules = extract_from_markers($htaccess_file, 'HTML5 Boilerplate');
  75. if ($h5bp_rules === array()) {
  76. $filename = dirname(__FILE__) . '/h5bp-htaccess';
  77. return insert_with_markers($htaccess_file, 'HTML5 Boilerplate', extract_from_markers($filename, 'HTML5 Boilerplate'));
  78. }
  79. }
  80. }
  81. return $content;
  82. }
  83. }