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

/lib/utils.php

https://github.com/rub1/lbrl
PHP | 111 lines | 80 code | 15 blank | 16 comment | 11 complexity | 0d763bb8fa0977f03c92efa075300336 MD5 | raw file
  1. <?php
  2. // Add post thumbnails (http://codex.wordpress.org/Post_Thumbnails)
  3. add_theme_support('post-thumbnails');
  4. // set_post_thumbnail_size(150, 150, false);
  5. // add_image_size('category-thumb', 300, 9999); // 300px wide (and unlimited height)
  6. // Add post formats (http://codex.wordpress.org/Post_Formats)
  7. // add_theme_support('post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat'));
  8. // Backwards compatibility for older than PHP 5.3.0
  9. if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); }
  10. // Define helper constants
  11. $get_theme_name = explode('/themes/', get_template_directory());
  12. define('WP_BASE', wp_base_dir());
  13. define('THEME_NAME', next($get_theme_name));
  14. define('RELATIVE_PLUGIN_PATH', str_replace(site_url() . '/', '', plugins_url()));
  15. define('FULL_RELATIVE_PLUGIN_PATH', WP_BASE . '/' . RELATIVE_PLUGIN_PATH);
  16. define('RELATIVE_CONTENT_PATH', str_replace(site_url() . '/', '', content_url()));
  17. define('THEME_PATH', RELATIVE_CONTENT_PATH . '/themes/' . THEME_NAME);
  18. // show home in menus
  19. function home_page_menu_args( $args ) {
  20. $args['show_home'] = true;
  21. return $args;
  22. }
  23. add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
  24. /**
  25. * Page titles
  26. */
  27. function title() {
  28. if (is_home()) {
  29. if (get_option('page_for_posts', true)) {
  30. echo get_the_title(get_option('page_for_posts', true));
  31. } else {
  32. _e('Latest Posts', 'roots');
  33. }
  34. } elseif (is_archive()) {
  35. $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
  36. if ($term) {
  37. echo $term->name;
  38. } elseif (is_post_type_archive()) {
  39. echo get_queried_object()->labels->name;
  40. } elseif (is_day()) {
  41. printf(__('Daily Archives: %s', 'roots'), get_the_date());
  42. } elseif (is_month()) {
  43. printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
  44. } elseif (is_year()) {
  45. printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
  46. } elseif (is_author()) {
  47. global $post;
  48. $author_id = $post->post_author;
  49. printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
  50. } else {
  51. single_cat_title();
  52. }
  53. } elseif (is_search()) {
  54. printf(__('Search Results for %s', 'roots'), get_search_query());
  55. } elseif (is_404()) {
  56. _e('File Not Found', 'roots');
  57. } else {
  58. the_title();
  59. }
  60. }
  61. /**
  62. * Show an admin notice if .htaccess isn't writable
  63. */
  64. function htaccess_writable() {
  65. if (!is_writable(get_home_path() . '.htaccess')) {
  66. if (current_user_can('administrator')) {
  67. 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>';"));
  68. }
  69. }
  70. }
  71. add_action('admin_init', 'htaccess_writable');
  72. // returns WordPress subdirectory if applicable
  73. function wp_base_dir() {
  74. preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);
  75. if (count($matches) === 3) {
  76. return end($matches);
  77. } else {
  78. return '';
  79. }
  80. }
  81. // opposite of built in WP functions for trailing slashes
  82. function leadingslashit($string) {
  83. return '/' . unleadingslashit($string);
  84. }
  85. function unleadingslashit($string) {
  86. return ltrim($string, '/');
  87. }
  88. function add_filters($tags, $function) {
  89. foreach($tags as $tag) {
  90. add_filter($tag, $function);
  91. }
  92. }
  93. function is_element_empty($element) {
  94. $element = trim($element);
  95. return empty($element) ? false : true;
  96. }