/**
* @param string $template
* @param array|string $_template_vars
* @param WPLib_Item_Base|object $item
*
* @note This is called via an instance as well as
* If this becomes deprecated we can prefix with an '_' and then
* use __call() and __callStatic() to allow it to be invoked.
* @see http://stackoverflow.com/a/7983863/102699
*/
static function the_template($template, $_template_vars = array(), $item = null)
{
$_filename = preg_replace('#(\\.php)$#', '', ltrim($template, '/')) . '.php';
$template = new stdClass();
$template->dir = get_stylesheet_directory();
/*
* If root path (i.e. "~/wp-content/...") add ABSPATH to the template file after removing ~
* If not root path, assume the template file is in /templates/ inside the theme directory
*/
$_templates_subdir = static::templates_subdir();
$template->filename = preg_match('#^~[\\/](.+)#', $_filename, $match) ? ABSPATH . $match[1] : "{$template->dir}/{$_templates_subdir}/{$_filename}";
if (!is_string($_template_vars) || false !== strpos($_template_vars, '=')) {
$_specialty = false;
if (is_string($_template_vars)) {
$_template_vars = wp_parse_args($_template_vars);
}
if (false === $_template_vars || is_null($_template_vars)) {
$_template_vars = array();
} else {
if (!is_array($_template_vars)) {
$message = __('Unexpected value for 2nd parameter passed to the_template(). Expected array, string, false or null but got %s.', 'wplib');
WPLib::trigger_error(sprintf($message, gettype($_template_vars)));
}
}
} else {
/**
* If a string is passed assume it is for a more specific template and behave like get_template_part().
*/
$_specialty = esc_attr($_template_vars);
$_template_vars = array();
$_specialty = preg_replace('#(\\.php)$#', "-{$_specialty}\$1", $template->filename);
}
if ($_specialty && is_file($_specialty)) {
/**
* We found the special template before the general one.
*/
$template->filename = $_specialty;
$_specialty = true;
}
if (true !== $_specialty && !is_file($template->filename)) {
/**
* No speciality template and no template at all.
*/
$template->filename = $template->dir = false;
if (!WPLib::is_production()) {
/**
* This is ONLY output if constant 'WPLIB_RUNMODE' is defined in wp-config.php.
*/
echo "\n<!--[FAILED Template File: {$template->filename} -->\n";
}
}
if (!$template->filename) {
$output = false;
} else {
if (!WPLib::doing_ajax() && !WPLib::is_production()) {
echo "\n<!--[Template File: {$template->filename} -->\n";
}
extract($_template_vars, EXTR_PREFIX_SAME, '_');
if ($item && ($_var_name = WPLib::get_constant('VAR_NAME', get_class($item)))) {
/*
* Assign the $item's preferred variable name in addition to '$item', i.e. '$brand'
* This is a very controlled use of extract() i.e. I know what I am doing here.
*/
extract(array($_var_name => $item));
}
$template->vars = $_template_vars;
unset($_template_vars, $_templates_subdir, $_filename, $_cache_key, $_var_name, $_specialty);
ob_start();
self::$_file_loading = $template->filename;
require $template->filename;
self::$_file_loading = false;
$output = ob_get_clean();
}
echo $output;
}