/** * Hijack `template_include` so that we can ensure a $theme variable is defined. * * @param string $template; * * @return static */ static function _template_include_999($template) { if (!$template && !WPLib::is_production()) { $message = __('<p>No template file found. You may have deleted the current theme or renamed the theme directory?</p>' . '<p>If you are a site admin <a href="%s">click here</a> to verify and possibly correct.</p>', 'wplib'); /* * This use of wp-admin theme URL is only presented to help the user * when the site is misconfigured. However, ironically, some code * sniffers constantly flag it so it is easier to obscure it from * the sniffer than to have to constantly see it flagged. */ $admin_path = 'wp-admin'; echo sprintf($message, esc_url(site_url("/{$admin_path}/themes.php"))); } else { global $theme; /* * Make $theme available inside the template. */ $theme = WPLib::theme(); if (WPLib::use_template_global_vars()) { /* * For compatibility with WordPress templates we need to * extract all the global variables into the current scope just * like WordPress does when it calls a template. Ironically * 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($GLOBALS, EXTR_SKIP); } include $template; } return false; }
/** * @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; }