static function the_template($template_slug, $_template_vars = array(), $item = null)
{
/*
* Calculate the md5 value for caching this template filename
*/
if (!self::is_development()) {
$_md5 = md5(serialize(array($template_slug, $_template_vars, get_class($item))));
} else {
$_md5 = $template_slug . '[' . get_class($item) . '][' . serialize($_template_vars) . ']';
}
if (!($template = self::cache_get($_cache_key = "template_file[{$_md5}]"))) {
$template = new stdClass();
$template->filenames_tried = array();
$template->found = false;
/**
* Ensure $_template_vars is an array
*/
$template->vars = is_string($_template_vars) ? wp_parse_args($_template_vars) : $_template_vars;
if (!is_array($template->vars)) {
$template->vars = array();
}
/*
* Ensure filename does not have a leading slash ('/') but does have a trailing '.php'
*/
$_filename = preg_replace('#(.+)(\\.php)?$#', '$1.php', ltrim($template_slug, '/'));
foreach (array('theme', 'module', 'app') as $template_type) {
switch ($template_type) {
case 'theme':
$template->dir = get_stylesheet_directory();
$template->subdir = static::templates_subdir();
break;
case 'module':
$_app_class = !empty($template->vars['@app']) ? $template->vars['@app'] : self::app_class();
$_module_class = !empty($template->vars['@module']) ? self::get_module_class($template->vars['@module'], $_app_class) : get_class($item);
$template->dir = self::get_module_dir($_module_class);
$template->subdir = 'templates';
break;
case 'app':
/**
* @note Not implemented yet.
*/
$_app_class = !empty($template->vars['@app']) ? $template->vars['@app'] : self::app_class();
$template->dir = call_user_func(array($_app_class, 'root_dir'));
$template->subdir = 'templates';
break;
}
$template->filename = "{$template->dir}/{$template->subdir}/{$_filename}";
if (!WPLib::is_found($template->filename)) {
$template->filenames_tried[$template_type] = $template->filename;
} else {
$template->found = true;
$template->var_name = self::get_constant('VAR_NAME', get_class($item));
$template->comments = "<!--[TEMPLATE FILE: {$template->filename} -->";
break;
}
}
self::cache_set($_cache_key, $template);
}
$template->add_comments = !self::doing_ajax() && !self::is_production();
if (!$template->found) {
if ($template->add_comments) {
/**
* This can be used by theme developers with view source to see which templates failed.
*
* @note FOR CODE REVIEWERS:
*
* This is ONLY output of constant 'WPLIB_RUNMODE' is defined in wp-config.php.
* In other words, this will NEVER run on your servers (unless you set WPLIB_RUNMODE.)
*/
echo "\n<!--[FAILED TEMPLATE FILE: {$template_slug}. Tried:\n";
foreach ($template->filenames_tried as $template_type => $template_filename) {
echo "\n\t{$template_type}: {$template_filename}";
}
echo "\n]-->";
}
} else {
if ($template->add_comments) {
echo $template->comments;
}
/*
* This use of extract() is to support templates in the same way
* that WordPress supports templates with variables that are accessible
* in the namespace. However some code sniffers constantly flag extract()
* so it is easier to hide it than to have to constantly see it flagged.
*
* OTOH if you are using WPLib and you think we should do a direct call
* to extract() here please add an issue so we can discuss the pros and
* cons at https://github.com/wplib/wplib/issues
*/
$function = 'extract';
$function($template->vars, EXTR_PREFIX_SAME, '_');
if ($template->var_name) {
/*
* Assign the $item's preferred variable name in addition to '$item', i.e. '$brand'
* This is a very controlled use of extract() i.e. we know what we are doing here.
*
* See a few lines above to explain ${'extract'}
*/
$function(array($template->var_name => $item));
}
unset($_template_vars, $_filename, $_cache_key, $_md5, $_app_class, $_module_class);
ob_start();
self::$_file_loading = $template->filename;
require $template->filename;
self::$_file_loading = false;
if (!$template->add_comments) {
echo ob_get_clean();
} else {
/**
* This can be used by theme developers with view source to see which templates failed.
*
* @note FOR CODE REVIEWERS:
*
* This is ONLY output if constant 'WPLIB_RUNMODE' is defined in wp-config.php.
* In other words, this will NEVER run on your servers (unless you set WPLIB_RUNMODE.)
*/
echo $template->comments;
echo ob_get_clean();
echo "\n<!--[END TEMPLATE FILE: {$template->filename} -->\n";
}
}
}