/**
 * This allows you to replace anything that hasn't been fetched by template_part()
 * yet, and you know the $slug to.
 * 
 * @see https://codex.wordpress.org/Function_Reference/get_template_part
 * @param string $slug        The slug name to replace for the generic template.
 * @param string $replacement The slug name of the replacement for the generic template.
 * @param string $name        The name of the specialized template.
 * @return mixed              TRUE if the part was successfully added, FALSE otherwise
 */
function template_use_part($slug, $replacement, $name = NULL)
{
    global $theme;
    $action = str_replace('\\', '/', "get_template_part_{$slug}");
    $templates = array();
    $name = (string) $name;
    if (!empty($name)) {
        $templates[] = "{$replacement}-{$name}.php";
    }
    $templates[] = "{$replacement}.php";
    $located = template_part__locate($templates);
    if (!empty($located)) {
        return $theme->use_part($action, $located, TRUE);
    }
    return FALSE;
}
/**
 * This allows you to replace anything that hasn't been fetched by template_part()
 * yet, and you know the $slug to.
 * 
 * @see https://codex.wordpress.org/Function_Reference/get_template_part
 * @param string|null $slug        The slug name to replace for the generic template.
 * @param string      $replacement The slug name of the replacement for the generic template.
 * @param string      $name        The name of the specialized template.
 * @return mixed                   TRUE if the part was successfully added, FALSE otherwise
 */
function template_use_part($slug, $replacement, $name = NULL)
{
    global $theme;
    $action = str_replace('\\', '/', 'get_template_part_' . $slug);
    $templates = array();
    $name = (string) $name;
    if (!empty($name)) {
        $templates[] = $replacement . '-' . $name . '.php';
    }
    $templates[] = $replacement . '.php';
    $located = template_part__locate($templates);
    if (!empty($located)) {
        return $theme->use_part($action, $located, TRUE);
    }
    return FALSE;
}
/**
 * A drop-in replacement for WordPress' get_template_part()
 * 
 * NOTE: This is largely copied from the WordPress 4.2.2 core
 * 
 * Improvements:
 *   - Function variables do not interfere with included file
 *   - Returns the the return value of the file
 *   - Better Debugging
 *   - Supports overrides via template_use_part()
 * 
 * @see https://codex.wordpress.org/Function_Reference/get_template_part
 * @param string      $slug The slug name for the generic template.
 * @param string|null $name The name of the specialized template.
 * @return mixed            The returned value of the file on success, NULL otherwise
 */
function template_part($slug, $name = NULL)
{
    global $debug;
    $action = str_replace('\\', '/', 'get_template_part_' . $slug);
    $debug->runtime_checkpoint('[Theme] Action: ' . $action);
    do_action($action, $slug, $name);
    $templates = array();
    $name = (string) $name;
    if ('' !== $name) {
        $templates[] = $slug . '-' . $name . '.php';
    }
    $templates[] = $slug . '.php';
    $located = template_part__locate($templates);
    if ($located) {
        return template_part__load($located, FALSE, $action);
    }
    $debug->runtime_checkpoint('[Theme] Failed Action: ' . $action);
    return NULL;
}
/**
 * A drop-in replacement for WordPress' get_header()
 * 
 * NOTE: This is largely copied from the WordPress 4.2.2 core
 * 
 * Improvements:
 *   - Function variables do not interfere with included file
 *   - Returns the the return value of the file
 *   - Better Debugging
 *   - Support overrides via template_use_part()
 *   - Also checks content directory
 * 
 * @see https://codex.wordpress.org/Function_Reference/get_header
 * @param string $slug The slug name for the generic template.
 * @param string $name The name of the specialized template.
 * @return mixed       The returned value of the file on success, NULL otherwise
 */
function theme_header($name = NULL)
{
    global $debug, $theme;
    $action = "get_header";
    $debug->runtime_checkpoint('[Theme] Action: ' . $action);
    do_action($action, $name);
    $locations = array();
    $name = (string) $name;
    if (!empty($name)) {
        $locations[] = "header-{$name}";
        $locations[] = $theme->content_sub_path . "/header-{$name}";
    }
    $locations[] = "header";
    $locations[] = $theme->content_sub_path . "/header";
    foreach ($locations as $loc) {
        if (template_part__locate($loc . '.php')) {
            return template_part($loc, $name);
        }
    }
    $debug->runtime_checkpoint('[Theme] Failed Action: ' . $action);
    return NULL;
}
/**
 * A drop-in replacement for WordPress' get_header(), get_footer(), & get_sidebar()
 * 
 * NOTE: This is largely copied from the WordPress 4.2.2 core
 * 
 * Improvements:
 *   - Function variables do not interfere with included file
 *   - Returns the the return value of the file
 *   - Better Debugging
 *   - Supports overrides via template_use_part()
 *   - Also checks content directory
 * 
 * @see https://codex.wordpress.org/Function_Reference/get_footer
 * @param string      $section The name of the section to load
 * @param string|null $name    The name of the specialized template.
 * @return mixed               The returned value of the file on success, NULL otherwise
 */
function theme__section($section, $name = NULL)
{
    global $debug, $theme;
    $action = 'get_' . $section;
    $debug->runtime_checkpoint('[Theme] Action: ' . $action);
    do_action($action, $name);
    $locations = array();
    $name = (string) $name;
    if (!empty($name)) {
        $locations[] = sprintf('%1$s-%2$s', $theme->content_sub_path, $section, $name);
        $locations[] = sprintf('%1$s/%2$s-%3$s', $theme->content_sub_path, $section, $name);
    }
    $locations[] = $section;
    $locations[] = $theme->content_sub_path . '/' . $section;
    foreach ($locations as $loc) {
        if (template_part__locate($loc . '.php')) {
            return template_part($loc, $name);
        }
    }
    $debug->runtime_checkpoint('[Theme] Failed Action: ' . $action);
    return NULL;
}