/**
 * Gets content from GitHub Gist
 * 
 * @param int $id GitHub Gist ID
 * @param int $ttl How long to cache (in seconds)
 * @param string $bump Bump value to force cache expirey.
 * @param string $file Name of file
 */
function embed_github_gist($id, $ttl = null, $bump = null, $file = null)
{
    $gist = '';
    if (!class_exists('WP_Http')) {
        require_once ABSPATH . WPINC . '/class-http.php';
    }
    $key = embed_github_gist_build_cache_key($id, $bump);
    if (embed_github_gist_bypass_cache() or false === ($gist = get_transient($key))) {
        $http = new WP_Http();
        if (embed_github_gist_prefer_inline_html() and function_exists('json_decode')) {
            $result = $http->request('https://gist.github.com/' . $id . '.json');
            $json = json_decode($result['body']);
            $gist = $json->div;
        } else {
            if (!$file) {
                $file = 'file';
            }
            $result = $http->request('https://gist.github.com/raw/' . $id . '/' . $file);
            $gist = '<script src="https://gist.github.com/' . $id . '.js?file=' . $file . '%5B345%5D" type="text/javascript"></script>';
            $gist .= '<noscript><div class="embed-github-gist-source"><code><pre>';
            $gist .= htmlentities($result['body']);
            $gist .= '</pre></code></div></noscript>';
        }
        unset($result, $http);
        if (!embed_github_gist_bypass_cache()) {
            if (!$ttl) {
                $ttl = EMBED_GISTHUB_DEFAULT_TTL;
            }
            set_transient($key, $gist, $ttl);
        }
    }
    return $gist;
}
/**
 * Gets content from GitHub Gist
 * 
 * @param int $id GitHub Gist ID
 * @param int $ttl How long to cache (in seconds)
 * @param string $bump Bump value to force cache expirey.
 * @param string $file Name of file
 */
function embed_github_gist($id, $ttl = null, $bump = null, $file = null)
{
    $gist = '';
    if (!class_exists('WP_Http')) {
        require_once ABSPATH . WPINC . '/class-http.php';
    }
    $key = embed_github_gist_build_cache_key($id, $bump, $file);
    if (embed_github_gist_bypass_cache() || false === ($gist = get_transient($key))) {
        $http = new WP_Http();
        $args = array('sslverify' => false);
        if (defined('EMBED_GISTHUB_USERNAME') && defined('EMBED_GISTHUB_PASSWORD')) {
            $args['headers'] = array('Authorization' => 'Basic ' . base64_encode(EMBED_GISTHUB_USERNAME . ':' . EMBED_GISTHUB_PASSWORD));
        }
        $result = $http->request('https://api.github.com/gists/' . $id, $args);
        if (is_wp_error($result)) {
            echo $result->get_error_message();
        }
        $json = json_decode($result['body'], true);
        if (200 != $result['response']['code']) {
            $html = '<div>Could not embed GitHub Gist ' . $id;
            if (isset($json['message'])) {
                $html .= ': ' . $json['message'];
            }
            $html .= '</div>';
            return $html;
        }
        $files = array();
        foreach ($json['files'] as $name => $fileInfo) {
            if ($file === null) {
                $files[$name] = $fileInfo;
            } else {
                if ($file == $name) {
                    $files[$name] = $fileInfo;
                    break;
                }
            }
        }
        $gist = '';
        if (count($files)) {
            if (embed_github_gist_prefer_inline_html()) {
                foreach ($files as $name => $fileInfo) {
                    $language = strtolower($fileInfo['language']);
                    $gist .= '<pre><code class="language-' . $language . ' ' . $language . '">';
                    $gist .= htmlentities($fileInfo['content']);
                    $gist .= '</code></pre>';
                }
            } else {
                $urlExtra = $file ? '?file=' . $file : '';
                $gist .= '<script src="https://gist.github.com/' . $id . '.js' . $urlExtra . '"></script>';
                $gist .= '<noscript>';
                foreach ($files as $name => $fileInfo) {
                    $language = strtolower($fileInfo['language']);
                    $gist .= '<pre><code class="language-' . $language . ' ' . $language . '">';
                    $gist .= htmlentities($fileInfo['content']);
                    $gist .= '</code></pre>';
                }
                $gist .= '</noscript>';
            }
        }
        unset($result, $http);
        if (!embed_github_gist_bypass_cache()) {
            if (!$ttl) {
                $ttl = EMBED_GISTHUB_DEFAULT_TTL;
            }
            set_transient($key, $gist, $ttl);
        }
    }
    return $gist;
}