diff --git a/README.md b/README.md index 1f888d9..c320ce7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,14 @@ Examples: * `[gist id=546764 file=file.txt bump=1]` * `[gist]http://gist.github.com/546764[/gist]` +This plugin also supports using [SyntaxHighlighter Evolved](http://wordpress.org/plugins/syntaxhighlighter/) +for highlighting rather then the default gist mockup. **NOTE** for this to work, the following line must be added to the top +of the wp-settings.php file located in the root of your wordpress install: `define('EMBED_GISTHUB_SYNTAXHIGHLIGHTER', true);`. +Also, in the SyntaxHighlighter Evolved Settings (inside wp-admin), the "Load All Brushes" checkbox must be enabled. + +Example Usage: + * `[gist id=546764 highlight=she]` + Cache is implemented with the Transients API to minimize delay on loading content. Default TTL (time to live) is 86400 seconds or one day. diff --git a/embed-github-gist.php b/embed-github-gist.php index fc2dde0..f79d8b1 100644 --- a/embed-github-gist.php +++ b/embed-github-gist.php @@ -1,13 +1,13 @@ 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 = '
Could not embed GitHub Gist '.$id; - if (isset($json['message'])) { - $html .= ': '. $json['message']; - } - $html .= '
'; - - 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 .= '
';
-                    $gist .= htmlentities($fileInfo['content']);
-                    $gist .= '
'; - } - } else { - $urlExtra = $file ? '?file='.$file : ''; - $gist .= ''; - $gist .= ''; - } - } - - unset($result, $http); - - if ( ! embed_github_gist_bypass_cache() ) { - if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; - set_transient($key, $gist, $ttl); - } - } - - return $gist; + $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 = '
Could not embed GitHub Gist '.$id; + if (isset($json['message'])) { + $html .= ': '. $json['message']; + } + $html .= '
'; + + 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_use_syntaxhighlighter() ) { + + // Get available languages (brushes) available in SyntaxHighlighter + global $SyntaxHighlighter; + $brushes = $SyntaxHighlighter->brushes; + + $http = new WP_Http; + foreach($files as $name => $fileInfo) { + + $language = strtolower($fileInfo['language']); + + // Verify the language of the Gist is supported by SyntaxHighlighter. + // If it doesn't, treat the Gist as Plain Text. + if(!array_key_exists($language, $brushes)) { + $language = "text"; + } + + + $raw_content = $http->request($fileInfo['raw_url']); + + if(is_wp_error($raw_content)) { + echo $result->get_error_message(); + } else { + $gist .= "
";
+						$gist .= $raw_content['body'];
+						$gist .= "
"; + } + } + + } else if ( embed_github_gist_prefer_inline_html() ) { + foreach ($files as $name => $fileInfo) { + $language = strtolower($fileInfo['language']); + $gist .= '
';
+					$gist .= htmlentities($fileInfo['content']);
+					$gist .= '
'; + } + } else { + $urlExtra = $file ? '?file='.$file : ''; + $gist .= ''; + $gist .= ''; + } + } + + unset($result, $http); + + if ( ! embed_github_gist_bypass_cache() ) { + if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; + set_transient($key, $gist, $ttl); + } + } + + return $gist; } /** @@ -147,28 +192,28 @@ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) { * @param mixed $content */ function handle_embed_github_gist_shortcode($atts, $content = null) { - extract(shortcode_atts(array( - 'id' => null, - 'file' => null, - 'ttl' => null, - 'bump' => null, - ), $atts)); - - if ( ! $id ) { - if ( $content ) { - if ( preg_match('/\s*https?.+\/(\d+)/', $content, $matches) ) { - $id = $matches[1]; - } - } - } - return $id ? embed_github_gist($id, $ttl, $bump, $file) : $content; + extract(shortcode_atts(array( + 'id' => null, + 'file' => null, + 'ttl' => null, + 'bump' => null, + ), $atts)); + + if ( ! $id ) { + if ( $content ) { + if ( preg_match('/\s*https?.+\/(\d+)/', $content, $matches) ) { + $id = $matches[1]; + } + } + } + return $id ? embed_github_gist($id, $ttl, $bump, $file) : $content; } /** * Init the plugin. */ function handle_embed_github_gist_init() { - add_shortcode('gist', 'handle_embed_github_gist_shortcode'); + add_shortcode('gist', 'handle_embed_github_gist_shortcode'); } /** @@ -179,7 +224,7 @@ function handle_embed_github_gist_init() { function embed_github_gist_post_candidate() { global $posts; - + foreach ($posts as $p) { if (preg_match('/\[gist[^\]]*\]/siU', $p->post_content)) { return true;