Exemplo n.º 1
0
function perch_blog_author_for_post($id_or_slug, $opts = array(), $return = false)
{
    $id_or_slug = rtrim($id_or_slug, '/');
    $default_opts = array('template' => 'author.html', 'skip-template' => false, 'split-items' => false, 'cache' => true);
    if (is_array($opts)) {
        $opts = array_merge($default_opts, $opts);
    } else {
        $opts = $default_opts;
    }
    if ($opts['skip-template'] || $opts['split-items']) {
        $return = true;
    }
    $cache = false;
    if ($opts['cache']) {
        $cache_key = 'perch_blog_author_for_post' . md5($id_or_slug . serialize($opts));
        $cache = PerchBlog_Cache::get_static($cache_key, 10);
    }
    if ($cache) {
        if ($return) {
            return $cache;
        }
        echo $cache;
        return '';
    }
    $API = new PerchAPI(1.0, 'perch_blog');
    $BlogPosts = new PerchBlog_Posts($API);
    if (is_numeric($id_or_slug)) {
        $Post = $BlogPosts->find($id_or_slug);
    } else {
        $Post = $BlogPosts->find_by_slug($id_or_slug);
    }
    if (is_object($Post)) {
        $Authors = new PerchBlog_Authors();
        $Author = $Authors->find($Post->authorID());
        if (is_object($Author)) {
            if ($opts['skip-template']) {
                return $Author->to_array();
            }
            $Template = $API->get('Template');
            $Template->set('blog/' . $opts['template'], 'blog');
            $r = $Template->render($Author);
            if ($r != '') {
                PerchBlog_Cache::save_static($cache_key, $r);
            }
            if ($return) {
                return $r;
            }
            echo $r;
        }
    }
    return false;
}