/**
  * Gets the source code of a template, given its name.
  *
  * @param  string $name string The name of the template to load
  *
  * @return string The template source code
  */
 function getSource($name)
 {
     if ($template = Template::find_one_by_title_with_fallback($name)) {
         return $template->content;
     } else {
         return false;
     }
 }
/**
 * Provides shortcode to display episode template.
 *
 * Usage:
 * 	
 * 	[podlove-template template="Template Title"]
 *
 * 	Parameters:
 * 		template: (required) Title of template to render.
 * 		autop:    (optional) Wraps blocks of text in p tags. 'yes' or 'no'. Default: 'yes'
 * 	
 * @param  array $attributes
 * @return string
 */
function template_shortcode($attributes)
{
    $defaults = array('title' => '', 'id' => '', 'template' => '', 'autop' => false);
    $attributes = array_merge($defaults, $attributes);
    if ($attributes['title'] !== '') {
        _deprecated_argument(__FUNCTION__, '1.3.14-alpha', 'The "title" attribute for [podlove-template] shortcode is deprecated. Use "id" instead.');
    }
    if ($attributes['id'] !== '') {
        _deprecated_argument(__FUNCTION__, '2.1.0', 'The "id" attribute for [podlove-template] shortcode is deprecated. Use "template" instead.');
    }
    // backward compatibility
    if ($attributes['template']) {
        $template_id = $attributes['template'];
    } elseif ($attributes['id']) {
        $template_id = $attributes['id'];
    } else {
        $template_id = $attributes['title'];
    }
    $permalink = get_permalink();
    /**
     * Cache key must be unique for *every permutation* of the content.
     * Meaning: If there are context based conditionals, the key must reflect them.
     */
    $tag_permutation = implode('', array_map(function ($tag) {
        return $tag() ? "1" : "0";
    }, \Podlove\Template\TwigFilter::$template_tags));
    /**
     * Cache key must change for any custom parameters.
     */
    $attr_permutation = implode('', array_map(function ($a) {
        return (string) $a;
    }, array_values($attributes)));
    $cache_key = $template_id . $permalink . $tag_permutation . $attr_permutation;
    $cache_key = apply_filters('podlove_template_shortcode_cache_key', $cache_key, $template_id);
    $cache = \Podlove\Cache\TemplateCache::get_instance();
    return $cache->cache_for($cache_key, function () use($template_id, $attributes) {
        if (!($template = Model\Template::find_one_by_title_with_fallback($template_id))) {
            return sprintf(__('Podlove Error: Whoops, there is no template with id "%s"', 'podlove'), $template_id);
        }
        $html = apply_filters('podlove_template_raw', $template->title, $attributes);
        // apply autop and shortcodes
        if (in_array($attributes['autop'], array('yes', 1, 'true'))) {
            $html = wpautop($html);
        }
        return do_shortcode($html);
    });
}
function podlove_autoinsert_templates_into_content($content)
{
    if (get_post_type() !== 'podcast' || post_password_required()) {
        return $content;
    }
    $template_assignments = TemplateAssignment::get_instance();
    if ($template_assignments->top) {
        if ($template = Template::find_one_by_title_with_fallback($template_assignments->top)) {
            $shortcode = '[podlove-template template="' . $template->title . '"]';
            if (stripos($content, $shortcode) === false) {
                $content = $shortcode . $content;
            }
        }
    }
    if ($template_assignments->bottom) {
        if ($template = Template::find_one_by_title_with_fallback($template_assignments->bottom)) {
            $shortcode = '[podlove-template template="' . $template->title . '"]';
            if (stripos($content, $shortcode) === false) {
                $content = $content . $shortcode;
            }
        }
    }
    return $content;
}