135 lines
3.4 KiB
PHP
135 lines
3.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Theme Wrapper
|
|
*
|
|
* @link http://scribu.net/wordpress/theme-wrappers.html
|
|
*/
|
|
function roots_template_path() {
|
|
return Roots_Wrapping::$main_template;
|
|
}
|
|
|
|
function roots_sidebar_path() {
|
|
return Roots_Wrapping::sidebar();
|
|
}
|
|
|
|
class Roots_Wrapping {
|
|
// Stores the full path to the main template file
|
|
static $main_template;
|
|
|
|
// Stores the base name of the template file; e.g. 'page' for 'page.php' etc.
|
|
static $base;
|
|
|
|
static function wrap($template) {
|
|
self::$main_template = $template;
|
|
|
|
self::$base = substr(basename(self::$main_template), 0, -4);
|
|
|
|
if (self::$base === 'index') {
|
|
self::$base = false;
|
|
}
|
|
|
|
$templates = array('base.php');
|
|
|
|
if (self::$base) {
|
|
array_unshift($templates, sprintf('base-%s.php', self::$base));
|
|
}
|
|
|
|
return locate_template($templates);
|
|
}
|
|
|
|
static function sidebar() {
|
|
$templates = array('templates/sidebar.php');
|
|
|
|
if (self::$base) {
|
|
array_unshift($templates, sprintf('templates/sidebar-%s.php', self::$base));
|
|
}
|
|
|
|
return locate_template($templates);
|
|
}
|
|
}
|
|
|
|
add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99);
|
|
|
|
|
|
/**
|
|
* Page titles
|
|
*/
|
|
function roots_title() {
|
|
if (is_home()) {
|
|
if (get_option('page_for_posts', true)) {
|
|
echo get_the_title(get_option('page_for_posts', true));
|
|
} else {
|
|
_e('Latest Posts', 'roots');
|
|
}
|
|
} elseif (is_archive()) {
|
|
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
|
|
if ($term) {
|
|
echo $term->name;
|
|
} elseif (is_post_type_archive()) {
|
|
echo get_queried_object()->labels->name;
|
|
} elseif (is_day()) {
|
|
printf(__('Daily Archives: %s', 'roots'), get_the_date());
|
|
} elseif (is_month()) {
|
|
printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
|
|
} elseif (is_year()) {
|
|
printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
|
|
} elseif (is_author()) {
|
|
global $post;
|
|
$author_id = $post->post_author;
|
|
printf(__('Author Archives: %s', 'roots'), get_the_author_meta('display_name', $author_id));
|
|
} else {
|
|
single_cat_title();
|
|
}
|
|
} elseif (is_search()) {
|
|
printf(__('Search Results for %s', 'roots'), get_search_query());
|
|
} elseif (is_404()) {
|
|
_e('Not Found', 'roots');
|
|
} else {
|
|
the_title();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show an admin notice if .htaccess isn't writable
|
|
*/
|
|
function roots_htaccess_writable() {
|
|
if (!is_writable(get_home_path() . '.htaccess')) {
|
|
if (current_user_can('administrator')) {
|
|
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>';"));
|
|
}
|
|
}
|
|
}
|
|
|
|
add_action('admin_init', 'roots_htaccess_writable');
|
|
|
|
// returns WordPress subdirectory if applicable
|
|
function wp_base_dir() {
|
|
preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches);
|
|
if (count($matches) === 3) {
|
|
return end($matches);
|
|
} else {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// opposite of built in WP functions for trailing slashes
|
|
function leadingslashit($string) {
|
|
return '/' . unleadingslashit($string);
|
|
}
|
|
|
|
function unleadingslashit($string) {
|
|
return ltrim($string, '/');
|
|
}
|
|
|
|
function add_filters($tags, $function) {
|
|
foreach($tags as $tag) {
|
|
add_filter($tag, $function);
|
|
}
|
|
}
|
|
|
|
function is_element_empty($element) {
|
|
$element = trim($element);
|
|
return empty($element) ? false : true;
|
|
}
|