1
+ <?php
2
+ /*
3
+ Plugin Name: Blazer Six Gist oEmbed
4
+ Version: 1.0
5
+ Plugin URI: https://gist.github.com/3031280
6
+ Description: Gist oEmbed and shortcode support with caching.
7
+ Author: Blazer Six, Inc.
8
+ Author URI: http://www.blazersix.com/
9
+ License: GPLv2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+
12
+ Alternatives
13
+ ------------
14
+ * oEmbed Gist by Takayuki Miyauchi - http://wordpress.org/extend/plugins/oembed-gist/
15
+ * Pretty Cacheable Gists by Zach Tollman - https://gist.github.com/2864688
16
+ * Spotify, Rdio, and Gist embeds for WordPress by Alex Mills - https://gist.github.com/2417309
17
+
18
+ ------------------------------------------------------------------------
19
+ Copyright 2012 Blazer Six, Inc.
20
+
21
+ This program is free software; you can redistribute it and/or modify
22
+ it under the terms of the GNU General Public License as published by
23
+ the Free Software Foundation; either version 2 of the License, or
24
+ (at your option) any later version.
25
+
26
+ This program is distributed in the hope that it will be useful,
27
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
28
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
+ GNU General Public License for more details.
30
+
31
+ You should have received a copy of the GNU General Public License
32
+ along with this program; if not, write to the Free Software
33
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
+ */
35
+
36
+
37
+ add_action ( 'plugins_loaded ' , array ( 'Blazer_Six_Gist_oEmbed ' , 'setup ' ) );
38
+
39
+ class Blazer_Six_Gist_oEmbed {
40
+ function setup () {
41
+ wp_embed_register_handler ( 'gist ' , '#(https://gist\.github\.com/([a-z0-9]+))(?:\#file_(.*))?#i ' , array ( __CLASS__ , 'wp_embed_handler ' ) );
42
+ wp_register_style ( 'github-gist ' , 'https://gist.github.com/stylesheets/gist/embed.css ' );
43
+
44
+ add_shortcode ( 'gist ' , array ( __CLASS__ , 'shortcode ' ) );
45
+ }
46
+
47
+ /**
48
+ * WP Embed Handler
49
+ *
50
+ * Parses Gist URLs for oEmbed support. Returns the value as a shortcode
51
+ * string to let the shortcode method handle processing. The value returned
52
+ * also doesn't have wpautop() applied, which is nice for source code.
53
+ *
54
+ * If a file is specified in the hash of the URL for a multi-file Gist, it
55
+ * will be picked up and only the single file will be displayed.
56
+ */
57
+ function wp_embed_handler ( $ matches , $ attr , $ url , $ rawattr ) {
58
+ $ shortcode = '[gist ' ;
59
+
60
+ if ( isset ( $ matches [2 ] ) && ! empty ( $ matches [2 ] ) )
61
+ $ shortcode .= ' id=" ' . esc_attr ( $ matches [2 ] ) . '" ' ;
62
+
63
+ if ( isset ( $ matches [3 ] ) && ! empty ( $ matches [3 ] ) )
64
+ $ shortcode .= ' file=" ' . esc_attr ( $ matches [3 ] ) . '" ' ;
65
+
66
+ $ shortcode .= '] ' . $ url . '[/gist] ' ;
67
+
68
+ return $ shortcode ; // Allows html to be returned after wpautop
69
+ }
70
+
71
+ /**
72
+ * Gist Shortcode
73
+ *
74
+ * Works with private Gists, too (I think).
75
+ *
76
+ * Cache functionality mimics WP_Embed so the cache is invalidated whenever
77
+ * a post is saved. @see WP_Embed->delete_oembed_caches()
78
+ *
79
+ * The $show_line_numbers attribute assumes custom CSS has been added. If
80
+ * the embedded stylehseet is being used, this value won't do anything aside
81
+ * from add a class.
82
+ *
83
+ * If $embed_stylesheet is set to true, the external stylesheet will be
84
+ * enqueued and output in the footer. If that's too late, set the default
85
+ * $embed_stylesheet value to false and enqueue the 'github-gist' style
86
+ * before wp_head.
87
+ *
88
+ * @see WP_Embed->shortcode()
89
+ */
90
+ function shortcode ( $ attr , $ content = null ) {
91
+ global $ post , $ wp_embed ;
92
+
93
+ $ defaults = apply_filters ( 'blazersix_gist_shortcode_defaults ' , array (
94
+ 'embed_stylesheet ' => apply_filters ( 'blazersix_gist_embed_stylesheet_default ' , true ),
95
+ 'id ' => '' ,
96
+ 'file ' => '' ,
97
+ 'show_line_numbers ' => true ,
98
+ 'show_meta ' => true
99
+ ) );
100
+
101
+ $ attr = shortcode_atts ( $ defaults , $ attr );
102
+ $ attr ['embed_stylesheet ' ] = ( ! $ attr ['embed_stylesheet ' ] || 'false ' === $ attr ['embed_stylesheet ' ] || '0 ' === $ attr ['embed_stylesheet ' ] ) ? false : true ;
103
+ $ attr ['show_line_numbers ' ] = ( ! $ attr ['show_line_numbers ' ] || 'false ' === $ attr ['show_line_numbers ' ] || '0 ' === $ attr ['show_line_numbers ' ] ) ? false : true ;
104
+ $ attr ['show_meta ' ] = ( ! $ attr ['show_meta ' ] || 'false ' === $ attr ['show_meta ' ] || '0 ' === $ attr ['show_meta ' ] ) ? false : true ;
105
+
106
+ extract ( $ attr , EXTR_SKIP );
107
+
108
+ if ( ! empty ( $ id ) )
109
+ $ json_url = 'http://gist.github.com/ ' . $ id . '.json ' ;
110
+
111
+ // The Gist ID and desired file are picked up from the URL if not passed as shortcode attributes
112
+ if ( ! empty ( $ content ) && ( ! isset ( $ json_url ) || ! isset ( $ file ) ) ) {
113
+ preg_match ( '#(https://gist\.github\.com/([a-z0-9]+))(?:\#file_(.*))?#i ' , $ content , $ matches );
114
+
115
+ if ( ! isset ( $ json_url ) && isset ( $ matches [1 ] ) && ! empty ( $ matches [1 ] ) )
116
+ $ json_url = $ matches [1 ] . '.json ' ;
117
+
118
+ if ( ! isset ( $ file ) && isset ( $ matches [3 ] ) && ! empty ( $ matches [3 ] ) )
119
+ $ file = $ matches [3 ];
120
+ }
121
+
122
+ if ( ! isset ( $ json_url ) )
123
+ return '' ;
124
+
125
+ if ( ! empty ( $ file ) )
126
+ $ json_url = add_query_arg ( 'file ' , urlencode ( $ file ), $ json_url );
127
+
128
+ if ( $ json_url && isset ( $ post ->ID ) ) {
129
+ // Check for a cached result (stored in the post meta)
130
+ $ cachekey = '_oembed_ ' . md5 ( $ json_url );
131
+ $ html = get_post_meta ( $ post ->ID , $ cachekey , true );
132
+
133
+ // Failures are cached
134
+ if ( '{{unknown}} ' === $ html )
135
+ return $ wp_embed ->maybe_make_link ( $ url );
136
+
137
+ // Retrieve html from Gist json endpoint
138
+ if ( empty ( $ html ) ) {
139
+ $ response = wp_remote_get ( $ json_url , array ( 'sslverify ' => false ) );
140
+ if ( 200 == wp_remote_retrieve_response_code ( $ response ) ) {
141
+ $ json = json_decode ( wp_remote_retrieve_body ( $ response ) );
142
+ if ( isset ( $ json ->div ) && ! empty ( $ json ->div ) ) {
143
+ $ html = $ json ->div ;
144
+ }
145
+ }
146
+
147
+ // Cache the result
148
+ $ cache = ( $ html ) ? $ html : '{{unknown}} ' ;
149
+ update_post_meta ( $ post ->ID , $ cachekey , $ cache );
150
+ }
151
+
152
+ // If there was a result, return it
153
+ if ( $ html ) {
154
+ if ( $ show_line_numbers )
155
+ $ html = str_replace ( 'class="highlight" ' , 'class="highlight show-line-numbers" ' , $ html );
156
+
157
+ if ( false === $ show_meta )
158
+ $ html = preg_replace ( '#<div class="gist-meta">.*?</div>#ms ' , '' , $ html );
159
+
160
+ if ( $ embed_stylesheet )
161
+ wp_enqueue_style ( 'github-gist ' ); // External stylesheet; line numbers won't work
162
+
163
+ return apply_filters ( 'blazersix_gist_embed_html ' , $ html , $ url , $ attr , $ post ->ID );
164
+ }
165
+ }
166
+ }
167
+ }
168
+ ?>
0 commit comments