예제 #1
0
/**
 * Contents shortcode.
 * embed a github file
 * Needed username & repository & path
 * @param $atts
 * @return string
 */
function ghcontents_shortcode($atts)
{
    $a = shortcode_atts(array('username' => get_option('wpgithub_defaultuser', 'seinoxygen'), 'repository' => get_option('wpgithub_defaultrepo', 'wp-github'), 'filepath' => '', 'language' => 'markup'), $atts);
    //$contents = json_encode($contents);
    $file_name = str_replace('/', '.', $a['filepath']);
    // Init the cache system.
    $cache = new WpGithubCache();
    // Set custom timeout in seconds.
    $cache->timeout = get_option('wpgithub_cache_time', 600);
    $contents = $cache->get($a['username'] . '.' . $a['repository'] . '.' . $file_name . '.contents.json');
    if ($contents == NULL) {
        $github = new Github($a['username'], $a['repository'], $a['filepath']);
        $contents = $github->get_contents();
        $cache->set($a['username'] . '.' . $a['repository'] . '.' . $file_name . '.contents.json', $contents);
    }
    $html = '<pre class="wp-github line-numbers language-' . $a['language'] . '"><code class="language-' . $a['language'] . '">';
    if (!isset($contents->message)) {
        $html .= base64_decode($contents->content);
    } else {
        $html .= __('Error : ' . $contents->message, 'wp-github') . '<br />';
        $html .= 'User : '******'username'] . '<br />Repo : ' . $a['repository'] . '<br />file : ' . $file_name;
    }
    //echo $a['username'] . '.' . $a['repository'] . '.'.$file_name.'.contents.json';
    $html .= '</code></pre>';
    return $html;
}