-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasy-github-gist.php
50 lines (46 loc) · 1.79 KB
/
easy-github-gist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
Plugin Name: Easy GitHub Gist
Plugin URI: https://github.com/sivan/easy-github-gist
Description: Easy GitHub Gist Plugin allows you to embed GitHub Gists from https://gist.github.com/.
Usage: Just put the GitHub Gist url in the content.
Version: 0.2
Author: Sivan
Author URI: http://lightcss.com/
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
function get_content_from_url($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
function gist_raw($id, $file) {
$request = 'https://raw.github.com/gist/'.$id.'/'.$file;
return get_content_from_url($request);
}
function gist_raw_html($gist_raw) {
return "<div style=\"margin-bottom:1em;padding:0;\">\n\t<noscript>\t\t<code>\t\t\t<pre style=\"overflow:auto;margin:0;padding:0;border:1px solid #ddd;\">\n".htmlentities($gist_raw)."\n\t\t\t</pre>\n\t\t</code>\n\t</noscript>\n</div>";
}
function gist_shortcode($atts) {
$id = $atts['id'];
$file = $atts['file'];
$html = sprintf("<script src=\"https://gist.github.com/%s.js%s\"></script>\n", $id, $file ? '?file='.$file : '');
$style = 'default';
$css = sprintf("<link rel=\"stylesheet\" href=\"%s/styles/%s.css\" />\n", plugins_url(), $style);
$html = $html.$css;
$gist_raw = gist_raw($id, $file);
if ($gist_raw != null) {
$html = $html.gist_raw_html($gist_raw);
}
return $html;
}
add_shortcode('gist','gist_shortcode');
//autoreplace gist links to shortcodes
function gist_shortcode_filter($content) {
return preg_replace('/https:\/\/gist.github.com\/([\d]+)[\.js\?]*[\#]*file[=|-|_]+([\w\.]+)(?![^<]*<\/a>)/i', '[gist id="${1}" file="${2}"]', $content );
}
add_filter( 'the_content', 'gist_shortcode_filter', 9);