forked from miya0001/oembed-gist
/
oembed-gist.php
144 lines (122 loc) · 2.77 KB
/
oembed-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
Plugin Name: oEmbed Gist
Plugin URI: https://github.com/miya0001/oembed-gist
Description: Embed source from gist.github.
Author: Takayuki Miyauchi
Version: 1.8.1
Author URI: http://firegoby.jp/
*/
$oe_gist = new gist();
$oe_gist->register();
class gist {
private $shotcode_tag = 'gist';
private $noscript;
private $regex = '#(https://gist.github.com/([^\/]+\/)?([a-zA-Z0-9]+)(\/[a-zA-Z0-9]+)?)(\#file(\-|_)(.+))?$#i';
function register()
{
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
public function plugins_loaded()
{
add_action( 'wp_head', array( $this, 'wp_head' ) );
load_plugin_textdomain(
'oembed-gist',
false,
dirname( plugin_basename( __FILE__ ) ).'/languages'
);
wp_embed_register_handler(
'oe-gist',
$this->get_gist_regex(),
array( $this, 'handler' )
);
add_shortcode( $this->get_shortcode_tag(), array( $this, 'shortcode' ) );
add_filter(
'jetpack_shortcodes_to_include',
array( $this, 'jetpack_shortcodes_to_include' )
);
}
public function jetpack_shortcodes_to_include( $incs )
{
$includes = array();
foreach ( $incs as $inc ) {
if ( !preg_match( "/gist\.php\z/", $inc ) ) {
$includes[] = $inc;
}
}
return $includes;
}
public function wp_head()
{
?>
<style>
.gist table {
margin-bottom: 0 !important;
}
.gist .line-numbers
{
width: 4em !important;
}
.gist .line,
.gist .line-number
{
font-size: 12px !important;
height: 18px !important;
line-height: 18px !important;
}
.gist .line
{
white-space: pre !important;
width: auto !important;
word-wrap: normal !important;
}
.gist .line span
{
word-wrap: normal !important;
}
</style>
<?php
}
public function handler( $m, $attr, $url, $rattr )
{
if ( !isset( $m[7] ) || !$m[7] ) {
$m[7] = null;
}
return $this->shortcode( array(
'url' => $m[1],
'id' => $m[3],
'file' => $m[7],
) );
}
public function shortcode( $p )
{
if ( isset( $p['url'] ) && $p['url'] ) {
$url = $p['url'];
} elseif ( preg_match( "/^[a-zA-Z0-9]+$/", $p['id'] ) ) {
$url = 'https://gist.github.com/' . $p['id'];
}
$noscript = sprintf(
__( 'View the code on <a href="%s">Gist</a>.', 'oembed-gist' ),
esc_url( $url )
);
$url = $url . '.js';
if ( isset( $p['file'] ) && $p['file'] ) { //RRD: Fixed line 79 error by adding isset()
$file = preg_replace( '/[\-\.]([a-z]+)$/', '.\1', $p['file'] );
$url = $url . '?file=' . $file;
}
return sprintf(
'<div class="oembed-gist"><script src="%s"></script><noscript>%s</noscript></div>',
$url,
$noscript
);
}
public function get_gist_regex()
{
return $this->regex;
}
private function get_shortcode_tag()
{
return apply_filters( 'oembed_gist_shortcode_tag', $this->shotcode_tag );
}
}
// EOF