PluginProbe
Github Embed / 1.1
Github Embed v1.1
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1
github-embed / github-embed.php

github-embed.php in Github Embed 1.1, at github-embed.php

280 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: Github Embed
5 Plugin URI: http://www.leewillis.co.uk/wordpress-plugins
6 Description: Paste the URL to a Github project into your posts or pages, and have the project information pulled in and displayed automatically
7 Version: 1.1
8 Author: Lee Willis
9 Author URI: http://www.leewillis.co.uk/
10 */
11
12 /**
13 * Copyright (c) 2013 Lee Willis. All rights reserved.
14 *
15 * Released under the GPL license
16 * http://www.opensource.org/licenses/gpl-license.php
17 *
18 * This is an add-on for WordPress
19 * http://wordpress.org/
20 *
21 * **********************************************************************
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 * **********************************************************************
32 */
33
34 class github_embed {
35
36
37
38 /**
39 * Constructor. Registers hooks and filters
40 */
41 public function __construct() {
42
43 add_action ( 'init', array ( $this, 'register_oembed_handler' ) );
44 add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) );
45 add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) );
46
47 // @TODO i18n
48
49 }
50
51
52
53 /**
54 * Enqueue the frontend CSS
55 * @return void
56 */
57 function enqueue_styles() {
58
59 wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) );
60 wp_enqueue_style ( 'github-embed' );
61
62 }
63
64
65
66 /**
67 * Register the oEmbed provider, and point it at a local endpoint since github
68 * doesn't directly support oEmbed yet. Our local endpoint will use the github
69 * API to fulfil the request.
70 * @param array $providers The current list of providers
71 * @return array The list, with our new provider added
72 */
73 public function register_oembed_handler() {
74
75 $oembed_url = home_url ();
76 $key = $this->get_key();
77 $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url);
78 wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true );
79
80 }
81
82
83
84 /**
85 * Generate a unique key that can be used on our requests to stop others
86 * hijacking our internal oEmbed API
87 * @return string The site key
88 */
89 private function get_key() {
90
91 $key = get_option ( 'github_oembed_key' );
92
93 if ( ! $key ) {
94 $key = md5 ( time() . rand ( 0,65535 ) );
95 add_option ( 'github_oembed_key', $key, '', 'yes' );
96 }
97
98 return $key;
99
100 }
101
102
103
104 /**
105 * Check whether this is an oembed request, handle if it is
106 * Ignore it if not.
107 * Insert rant here about WP's lack of a front-end AJAX handler.
108 */
109 public function maybe_handle_oembed() {
110
111 if ( isset ( $_GET['github_oembed'] ) ) {
112 return $this->handle_oembed();
113 }
114
115 }
116
117
118
119 /**
120 * Handle an oembed request
121 */
122 public function handle_oembed() {
123
124 // Check this request is valid
125 if ( $_GET['github_oembed'] != $this->get_key() ) {
126 header ( 'HTTP/1.0 403 Forbidden' );
127 die ( 'Sad Octocat is sad.' );
128 }
129
130 // Check we have the required information
131 $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
132 $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
133
134 if ( ! empty ( $format ) && $format != 'json' ) {
135 header ( 'HTTP/1.0 501 Not implemented' );
136 die ( 'This octocat only does json' );
137 }
138
139 if ( ! $url ) {
140 header ( 'HTTP/1.0 404 Not Found' );
141 die ( 'Octocat is lost, and afraid' );
142 }
143
144 if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
145
146 $this->oembed_github_repo ( $matches[1], $matches[2] );
147
148 } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
149
150 $this->oembed_github_author ( $matches[1] );
151
152 }
153
154 }
155
156
157
158 /**
159 * Retrieve the information from github for a repo, and
160 * output it as an oembed response
161 */
162 private function oembed_github_repo ( $owner, $repository ) {
163
164 $repository = trim ( $repository, '/' );
165
166 $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array (
167 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
168
169 if ( is_wp_error( $results ) ||
170 ! isset ( $results['response']['code'] ) ||
171 $results['response']['code'] != '200' ) {
172 header ( 'HTTP/1.0 404 Not Found' );
173 die ( 'Octocat is lost, and afraid' );
174 }
175
176 $repo = json_decode ( $results['body'] );
177
178 $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array (
179 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
180
181 if ( is_wp_error( $results ) ||
182 ! isset ( $results['response']['code'] ) ||
183 $results['response']['code'] != '200' ) {
184 header ( 'HTTP/1.0 404 Not Found' );
185 die ( 'Octocat is lost, and afraid' );
186 }
187
188 $commits = json_decode ( $results['body'] );
189
190 $response = new stdClass();
191 $response->type = 'rich';
192 $response->width = '10';
193 $response->height = '10';
194 $response->version = '1.0';
195 $response->title = $repo->description;
196
197 // @TODO This should all be templated
198 $response->html = '<div class="github-embed github-embed-repository">';
199 $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
200 $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>";
201 $response->html .= esc_html($repo->forks_count)." forks.<br/>";
202 $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>";
203
204 if ( count ( $commits ) ) {
205
206 $cnt = 0;
207 $response->html .= 'Recent commits:';
208 $response->html .= '<ul class="github_commits">';
209
210 foreach ( $commits as $commit ) {
211
212 if ($cnt > 4)
213 break;
214
215 $response->html .= '<li class="github_commit">';
216 $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
217 $response->html .= esc_html($commit->commit->committer->name);
218 $response->html .= '</li>';
219
220 $cnt++;
221
222 }
223
224 $response->html .= '</ul>';
225
226 }
227 $response->html .= '</p>';
228 $response->html .= '</div>';
229
230 header ( 'Content-Type: application/json' );
231 echo json_encode ( $response );
232 die();
233
234 }
235
236
237
238 /**
239 * Retrieve the information from github for an author, and output
240 * it as an oembed response
241 */
242 private function oembed_github_author ( $owner ) {
243
244 $owner = trim ( $owner, '/' );
245
246 $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array (
247 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
248
249 if ( is_wp_error( $results ) ||
250 ! isset ( $results['response']['code'] ) ||
251 $results['response']['code'] != '200' ) {
252 header ( 'HTTP/1.0 404 Not Found' );
253 die ( 'Octocat is lost, and afraid' );
254 }
255
256 $owner_info = json_decode ( $results['body'] );
257
258 $response = new stdClass();
259 $response->type = 'rich';
260 $response->width = '10';
261 $response->height = '10';
262 $response->version = '1.0';
263 $response->title = $owner_info->name;
264
265 // @TODO This should all be templated
266 $response->html = '<div class="github-embed github-embed-user">';
267 $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>";
268 $response->html .= esc_html($owner_info->public_repos).' repositories, ';
269 $response->html .= esc_html($owner_info->followers).' followers.</p>';
270 $response->html .= '</div>';
271
272 header ( 'Content-Type: application/json' );
273 echo json_encode ( $response );
274 die();
275
276 }
277
278 }
279
280 $github_embed = new github_embed();