/wp-content/themes/peppers/lib/utils.php

https://bitbucket.org/zumwalt/eat-with-us · PHP · 132 lines · 94 code · 19 blank · 19 comment · 13 complexity · 9dc15735fcfeb8ecc20e8bb41820eece MD5 · raw file

  1. <?php
  2. /**
  3. * Theme wrapper
  4. *
  5. * @link http://scribu.net/wordpress/theme-wrappers.html
  6. */
  7. function roots_template_path() {
  8. return Roots_Wrapping::$main_template;
  9. }
  10. function roots_sidebar_path() {
  11. return Roots_Wrapping::sidebar();
  12. }
  13. class Roots_Wrapping {
  14. // Stores the full path to the main template file
  15. static $main_template;
  16. // Stores the base name of the template file; e.g. 'page' for 'page.php' etc.
  17. static $base;
  18. static function wrap($template) {
  19. self::$main_template = $template;
  20. self::$base = substr(basename(self::$main_template), 0, -4);
  21. if (self::$base === 'index') {
  22. self::$base = false;
  23. }
  24. $templates = array('base.php');
  25. if (self::$base) {
  26. array_unshift($templates, sprintf('base-%s.php', self::$base));
  27. }
  28. return locate_template($templates);
  29. }
  30. static function sidebar() {
  31. $templates = array('templates/sidebar.php');
  32. if (self::$base) {
  33. array_unshift($templates, sprintf('templates/sidebar-%s.php', self::$base));
  34. }
  35. return locate_template($templates);
  36. }
  37. }
  38. add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99);
  39. /**
  40. * Page titles
  41. */
  42. function roots_title() {
  43. if (is_home()) {
  44. if (get_option('page_for_posts', true)) {
  45. echo get_the_title(get_option('page_for_posts', true));
  46. } else {
  47. _e('Latest Posts', 'roots');
  48. }
  49. } elseif (is_archive()) {
  50. $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
  51. if ($term) {
  52. echo $term->name;
  53. } elseif (is_post_type_archive()) {
  54. echo get_queried_object()->labels->name;
  55. } elseif (is_day()) {
  56. printf(__('Daily Archives: %s', 'roots'), get_the_date());
  57. } elseif (is_month()) {
  58. printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
  59. } elseif (is_year()) {
  60. printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
  61. } elseif (is_author()) {
  62. printf(__('Author Archives: %s', 'roots'), get_the_author());
  63. } else {
  64. single_cat_title();
  65. }
  66. } elseif (is_search()) {
  67. printf(__('Search Results for %s', 'roots'), get_search_query());
  68. } elseif (is_404()) {
  69. _e('Not Found', 'roots');
  70. } else {
  71. the_title();
  72. }
  73. }
  74. /**
  75. * Show an admin notice if .htaccess isn't writable
  76. */
  77. function roots_htaccess_writable() {
  78. if (!is_writable(get_home_path() . '.htaccess')) {
  79. if (current_user_can('administrator')) {
  80. 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>';"));
  81. }
  82. }
  83. }
  84. add_action('admin_init', 'roots_htaccess_writable');
  85. /**
  86. * Return WordPress subdirectory if applicable
  87. */
  88. function wp_base_dir() {
  89. preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);
  90. if (count($matches) === 3) {
  91. return end($matches);
  92. } else {
  93. return '';
  94. }
  95. }
  96. /**
  97. * Opposite of built in WP functions for trailing slashes
  98. */
  99. function leadingslashit($string) {
  100. return '/' . unleadingslashit($string);
  101. }
  102. function unleadingslashit($string) {
  103. return ltrim($string, '/');
  104. }
  105. function add_filters($tags, $function) {
  106. foreach($tags as $tag) {
  107. add_filter($tag, $function);
  108. }
  109. }
  110. function is_element_empty($element) {
  111. $element = trim($element);
  112. return empty($element) ? false : true;
  113. }